mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-10 12:07:58 -06:00
Changed saved views handling, changed repository structure with automatic persistence.
This commit is contained in:
@@ -9,54 +9,36 @@ class SavedViewCubit extends Cubit<SavedViewState> {
|
||||
final SavedViewRepository _repository;
|
||||
StreamSubscription? _subscription;
|
||||
|
||||
SavedViewCubit(this._repository) : super(SavedViewState(value: {})) {
|
||||
SavedViewCubit(this._repository) : super(const SavedViewState()) {
|
||||
_subscription = _repository.values.listen(
|
||||
(savedViews) {
|
||||
if (savedViews == null) {
|
||||
emit(state.copyWith(isLoaded: false));
|
||||
if (savedViews?.hasLoaded ?? false) {
|
||||
emit(state.copyWith(value: savedViews?.values, hasLoaded: true));
|
||||
} else {
|
||||
emit(state.copyWith(value: savedViews, isLoaded: true));
|
||||
emit(state.copyWith(hasLoaded: false));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void selectView(SavedView? view) {
|
||||
emit(state.copyWith(
|
||||
selectedSavedViewId: view?.id,
|
||||
overwriteSelectedSavedViewId: true,
|
||||
));
|
||||
}
|
||||
|
||||
Future<SavedView> add(SavedView view) async {
|
||||
final savedView = await _repository.create(view);
|
||||
emit(state.copyWith(value: {...state.value, savedView.id!: savedView}));
|
||||
return savedView;
|
||||
}
|
||||
|
||||
Future<int> remove(SavedView view) async {
|
||||
final id = await _repository.delete(view);
|
||||
if (state.selectedSavedViewId == id) {
|
||||
resetSelection();
|
||||
}
|
||||
return id;
|
||||
Future<int> remove(SavedView view) {
|
||||
return _repository.delete(view);
|
||||
}
|
||||
|
||||
Future<void> initialize() async {
|
||||
final views = await _repository.findAll();
|
||||
final values = {for (var element in views) element.id!: element};
|
||||
emit(SavedViewState(value: values, isLoaded: true));
|
||||
emit(SavedViewState(value: values, hasLoaded: true));
|
||||
}
|
||||
|
||||
Future<void> reload() => initialize();
|
||||
|
||||
void resetSelection() {
|
||||
emit(SavedViewState(
|
||||
value: state.value,
|
||||
isLoaded: true,
|
||||
));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
_subscription?.cancel();
|
||||
|
||||
Reference in New Issue
Block a user