Add a link to trash after removing items

This commit is contained in:
Ming Ming 2022-04-23 03:09:44 +08:00
parent 0356e22be7
commit 7573f00bfb
7 changed files with 54 additions and 1 deletions

View file

@ -0,0 +1,25 @@
import 'package:flutter/widgets.dart';
class NavigationManager {
factory NavigationManager() => _inst;
NavigationManager._();
void setHandler(NavigationHandler handler) {
_handler = handler;
}
void unsetHandler(NavigationHandler handler) {
_handler = null;
}
NavigatorState? getNavigator() => _handler?.getNavigator();
NavigationHandler? _handler;
static final _inst = NavigationManager._();
}
abstract class NavigationHandler {
NavigatorState? getNavigator();
}

View file

@ -333,6 +333,7 @@ class _FavoriteBrowserState extends State<FavoriteBrowser>
await RemoveSelectionHandler()(
account: widget.account,
selectedFiles: selectedFiles,
isMoveToTrash: true,
);
}

View file

@ -7,8 +7,10 @@ import 'package:nc_photos/debug_util.dart';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/navigation_manager.dart';
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/use_case/remove.dart';
import 'package:nc_photos/widget/trashbin_browser.dart';
class RemoveSelectionHandler {
/// Remove [selectedFiles] and return the removed count
@ -17,6 +19,7 @@ class RemoveSelectionHandler {
required List<File> selectedFiles,
bool shouldCleanupAlbum = true,
bool isRemoveOpened = false,
bool isMoveToTrash = false,
}) async {
final String processingText, successText;
final String Function(int) failureText;
@ -52,15 +55,27 @@ class RemoveSelectionHandler {
},
shouldCleanUp: shouldCleanupAlbum,
);
final trashAction = isMoveToTrash
? SnackBarAction(
label: L10n.global().albumTrashLabel,
onPressed: () {
NavigationManager().getNavigator()?.pushNamed(
TrashbinBrowser.routeName,
arguments: TrashbinBrowserArguments(account));
},
)
: null;
if (failureCount == 0) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(successText),
duration: k.snackBarDurationNormal,
action: trashAction,
));
} else {
SnackBarManager().showSnackBar(SnackBar(
content: Text(failureText(failureCount)),
duration: k.snackBarDurationNormal,
action: trashAction,
));
}
return selectedFiles.length - failureCount;

View file

@ -425,6 +425,7 @@ class _HomePhotosState extends State<HomePhotos>
await RemoveSelectionHandler()(
account: widget.account,
selectedFiles: selectedFiles,
isMoveToTrash: true,
);
}

View file

@ -4,6 +4,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/event/event.dart';
import 'package:nc_photos/language_util.dart' as language_util;
import 'package:nc_photos/navigation_manager.dart';
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/theme.dart';
@ -49,11 +50,13 @@ class MyApp extends StatefulWidget {
static late BuildContext _globalContext;
}
class _MyAppState extends State<MyApp> implements SnackBarHandler {
class _MyAppState extends State<MyApp>
implements SnackBarHandler, NavigationHandler {
@override
void initState() {
super.initState();
SnackBarManager().registerHandler(this);
NavigationManager().setHandler(this);
_themeChangedListener =
AppEventListener<ThemeChangedEvent>(_onThemeChangedEvent)..begin();
_langChangedListener =
@ -77,6 +80,7 @@ class _MyAppState extends State<MyApp> implements SnackBarHandler {
initialRoute: Splash.routeName,
onGenerateRoute: _onGenerateRoute,
navigatorObservers: <NavigatorObserver>[MyApp.routeObserver],
navigatorKey: _navigatorKey,
scaffoldMessengerKey: _scaffoldMessengerKey,
locale: language_util.getSelectedLocale(),
localizationsDelegates: AppLocalizations.localizationsDelegates,
@ -107,6 +111,7 @@ class _MyAppState extends State<MyApp> implements SnackBarHandler {
void dispose() {
super.dispose();
SnackBarManager().unregisterHandler(this);
NavigationManager().unsetHandler(this);
_themeChangedListener.end();
_langChangedListener.end();
}
@ -115,6 +120,9 @@ class _MyAppState extends State<MyApp> implements SnackBarHandler {
showSnackBar(SnackBar snackBar) =>
_scaffoldMessengerKey.currentState?.showSnackBar(snackBar);
@override
getNavigator() => _navigatorKey.currentState;
ThemeData _getLightTheme() => ThemeData(
brightness: Brightness.light,
primarySwatch: AppTheme.primarySwatchLight,
@ -489,6 +497,7 @@ class _MyAppState extends State<MyApp> implements SnackBarHandler {
}
final _scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
final _navigatorKey = GlobalKey<NavigatorState>();
late AppEventListener<ThemeChangedEvent> _themeChangedListener;
late AppEventListener<LanguageChangedEvent> _langChangedListener;

View file

@ -392,6 +392,7 @@ class _PersonBrowserState extends State<PersonBrowser>
await RemoveSelectionHandler()(
account: widget.account,
selectedFiles: selectedFiles,
isMoveToTrash: true,
);
}

View file

@ -568,6 +568,7 @@ class _ViewerState extends State<Viewer>
account: widget.account,
selectedFiles: [file],
isRemoveOpened: true,
isMoveToTrash: true,
);
if (count > 0) {
Navigator.of(context).pop();