@@ -13,6 +13,50 @@ import '../api/route/messages.dart';
13
13
import '../credential_fixture.dart' as credentials;
14
14
import 'message_list.dart' ;
15
15
16
+ /// Store for the user's cross-account data.
17
+ ///
18
+ /// This includes data that is independent of the account, like some settings.
19
+ /// It also includes a small amount of data for each account: enough to
20
+ /// authenticate as the active account, if there is one.
21
+ class GlobalStore extends ChangeNotifier {
22
+ GlobalStore ._({required Map <int , Account > accounts})
23
+ : _accounts = accounts;
24
+
25
+ // For convenience, a number we won't use as an ID in the database table.
26
+ static const fixtureAccountId = - 1 ;
27
+
28
+ // We keep the API simple and synchronous for the bulk of the app's code
29
+ // by doing this loading up front before constructing a [GlobalStore].
30
+ static Future <GlobalStore > load () async {
31
+ const accounts = {fixtureAccountId: _fixtureAccount};
32
+ return GlobalStore ._(accounts: accounts);
33
+ }
34
+
35
+ final Map <int , Account > _accounts;
36
+
37
+ // TODO settings (those that are per-device rather than per-account)
38
+ // TODO push token, and other data corresponding to GlobalSessionState
39
+
40
+ // Just an Iterable, not the actual Map, to avoid clients mutating the map.
41
+ // Mutations should go through the setters/mutators below.
42
+ Iterable <Account > get accounts => _accounts.values;
43
+
44
+ Account ? getAccount (int id) => _accounts[id];
45
+
46
+ // TODO add setters/mutators; will want to write to database
47
+ // Future<void> insertAccount...
48
+ // Future<void> updateAccount...
49
+
50
+ // TODO add a registry of [PerAccountStore]s, like the latter's of [MessageListView]
51
+ // That will allow us to have many [PerAccountRoot] widgets for a given
52
+ // account, e.g. at the top of each page; and to access server data from
53
+ // outside any [PerAccountRoot], e.g. for handling a notification.
54
+ }
55
+
56
+ /// Store for the user's data for a given Zulip account.
57
+ ///
58
+ /// This should always have a consistent snapshot of the state on the server,
59
+ /// as maintained by the Zulip event system.
16
60
class PerAccountStore extends ChangeNotifier {
17
61
PerAccountStore ._({
18
62
required this .account,
@@ -23,9 +67,10 @@ class PerAccountStore extends ChangeNotifier {
23
67
required this .subscriptions,
24
68
});
25
69
26
- // Load the user's data from storage. (Once we have such a thing.)
27
- static Future <PerAccountStore > load () async {
28
- const account = _fixtureAccount;
70
+ /// Load the user's data from the server, and start an event queue going.
71
+ ///
72
+ /// In the future this might load an old snapshot from local storage first.
73
+ static Future <PerAccountStore > load (Account account) async {
29
74
final connection = ApiConnection (auth: account);
30
75
31
76
final stopwatch = Stopwatch ()..start ();
@@ -124,6 +169,7 @@ const Account _fixtureAccount = Account(
124
169
apiKey: credentials.api_key,
125
170
);
126
171
172
+ @immutable
127
173
class Account implements Auth {
128
174
const Account (
129
175
{required this .realmUrl, required this .email, required this .apiKey});
0 commit comments