From 3b58d229dad384e7b115dd04aa79f46deae027ec Mon Sep 17 00:00:00 2001 From: Pun Butrach Date: Sun, 18 Feb 2024 08:32:32 +0700 Subject: [PATCH] refactor: PopScope Migration (#1674) Co-authored-by: Benjamin Halko --- lib/services/manager_api.dart | 4 +-- lib/ui/views/installer/installer_view.dart | 18 +++++++------ .../views/installer/installer_viewmodel.dart | 26 +++++++++---------- lib/ui/views/navigation/navigation_view.dart | 10 +++---- 4 files changed, 28 insertions(+), 30 deletions(-) diff --git a/lib/services/manager_api.dart b/lib/services/manager_api.dart index 5257dddf8e..091ddf2256 100644 --- a/lib/services/manager_api.dart +++ b/lib/services/manager_api.dart @@ -619,8 +619,8 @@ class ManagerAPI { return showDialog( barrierDismissible: false, context: context, - builder: (context) => WillPopScope( - onWillPop: () async => false, + builder: (context) => PopScope( + canPop: false, child: AlertDialog( title: Text(t.warning), content: ValueListenableBuilder( diff --git a/lib/ui/views/installer/installer_view.dart b/lib/ui/views/installer/installer_view.dart index 2ff230b39e..c576a6a25d 100644 --- a/lib/ui/views/installer/installer_view.dart +++ b/lib/ui/views/installer/installer_view.dart @@ -16,12 +16,15 @@ class InstallerView extends StatelessWidget { return ViewModelBuilder.reactive( onViewModelReady: (model) => model.initialize(context), viewModelBuilder: () => InstallerViewModel(), - builder: (context, model, child) => WillPopScope( - /* - TODO(any): migrate to [PopScope], - we've tried to migrate it two times but - reverted it because we couldn't exit out of the screen. - */ + builder: (context, model, child) => PopScope( + canPop: !model.isPatching, + onPopInvoked: (bool didPop) { + if (didPop) { + model.onPop(); + } else { + model.onPopAttempt(context); + } + }, child: SafeArea( top: false, bottom: model.isPatching, @@ -83,7 +86,7 @@ class InstallerView extends StatelessWidget { maxLines: 1, overflow: TextOverflow.ellipsis, ), - onBackButtonPressed: () => model.onWillPop(context), + onBackButtonPressed: () => Navigator.maybePop(context), bottom: PreferredSize( preferredSize: const Size(double.infinity, 1.0), child: GradientProgressIndicator(progress: model.progress), @@ -111,7 +114,6 @@ class InstallerView extends StatelessWidget { ), ), ), - onWillPop: () => model.onWillPop(context), ), ); } diff --git a/lib/ui/views/installer/installer_viewmodel.dart b/lib/ui/views/installer/installer_viewmodel.dart index 1e4c961560..d47836dee8 100644 --- a/lib/ui/views/installer/installer_viewmodel.dart +++ b/lib/ui/views/installer/installer_viewmodel.dart @@ -526,25 +526,23 @@ class InstallerViewModel extends BaseViewModel { } } - Future onWillPop(BuildContext context) async { - if (isPatching) { - if (!cancel) { - cancel = true; - _toast.showBottom(t.installerView.pressBackAgain); - } else if (!isCanceled) { - await stopPatcher(); - } else { - _toast.showBottom(t.installerView.noExit); - } - return false; + Future onPopAttempt(BuildContext context) async { + if (!cancel) { + cancel = true; + _toast.showBottom(t.installerView.pressBackAgain); + } else if (!isCanceled) { + await stopPatcher(); + } else { + _toast.showBottom(t.installerView.noExit); } + } + + void onPop() { if (!cancel) { cleanPatcher(); } else { _patcherAPI.cleanPatcher(); } - screenshotCallback.dispose(); - Navigator.of(context).pop(); - return true; + ScreenshotCallback().dispose(); } } diff --git a/lib/ui/views/navigation/navigation_view.dart b/lib/ui/views/navigation/navigation_view.dart index 65a41ca375..44cb267838 100644 --- a/lib/ui/views/navigation/navigation_view.dart +++ b/lib/ui/views/navigation/navigation_view.dart @@ -13,13 +13,11 @@ class NavigationView extends StatelessWidget { return ViewModelBuilder.reactive( onViewModelReady: (model) => model.initialize(context), viewModelBuilder: () => locator(), - builder: (context, model, child) => WillPopScope( - onWillPop: () async { - if (model.currentIndex == 0) { - return true; - } else { + builder: (context, model, child) => PopScope( + canPop: model.currentIndex == 0, + onPopInvoked: (bool didPop) { + if (!didPop) { model.setIndex(0); - return false; } }, child: Scaffold(