Skip to content

Commit

Permalink
feat: improve predictive back (#1487)
Browse files Browse the repository at this point in the history
Signed-off-by: validcube <[email protected]>
  • Loading branch information
validcube authored Nov 20, 2023
1 parent 584605e commit eb3d895
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
4 changes: 2 additions & 2 deletions lib/services/manager_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,8 @@ class ManagerAPI {
return showDialog(
barrierDismissible: false,
context: context,
builder: (context) => WillPopScope(
onWillPop: () async => false,
builder: (context) => PopScope(
canPop: false,
child: AlertDialog(
backgroundColor: Theme.of(context).colorScheme.secondaryContainer,
title: I18nText('warning'),
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/views/installer/installer_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class InstallerView extends StatelessWidget {
return ViewModelBuilder<InstallerViewModel>.reactive(
onViewModelReady: (model) => model.initialize(context),
viewModelBuilder: () => InstallerViewModel(),
builder: (context, model, child) => WillPopScope(
builder: (context, model, child) => PopScope(
onPopInvoked: (bool didPop) => model.onPopInvoked(context, didPop),
child: SafeArea(
top: false,
bottom: model.isPatching,
Expand Down Expand Up @@ -83,7 +84,7 @@ class InstallerView extends StatelessWidget {
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
onBackButtonPressed: () => model.onWillPop(context),
onBackButtonPressed: () => model.onBackButtonInvoked(context),
bottom: PreferredSize(
preferredSize: const Size(double.infinity, 1.0),
child: GradientProgressIndicator(progress: model.progress),
Expand Down Expand Up @@ -111,7 +112,6 @@ class InstallerView extends StatelessWidget {
),
),
),
onWillPop: () => model.onWillPop(context),
),
);
}
Expand Down
47 changes: 30 additions & 17 deletions lib/ui/views/installer/installer_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ class InstallerViewModel extends BaseViewModel {
Future<void> copyLogs() async {
final info = await AboutInfo.getInfo();
dynamic getValue(String patchName, Option option) {
final Option? savedOption = _managerAPI.getPatchOption(_app.packageName, patchName, option.key);
final Option? savedOption =
_managerAPI.getPatchOption(_app.packageName, patchName, option.key);
if (savedOption != null) {
return savedOption.value;
} else {
Expand All @@ -203,7 +204,6 @@ class InstallerViewModel extends BaseViewModel {
'App: ${_app.packageName} v${_app.version}',
'Patches version: ${_managerAPI.patchesVersion}',
'Patches: ${_patches.map((p) => p.name + (p.options.isEmpty ? '' : ' [${p.options.map((o) => '${o.title}: ${getValue(p.name, o)}').join(", ")}]')).toList().join(", ")}',

'\n- Settings',
'Allow changing patch selection: ${_managerAPI.isPatchesChangeEnabled()}',
'Version compatibility check: ${_managerAPI.isVersionCompatibilityCheckEnabled()}',
Expand Down Expand Up @@ -427,25 +427,38 @@ class InstallerViewModel extends BaseViewModel {
}
}

Future<bool> onWillPop(BuildContext context) async {
if (isPatching) {
bool canPop() {
return !isPatching;
}

void onBackButtonInvoked(BuildContext context) {
if (canPop()) {
onPopInvoked(context, true);
} else {
onPopInvoked(context, false);
}
}

Future<void> onPopInvoked(BuildContext context, bool didPop) async {
if (didPop) {
if (!cancel) {
cancel = true;
_toast.showBottom('installerView.pressBackAgain');
} else if (!isCanceled) {
await stopPatcher();
cleanPatcher();
} else {
_toast.showBottom('installerView.noExit');
_patcherAPI.cleanPatcher();
}
return false;
}
if (!cancel) {
cleanPatcher();
screenshotCallback.dispose();
Navigator.of(context).pop();
} else {
_patcherAPI.cleanPatcher();
if (isPatching) {
if (!cancel) {
cancel = true;
_toast.showBottom('installerView.pressBackAgain');
} else if (!isCanceled) {
await stopPatcher();
} else {
_toast.showBottom('installerView.noExit');
}
}
}
screenshotCallback.dispose();
Navigator.of(context).pop();
return true;
}
}
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 eb3d895

Please sign in to comment.