Skip to content

Commit

Permalink
Use riverpod providers for preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
petlyh committed Nov 24, 2024
1 parent 3b47cb9 commit 2039b50
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 107 deletions.
54 changes: 26 additions & 28 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ import "package:dynamic_color/dynamic_color.dart";
import "package:flutter/foundation.dart";
import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:flutter_riverpod/flutter_riverpod.dart"
hide ChangeNotifierProvider, Provider;
import "package:jsdict/providers/theme_provider.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:jsdict/providers/prefs.dart";
import "package:jsdict/screens/search/search_screen.dart";
import "package:jsdict/singletons.dart";
import "package:provider/provider.dart";
import "package:podprefs/podprefs.dart";

void main() async {
await registerSingletons();
registerKanjivgLicense();
runApp(const JsDictApp());

runApp(
ProviderScope(
overrides: [await initializePreferences()],
child: const JsDictApp(),
),
);
}

void registerKanjivgLicense() => LicenseRegistry.addLicense(() async* {
Expand All @@ -22,7 +27,7 @@ void registerKanjivgLicense() => LicenseRegistry.addLicense(() async* {
);
});

class JsDictApp extends StatelessWidget {
class JsDictApp extends ConsumerWidget {
const JsDictApp();

static const _mainColor = Color(0xFF27CA27);
Expand Down Expand Up @@ -75,30 +80,23 @@ class JsDictApp extends StatelessWidget {
);

@override
Widget build(BuildContext context) {
return DynamicColorBuilder(
builder: (lightDynamic, darkDynamic) => ProviderScope(
child: MultiProvider(
providers: [ChangeNotifierProvider(create: (_) => ThemeProvider())],
builder: (context, _) {
final themeProvider = Provider.of<ThemeProvider>(context);
Widget build(BuildContext context, WidgetRef ref) {
final isDynamicColorsEnabled = ref.watch(prefDynamicColors);

return MaterialApp(
title: _createTitle("JS-Dict"),
themeMode: themeProvider.currentTheme,
theme: _createThemeData(
dynamicColorScheme: lightDynamic,
isDynamicColorsEnabled: themeProvider.dynamicColors,
),
darkTheme: _createThemeData(
dynamicColorScheme: darkDynamic,
isDynamicColorsEnabled: themeProvider.dynamicColors,
brightness: Brightness.dark,
),
home: const SearchScreen(),
);
},
return DynamicColorBuilder(
builder: (lightDynamic, darkDynamic) => MaterialApp(
title: _createTitle("JS-Dict"),
themeMode: ref.watch(prefThemeMode),
theme: _createThemeData(
dynamicColorScheme: lightDynamic,
isDynamicColorsEnabled: isDynamicColorsEnabled,
),
darkTheme: _createThemeData(
dynamicColorScheme: darkDynamic,
isDynamicColorsEnabled: isDynamicColorsEnabled,
brightness: Brightness.dark,
),
home: const SearchScreen(),
),
);
}
Expand Down
10 changes: 10 additions & 0 deletions lib/providers/prefs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "package:flutter/material.dart";
import "package:podprefs/podprefs.dart";

final prefThemeMode = createMappedPreference("ThemeMode", ThemeMode.system, {
ThemeMode.system: "System",
ThemeMode.light: "Light",
ThemeMode.dark: "Dark",
});

final prefDynamicColors = createBoolPreference("DynamicColors", true);
34 changes: 0 additions & 34 deletions lib/providers/theme_provider.dart

This file was deleted.

69 changes: 47 additions & 22 deletions lib/screens/settings_screen.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import "package:dynamic_color/dynamic_color.dart";
import "package:flutter/material.dart";
import "package:jsdict/providers/theme_provider.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:jsdict/providers/prefs.dart";
import "package:package_info_plus/package_info_plus.dart";
import "package:provider/provider.dart";
import "package:podprefs/podprefs.dart";

class SettingScreen extends StatelessWidget {
const SettingScreen();
Expand All @@ -17,33 +18,20 @@ class SettingScreen extends StatelessWidget {
ListTile(
leading: const Icon(Icons.water_drop, size: 32),
title: const Text("Theme"),
trailing: Consumer<ThemeProvider>(
builder: (context, provider, _) {
return DropdownButton(
value: provider.currentThemeString,
items: ThemeProvider.themes
.map(
(theme) => DropdownMenuItem(
value: theme,
child: Text(theme),
),
)
.toList(),
onChanged: (value) => provider.setTheme(value!),
);
trailing: _EnumPref(
preference: prefThemeMode,
stringMap: const {
ThemeMode.system: "System",
ThemeMode.light: "Light",
ThemeMode.dark: "Dark",
},
),
),
if (dynamicColorScheme != null)
ListTile(
leading: const Icon(Icons.format_color_fill, size: 32),
title: const Text("Dynamic Colors"),
trailing: Consumer<ThemeProvider>(
builder: (context, provider, _) => Switch(
value: provider.dynamicColors,
onChanged: provider.setDynamicColors,
),
),
trailing: _BoolPref(preference: prefDynamicColors),
),
const Divider(),
ListTile(
Expand All @@ -67,3 +55,40 @@ class SettingScreen extends StatelessWidget {
);
}
}

class _EnumPref<T> extends ConsumerWidget {
const _EnumPref({required this.preference, required this.stringMap});

final Preference<T> preference;
final Map<T, String> stringMap;

@override
Widget build(BuildContext context, WidgetRef ref) {
return DropdownButton(
value: ref.watch(preference),
items: stringMap.entries
.map(
(entry) => DropdownMenuItem(
value: entry.key,
child: Text(entry.value),
),
)
.toList(),
onChanged: (value) => ref.read(preference.notifier).update(value as T),
);
}
}

class _BoolPref extends ConsumerWidget {
const _BoolPref({required this.preference});

final Preference<bool> preference;

@override
Widget build(BuildContext context, WidgetRef ref) {
return Switch(
value: ref.watch(preference),
onChanged: ref.read(preference.notifier).update,
);
}
}
6 changes: 0 additions & 6 deletions lib/singletons.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import "package:flutter/material.dart";
import "package:get_it/get_it.dart";
import "package:jsdict/packages/jisho_client/jisho_client.dart";
import "package:shared_preferences/shared_preferences.dart";

Future<void> registerSingletons() async {
WidgetsFlutterBinding.ensureInitialized();

GetIt.I.registerLazySingleton<JishoClient>(JishoClient.new);
GetIt.I.registerSingletonAsync<SharedPreferences>(
SharedPreferences.getInstance,
);

return await GetIt.I.allReady();
}

JishoClient getClient() => GetIt.I<JishoClient>();

SharedPreferences getPreferences() => GetIt.I<SharedPreferences>();
25 changes: 9 additions & 16 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -661,14 +661,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.6"
nested:
dependency: transitive
description:
name: nested
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
node_preamble:
dependency: transitive
description:
Expand Down Expand Up @@ -789,6 +781,15 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.8"
podprefs:
dependency: "direct main"
description:
path: "."
ref: "0.1.0"
resolved-ref: "4ae7cce8dbfd59d8127b775c88a41e92e4d816f8"
url: "https://github.com/petlyh/podprefs"
source: git
version: "0.1.0"
pool:
dependency: transitive
description:
Expand All @@ -797,14 +798,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.5.1"
provider:
dependency: "direct main"
description:
name: provider
sha256: c8a055ee5ce3fd98d6fc872478b03823ffdb448699c6ebdbbc71d59b596fd48c
url: "https://pub.dev"
source: hosted
version: "6.1.2"
pub_semver:
dependency: transitive
description:
Expand Down
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ dependencies:
http: ^1.0.0
infinite_scroll_pagination: ^4.0.0
package_info_plus: ^8.0.2
provider: ^6.0.5
podprefs:
git:
url: https://github.com/petlyh/podprefs
ref: 0.1.0
receive_sharing_intent: ^1.6.8
ruby_text:
git: # fork adds extra alignment options
Expand Down

0 comments on commit 2039b50

Please sign in to comment.