Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

themes bandaid #37

Merged
merged 10 commits into from
Jun 10, 2023
Binary file modified assets/default_themes/dark.zip
Binary file not shown.
Binary file modified assets/default_themes/light.zip
Binary file not shown.
21 changes: 21 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

Expand Down
2 changes: 1 addition & 1 deletion lib/models/isar/stack_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ class StackTheme {
themeId: json["id"] as String,
)
: null
..assetsV2 = version == 2
..assetsV2 = version >= 2
? ThemeAssetsV2.fromJson(
json: Map<String, dynamic>.from(json["assets"] as Map),
applicationThemesDirectoryPath: applicationThemesDirectoryPath,
Expand Down
29 changes: 21 additions & 8 deletions lib/themes/theme_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final pThemeService = Provider<ThemeService>((ref) {
});

class ThemeService {
static const _currentDefaultThemeVersion = 2;
static const _currentDefaultThemeVersion = 3;
ThemeService._();
static ThemeService? _instance;
static ThemeService get instance => _instance ??= ThemeService._();
Expand All @@ -40,16 +40,20 @@ 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<int>);
final json = jsonDecode(jsonString) as Map;

final theme = StackTheme.fromJson(
json: Map<String, dynamic>.from(json),
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) {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -159,9 +166,15 @@ class ThemeService {

// TODO more thorough check/verification of theme
Future<bool> 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();
Expand Down