Skip to content

Commit

Permalink
Refactor settings theme mode and migrate to hive
Browse files Browse the repository at this point in the history
  • Loading branch information
dhzdhd committed Nov 12, 2023
1 parent 02b93a8 commit 30ecb8e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
2 changes: 0 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:excode/src/home/services/api.dart';
import 'package:excode/src/settings/providers/settings_provider.dart';
import 'package:excode/src/settings/services/settings_service.dart';
import 'package:flutter/foundation.dart';
// import 'package:leak_tracker/leak_tracker.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
Expand All @@ -20,7 +19,6 @@ void main() async {
await initFileStorage();
await initDatabase();
await ApiHandler.initRuntimeVersionData();
await SettingsService.initPrefs();

if (defaultTargetPlatform == TargetPlatform.android) {
await initDownloader();
Expand Down
33 changes: 19 additions & 14 deletions lib/src/settings/providers/theme_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const _accentLightColor = Color.fromARGB(255, 85, 136, 156);
const _primaryLightColor = Color.fromARGB(255, 227, 245, 255);
const _secondaryLightColor = Color.fromARGB(255, 244, 247, 255);
final lightTheme = ThemeDataModel(
mode: CustomThemeMode.light,
primaryColor: _primaryLightColor,
secondaryColor: _secondaryLightColor,
invertedColor: _primaryDarkColor,
Expand Down Expand Up @@ -77,6 +78,7 @@ const _accentDarkColor = Color.fromARGB(255, 100, 255, 218);
const _primaryDarkColor = Color.fromARGB(255, 9, 28, 50);
const _secondaryDarkColor = Color.fromARGB(255, 25, 41, 60);
final darkTheme = ThemeDataModel(
mode: CustomThemeMode.dark,
primaryColor: _primaryDarkColor,
secondaryColor: _secondaryDarkColor,
invertedColor: _primaryLightColor,
Expand Down Expand Up @@ -147,36 +149,39 @@ class ThemeDataModel {
final Color invertedColor;
final Color accentColor;
final ThemeData theme;
final CustomThemeMode mode;

ThemeDataModel({
required this.primaryColor,
required this.secondaryColor,
required this.invertedColor,
required this.accentColor,
required this.theme,
required this.mode,
});
}

enum CustomThemeMode { light, dark }

ThemeDataModel getThemeFromString(String? theme) {
return theme == null
? ThemeMode.system == ThemeMode.dark
? darkTheme
: lightTheme
: theme == 'dark'
? darkTheme
: lightTheme;
return switch (theme) {
'dark' => darkTheme,
'light' => lightTheme,
_ => lightTheme,
};
}

class _ThemeModel extends StateNotifier<ThemeDataModel> {
_ThemeModel() : super(getThemeFromString(SettingsService.getTheme()));

void changeTheme(ThemeMode mode) {
if (mode == ThemeMode.light) {
SettingsService.setTheme('light');
state = lightTheme;
} else {
SettingsService.setTheme('dark');
state = darkTheme;
void changeTheme(CustomThemeMode mode) {
switch (mode) {
case CustomThemeMode.light:
SettingsService.setTheme('light');
state = lightTheme;
case CustomThemeMode.dark:
SettingsService.setTheme('dark');
state = darkTheme;
}
}
}
12 changes: 3 additions & 9 deletions lib/src/settings/services/settings_service.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import 'package:excode/src/factory.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SettingsService {
static late final SharedPreferences _prefs;

static Future<void> initPrefs() async {
_prefs = await SharedPreferences.getInstance();
}

static String getTheme() {
final brightness =
SchedulerBinding.instance.platformDispatcher.platformBrightness;
final isDarkMode = brightness == Brightness.dark;
return _prefs.getString('theme') ?? (isDarkMode ? 'dark' : 'light');
return box.get('theme') ?? (isDarkMode ? 'dark' : 'light');
}

static Future<void> setTheme(String theme) async {
/// Accepts dark or light only
await _prefs.setString('theme', theme);
await box.put('theme', theme);
}
}
11 changes: 5 additions & 6 deletions lib/src/settings/views/settings_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class SettingsView extends HookConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final theme = useState(ThemeMode.system);
final editorTheme = ref.watch(editorThemeStateProvider);
final globalTheme = ref.watch(themeStateProvider);

final settingsNotifier = ref.read(settingsProvider.notifier);
final tabSize =
ref.watch(settingsProvider.select((value) => value.tabSize));
Expand Down Expand Up @@ -68,18 +68,17 @@ class SettingsView extends HookConsumerWidget {
leading: const Icon(Icons.palette),
title: const Text('Theme'),
trailing: StyledDropdownContainer(
child: DropdownButton<ThemeMode>(
child: DropdownButton<CustomThemeMode>(
dropdownColor: globalTheme.primaryColor,
focusColor: globalTheme.secondaryColor,
isExpanded: true,
onChanged: ((val) {
theme.value = val!;
ref
.watch(themeStateProvider.notifier)
.changeTheme(theme.value);
.changeTheme(val!);
}),
value: theme.value,
items: ThemeMode.values
value: globalTheme.mode,
items: CustomThemeMode.values
.map((e) => DropdownMenuItem(
value: e,
child: Text(e.name.capitalize()),
Expand Down

0 comments on commit 30ecb8e

Please sign in to comment.