Skip to content

Commit 62c332f

Browse files
committed
store [nfc]: Use Account type generated for database
This will keep things simple when we start storing these in the database.
1 parent 9bd35bf commit 62c332f

File tree

5 files changed

+39
-37
lines changed

5 files changed

+39
-37
lines changed

lib/model/database.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'package:path_provider/path_provider.dart';
77

88
part 'database.g.dart';
99

10-
// TODO unify with Account in store.dart
1110
class Accounts extends Table {
1211
Column<int> get id => integer().autoIncrement()();
1312

lib/model/store.dart

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import '../api/model/model.dart';
99
import '../api/route/events.dart';
1010
import '../api/route/messages.dart';
1111
import '../credential_fixture.dart' as credentials;
12+
import 'database.dart';
1213
import 'message_list.dart';
1314

15+
export 'package:drift/drift.dart' show Value;
16+
export 'database.dart' show Account, AccountsCompanion;
17+
1418
/// Store for all the user's data.
1519
///
1620
/// From UI code, use [GlobalStoreWidget.of] to get hold of an appropriate
@@ -109,16 +113,16 @@ abstract class GlobalStore extends ChangeNotifier {
109113
// TODO(#13): rewrite these setters/mutators with a database
110114

111115
/// Add an account to the store, returning its assigned account ID.
112-
Future<int> insertAccount(Account account) async {
113-
final accountId = await doInsertAccount(account);
114-
assert(!_accounts.containsKey(accountId));
115-
_accounts[accountId] = account;
116+
Future<int> insertAccount(AccountsCompanion data) async {
117+
final account = await doInsertAccount(data);
118+
assert(!_accounts.containsKey(account.id));
119+
_accounts[account.id] = account;
116120
notifyListeners();
117-
return accountId;
121+
return account.id;
118122
}
119123

120124
/// Add an account to the underlying data store.
121-
Future<int> doInsertAccount(Account account);
125+
Future<Account> doInsertAccount(AccountsCompanion data);
122126

123127
// More mutators as needed:
124128
// Future<void> updateAccount...
@@ -203,27 +207,6 @@ class PerAccountStore extends ChangeNotifier {
203207
}
204208
}
205209

206-
@immutable
207-
class Account {
208-
const Account({
209-
required this.realmUrl,
210-
required this.userId,
211-
required this.email,
212-
required this.apiKey,
213-
required this.zulipFeatureLevel,
214-
required this.zulipVersion,
215-
required this.zulipMergeBase,
216-
});
217-
218-
final Uri realmUrl;
219-
final int userId;
220-
final String email;
221-
final String apiKey;
222-
final int zulipFeatureLevel;
223-
final String zulipVersion;
224-
final String? zulipMergeBase;
225-
}
226-
227210
class LiveGlobalStore extends GlobalStore {
228211
LiveGlobalStore._({required super.accounts}) : super();
229212

@@ -245,10 +228,19 @@ class LiveGlobalStore extends GlobalStore {
245228
int _nextAccountId = 1;
246229

247230
@override
248-
Future<int> doInsertAccount(Account account) async {
231+
Future<Account> doInsertAccount(AccountsCompanion data) async {
249232
final accountId = _nextAccountId;
250233
_nextAccountId++;
251-
return accountId;
234+
return Account(
235+
id: accountId,
236+
realmUrl: data.realmUrl.value,
237+
userId: data.userId.value,
238+
email: data.email.value,
239+
apiKey: data.apiKey.value,
240+
zulipFeatureLevel: data.zulipFeatureLevel.value,
241+
zulipVersion: data.zulipVersion.value,
242+
zulipMergeBase: data.zulipMergeBase.value,
243+
);
252244
}
253245
}
254246

@@ -257,6 +249,7 @@ class LiveGlobalStore extends GlobalStore {
257249
/// See "Server credentials" in the project README for how to fill in the
258250
/// `credential_fixture.dart` file this requires.
259251
final Account _fixtureAccount = Account(
252+
id: LiveGlobalStore.fixtureAccountId,
260253
realmUrl: Uri.parse(credentials.realmUrl),
261254
email: credentials.email,
262255
apiKey: credentials.apiKey,

lib/widgets/login.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,16 @@ class _EmailPasswordLoginPageState extends State<EmailPasswordLoginPage> {
135135
return;
136136
}
137137

138-
final account = Account(
138+
final globalStore = GlobalStoreWidget.of(context);
139+
final accountId = await globalStore.insertAccount(AccountsCompanion.insert(
139140
realmUrl: realmUrl,
140141
email: result.email,
141142
apiKey: result.apiKey,
142143
userId: userId,
143144
zulipFeatureLevel: widget.serverSettings.zulipFeatureLevel,
144145
zulipVersion: widget.serverSettings.zulipVersion,
145-
zulipMergeBase: widget.serverSettings.zulipMergeBase,
146-
);
147-
final globalStore = GlobalStoreWidget.of(context);
148-
final accountId = await globalStore.insertAccount(account);
146+
zulipMergeBase: Value(widget.serverSettings.zulipMergeBase),
147+
));
149148
if (context.mounted) {} // https://github.com/dart-lang/linter/issues/4007
150149
else {
151150
return;

test/example_data.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const String recentZulipVersion = '6.1';
88
const int recentZulipFeatureLevel = 164;
99

1010
final Account selfAccount = Account(
11+
id: 1001,
1112
realmUrl: realmUrl,
1213
email: 'self@example',
1314
apiKey: 'asdfqwer',
@@ -18,6 +19,7 @@ final Account selfAccount = Account(
1819
);
1920

2021
final Account otherAccount = Account(
22+
id: 1002,
2123
realmUrl: realmUrl,
2224
email: 'other@example',
2325
apiKey: 'sdfgwert',

test/model/store_test.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,18 @@ class TestGlobalStore extends GlobalStore {
113113
int _nextAccountId = 1;
114114

115115
@override
116-
Future<int> doInsertAccount(Account account) async {
116+
Future<Account> doInsertAccount(AccountsCompanion data) async {
117117
final accountId = _nextAccountId;
118118
_nextAccountId++;
119-
return accountId;
119+
return Account(
120+
id: accountId,
121+
realmUrl: data.realmUrl.value,
122+
userId: data.userId.value,
123+
email: data.email.value,
124+
apiKey: data.apiKey.value,
125+
zulipFeatureLevel: data.zulipFeatureLevel.value,
126+
zulipVersion: data.zulipVersion.value,
127+
zulipMergeBase: data.zulipMergeBase.value,
128+
);
120129
}
121130
}

0 commit comments

Comments
 (0)