Compare commits
2 commits
d3a8a401c4
...
235cfd21dc
Author | SHA1 | Date | |
---|---|---|---|
235cfd21dc | |||
53d13927fd |
5 changed files with 93 additions and 35 deletions
8
lib/features/core/data/extensions/object_extensions.dart
Normal file
8
lib/features/core/data/extensions/object_extensions.dart
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
extension ObjectExtensions on Object? {
|
||||||
|
E asType<E>() => this as E;
|
||||||
|
E? asNullableType<E>() => this as E?;
|
||||||
|
}
|
||||||
|
|
||||||
|
extension AsCallback<T extends Object> on T {
|
||||||
|
T Function() get asCallback => () => this;
|
||||||
|
}
|
14
lib/features/core/data/extensions/string_extensions.dart
Normal file
14
lib/features/core/data/extensions/string_extensions.dart
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
extension StringExtensions on String {
|
||||||
|
/// Returns true if given word contains atleast all the characters in [targetChars], and `false` otherwise
|
||||||
|
///
|
||||||
|
/// Very efficient `O(n)` instead of naive `O(n*m)`
|
||||||
|
bool containsAllCharacters({required String targetChars}) {
|
||||||
|
final Set<String> characterSet = Set.from(targetChars.split(''));
|
||||||
|
for (final testChar in split('')) {
|
||||||
|
characterSet.remove(testChar);
|
||||||
|
if (characterSet.isEmpty) return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
import 'dart:async';
|
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/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/data/extensions/map_extensions.dart';
|
||||||
|
@ -68,21 +70,25 @@ class ImagesService {
|
||||||
/// There are lots of optimizations possible for new inputs, for example reducing search frontier
|
/// 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,
|
/// by using set-cover/subsetting optimizations on backspace, and so on, but again, not the point,
|
||||||
/// I think.
|
/// I think.
|
||||||
Future<List<ImageModel>> searchImages(
|
Future<List<ImageModel>> searchImages({
|
||||||
{required SearchOption searchOption,
|
required SearchOption searchOption,
|
||||||
required String imageNamePart,
|
required String imageNamePart,
|
||||||
bool treatAsInSequence = false}) 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:
|
||||||
final rankedKeys = _imageModels.keys
|
final rankedKeys = _imageModels.keys
|
||||||
//todo(mehul): Implement atleast-matching-all-parts
|
// Reduce number of results by atleast occurring
|
||||||
.where(
|
.where((final imageName) => treatAsInSequence
|
||||||
(final imageName) => imageName.contains(treatAsInSequence ? imageNamePart : ''))
|
? imageName.contains(imageNamePart)
|
||||||
|
: imageName.containsAllCharacters(targetChars: imageNamePart))
|
||||||
.toList(growable: false)
|
.toList(growable: false)
|
||||||
|
// Sorting by the highest similarity first
|
||||||
..sort((final a, final b) =>
|
..sort((final a, final b) =>
|
||||||
ConstSorters.stringsSimilarityTarget(targetWord: imageNamePart, a, b));
|
ConstSorters.stringsSimilarityTarget(targetWord: imageNamePart, a, b))
|
||||||
|
..reversed;
|
||||||
return _imageModels.valuesByKeys(keys: rankedKeys).toList(growable: false);
|
return _imageModels.valuesByKeys(keys: rankedKeys).toList(growable: false);
|
||||||
case SearchOption.web:
|
case SearchOption.web:
|
||||||
return (await _imagesApi.searchImages(
|
return (await _imagesApi.searchImages(
|
||||||
|
|
|
@ -64,8 +64,6 @@ class GalleryViewModel extends BaseViewModel {
|
||||||
.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),
|
||||||
|
@ -79,7 +77,7 @@ class GalleryViewModel extends BaseViewModel {
|
||||||
// If transitioning from 'Searching', clear previous results immediately
|
// If transitioning from 'Searching', clear previous results immediately
|
||||||
if (_isSearchingNotifier.value) {
|
if (_isSearchingNotifier.value) {
|
||||||
_imageSearchResultsNotifier.value = [];
|
_imageSearchResultsNotifier.value = [];
|
||||||
_loggingService.info('Clearing results on view mode change');
|
_loggingService.info('Clearing of results on view mode change');
|
||||||
}
|
}
|
||||||
|
|
||||||
_isSearchingNotifier.value = !_isSearchingNotifier.value;
|
_isSearchingNotifier.value = !_isSearchingNotifier.value;
|
||||||
|
@ -89,7 +87,12 @@ class GalleryViewModel extends BaseViewModel {
|
||||||
|
|
||||||
void onSearchOptionChanged(SearchOption? option) {
|
void onSearchOptionChanged(SearchOption? option) {
|
||||||
_searchOptionNotifier.value = option!;
|
_searchOptionNotifier.value = option!;
|
||||||
_loggingService.info('Switching over to $option search');
|
_loggingService.info('Switched over to $option search');
|
||||||
|
|
||||||
|
_imageSearchResultsNotifier.value = [];
|
||||||
|
_loggingService.info('Cleared resultsw from view');
|
||||||
|
|
||||||
|
//todo(mehul): Either redo search or force user to type in by clearing field
|
||||||
}
|
}
|
||||||
|
|
||||||
void onPromptPressed() => _isDisplayingPressingPrompt.value = false;
|
void onPromptPressed() => _isDisplayingPressingPrompt.value = false;
|
||||||
|
|
|
@ -24,29 +24,56 @@ class _SearchGalleryView extends StatelessWidget {
|
||||||
displayedWidget = const CircularProgressIndicator();
|
displayedWidget = const CircularProgressIndicator();
|
||||||
break;
|
break;
|
||||||
case ConnectionState.done:
|
case ConnectionState.done:
|
||||||
displayedWidget = Wrap(
|
displayedWidget = ValueListenableBuilder<SearchOption>(
|
||||||
runSpacing: 24,
|
valueListenable: galleryViewModel.searchOptionListenable,
|
||||||
spacing: 8,
|
builder: (context, final searchOption, child) {
|
||||||
alignment: WrapAlignment.center,
|
switch (searchOption) {
|
||||||
runAlignment: WrapAlignment.center,
|
case SearchOption.local:
|
||||||
crossAxisAlignment: WrapCrossAlignment.center,
|
return Wrap(
|
||||||
children: [
|
runSpacing: 24,
|
||||||
for (final imageResult in resultsImageModels)
|
spacing: 8,
|
||||||
Image.network(
|
alignment: WrapAlignment.center,
|
||||||
imageResult.uri.toString(),
|
runAlignment: WrapAlignment.center,
|
||||||
loadingBuilder: (context, final child, final loadingProgress) =>
|
crossAxisAlignment: WrapCrossAlignment.center,
|
||||||
loadingProgress == null
|
children: [
|
||||||
? child
|
for (final resultsImageModel in resultsImageModels)
|
||||||
: Center(
|
CachedNetworkImage(
|
||||||
child: CircularProgressIndicator(
|
imageUrl: resultsImageModel.uri.toString(),
|
||||||
value: loadingProgress.expectedTotalBytes != null
|
cacheKey: resultsImageModel.imageIndex.toString(),
|
||||||
? loadingProgress.cumulativeBytesLoaded /
|
progressIndicatorBuilder: (_, __, final progress) =>
|
||||||
loadingProgress.expectedTotalBytes!
|
CircularProgressIndicator(
|
||||||
: null,
|
value: galleryViewModel.downloadProgressValue(progress: progress),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
],
|
||||||
],
|
);
|
||||||
|
case SearchOption.web:
|
||||||
|
return Wrap(
|
||||||
|
runSpacing: 24,
|
||||||
|
spacing: 8,
|
||||||
|
alignment: WrapAlignment.center,
|
||||||
|
runAlignment: WrapAlignment.center,
|
||||||
|
crossAxisAlignment: WrapCrossAlignment.center,
|
||||||
|
children: [
|
||||||
|
for (final imageResult in resultsImageModels)
|
||||||
|
Image.network(
|
||||||
|
imageResult.uri.toString(),
|
||||||
|
loadingBuilder: (context, final child, final loadingProgress) =>
|
||||||
|
loadingProgress == null
|
||||||
|
? child
|
||||||
|
: Center(
|
||||||
|
child: CircularProgressIndicator(
|
||||||
|
value: loadingProgress.expectedTotalBytes != null
|
||||||
|
? loadingProgress.cumulativeBytesLoaded /
|
||||||
|
loadingProgress.expectedTotalBytes!
|
||||||
|
: null,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue