diff --git a/lib/main.dart b/lib/main.dart index 0ce76e9..be1d3d7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -7,6 +7,7 @@ import 'pages/collections_list_page.dart'; import 'pages/search_page.dart'; import 'pages/settings_page.dart'; import 'service/search_provider.dart'; +import 'service/settings_provider.dart'; import 'service/shared_link_provider.dart'; import 'service/storage.dart'; import 'service/share_intent_service.dart'; @@ -20,6 +21,7 @@ void main() async { providers: [ ChangeNotifierProvider(create: (_) => SharedLinkProvider()), ChangeNotifierProvider(create: (_) => SearchProvider()), + ChangeNotifierProvider(create: (_) => SettingsProvider()), ], child: const MapsBookmarks(), ), diff --git a/lib/service/settings_provider.dart b/lib/service/settings_provider.dart new file mode 100644 index 0000000..19dcc1c --- /dev/null +++ b/lib/service/settings_provider.dart @@ -0,0 +1,20 @@ +import 'package:flutter/material.dart'; + +import '../model/settings.dart'; +import 'storage.dart'; + +class SettingsProvider extends ChangeNotifier { + SettingsProvider() : _settings = Storage.loadSettings(); + + Settings _settings; + + Settings get settings => _settings; + + void setExportDirectoryPath(String path, {bool silent = false}) { + _settings = _settings.copyWith(exportDirectoryPath: path); + _saveSettings(); + if (!silent) notifyListeners(); + } + + void _saveSettings() => Storage.saveSettings(_settings); +} diff --git a/lib/service/storage.dart b/lib/service/storage.dart index 8ae8bac..bbe0385 100644 --- a/lib/service/storage.dart +++ b/lib/service/storage.dart @@ -4,22 +4,46 @@ import 'package:shared_preferences/shared_preferences.dart'; import '../model/bookmark.dart'; import '../model/collection.dart'; +import '../model/settings.dart'; import 'json_file_service.dart'; class Storage { static const String _bookmarksKey = 'bookmarks'; static const String _collectionsKey = 'collections'; static SharedPreferencesWithCache? _prefsWithCache; + static const String _settingsKey = 'settings'; static const String _statsKey = 'stats'; static Future initialize() async { _prefsWithCache = await SharedPreferencesWithCache.create( cacheOptions: const SharedPreferencesWithCacheOptions( - allowList: {_collectionsKey, _bookmarksKey, _statsKey}, + allowList: { + _collectionsKey, + _bookmarksKey, + _statsKey, + _settingsKey, + }, ), ); } + static Settings loadSettings() { + final jsonString = _prefs.getString(_settingsKey); + if (jsonString != null) { + final json = jsonDecode(jsonString) as Map; + return Settings.fromJson(json); + } else { + final settings = Settings.defaults(); + saveSettings(settings); + return settings; + } + } + + static Future saveSettings(Settings settings) { + final json = jsonEncode(settings.toJson()); + return _prefs.setString(_settingsKey, json); + } + static List loadCollections() { final jsonString = _prefs.getString(_collectionsKey) ?? '[]'; final jsonList = jsonDecode(jsonString) as List; @@ -156,15 +180,6 @@ class Storage { await _prefs.setString(_statsKey, jsonEncode(stats)); } - static SharedPreferencesWithCache get _prefs { - if (_prefsWithCache == null) { - throw StateError( - 'BookmarkStorage not initialized. Call initialize() first.', - ); - } - return _prefsWithCache!; - } - static Future exportToJsonFile() => JsonFileService.exportToJson( collections: loadCollections(), bookmarks: loadBookmarks(), @@ -180,4 +195,13 @@ class Storage { } return false; } + + static SharedPreferencesWithCache get _prefs { + if (_prefsWithCache == null) { + throw StateError( + 'BookmarkStorage not initialized. Call initialize() first.', + ); + } + return _prefsWithCache!; + } }