Skip to content

Commit

Permalink
refactor: PopScope Migration (#1674)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Halko <benjaminhalko@hotmail.com>
  • Loading branch information
validcube and BenjaminHalko committed Feb 18, 2024
1 parent f5ba84d commit 3b58d22
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
4 changes: 2 additions & 2 deletions lib/services/manager_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 10 additions & 8 deletions lib/ui/views/installer/installer_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ class InstallerView extends StatelessWidget {
return ViewModelBuilder<InstallerViewModel>.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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -111,7 +114,6 @@ class InstallerView extends StatelessWidget {
),
),
),
onWillPop: () => model.onWillPop(context),
),
);
}
Expand Down
26 changes: 12 additions & 14 deletions lib/ui/views/installer/installer_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -526,25 +526,23 @@ class InstallerViewModel extends BaseViewModel {
}
}

Future<bool> 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<void> 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();
}
}
10 changes: 4 additions & 6 deletions lib/ui/views/navigation/navigation_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ class NavigationView extends StatelessWidget {
return ViewModelBuilder<NavigationViewModel>.reactive(
onViewModelReady: (model) => model.initialize(context),
viewModelBuilder: () => locator<NavigationViewModel>(),
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(
Expand Down

0 comments on commit 3b58d22

Please sign in to comment.