diff --git a/lib/models/settings.dart b/lib/models/settings.dart index 5511556..e1d235c 100644 --- a/lib/models/settings.dart +++ b/lib/models/settings.dart @@ -1,6 +1,5 @@ import 'package:flutter_data/flutter_data.dart'; import 'package:json_annotation/json_annotation.dart'; -import 'package:zapstore/main.dart'; import 'package:zapstore/models/user.dart'; part 'settings.g.dart'; @@ -12,8 +11,6 @@ class Settings extends DataModel { String get id => '_'; final BelongsTo user = BelongsTo(); bool isLoggedIn = false; - @JsonKey(defaultValue: 1) - int dbVersion = kDbVersion; } mixin SettingsAdapter on Adapter { diff --git a/lib/models/settings.g.dart b/lib/models/settings.g.dart index ea6ef74..621225e 100644 --- a/lib/models/settings.g.dart +++ b/lib/models/settings.g.dart @@ -60,11 +60,9 @@ extension SettingsRelationshipGraphNodeX on RelationshipGraphNode { // JsonSerializableGenerator // ************************************************************************** -Settings _$SettingsFromJson(Map json) => Settings() - ..isLoggedIn = json['isLoggedIn'] as bool - ..dbVersion = (json['dbVersion'] as num?)?.toInt() ?? 1; +Settings _$SettingsFromJson(Map json) => + Settings()..isLoggedIn = json['isLoggedIn'] as bool; Map _$SettingsToJson(Settings instance) => { 'isLoggedIn': instance.isLoggedIn, - 'dbVersion': instance.dbVersion, }; diff --git a/lib/navigation/app_initializer.dart b/lib/navigation/app_initializer.dart index 60e8ddb..3875f3f 100644 --- a/lib/navigation/app_initializer.dart +++ b/lib/navigation/app_initializer.dart @@ -1,6 +1,7 @@ import 'package:collection/collection.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_data/flutter_data.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:zapstore/main.dart'; import 'package:zapstore/main.data.dart'; import 'package:zapstore/models/app.dart'; @@ -11,15 +12,20 @@ import 'package:zapstore/widgets/app_curation_container.dart'; AppLifecycleListener? _lifecycleListener; final appInitializer = FutureProvider((ref) async { + final prefs = await SharedPreferences.getInstance(); + final dbVersion = prefs.getInt('dbVersion'); + + // Perform migration + if (dbVersion == null || dbVersion < kDbVersion) { + final storage = ref.read(localStorageProvider); + await storage.initialize(inIsolate: true); + await storage.destroy(); + await prefs.setInt('dbVersion', kDbVersion); + } + // Initialize Flutter Data await ref.read(initializeFlutterData(adapterProvidersMap).future); - // Check DB version - final userDbVersion = ref.settings.findOneLocalById('_')!.dbVersion; - if (userDbVersion < kDbVersion) { - await ref.read(localStorageProvider).destroy(); - } - _lifecycleListener = AppLifecycleListener( onStateChange: (state) async { if (state == AppLifecycleState.resumed) { diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 78a1cf6..b2ca7fe 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -8,6 +8,7 @@ import Foundation import app_links import device_info_plus import path_provider_foundation +import shared_preferences_foundation import sqflite import sqlite3_flutter_libs import url_launcher_macos @@ -16,6 +17,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { AppLinksMacosPlugin.register(with: registry.registrar(forPlugin: "AppLinksMacosPlugin")) DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) + SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin")) Sqlite3FlutterLibsPlugin.register(with: registry.registrar(forPlugin: "Sqlite3FlutterLibsPlugin")) UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin")) diff --git a/pubspec.lock b/pubspec.lock index f54ad28..590de6d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -962,6 +962,62 @@ packages: url: "https://pub.dev" source: hosted version: "0.28.0" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + sha256: "746e5369a43170c25816cc472ee016d3a66bc13fcf430c0bc41ad7b4b2922051" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + sha256: "480ba4345773f56acda9abf5f50bd966f581dac5d514e5fc4a18c62976bbba7e" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + shared_preferences_foundation: + dependency: transitive + description: + name: shared_preferences_foundation + sha256: "07e050c7cd39bad516f8d64c455f04508d09df104be326d8c02551590a0d513d" + url: "https://pub.dev" + source: hosted + version: "2.5.3" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + sha256: "580abfd40f415611503cae30adf626e6656dfb2f0cee8f465ece7b6defb40f2f" + url: "https://pub.dev" + source: hosted + version: "2.4.1" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + sha256: "57cbf196c486bc2cf1f02b85784932c6094376284b3ad5779d1b1c6c6a816b80" + url: "https://pub.dev" + source: hosted + version: "2.4.1" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + sha256: d2ca4132d3946fec2184261726b355836a82c33d7d5b67af32692aff18a4684e + url: "https://pub.dev" + source: hosted + version: "2.4.2" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + sha256: "94ef0f72b2d71bc3e700e025db3710911bd51a71cefb65cc609dd0d9a982e3c1" + url: "https://pub.dev" + source: hosted + version: "2.4.1" shelf: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 8f89409..3334120 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -48,6 +48,7 @@ dependencies: dart_emoji: ^0.2.0+2 remove_markdown: ^0.0.2 install_plugin: ^2.1.0 + shared_preferences: ^2.3.2 dependency_overrides: purplebase: