Stop showing errors if page is invisible

This commit is contained in:
Ming Ming 2021-07-11 12:48:50 +08:00
parent c89d999b73
commit 4532fb45bf
4 changed files with 66 additions and 10 deletions

View file

@ -28,6 +28,7 @@ import 'package:nc_photos/widget/archive_viewer.dart';
import 'package:nc_photos/widget/dynamic_album_viewer.dart';
import 'package:nc_photos/widget/home_app_bar.dart';
import 'package:nc_photos/widget/new_album_dialog.dart';
import 'package:nc_photos/widget/page_visibility_mixin.dart';
import 'package:tuple/tuple.dart';
class HomeAlbums extends StatefulWidget {
@ -42,7 +43,8 @@ class HomeAlbums extends StatefulWidget {
final Account account;
}
class _HomeAlbumsState extends State<HomeAlbums> {
class _HomeAlbumsState extends State<HomeAlbums>
with RouteAware, PageVisibilityMixin<HomeAlbums> {
@override
initState() {
super.initState();
@ -307,10 +309,12 @@ class _HomeAlbumsState extends State<HomeAlbums> {
_transformItems(state.albums);
} else if (state is ListAlbumBlocFailure) {
_transformItems(state.albums);
SnackBarManager().showSnackBar(SnackBar(
content: Text(exception_util.toUserString(state.exception, context)),
duration: k.snackBarDurationNormal,
));
if (isPageVisible()) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(exception_util.toUserString(state.exception, context)),
duration: k.snackBarDurationNormal,
));
}
} else if (state is ListAlbumBlocInconsistent) {
_reqQuery();
}

View file

@ -33,6 +33,7 @@ import 'package:nc_photos/use_case/update_property.dart';
import 'package:nc_photos/widget/album_picker_dialog.dart';
import 'package:nc_photos/widget/home_app_bar.dart';
import 'package:nc_photos/widget/measure.dart';
import 'package:nc_photos/widget/page_visibility_mixin.dart';
import 'package:nc_photos/widget/photo_list_item.dart';
import 'package:nc_photos/widget/popup_menu_zoom.dart';
import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart';
@ -51,7 +52,11 @@ class HomePhotos extends StatefulWidget {
}
class _HomePhotosState extends State<HomePhotos>
with WidgetsBindingObserver, SelectableItemStreamListMixin<HomePhotos> {
with
WidgetsBindingObserver,
SelectableItemStreamListMixin<HomePhotos>,
RouteAware,
PageVisibilityMixin {
@override
initState() {
super.initState();
@ -256,10 +261,12 @@ class _HomePhotosState extends State<HomePhotos>
}
} else if (state is ScanDirBlocFailure) {
_transformItems(state.files);
SnackBarManager().showSnackBar(SnackBar(
content: Text(exception_util.toUserString(state.exception, context)),
duration: k.snackBarDurationNormal,
));
if (isPageVisible()) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(exception_util.toUserString(state.exception, context)),
duration: k.snackBarDurationNormal,
));
}
} else if (state is ScanDirBlocInconsistent) {
_reqQuery();
}

View file

@ -24,6 +24,10 @@ import 'package:nc_photos/widget/viewer.dart';
class MyApp extends StatefulWidget {
@override
createState() => _MyAppState();
static RouteObserver get routeObserver => _routeObserver;
static final _routeObserver = RouteObserver<PageRoute>();
}
class _MyAppState extends State<MyApp> implements SnackBarHandler {
@ -46,6 +50,7 @@ class _MyAppState extends State<MyApp> implements SnackBarHandler {
themeMode: Pref.inst().isDarkTheme() ? ThemeMode.dark : ThemeMode.light,
initialRoute: Splash.routeName,
onGenerateRoute: _onGenerateRoute,
navigatorObservers: <NavigatorObserver>[MyApp.routeObserver],
scaffoldMessengerKey: _scaffoldMessengerKey,
locale: language_util.getSelectedLocale(context),
localizationsDelegates: AppLocalizations.localizationsDelegates,

View file

@ -0,0 +1,40 @@
import 'package:flutter/widgets.dart';
import 'package:nc_photos/widget/my_app.dart';
mixin PageVisibilityMixin<T extends StatefulWidget> on State<T>, RouteAware {
@override
didChangeDependencies() {
super.didChangeDependencies();
MyApp.routeObserver.subscribe(this, ModalRoute.of(context));
}
@override
dispose() {
MyApp.routeObserver.unsubscribe(this);
super.dispose();
}
@override
didPush() {
_isVisible = true;
}
@override
didPushNext() {
_isVisible = false;
}
@override
didPop() {
_isVisible = false;
}
@override
didPopNext() {
_isVisible = true;
}
bool isPageVisible() => _isVisible;
var _isVisible = true;
}