live local search

todos

improve local search
This commit is contained in:
Mguy13 2022-12-23 20:45:30 +01:00
parent 4ade7f1682
commit a0ed894016
9 changed files with 151 additions and 42 deletions

View file

@ -1,12 +1,16 @@
import 'dart:async';
import 'package:mc_gallery/features/core/data/extensions/string_extensions.dart';
import '/features/core/data/constants/const_sorters.dart';
import '/features/core/data/extensions/iterable_extensions.dart';
import '/features/core/data/extensions/map_extensions.dart';
import '/features/core/services/logging_service.dart';
import '/features/core/utils/mutex.dart';
import '/features/home/data/models/image_model.dart';
import '/locator.dart';
import '../abstracts/images_api.dart';
import '../data/enums/search_option.dart';
import '../data/models/image_model.dart';
/// Handles fetching and storing of Images.
///
@ -24,8 +28,9 @@ class ImagesService {
final ImagesApi _imagesApi;
final LoggingService _loggingService;
late final Iterable<ImageModel> _imageModels;
Iterable<ImageModel> get imageModels => _imageModels.deepCopy;
late final Map<String, ImageModel> _imageModels;
Iterable<ImageModel> get imageModels => _imageModels.values.deepCopy;
final Mutex _searchMutex = Mutex();
/// Manual initialization triggering
@ -36,7 +41,10 @@ class ImagesService {
Future<void> _init() async {
_loggingService.info('Fetching and creating image models...');
_imageModels = await _imagesApi.fetchImageUri(token: '');
_imageModels = {
for (final imageModel in await _imagesApi.fetchImageUri(token: ''))
imageModel.imageName: imageModel
};
_imageModels.isNotEmpty
? _loggingService.good("Created ${_imageModels.length} images' models")
@ -49,19 +57,38 @@ class ImagesService {
int get lastAvailableImageIndex => _imageModels.length - 1;
int get numberOfImages => _imageModels.length;
ImageModel imageModelAt({required int index}) => _imageModels.elementAt(index);
ImageModel imageModelAt({required int index}) => _imageModels.values.elementAt(index);
Future<void> get lastQueryIsCompleted => _searchMutex.lastOperationCompletionAwaiter;
/// Performs searching on images, both locally and by a Web API endpoint.
///
/// For now, a simple mechanism is used for handling async calls between (posssible) API fetches ->
/// just 'pile-up'. A mechanism can be made to 'cancel' a fetch if a newer search request comes in,
/// but that may be more complicated, and not the point of the assignment I think.
/// There are lots of optimizations possible for new inputs, for example reducing search frontier
/// by using set-cover/subsetting optimizations on backspace, and so on, but again, not the point,
/// I think.
Future<List<ImageModel>> searchImages({
required SearchOption searchOption,
required String imageNamePart,
bool treatAsInSequence = false,
}) async {
return await _searchMutex.lockAndRun(run: (final unlock) async {
try {
switch (searchOption) {
case SearchOption.local:
return [];
final rankedKeys = _imageModels.keys
// Reduce number of results by atleast occurring
.where((final imageName) => treatAsInSequence
? imageName.contains(imageNamePart)
: imageName.containsAllCharacters(targetChars: imageNamePart))
.toList(growable: false)
// Sorting by the highest similarity first
..sort((final a, final b) =>
ConstSorters.stringsSimilarityTarget(targetWord: imageNamePart, a, b))
..reversed;
return _imageModels.valuesByKeys(keys: rankedKeys).toList(growable: false);
case SearchOption.web:
return await _imagesApi.searchImages(
searchStr: imageNamePart,