diff --git a/assets/default_themes/dark.zip b/assets/default_themes/dark.zip index e2291ba2b..9823262a9 100644 Binary files a/assets/default_themes/dark.zip and b/assets/default_themes/dark.zip differ diff --git a/assets/default_themes/light.zip b/assets/default_themes/light.zip index c9579152d..649716126 100644 Binary files a/assets/default_themes/light.zip and b/assets/default_themes/light.zip differ diff --git a/lib/main.dart b/lib/main.dart index d2ef627f8..2af01e602 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -189,6 +189,27 @@ void main() async { // check and update or install default themes await ThemeService.instance.checkDefaultThemesOnStartup(); + // verify current user preference theme and revert to default + // if problems are found to prevent app being unusable + if (!(await ThemeService.instance + .verifyInstalled(themeId: Prefs.instance.themeId))) { + Prefs.instance.themeId = "light"; + } + + // verify current user preference light brightness theme and revert to default + // if problems are found to prevent app being unusable + if (!(await ThemeService.instance + .verifyInstalled(themeId: Prefs.instance.systemBrightnessLightThemeId))) { + Prefs.instance.systemBrightnessLightThemeId = "light"; + } + + // verify current user preference dark brightness theme and revert to default + // if problems are found to prevent app being unusable + if (!(await ThemeService.instance + .verifyInstalled(themeId: Prefs.instance.systemBrightnessDarkThemeId))) { + Prefs.instance.systemBrightnessDarkThemeId = "dark"; + } + runApp(const ProviderScope(child: MyApp())); } diff --git a/lib/models/isar/stack_theme.dart b/lib/models/isar/stack_theme.dart index f072feb02..a76be7ad9 100644 --- a/lib/models/isar/stack_theme.dart +++ b/lib/models/isar/stack_theme.dart @@ -1507,7 +1507,7 @@ class StackTheme { themeId: json["id"] as String, ) : null - ..assetsV2 = version == 2 + ..assetsV2 = version >= 2 ? ThemeAssetsV2.fromJson( json: Map.from(json["assets"] as Map), applicationThemesDirectoryPath: applicationThemesDirectoryPath, diff --git a/lib/themes/theme_service.dart b/lib/themes/theme_service.dart index ee831af83..db5b851e1 100644 --- a/lib/themes/theme_service.dart +++ b/lib/themes/theme_service.dart @@ -17,7 +17,7 @@ final pThemeService = Provider((ref) { }); class ThemeService { - static const _currentDefaultThemeVersion = 2; + static const _currentDefaultThemeVersion = 3; ThemeService._(); static ThemeService? _instance; static ThemeService get instance => _instance ??= ThemeService._(); @@ -40,9 +40,7 @@ class ThemeService { throw Exception("Invalid theme archive: Missing theme.json"); } - final OutputStream os = OutputStream(); - themeJsonFiles.first.decompress(os); - final String jsonString = utf8.decode(os.getBytes()); + final jsonString = utf8.decode(themeJsonFiles.first.content as List); final json = jsonDecode(jsonString) as Map; final theme = StackTheme.fromJson( @@ -50,6 +48,12 @@ class ThemeService { applicationThemesDirectoryPath: themesDir.path, ); + try { + theme.assets; + } catch (_) { + throw Exception("Invalid theme: Failed to create assets object"); + } + final String assetsPath = "${themesDir.path}/${theme.themeId}"; for (final file in archive.files) { @@ -84,7 +88,10 @@ class ThemeService { await db.isar.writeTxn(() async { await db.isar.stackThemes.delete(isarId); }); - await Directory("${themesDir.path}/$themeId").delete(recursive: true); + final dir = Directory("${themesDir.path}/$themeId"); + if (dir.existsSync()) { + await dir.delete(recursive: true); + } } else { Logging.instance.log( "Failed to delete theme $themeId", @@ -159,9 +166,15 @@ class ThemeService { // TODO more thorough check/verification of theme Future verifyInstalled({required String themeId}) async { - final dbHasTheme = - await db.isar.stackThemes.where().themeIdEqualTo(themeId).count() > 0; - if (dbHasTheme) { + final theme = + await db.isar.stackThemes.where().themeIdEqualTo(themeId).findFirst(); + if (theme != null) { + try { + theme.assets; + } catch (_) { + return false; + } + final themesDir = await StackFileSystem.applicationThemesDirectory(); final jsonFileExists = await File("${themesDir.path}/$themeId/theme.json").exists();