live local search
todos
This commit is contained in:
parent
c34d4f23ae
commit
64ed6ae1ac
6 changed files with 74 additions and 23 deletions
8
lib/features/core/data/constants/const_sorters.dart
Normal file
8
lib/features/core/data/constants/const_sorters.dart
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import 'package:string_similarity/string_similarity.dart';
|
||||||
|
|
||||||
|
abstract class ConstSorters {
|
||||||
|
/// Uses Dice's Coefficient as a similarity metric, for a 2-way comparison, between a [targetWord]
|
||||||
|
/// and given words.
|
||||||
|
static int stringsSimilarityTarget(String a, String b, {required String targetWord}) =>
|
||||||
|
a.similarityTo(targetWord).compareTo(b.similarityTo(targetWord));
|
||||||
|
}
|
|
@ -1,3 +1,6 @@
|
||||||
extension MapExtensions<A, B> on Map<A, B> {
|
extension MapExtensions<A, B> on Map<A, B> {
|
||||||
Map<A, B> get deepCopy => {...this};
|
Map<A, B> get deepCopy => {...this};
|
||||||
|
|
||||||
|
/// Returns the values of a [Map] at given [keys] indices.
|
||||||
|
Iterable<B> valuesByKeys({required Iterable<A> keys}) => keys.map((final key) => this[key]!);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
|
import '/features/core/data/constants/const_sorters.dart';
|
||||||
import '/features/core/data/extensions/iterable_extensions.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/services/logging_service.dart';
|
||||||
import '/features/core/utils/mutex.dart';
|
import '/features/core/utils/mutex.dart';
|
||||||
import '/features/home/data/models/image_model.dart';
|
|
||||||
import '/locator.dart';
|
import '/locator.dart';
|
||||||
import '../abstracts/images_api.dart';
|
import '../abstracts/images_api.dart';
|
||||||
import '../data/enums/search_option.dart';
|
import '../data/enums/search_option.dart';
|
||||||
|
import '../data/models/image_model.dart';
|
||||||
|
|
||||||
/// Handles fetching and storing of Images.
|
/// Handles fetching and storing of Images.
|
||||||
///
|
///
|
||||||
|
@ -24,8 +26,9 @@ class ImagesService {
|
||||||
final ImagesApi _imagesApi;
|
final ImagesApi _imagesApi;
|
||||||
final LoggingService _loggingService;
|
final LoggingService _loggingService;
|
||||||
|
|
||||||
late final Iterable<ImageModel> _imageModels;
|
late final Map<String, ImageModel> _imageModels;
|
||||||
Iterable<ImageModel> get imageModels => _imageModels.deepCopy;
|
Iterable<ImageModel> get imageModels => _imageModels.values.deepCopy;
|
||||||
|
|
||||||
final Mutex _searchMutex = Mutex();
|
final Mutex _searchMutex = Mutex();
|
||||||
|
|
||||||
/// Manual initialization triggering
|
/// Manual initialization triggering
|
||||||
|
@ -36,7 +39,10 @@ class ImagesService {
|
||||||
|
|
||||||
Future<void> _init() async {
|
Future<void> _init() async {
|
||||||
_loggingService.info('Fetching and creating image models...');
|
_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
|
_imageModels.isNotEmpty
|
||||||
? _loggingService.good("Created ${_imageModels.length} images' models")
|
? _loggingService.good("Created ${_imageModels.length} images' models")
|
||||||
|
@ -49,19 +55,34 @@ class ImagesService {
|
||||||
int get lastAvailableImageIndex => _imageModels.length - 1;
|
int get lastAvailableImageIndex => _imageModels.length - 1;
|
||||||
int get numberOfImages => _imageModels.length;
|
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;
|
Future<void> get lastQueryIsCompleted => _searchMutex.lastOperationCompletionAwaiter;
|
||||||
|
|
||||||
Future<List<ImageModel>> searchImages({
|
/// Performs searching on images, both locally and by a Web API endpoint.
|
||||||
required SearchOption searchOption,
|
///
|
||||||
|
/// 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,
|
required String imageNamePart,
|
||||||
}) async {
|
bool treatAsInSequence = false}) async {
|
||||||
return await _searchMutex.lockAndRun(run: (final unlock) async {
|
return await _searchMutex.lockAndRun(run: (final unlock) async {
|
||||||
try {
|
try {
|
||||||
switch (searchOption) {
|
switch (searchOption) {
|
||||||
case SearchOption.local:
|
case SearchOption.local:
|
||||||
return [];
|
final rankedKeys = _imageModels.keys
|
||||||
|
//todo(mehul): Implement atleast-matching-all-parts
|
||||||
|
.where(
|
||||||
|
(final imageName) => imageName.contains(treatAsInSequence ? imageNamePart : ''))
|
||||||
|
.toList(growable: false)
|
||||||
|
..sort((final a, final b) =>
|
||||||
|
ConstSorters.stringsSimilarityTarget(targetWord: imageNamePart, a, b));
|
||||||
|
return _imageModels.valuesByKeys(keys: rankedKeys).toList(growable: false);
|
||||||
case SearchOption.web:
|
case SearchOption.web:
|
||||||
return await _imagesApi.searchImages(
|
return await _imagesApi.searchImages(
|
||||||
searchStr: imageNamePart,
|
searchStr: imageNamePart,
|
||||||
|
|
|
@ -7,12 +7,12 @@ import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||||
import '/features/core/abstracts/base_view_model.dart';
|
import '/features/core/abstracts/base_view_model.dart';
|
||||||
import '/features/core/services/logging_service.dart';
|
import '/features/core/services/logging_service.dart';
|
||||||
import '/features/core/services/navigation_service.dart';
|
import '/features/core/services/navigation_service.dart';
|
||||||
import '/features/home/data/models/image_model.dart';
|
|
||||||
import '/features/home/services/image_cache_manager_service.dart';
|
|
||||||
import '/features/home/services/images_service.dart';
|
|
||||||
import '/features/home/views/image_carousel/image_carousel_view.dart';
|
|
||||||
import '/locator.dart';
|
import '/locator.dart';
|
||||||
import '../../data/enums/search_option.dart';
|
import '../../data/enums/search_option.dart';
|
||||||
|
import '../../data/models/image_model.dart';
|
||||||
|
import '../../services/image_cache_manager_service.dart';
|
||||||
|
import '../../services/images_service.dart';
|
||||||
|
import '../image_carousel/image_carousel_view.dart';
|
||||||
|
|
||||||
class GalleryViewModel extends BaseViewModel {
|
class GalleryViewModel extends BaseViewModel {
|
||||||
GalleryViewModel({
|
GalleryViewModel({
|
||||||
|
@ -54,17 +54,22 @@ class GalleryViewModel extends BaseViewModel {
|
||||||
// If empty-string (from backspacing) -> reset state.
|
// If empty-string (from backspacing) -> reset state.
|
||||||
if (searchTerm.isEmpty) {
|
if (searchTerm.isEmpty) {
|
||||||
_imageSearchResultsNotifier.value = [];
|
_imageSearchResultsNotifier.value = [];
|
||||||
|
_loggingService.info('Clearing results on search string removal');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detached call to prevent UI blocking
|
// Detached call to prevent UI blocking
|
||||||
unawaited(_imagesService
|
unawaited(
|
||||||
|
_imagesService
|
||||||
.searchImages(
|
.searchImages(
|
||||||
imageNamePart: searchTerm,
|
imageNamePart: searchTerm,
|
||||||
searchOption: searchOptionListenable.value,
|
searchOption: searchOptionListenable.value,
|
||||||
|
// todo(mehul): When implemented, remove this
|
||||||
|
treatAsInSequence: true,
|
||||||
)
|
)
|
||||||
.then(
|
.then(
|
||||||
(final fetchedImageModels) => _imageSearchResultsNotifier.value = fetchedImageModels));
|
(final fetchedImageModels) => _imageSearchResultsNotifier.value = fetchedImageModels),
|
||||||
|
);
|
||||||
|
|
||||||
// Force-update to trigger listening to `lastQueryResultDone()`.
|
// Force-update to trigger listening to `lastQueryResultDone()`.
|
||||||
_imageSearchResultsNotifier.notifyListeners();
|
_imageSearchResultsNotifier.notifyListeners();
|
||||||
|
@ -72,14 +77,20 @@ class GalleryViewModel extends BaseViewModel {
|
||||||
|
|
||||||
void searchPressed() {
|
void searchPressed() {
|
||||||
// If transitioning from 'Searching', clear previous results immediately
|
// If transitioning from 'Searching', clear previous results immediately
|
||||||
if (_isSearchingNotifier.value) _imageSearchResultsNotifier.value = [];
|
if (_isSearchingNotifier.value) {
|
||||||
|
_imageSearchResultsNotifier.value = [];
|
||||||
|
_loggingService.info('Clearing results on view mode change');
|
||||||
|
}
|
||||||
|
|
||||||
_isSearchingNotifier.value = !_isSearchingNotifier.value;
|
_isSearchingNotifier.value = !_isSearchingNotifier.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> get lastQueryResultDone => _imagesService.lastQueryIsCompleted;
|
Future<void> get lastQueryResultDone => _imagesService.lastQueryIsCompleted;
|
||||||
|
|
||||||
void onSearchOptionChanged(SearchOption? option) => _searchOptionNotifier.value = option!;
|
void onSearchOptionChanged(SearchOption? option) {
|
||||||
|
_searchOptionNotifier.value = option!;
|
||||||
|
_loggingService.info('Switching over to $option search');
|
||||||
|
}
|
||||||
|
|
||||||
void onPromptPressed() => _isDisplayingPressingPrompt.value = false;
|
void onPromptPressed() => _isDisplayingPressingPrompt.value = false;
|
||||||
|
|
||||||
|
|
|
@ -546,6 +546,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.1.1"
|
version: "1.1.1"
|
||||||
|
string_similarity:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: string_similarity
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.0"
|
||||||
synchronized:
|
synchronized:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|
|
@ -29,6 +29,7 @@ dependencies:
|
||||||
intl_utils: ^2.8.1
|
intl_utils: ^2.8.1
|
||||||
connectivity_plus: ^3.0.2
|
connectivity_plus: ^3.0.2
|
||||||
internet_connection_checker: ^1.0.0+1
|
internet_connection_checker: ^1.0.0+1
|
||||||
|
string_similarity: ^2.0.0
|
||||||
|
|
||||||
# Util frontend
|
# Util frontend
|
||||||
flutter_markdown: ^0.6.13
|
flutter_markdown: ^0.6.13
|
||||||
|
|
Loading…
Reference in a new issue