-
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
4b49524
commit 655967d
Showing
8 changed files
with
390 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
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/common/error_dialog_handler.dart'; | ||
import 'package:miria/view/themes/built_in_color_themes.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
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 SpecifiedException("テーマの形式が間違っています"); | ||
} | ||
if (state.any((element) => element.id == theme.id)) { | ||
throw SpecifiedException("このテーマは既にインストールされています"); | ||
} | ||
|
||
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.
Oops, something went wrong.