Move load indicator to app bar in home pages

This commit is contained in:
Ming Ming 2022-11-12 18:45:29 +08:00
parent eddb94a3e6
commit 10aa594057
4 changed files with 132 additions and 91 deletions

View file

@ -0,0 +1,17 @@
import 'package:flutter/material.dart';
class AppBarCircularProgressIndicator extends StatelessWidget {
const AppBarCircularProgressIndicator({super.key});
@override
Widget build(BuildContext context) {
return const Center(
child: SizedBox.square(
dimension: 24,
child: CircularProgressIndicator(
strokeWidth: 2,
),
),
);
}
}

View file

@ -140,12 +140,7 @@ class _HomeAlbumsState extends State<HomeAlbums>
), ),
Align( Align(
alignment: Alignment.bottomCenter, alignment: Alignment.bottomCenter,
child: Column( child: SizedBox(
mainAxisSize: MainAxisSize.min,
children: [
if (state is ListAlbumBlocLoading)
const LinearProgressIndicator(),
SizedBox(
width: double.infinity, width: double.infinity,
height: _calcBottomAppBarExtent(context), height: _calcBottomAppBarExtent(context),
child: ClipRect( child: ClipRect(
@ -157,8 +152,6 @@ class _HomeAlbumsState extends State<HomeAlbums>
), ),
), ),
), ),
],
),
), ),
], ],
); );
@ -193,8 +186,14 @@ class _HomeAlbumsState extends State<HomeAlbums>
} }
Widget _buildNormalAppBar(BuildContext context) { Widget _buildNormalAppBar(BuildContext context) {
return BlocBuilder(
bloc: _bloc,
buildWhen: (previous, current) =>
previous is ListAlbumBlocLoading != current is ListAlbumBlocLoading,
builder: (context, state) {
return HomeSliverAppBar( return HomeSliverAppBar(
account: widget.account, account: widget.account,
isShowProgressIcon: state is ListAlbumBlocLoading,
menuActions: [ menuActions: [
PopupMenuItem( PopupMenuItem(
value: _menuValueSort, value: _menuValueSort,
@ -217,6 +216,8 @@ class _HomeAlbumsState extends State<HomeAlbums>
} }
}, },
); );
},
);
} }
SelectableItem _buildArchiveItem(BuildContext context) { SelectableItem _buildArchiveItem(BuildContext context) {

View file

@ -9,6 +9,7 @@ import 'package:nc_photos/pref.dart';
import 'package:nc_photos/theme.dart'; import 'package:nc_photos/theme.dart';
import 'package:nc_photos/url_launcher_util.dart'; import 'package:nc_photos/url_launcher_util.dart';
import 'package:nc_photos/widget/account_picker_dialog.dart'; import 'package:nc_photos/widget/account_picker_dialog.dart';
import 'package:nc_photos/widget/app_bar_circular_progress_indicator.dart';
import 'package:nc_photos/widget/app_bar_title_container.dart'; import 'package:nc_photos/widget/app_bar_title_container.dart';
import 'package:nc_photos/widget/settings.dart'; import 'package:nc_photos/widget/settings.dart';
@ -20,6 +21,7 @@ class HomeSliverAppBar extends StatelessWidget {
this.actions, this.actions,
this.menuActions, this.menuActions,
this.onSelectedMenuActions, this.onSelectedMenuActions,
this.isShowProgressIcon = false,
}) : super(key: key); }) : super(key: key);
@override @override
@ -38,7 +40,9 @@ class HomeSliverAppBar extends StatelessWidget {
child: AppBarTitleContainer( child: AppBarTitleContainer(
title: Text(accountLabel ?? account.address), title: Text(accountLabel ?? account.address),
subtitle: accountLabel == null ? Text(account.username2) : null, subtitle: accountLabel == null ? Text(account.username2) : null,
icon: account.scheme == "http" icon: isShowProgressIcon
? const AppBarCircularProgressIndicator()
: (account.scheme == "http"
? Icon( ? Icon(
Icons.no_encryption_outlined, Icons.no_encryption_outlined,
color: Theme.of(context).colorScheme.error, color: Theme.of(context).colorScheme.error,
@ -46,7 +50,7 @@ class HomeSliverAppBar extends StatelessWidget {
: Icon( : Icon(
Icons.https, Icons.https,
color: Theme.of(context).colorScheme.primary, color: Theme.of(context).colorScheme.primary,
), )),
), ),
), ),
floating: true, floating: true,
@ -103,6 +107,7 @@ class HomeSliverAppBar extends StatelessWidget {
/// much >= 0 /// much >= 0
final List<PopupMenuEntry<int>>? menuActions; final List<PopupMenuEntry<int>>? menuActions;
final void Function(int)? onSelectedMenuActions; final void Function(int)? onSelectedMenuActions;
final bool isShowProgressIcon;
static const _menuValueAbout = -1; static const _menuValueAbout = -1;
static const _menuValueHelp = -2; static const _menuValueHelp = -2;

View file

@ -205,13 +205,7 @@ class _HomePhotosState extends State<HomePhotos>
), ),
Align( Align(
alignment: Alignment.bottomCenter, alignment: Alignment.bottomCenter,
child: Column( child: SizedBox(
mainAxisSize: MainAxisSize.min,
children: [
if (state is ScanAccountDirBlocLoading ||
_buildItemQueue.isProcessing)
const LinearProgressIndicator(),
SizedBox(
width: double.infinity, width: double.infinity,
height: _calcBottomAppBarExtent(context), height: _calcBottomAppBarExtent(context),
child: ClipRect( child: ClipRect(
@ -223,8 +217,6 @@ class _HomePhotosState extends State<HomePhotos>
), ),
), ),
), ),
],
),
), ),
], ],
); );
@ -287,8 +279,17 @@ class _HomePhotosState extends State<HomePhotos>
} }
Widget _buildNormalAppBar(BuildContext context) { Widget _buildNormalAppBar(BuildContext context) {
return BlocBuilder(
bloc: _bloc,
buildWhen: (previous, current) =>
previous is ScanAccountDirBlocLoading !=
current is ScanAccountDirBlocLoading,
builder: (context, state) {
return HomeSliverAppBar( return HomeSliverAppBar(
account: widget.account, account: widget.account,
isShowProgressIcon: (state is ScanAccountDirBlocLoading ||
_buildItemQueue.isProcessing) &&
!_isRefreshIndicatorActive,
actions: [ actions: [
ZoomMenuButton( ZoomMenuButton(
initialZoom: _thumbZoomLevel, initialZoom: _thumbZoomLevel,
@ -314,6 +315,8 @@ class _HomePhotosState extends State<HomePhotos>
} }
}, },
); );
},
);
} }
Widget _buildSmartAlbumList(BuildContext context) { Widget _buildSmartAlbumList(BuildContext context) {
@ -625,12 +628,26 @@ class _HomePhotosState extends State<HomePhotos>
} }
Future<void> _waitRefresh() async { Future<void> _waitRefresh() async {
setState(() {
_isRefreshIndicatorActive = true;
});
try {
while (true) { while (true) {
await Future.delayed(const Duration(seconds: 1)); await Future.delayed(const Duration(seconds: 1));
if (_bloc.state is! ScanAccountDirBlocLoading) { if (_bloc.state is! ScanAccountDirBlocLoading) {
return; return;
} }
} }
} finally {
// To prevent the app bar icon appearing for a very short while
unawaited(Future.delayed(const Duration(seconds: 2)).then((_) {
if (mounted) {
setState(() {
_isRefreshIndicatorActive = false;
});
}
}));
}
} }
void _setThumbZoomLevel(int level) { void _setThumbZoomLevel(int level) {
@ -745,6 +762,7 @@ class _HomePhotosState extends State<HomePhotos>
late final _Web? _web = platform_k.isWeb ? _Web(this) : null; late final _Web? _web = platform_k.isWeb ? _Web(this) : null;
var _isScrollbarVisible = false; var _isScrollbarVisible = false;
var _isRefreshIndicatorActive = false;
static final _log = Logger("widget.home_photos._HomePhotosState"); static final _log = Logger("widget.home_photos._HomePhotosState");
static const _menuValueRefresh = 0; static const _menuValueRefresh = 0;