Skip to content

Commit

Permalink
Upgrade preferences situation
Browse files Browse the repository at this point in the history
Migrated to SharedPreferencesWithCache
  • Loading branch information
TechnicJelle committed Sep 7, 2024
1 parent cf8c590 commit 8cf60e5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ String get blueMapCliJarName => blueMapCliJarUrl.split("/").last;
const String commit = String.fromEnvironment("commit", defaultValue: "development");

Future<void> main() async {
await Prefs.init();
await initPrefs();

runApp(const ProviderScope(child: MyApp()));
}
Expand Down
50 changes: 22 additions & 28 deletions lib/prefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,42 @@ import "dart:io";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:shared_preferences/shared_preferences.dart";

late final SharedPreferencesWithCache _prefs;

Future<void> initPrefs() async {
_prefs = await SharedPreferencesWithCache.create(
cacheOptions: const SharedPreferencesWithCacheOptions(
allowList: {
JavaPathNotifier._javaPathKey,
ProjectDirectoryNotifier._projectPathKey,
},
),
);
}

class JavaPathNotifier extends Notifier<String?> {
static const String javaPathKey = "java_path";
static const String _javaPathKey = "java_path";

@override
String? build() {
return Prefs.instance._prefs.getString(javaPathKey);
return _prefs.getString(_javaPathKey);
}

void setJavaPath(String javaPath) {
Prefs.instance._prefs.setString(javaPathKey, javaPath);
state = build();
state = javaPath;
_prefs.setString(_javaPathKey, javaPath);
}
}

final javaPathProvider =
NotifierProvider<JavaPathNotifier, String?>(() => JavaPathNotifier());

class ProjectDirectoryNotifier extends Notifier<Directory?> {
static const String projectPathKey = "project_path";
static const String _projectPathKey = "project_path";

@override
Directory? build() {
final String? bluemapJarPath = Prefs.instance._prefs.getString(projectPathKey);
final String? bluemapJarPath = _prefs.getString(_projectPathKey);
if (bluemapJarPath == null) {
return null;
} else {
Expand All @@ -34,34 +47,15 @@ class ProjectDirectoryNotifier extends Notifier<Directory?> {
}

void openProject(Directory projectDirectory) {
Prefs.instance._prefs.setString(projectPathKey, projectDirectory.path);
state = build();
state = projectDirectory;
_prefs.setString(_projectPathKey, projectDirectory.path);
}

void closeProject() {
Prefs.instance._prefs.remove(projectPathKey);
state = null;
_prefs.remove(_projectPathKey);
}
}

final projectDirectoryProvider = NotifierProvider<ProjectDirectoryNotifier, Directory?>(
() => ProjectDirectoryNotifier());

class Prefs {
// == Static ==
static late Prefs _instance;

static Prefs get instance => _instance;

// == Private Variables ==
final SharedPreferences _prefs;

// == Constructors ==
Prefs._(this._prefs);

// == Public Methods ==
static Future<void> init() async {
final prefs = await SharedPreferences.getInstance();
_instance = Prefs._(prefs);
}
}

0 comments on commit 8cf60e5

Please sign in to comment.