refactor: PopScope Migration (#1674)

Co-authored-by: Benjamin Halko <benjaminhalko@hotmail.com>
This commit is contained in:
Pun Butrach 2024-02-18 08:32:32 +07:00 committed by GitHub
parent f5ba84d81e
commit 3b58d229da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 30 deletions

View File

@ -619,8 +619,8 @@ class ManagerAPI {
return showDialog( return showDialog(
barrierDismissible: false, barrierDismissible: false,
context: context, context: context,
builder: (context) => WillPopScope( builder: (context) => PopScope(
onWillPop: () async => false, canPop: false,
child: AlertDialog( child: AlertDialog(
title: Text(t.warning), title: Text(t.warning),
content: ValueListenableBuilder( content: ValueListenableBuilder(

View File

@ -16,12 +16,15 @@ class InstallerView extends StatelessWidget {
return ViewModelBuilder<InstallerViewModel>.reactive( return ViewModelBuilder<InstallerViewModel>.reactive(
onViewModelReady: (model) => model.initialize(context), onViewModelReady: (model) => model.initialize(context),
viewModelBuilder: () => InstallerViewModel(), viewModelBuilder: () => InstallerViewModel(),
builder: (context, model, child) => WillPopScope( builder: (context, model, child) => PopScope(
/* canPop: !model.isPatching,
TODO(any): migrate to [PopScope], onPopInvoked: (bool didPop) {
we've tried to migrate it two times but if (didPop) {
reverted it because we couldn't exit out of the screen. model.onPop();
*/ } else {
model.onPopAttempt(context);
}
},
child: SafeArea( child: SafeArea(
top: false, top: false,
bottom: model.isPatching, bottom: model.isPatching,
@ -83,7 +86,7 @@ class InstallerView extends StatelessWidget {
maxLines: 1, maxLines: 1,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
), ),
onBackButtonPressed: () => model.onWillPop(context), onBackButtonPressed: () => Navigator.maybePop(context),
bottom: PreferredSize( bottom: PreferredSize(
preferredSize: const Size(double.infinity, 1.0), preferredSize: const Size(double.infinity, 1.0),
child: GradientProgressIndicator(progress: model.progress), child: GradientProgressIndicator(progress: model.progress),
@ -111,7 +114,6 @@ class InstallerView extends StatelessWidget {
), ),
), ),
), ),
onWillPop: () => model.onWillPop(context),
), ),
); );
} }

View File

@ -526,8 +526,7 @@ class InstallerViewModel extends BaseViewModel {
} }
} }
Future<bool> onWillPop(BuildContext context) async { Future<void> onPopAttempt(BuildContext context) async {
if (isPatching) {
if (!cancel) { if (!cancel) {
cancel = true; cancel = true;
_toast.showBottom(t.installerView.pressBackAgain); _toast.showBottom(t.installerView.pressBackAgain);
@ -536,15 +535,14 @@ class InstallerViewModel extends BaseViewModel {
} else { } else {
_toast.showBottom(t.installerView.noExit); _toast.showBottom(t.installerView.noExit);
} }
return false;
} }
void onPop() {
if (!cancel) { if (!cancel) {
cleanPatcher(); cleanPatcher();
} else { } else {
_patcherAPI.cleanPatcher(); _patcherAPI.cleanPatcher();
} }
screenshotCallback.dispose(); ScreenshotCallback().dispose();
Navigator.of(context).pop();
return true;
} }
} }

View File

@ -13,13 +13,11 @@ class NavigationView extends StatelessWidget {
return ViewModelBuilder<NavigationViewModel>.reactive( return ViewModelBuilder<NavigationViewModel>.reactive(
onViewModelReady: (model) => model.initialize(context), onViewModelReady: (model) => model.initialize(context),
viewModelBuilder: () => locator<NavigationViewModel>(), viewModelBuilder: () => locator<NavigationViewModel>(),
builder: (context, model, child) => WillPopScope( builder: (context, model, child) => PopScope(
onWillPop: () async { canPop: model.currentIndex == 0,
if (model.currentIndex == 0) { onPopInvoked: (bool didPop) {
return true; if (!didPop) {
} else {
model.setIndex(0); model.setIndex(0);
return false;
} }
}, },
child: Scaffold( child: Scaffold(