mirror of
https://gitlab.com/veilid/veilidchat.git
synced 2025-05-02 06:16:16 -04:00
more refactor
This commit is contained in:
parent
ba4ef05a28
commit
b83aa3a64b
39 changed files with 722 additions and 514 deletions
34
lib/settings/preferences_repository.dart
Normal file
34
lib/settings/preferences_repository.dart
Normal file
|
@ -0,0 +1,34 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../tools/tools.dart';
|
||||
import 'models/models.dart';
|
||||
|
||||
class PreferencesRepository {
|
||||
PreferencesRepository._();
|
||||
|
||||
late final SharedPreferencesValue<Preferences> _data;
|
||||
|
||||
Preferences get value => _data.requireValue;
|
||||
Stream<Preferences> get stream => _data.stream;
|
||||
|
||||
//////////////////////////////////////////////////////////////
|
||||
/// Singleton initialization
|
||||
|
||||
static PreferencesRepository instance = PreferencesRepository._();
|
||||
|
||||
Future<void> init() async {
|
||||
final sharedPreferences = await SharedPreferences.getInstance();
|
||||
_data = SharedPreferencesValue<Preferences>(
|
||||
sharedPreferences: sharedPreferences,
|
||||
keyName: 'preferences',
|
||||
valueFromJson: (obj) =>
|
||||
obj != null ? Preferences.fromJson(obj) : Preferences.defaults,
|
||||
valueToJson: (val) => val.toJson());
|
||||
await _data.get();
|
||||
}
|
||||
|
||||
Future<void> set(Preferences value) => _data.set(value);
|
||||
Future<Preferences> get() => _data.get();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue