2022-12-31 08:52:22 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:bloc/bloc.dart';
|
|
|
|
import 'package:copy_with/copy_with.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/di_container.dart';
|
2023-02-20 15:21:35 +01:00
|
|
|
import 'package:nc_photos/entity/sqlite/database.dart' as sql;
|
2022-12-31 08:52:22 +01:00
|
|
|
import 'package:np_codegen/np_codegen.dart';
|
|
|
|
import 'package:to_string/to_string.dart';
|
|
|
|
|
|
|
|
part 'expert.g.dart';
|
|
|
|
|
2023-01-13 18:53:38 +01:00
|
|
|
@genCopyWith
|
2022-12-31 08:52:22 +01:00
|
|
|
@toString
|
|
|
|
class ExpertSettingsState {
|
|
|
|
const ExpertSettingsState({
|
|
|
|
this.lastSuccessful,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() => _$toString();
|
|
|
|
|
|
|
|
final ExpertSettingsEvent? lastSuccessful;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class ExpertSettingsEvent {
|
|
|
|
const ExpertSettingsEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
@toString
|
|
|
|
class ExpertSettingsClearCacheDatabase extends ExpertSettingsEvent {
|
|
|
|
ExpertSettingsClearCacheDatabase();
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() => _$toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
class ExpertSettingsError {
|
|
|
|
const ExpertSettingsError(this.ev, [this.error, this.stackTrace]);
|
|
|
|
|
|
|
|
final ExpertSettingsEvent ev;
|
|
|
|
final Object? error;
|
|
|
|
final StackTrace? stackTrace;
|
|
|
|
}
|
|
|
|
|
|
|
|
@npLog
|
|
|
|
class ExpertSettingsBloc
|
|
|
|
extends Bloc<ExpertSettingsEvent, ExpertSettingsState> {
|
|
|
|
ExpertSettingsBloc(DiContainer c)
|
|
|
|
: _c = c,
|
|
|
|
super(const ExpertSettingsState()) {
|
|
|
|
on<ExpertSettingsClearCacheDatabase>(_onClearCacheDatabase);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool require(DiContainer c) =>
|
|
|
|
DiContainer.has(c, DiType.pref) && DiContainer.has(c, DiType.sqliteDb);
|
|
|
|
|
|
|
|
Stream<ExpertSettingsError> errorStream() => _errorStream.stream;
|
|
|
|
|
|
|
|
Future<void> _onClearCacheDatabase(ExpertSettingsClearCacheDatabase ev,
|
|
|
|
Emitter<ExpertSettingsState> emit) async {
|
|
|
|
try {
|
|
|
|
await _c.sqliteDb.use((db) async {
|
|
|
|
await db.truncate();
|
|
|
|
final accounts = _c.pref.getAccounts3Or([]);
|
|
|
|
for (final a in accounts) {
|
|
|
|
await db.insertAccountOf(a);
|
|
|
|
}
|
|
|
|
});
|
2023-01-13 18:53:38 +01:00
|
|
|
emit(state.copyWith(lastSuccessful: ev));
|
2022-12-31 08:52:22 +01:00
|
|
|
} catch (e, stackTrace) {
|
|
|
|
_log.shout("[_onClearCacheDatabase] Uncaught exception", e, stackTrace);
|
|
|
|
_errorStream.add(ExpertSettingsError(ev, e, stackTrace));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final DiContainer _c;
|
|
|
|
final _errorStream = StreamController<ExpertSettingsError>.broadcast();
|
|
|
|
}
|