Files
sharedinbox/lib/data/repositories/account_repository_impl.dart
T
Thomas GüttlerandClaude Sonnet 4.6 e3ba18285d refactor: enforce always_use_package_imports across all lib files
Added lint rule to analysis_options.yaml and ran dart fix --apply to convert
125 relative imports in 33 files to package:sharedinbox/... style.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-24 16:30:59 +02:00

107 lines
3.5 KiB
Dart

import 'package:drift/drift.dart' show Value;
import 'package:sharedinbox/core/models/account.dart' as model;
import 'package:sharedinbox/core/repositories/account_repository.dart';
import 'package:sharedinbox/core/storage/secure_storage.dart';
import 'package:sharedinbox/data/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,
accountType: Value(account.type.name),
jmapUrl: Value(account.jmapUrl),
username: Value(account.username),
verbose: Value(account.verbose),
),
);
await _storage.write(key: _passwordKey(account.id), value: password);
}
@override
Future<void> updateAccount(model.Account account, {String? password}) async {
await (_db.update(_db.accounts)..where((t) => t.id.equals(account.id)))
.write(
AccountsCompanion(
displayName: Value(account.displayName),
email: Value(account.email),
imapHost: Value(account.imapHost),
imapPort: Value(account.imapPort),
imapSsl: Value(account.imapSsl),
smtpHost: Value(account.smtpHost),
smtpPort: Value(account.smtpPort),
smtpSsl: Value(account.smtpSsl),
accountType: Value(account.type.name),
jmapUrl: Value(account.jmapUrl),
username: Value(account.username),
verbose: Value(account.verbose),
),
);
if (password != null) {
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,
username: row.username,
type: model.AccountType.values.byName(row.accountType),
imapHost: row.imapHost,
imapPort: row.imapPort,
imapSsl: row.imapSsl,
smtpHost: row.smtpHost,
smtpPort: row.smtpPort,
smtpSsl: row.smtpSsl,
jmapUrl: row.jmapUrl,
verbose: row.verbose,
);
}