nc-photos/app/lib/entity/search.dart

110 lines
2.3 KiB
Dart
Raw Normal View History

2022-08-06 06:21:11 +02:00
import 'package:nc_photos/account.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file_util.dart' as file_util;
2023-02-20 15:21:35 +01:00
import 'package:nc_photos/entity/sqlite/files_query_builder.dart' as sql;
import 'package:np_collection/np_collection.dart';
2022-12-08 16:39:13 +01:00
import 'package:to_string/to_string.dart';
2022-08-06 06:21:11 +02:00
2022-12-08 16:39:13 +01:00
part 'search.g.dart';
@toString
2022-08-06 06:21:11 +02:00
class SearchCriteria {
SearchCriteria(String input, this.filters) : input = input.trim();
2022-08-06 06:21:11 +02:00
SearchCriteria copyWith({
String? input,
2022-08-06 06:21:11 +02:00
List<SearchFilter>? filters,
}) =>
SearchCriteria(
input ?? this.input,
2022-08-06 06:21:11 +02:00
filters ?? List.of(this.filters),
);
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
2022-08-06 06:21:11 +02:00
final String input;
2022-12-08 16:39:13 +01:00
@Format(r"${$?.toReadableString()}")
2022-08-06 06:21:11 +02:00
final List<SearchFilter> filters;
}
abstract class SearchFilter {
void apply(sql.FilesQueryBuilder query);
bool isSatisfy(File file);
2022-08-06 06:21:11 +02:00
}
enum SearchFileType {
image,
video,
}
extension on SearchFileType {
String toSqlPattern() {
switch (this) {
case SearchFileType.image:
return "image/%";
case SearchFileType.video:
return "video/%";
}
}
}
2022-12-08 16:39:13 +01:00
@toString
2022-08-06 06:21:11 +02:00
class SearchFileTypeFilter implements SearchFilter {
const SearchFileTypeFilter(this.type);
@override
apply(sql.FilesQueryBuilder query) {
query.byMimePattern(type.toSqlPattern());
}
@override
isSatisfy(File file) {
switch (type) {
case SearchFileType.image:
return file_util.isSupportedImageFormat(file);
case SearchFileType.video:
return file_util.isSupportedVideoFormat(file);
}
}
2022-08-06 06:21:11 +02:00
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
2022-08-06 06:21:11 +02:00
final SearchFileType type;
}
2022-12-08 16:39:13 +01:00
@toString
2022-08-06 06:21:11 +02:00
class SearchFavoriteFilter implements SearchFilter {
const SearchFavoriteFilter(this.value);
@override
apply(sql.FilesQueryBuilder query) {
query.byFavorite(value);
}
@override
isSatisfy(File file) => (file.isFavorite ?? false) == value;
2022-08-06 06:21:11 +02:00
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
2022-08-06 06:21:11 +02:00
final bool value;
}
class SearchRepo {
const SearchRepo(this.dataSrc);
Future<List<File>> list(Account account, SearchCriteria criteria) =>
dataSrc.list(account, criteria);
final SearchDataSource dataSrc;
}
abstract class SearchDataSource {
/// List all results from a given search criteria
Future<List<File>> list(Account account, SearchCriteria criteria);
}