Skip to content

Commit

Permalink
⚡️ Improve LocallyAvailableBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Aug 5, 2024
1 parent 910a6c9 commit 3f288f6
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions lib/src/widgets/locally_available_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ final class LocallyAvailableBuilder extends StatefulWidget {
const LocallyAvailableBuilder({
super.key,
required this.asset,
required this.builder,
this.isOriginal = true,
required this.builder,
this.progressBuilder,
});

final AssetEntity asset;
final Widget Function(BuildContext context, AssetEntity asset) builder;
final bool isOriginal;
final Widget Function(BuildContext context, AssetEntity asset) builder;
final Widget Function(
BuildContext context,
PMRequestState? state,
double? progress,
)? progressBuilder;

@override
State<LocallyAvailableBuilder> createState() =>
Expand Down Expand Up @@ -86,36 +92,35 @@ class _LocallyAvailableBuilderState extends State<LocallyAvailableBuilder> {
});
}

Widget _indicator(BuildContext context) {
Widget _buildIndicator(BuildContext context) {
return StreamBuilder<PMProgressState>(
stream: _progressHandler!.stream,
stream: _progressHandler?.stream,
initialData: const PMProgressState(0, PMRequestState.prepare),
builder: (BuildContext c, AsyncSnapshot<PMProgressState> s) {
if (s.hasData) {
final double progress = s.data!.progress;
final PMRequestState state = s.data!.state;
return Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(
state == PMRequestState.failed
? Icons.cloud_off
: Icons.cloud_queue,
color: context.iconTheme.color?.withOpacity(.4),
size: 28,
),
if (state != PMRequestState.success &&
state != PMRequestState.failed)
ScaleText(
' iCloud ${(progress * 100).toInt()}%',
style: TextStyle(
color: context.textTheme.bodyMedium?.color?.withOpacity(.4),
),
),
],
);
final PMRequestState? state = s.data?.state;
final double? progress = s.data?.progress;
if (widget.progressBuilder case final builder?) {
return builder(context, state, progress);
}
return const SizedBox.shrink();
return Row(
children: [
Icon(
state == PMRequestState.failed
? Icons.cloud_off
: Icons.cloud_queue,
color: context.iconTheme.color?.withOpacity(.4),
size: 28,
),
if (state != PMRequestState.success &&
state != PMRequestState.failed)
ScaleText(
' iCloud ${((progress ?? 0) * 100).toInt()}%',
style: TextStyle(
color: context.textTheme.bodyMedium?.color?.withOpacity(.4),
),
),
],
);
},
);
}
Expand All @@ -125,9 +130,6 @@ class _LocallyAvailableBuilderState extends State<LocallyAvailableBuilder> {
if (_isLocallyAvailable) {
return widget.builder(context, widget.asset);
}
if (_progressHandler != null) {
return Center(child: _indicator(context));
}
return const SizedBox.shrink();
return Center(child: FittedBox(child: _buildIndicator(context)));
}
}

0 comments on commit 3f288f6

Please sign in to comment.