Files
MoneyMgr/moneymgr_mobile/lib/services/storage/prefs.dart
Pierre HUBERT 5b16ca6162
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Load profile information on startup
2025-07-08 19:34:40 +02:00

46 lines
1.3 KiB
Dart

import 'dart:convert';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:moneymgr_mobile/services/api/auth_api.dart';
import 'package:moneymgr_mobile/services/api/server_api.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared_preferences/shared_preferences.dart';
part 'prefs.g.dart';
@riverpod
Future<SharedPreferencesWithCache> prefs(Ref ref) =>
SharedPreferencesWithCache.create(
cacheOptions: const SharedPreferencesWithCacheOptions(),
);
extension MoneyMgrSharedPreferences on SharedPreferencesWithCache {
ServerConfig? serverConfig() {
final json = getString("serverConfig");
if (json != null) return ServerConfig.fromJson(jsonDecode(json));
return null;
}
Future<void> setServerConfig(ServerConfig config) async {
await setString("serverConfig", jsonEncode(config));
}
Future<void> clearServerConfig() async {
await remove("serverConfig");
}
AuthInfo? authInfo() {
final json = getString("authInfo");
if (json != null) return AuthInfo.fromJson(jsonDecode(json));
return null;
}
Future<void> setAuthInfo(AuthInfo info) async {
await setString("authInfo", jsonEncode(info));
}
Future<void> clearAuthInfo() async {
await remove("authInfo");
}
}