import "dart:convert"; import "package:fast_immutable_collections/fast_immutable_collections.dart"; import "package:flutter_riverpod/flutter_riverpod.dart"; import "package:nexus/controllers/key.dart"; import "package:nexus/models/account.dart"; class AccountsController extends AsyncNotifier> { @override Future> build() async { final stored = await ref.watch( KeyController.provider(KeyController.accountsKey).future, ); final accounts = IList( (json.decode(stored ?? "[]") as List).map( (account) => .fromJson(account), ), ); // Installs from before account switching are logged into the legacy slot return accounts.isEmpty ? .new([const .new(id: Account.legacyId)]) : accounts; } Future _set(IList accounts) async { state = .data(accounts); await ref .read(KeyController.provider(KeyController.accountsKey).notifier) .set(json.encode(accounts.map((account) => account.toJson()).toList())); } Future> get _current async { await future; return state.requireValue; } Future save(Account account) async { final accounts = await _current; final index = accounts.indexWhere((existing) => existing.id == account.id); await _set( index == -1 ? accounts.add(account) : accounts.replace(index, account), ); } Future reset(String id) async { final accounts = await _current; final index = accounts.indexWhere((account) => account.id == id); if (index != -1) await _set(accounts.replace(index, .new(id: id))); } Future remove(String id) async => _set((await _current).removeWhere((account) => account.id == id)); static final provider = AsyncNotifierProvider>( AccountsController.new, ); }