mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-11 10:08:02 -06:00
WIP - Reimplemented document search
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:hydrated_bloc/hydrated_bloc.dart';
|
||||
import 'package:paperless_api/paperless_api.dart';
|
||||
import 'package:paperless_mobile/features/paged_document_view/documents_paging_mixin.dart';
|
||||
|
||||
import 'document_search_state.dart';
|
||||
|
||||
class DocumentSearchCubit extends HydratedCubit<DocumentSearchState>
|
||||
with DocumentsPagingMixin {
|
||||
////
|
||||
DocumentSearchCubit(this.api) : super(const DocumentSearchState());
|
||||
|
||||
@override
|
||||
final PaperlessDocumentsApi api;
|
||||
|
||||
///
|
||||
/// Requests results based on [query] and adds [query] to the
|
||||
/// search history, removing old occurrences and trimming the list to
|
||||
/// the last 5 searches.
|
||||
///
|
||||
Future<void> updateResults(String query) async {
|
||||
await updateFilter(
|
||||
filter: state.filter.copyWith(query: TextQuery.titleAndContent(query)),
|
||||
);
|
||||
emit(
|
||||
state.copyWith(
|
||||
searchHistory: [
|
||||
query,
|
||||
...state.searchHistory.where((element) => element != query)
|
||||
].take(5).toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void removeHistoryEntry(String suggestion) {
|
||||
emit(state.copyWith(
|
||||
searchHistory: state.searchHistory
|
||||
.whereNot((element) => element == suggestion)
|
||||
.toList(),
|
||||
));
|
||||
}
|
||||
|
||||
Future<List<String>> findSuggestions(String query) {
|
||||
return api.autocomplete(query);
|
||||
}
|
||||
|
||||
@override
|
||||
DocumentSearchState? fromJson(Map<String, dynamic> json) {
|
||||
return DocumentSearchState.fromJson(json);
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, dynamic>? toJson(DocumentSearchState state) {
|
||||
return state.toJson();
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:paperless_api/paperless_api.dart';
|
||||
import 'package:paperless_mobile/features/paged_document_view/model/documents_paged_state.dart';
|
||||
|
||||
part 'document_search_state.g.dart';
|
||||
|
||||
@JsonSerializable(ignoreUnannotated: true)
|
||||
class DocumentSearchState extends DocumentsPagedState {
|
||||
@JsonKey()
|
||||
final List<String> searchHistory;
|
||||
|
||||
const DocumentSearchState({
|
||||
this.searchHistory = const [],
|
||||
super.filter,
|
||||
super.hasLoaded,
|
||||
super.isLoading,
|
||||
super.value,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object> get props => [
|
||||
hasLoaded,
|
||||
isLoading,
|
||||
filter,
|
||||
value,
|
||||
searchHistory,
|
||||
];
|
||||
|
||||
@override
|
||||
DocumentSearchState copyWithPaged({
|
||||
bool? hasLoaded,
|
||||
bool? isLoading,
|
||||
List<PagedSearchResult<DocumentModel>>? value,
|
||||
DocumentFilter? filter,
|
||||
}) {
|
||||
return copyWith(
|
||||
hasLoaded: hasLoaded,
|
||||
isLoading: isLoading,
|
||||
filter: filter,
|
||||
value: value,
|
||||
);
|
||||
}
|
||||
|
||||
DocumentSearchState copyWith({
|
||||
List<String>? searchHistory,
|
||||
bool? hasLoaded,
|
||||
bool? isLoading,
|
||||
List<PagedSearchResult<DocumentModel>>? value,
|
||||
DocumentFilter? filter,
|
||||
List<String>? suggestions,
|
||||
}) {
|
||||
return DocumentSearchState(
|
||||
value: value ?? this.value,
|
||||
filter: filter ?? this.filter,
|
||||
hasLoaded: hasLoaded ?? this.hasLoaded,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
searchHistory: searchHistory ?? this.searchHistory,
|
||||
);
|
||||
}
|
||||
|
||||
factory DocumentSearchState.fromJson(Map<String, dynamic> json) =>
|
||||
_$DocumentSearchStateFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$DocumentSearchStateToJson(this);
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'document_search_state.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
DocumentSearchState _$DocumentSearchStateFromJson(Map<String, dynamic> json) =>
|
||||
DocumentSearchState(
|
||||
searchHistory: (json['searchHistory'] as List<dynamic>?)
|
||||
?.map((e) => e as String)
|
||||
.toList() ??
|
||||
const [],
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$DocumentSearchStateToJson(
|
||||
DocumentSearchState instance) =>
|
||||
<String, dynamic>{
|
||||
'searchHistory': instance.searchHistory,
|
||||
};
|
||||
Reference in New Issue
Block a user