Skip to content

Commit

Permalink
テーマをインストールできるように
Browse files Browse the repository at this point in the history
  • Loading branch information
poppingmoon committed Nov 24, 2023
1 parent 4b49524 commit 655967d
Show file tree
Hide file tree
Showing 8 changed files with 390 additions and 89 deletions.
12 changes: 12 additions & 0 deletions lib/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:file/local.dart';
import 'package:flutter/widgets.dart';
import 'package:miria/model/account.dart';
import 'package:miria/model/acct.dart';
import 'package:miria/model/color_theme.dart';
import 'package:miria/model/tab_setting.dart';
import 'package:miria/repository/account_repository.dart';
import 'package:miria/repository/account_settings_repository.dart';
Expand All @@ -14,6 +15,7 @@ import 'package:miria/repository/favorite_repository.dart';
import 'package:miria/repository/general_settings_repository.dart';
import 'package:miria/repository/hybrid_timeline_repository.dart';
import 'package:miria/repository/import_export_repository.dart';
import 'package:miria/repository/color_theme_repository.dart';
import 'package:miria/repository/main_stream_repository.dart';
import 'package:miria/repository/global_time_line_repository.dart';
import 'package:miria/repository/home_time_line_repository.dart';
Expand Down Expand Up @@ -266,3 +268,13 @@ final misskeyServerListNotifierProvider = AsyncNotifierProvider.autoDispose<
);

final cacheManagerProvider = Provider<BaseCacheManager?>((ref) => null);

final colorThemeRepositoryProvider =
NotifierProvider<ColorThemeRepository, List<ColorTheme>>(
ColorThemeRepository.new,
);

final installedThemeCodeRepositoryProvider =
NotifierProvider<InstalledThemeCodeRepository, List<String>>(
InstalledThemeCodeRepository.new,
);
88 changes: 88 additions & 0 deletions lib/repository/color_theme_repository.dart
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;
}
}
2 changes: 2 additions & 0 deletions lib/router/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import 'package:miria/view/photo_edit_page/photo_edit_page.dart';
import 'package:miria/view/settings_page/account_settings_page/account_list.dart';
import 'package:miria/view/settings_page/app_info_page/app_info_page.dart';
import 'package:miria/view/settings_page/general_settings_page/general_settings_page.dart';
import 'package:miria/view/settings_page/general_settings_page/installed_themes_page.dart';
import 'package:miria/view/settings_page/import_export_page/import_export_page.dart';
import 'package:miria/view/settings_page/tab_settings_page/tab_settings_list_page.dart';
import 'package:miria/view/several_account_settings_page/hard_mute_page/hard_mute_page.dart';
Expand Down Expand Up @@ -97,5 +98,6 @@ class AppRouter extends _$AppRouter {
AutoRoute(page: SharingAccountSelectRoute.page),
// きしょ……
AutoRoute(page: MisskeyRouteRoute.page),
AutoRoute(page: InstalledThemesRoute.page),
];
}
116 changes: 68 additions & 48 deletions lib/router/app_router.gr.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 655967d

Please sign in to comment.