Skip to content

Commit

Permalink
feat: Apply dark/light mode to system navigation bar too
Browse files Browse the repository at this point in the history
  • Loading branch information
ponces committed Sep 15, 2022
1 parent 5d29603 commit 9f58757
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
22 changes: 15 additions & 7 deletions lib/ui/views/navigation/navigation_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ignore_for_file: use_build_context_synchronously
import 'package:dynamic_themes/dynamic_themes.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:injectable/injectable.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:revanced_manager/services/root_api.dart';
Expand All @@ -15,14 +16,21 @@ class NavigationViewModel extends IndexTrackingViewModel {
void initialize(BuildContext context) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (prefs.getBool('useDarkTheme') == null) {
if (MediaQuery.of(context).platformBrightness == Brightness.light) {
await prefs.setBool('useDarkTheme', false);
DynamicTheme.of(context)!.setTheme(0);
} else {
await prefs.setBool('useDarkTheme', true);
DynamicTheme.of(context)!.setTheme(1);
}
bool isDark =
MediaQuery.of(context).platformBrightness != Brightness.light;
await prefs.setBool('useDarkTheme', isDark);
await DynamicTheme.of(context)!.setTheme(isDark ? 1 : 0);
}
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
systemNavigationBarColor:
DynamicTheme.of(context)!.theme.colorScheme.surface,
systemNavigationBarIconBrightness:
DynamicTheme.of(context)!.theme.brightness == Brightness.light
? Brightness.dark
: Brightness.light,
),
);
RootAPI().hasRootPermissions();
Permission.requestInstallPackages.request();
Permission.ignoreBatteryOptimizations.request();
Expand Down
23 changes: 19 additions & 4 deletions lib/ui/views/settings/settings_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:dynamic_themes/dynamic_themes.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:logcat/logcat.dart';
import 'package:path_provider/path_provider.dart';
Expand Down Expand Up @@ -50,10 +51,16 @@ class SettingsViewModel extends BaseViewModel {
await _managerAPI.setUseDynamicTheme(value);
int currentTheme = DynamicTheme.of(context)!.themeId;
if (currentTheme.isEven) {
DynamicTheme.of(context)!.setTheme(value ? 2 : 0);
await DynamicTheme.of(context)!.setTheme(value ? 2 : 0);
} else {
DynamicTheme.of(context)!.setTheme(value ? 3 : 1);
await DynamicTheme.of(context)!.setTheme(value ? 3 : 1);
}
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
systemNavigationBarColor:
DynamicTheme.of(context)!.theme.colorScheme.surface,
),
);
notifyListeners();
}

Expand All @@ -65,10 +72,18 @@ class SettingsViewModel extends BaseViewModel {
await _managerAPI.setUseDarkTheme(value);
int currentTheme = DynamicTheme.of(context)!.themeId;
if (currentTheme < 2) {
DynamicTheme.of(context)!.setTheme(value ? 1 : 0);
await DynamicTheme.of(context)!.setTheme(value ? 1 : 0);
} else {
DynamicTheme.of(context)!.setTheme(value ? 3 : 2);
await DynamicTheme.of(context)!.setTheme(value ? 3 : 2);
}
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
systemNavigationBarColor:
DynamicTheme.of(context)!.theme.colorScheme.surface,
systemNavigationBarIconBrightness:
value ? Brightness.light : Brightness.dark,
),
);
notifyListeners();
}

Expand Down

0 comments on commit 9f58757

Please sign in to comment.