74 lines
2.2 KiB
Dart
74 lines
2.2 KiB
Dart
import '../../core/models/account.dart' as model;
|
|
import '../../core/repositories/account_repository.dart';
|
|
import '../../core/storage/secure_storage.dart';
|
|
import '../db/database.dart';
|
|
|
|
class AccountRepositoryImpl implements AccountRepository {
|
|
AccountRepositoryImpl(this._db, this._storage);
|
|
|
|
final AppDatabase _db;
|
|
final SecureStorage _storage;
|
|
|
|
@override
|
|
Stream<List<model.Account>> observeAccounts() {
|
|
return _db.select(_db.accounts).watch().map(
|
|
(rows) => rows.map(_toModel).toList(),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Future<model.Account?> getAccount(String id) async {
|
|
final row = await (_db.select(_db.accounts)
|
|
..where((t) => t.id.equals(id)))
|
|
.getSingleOrNull();
|
|
return row == null ? null : _toModel(row);
|
|
}
|
|
|
|
@override
|
|
Future<void> addAccount(model.Account account, String password) async {
|
|
await _db.into(_db.accounts).insertOnConflictUpdate(
|
|
AccountsCompanion.insert(
|
|
id: account.id,
|
|
displayName: account.displayName,
|
|
email: account.email,
|
|
imapHost: account.imapHost,
|
|
imapPort: account.imapPort,
|
|
imapSsl: account.imapSsl,
|
|
smtpHost: account.smtpHost,
|
|
smtpPort: account.smtpPort,
|
|
smtpSsl: account.smtpSsl,
|
|
),
|
|
);
|
|
await _storage.write(key: _passwordKey(account.id), value: password);
|
|
}
|
|
|
|
@override
|
|
Future<void> removeAccount(String id) async {
|
|
await (_db.delete(_db.accounts)..where((t) => t.id.equals(id))).go();
|
|
await _storage.delete(key: _passwordKey(id));
|
|
}
|
|
|
|
@override
|
|
Future<String> getPassword(String accountId) async {
|
|
final pw = await _storage.read(key: _passwordKey(accountId));
|
|
if (pw == null) {
|
|
throw StateError('No password stored for account $accountId');
|
|
}
|
|
return pw;
|
|
}
|
|
|
|
String _passwordKey(String accountId) => 'account_password_$accountId';
|
|
|
|
model.Account _toModel(Account row) => model.Account(
|
|
id: row.id,
|
|
displayName: row.displayName,
|
|
email: row.email,
|
|
imapHost: row.imapHost,
|
|
imapPort: row.imapPort,
|
|
imapSsl: row.imapSsl,
|
|
smtpHost: row.smtpHost,
|
|
smtpPort: row.smtpPort,
|
|
smtpSsl: row.smtpSsl,
|
|
);
|
|
}
|