41 lines
882 B
Dart
41 lines
882 B
Dart
import 'package:boardgames_app/models/user.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:localstorage/localstorage.dart';
|
|
|
|
class State with ChangeNotifier {
|
|
static final LocalStorage _storage = LocalStorage('board_games.json');
|
|
static final State _instance = State._();
|
|
late Future<void> ready;
|
|
// Global State
|
|
final GameUser user = GameUser();
|
|
|
|
State._() {
|
|
ready = init();
|
|
}
|
|
|
|
Future<void> init() async {
|
|
if (!(await _storage.ready)) {
|
|
// TODO: Make it more resilient, in case of failures send signal, proceed?
|
|
throw Exception("Failed to init the store");
|
|
}
|
|
|
|
await Future.wait([
|
|
user.init(),
|
|
]);
|
|
}
|
|
|
|
factory State.get() {
|
|
return _instance;
|
|
}
|
|
|
|
LocalStorage get storage {
|
|
return _storage;
|
|
}
|
|
}
|
|
|
|
mixin WithState {
|
|
// state with ready storage
|
|
State state = State.get();
|
|
Future<void> init();
|
|
}
|