revanced-manager/lib/ui/views/navigation/navigation_viewmodel.dart

87 lines
3.1 KiB
Dart
Raw Normal View History

// ignore_for_file: use_build_context_synchronously
import 'package:device_info_plus/device_info_plus.dart';
import 'package:dynamic_themes/dynamic_themes.dart';
2022-09-05 10:07:38 +02:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2022-09-05 10:07:38 +02:00
import 'package:injectable/injectable.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/services/root_api.dart';
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';
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 {
Future<void> initialize(BuildContext context) async {
locator<Toast>().initialize(context);
2023-06-12 05:29:14 +02:00
final SharedPreferences prefs = await SharedPreferences.getInstance();
await requestManageExternalStorage();
2023-06-12 06:26:49 +02:00
if (prefs.getBool('permissionsRequested') == null) {
2023-06-12 05:29:14 +02:00
await Permission.storage.request();
await prefs.setBool('permissionsRequested', true);
await RootAPI().hasRootPermissions().then(
(value) => Permission.requestInstallPackages.request().then(
2023-06-12 05:29:14 +02:00
(value) => Permission.ignoreBatteryOptimizations.request(),
),
);
}
2023-06-12 06:26:49 +02:00
final dynamicTheme = DynamicTheme.of(context)!;
if (prefs.getInt('themeMode') == null) {
await prefs.setInt('themeMode', 0);
await dynamicTheme.setTheme(0);
}
// Force disable Material You on Android 11 and below
if (dynamicTheme.themeId.isOdd) {
const int ANDROID_12_SDK_VERSION = 31;
final AndroidDeviceInfo info = await DeviceInfoPlugin().androidInfo;
if (info.version.sdkInt < ANDROID_12_SDK_VERSION) {
await prefs.setInt('themeMode', 0);
await prefs.setBool('useDynamicTheme', false);
await dynamicTheme.setTheme(0);
}
}
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
2022-09-18 23:20:08 +02:00
systemNavigationBarColor: Colors.transparent,
systemNavigationBarIconBrightness:
2023-06-12 05:29:14 +02:00
DynamicTheme.of(context)!.theme.brightness == Brightness.light
? Brightness.dark
: Brightness.light,
),
);
}
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();
}
}
}