2022-09-15 00:23:03 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
import 'package:dynamic_themes/dynamic_themes.dart';
|
2022-09-05 10:07:38 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-15 14:27:48 +02:00
|
|
|
import 'package:flutter/services.dart';
|
2022-09-05 10:07:38 +02:00
|
|
|
import 'package:injectable/injectable.dart';
|
2022-09-18 23:13:29 +02:00
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
2022-09-20 01:49:31 +02:00
|
|
|
import 'package:revanced_manager/app/app.locator.dart';
|
2022-09-15 00:30:02 +02:00
|
|
|
import 'package:revanced_manager/services/root_api.dart';
|
2022-09-20 01:49:31 +02:00
|
|
|
import 'package:revanced_manager/services/toast.dart';
|
2022-09-05 10:07:38 +02:00
|
|
|
import 'package:revanced_manager/ui/views/home/home_view.dart';
|
|
|
|
import 'package:revanced_manager/ui/views/patcher/patcher_view.dart';
|
|
|
|
import 'package:revanced_manager/ui/views/settings/settings_view.dart';
|
2022-09-15 00:23:03 +02:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2022-09-05 10:07:38 +02:00
|
|
|
import 'package:stacked/stacked.dart';
|
|
|
|
|
|
|
|
@lazySingleton
|
|
|
|
class NavigationViewModel extends IndexTrackingViewModel {
|
2023-01-30 13:35:06 +01:00
|
|
|
Future<void> initialize(BuildContext context) async {
|
2022-09-20 01:49:31 +02:00
|
|
|
locator<Toast>().initialize(context);
|
2023-06-12 05:29:14 +02:00
|
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
2023-06-12 06:26:49 +02:00
|
|
|
requestManageExternalStorage();
|
|
|
|
|
2022-09-18 23:13:29 +02:00
|
|
|
if (prefs.getBool('permissionsRequested') == null) {
|
2023-06-12 05:29:14 +02:00
|
|
|
await Permission.storage.request();
|
|
|
|
await Permission.manageExternalStorage.request();
|
2022-09-18 23:13:29 +02:00
|
|
|
await prefs.setBool('permissionsRequested', true);
|
|
|
|
RootAPI().hasRootPermissions().then(
|
|
|
|
(value) => Permission.requestInstallPackages.request().then(
|
2023-06-12 05:29:14 +02:00
|
|
|
(value) => Permission.ignoreBatteryOptimizations.request(),
|
2022-09-18 23:13:29 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-06-12 06:26:49 +02:00
|
|
|
|
2022-09-15 00:23:03 +02:00
|
|
|
if (prefs.getBool('useDarkTheme') == null) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final bool isDark =
|
2022-09-15 14:27:48 +02:00
|
|
|
MediaQuery.of(context).platformBrightness != Brightness.light;
|
|
|
|
await prefs.setBool('useDarkTheme', isDark);
|
|
|
|
await DynamicTheme.of(context)!.setTheme(isDark ? 1 : 0);
|
2022-09-15 00:23:03 +02:00
|
|
|
}
|
2022-09-24 14:49:46 +02:00
|
|
|
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
|
2022-09-15 14:27:48 +02:00
|
|
|
SystemChrome.setSystemUIOverlayStyle(
|
|
|
|
SystemUiOverlayStyle(
|
2022-09-18 23:20:08 +02:00
|
|
|
systemNavigationBarColor: Colors.transparent,
|
2022-09-15 14:27:48 +02:00
|
|
|
systemNavigationBarIconBrightness:
|
2023-06-12 05:29:14 +02:00
|
|
|
DynamicTheme.of(context)!.theme.brightness == Brightness.light
|
2022-09-15 14:27:48 +02:00
|
|
|
? Brightness.dark
|
|
|
|
: Brightness.light,
|
|
|
|
),
|
|
|
|
);
|
2022-09-15 00:23:03 +02:00
|
|
|
}
|
|
|
|
|
2023-06-12 06:26:49 +02:00
|
|
|
Future<void> requestManageExternalStorage() async {
|
|
|
|
final manageExternalStorageStatus =
|
|
|
|
await Permission.manageExternalStorage.status;
|
|
|
|
if (manageExternalStorageStatus.isDenied) {
|
|
|
|
await Permission.manageExternalStorage.request();
|
|
|
|
}
|
|
|
|
if (manageExternalStorageStatus.isPermanentlyDenied) {
|
|
|
|
await openAppSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 10:07:38 +02:00
|
|
|
Widget getViewForIndex(int index) {
|
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
return const HomeView();
|
|
|
|
case 1:
|
|
|
|
return const PatcherView();
|
|
|
|
case 2:
|
2022-09-07 03:37:25 +02:00
|
|
|
return const SettingsView();
|
2022-09-05 10:07:38 +02:00
|
|
|
default:
|
|
|
|
return const HomeView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|