-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07e6895
commit 26975cc
Showing
11 changed files
with
757 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:json5/json5.dart'; | ||
import 'package:miria/model/color_theme.dart'; | ||
import 'package:miria/model/misskey_theme.dart'; | ||
import 'package:miria/providers.dart'; | ||
import 'package:miria/view/themes/built_in_color_themes.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
sealed class ColorThemeException implements Exception {} | ||
|
||
class InvalidThemeFormatException implements ColorThemeException {} | ||
|
||
class DuplicatedThemeException implements ColorThemeException {} | ||
|
||
class ColorThemeRepository extends Notifier<List<ColorTheme>> { | ||
@override | ||
List<ColorTheme> build() { | ||
final codes = ref.watch(installedThemeCodeRepositoryProvider); | ||
return [ | ||
...builtInColorThemes, | ||
...codes.map((code) { | ||
try { | ||
return ColorTheme.misskey( | ||
MisskeyTheme.fromJson(json5Decode(code) as Map<String, dynamic>), | ||
); | ||
} catch (e) { | ||
return null; | ||
} | ||
}).nonNulls, | ||
]; | ||
} | ||
|
||
Future<void> addTheme(String code) async { | ||
final ColorTheme theme; | ||
try { | ||
theme = ColorTheme.misskey( | ||
MisskeyTheme.fromJson(json5Decode(code) as Map<String, dynamic>), | ||
); | ||
} catch (e) { | ||
throw InvalidThemeFormatException(); | ||
} | ||
if (state.any((element) => element.id == theme.id)) { | ||
throw DuplicatedThemeException(); | ||
} | ||
|
||
await ref | ||
.read(installedThemeCodeRepositoryProvider.notifier) | ||
.addTheme(code); | ||
} | ||
|
||
Future<void> removeTheme(String id) async { | ||
await ref | ||
.read(installedThemeCodeRepositoryProvider.notifier) | ||
.removeTheme(id); | ||
} | ||
} | ||
|
||
class InstalledThemeCodeRepository extends Notifier<List<String>> { | ||
final prefsKey = "themes"; | ||
|
||
@override | ||
List<String> build() { | ||
Future(load); | ||
return []; | ||
} | ||
|
||
Future<void> load() async { | ||
final prefs = await SharedPreferences.getInstance(); | ||
final themes = prefs.getStringList(prefsKey); | ||
if (themes == null) { | ||
return; | ||
} | ||
state = themes; | ||
} | ||
|
||
Future<void> addTheme(String code) async { | ||
final prefs = await SharedPreferences.getInstance(); | ||
final codes = prefs.getStringList(prefsKey) ?? []; | ||
codes.add(code); | ||
await prefs.setStringList(prefsKey, codes); | ||
state = codes; | ||
} | ||
|
||
Future<void> removeTheme(String id) async { | ||
final prefs = await SharedPreferences.getInstance(); | ||
final codes = prefs.getStringList(prefsKey); | ||
codes!.removeWhere( | ||
(code) => (json5Decode(code) as Map<String, dynamic>)["id"] == id, | ||
); | ||
await prefs.setStringList(prefsKey, codes); | ||
state = codes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.