live WEB search
This commit is contained in:
parent
47945dbec7
commit
4ade7f1682
19 changed files with 503 additions and 59 deletions
|
@ -1,3 +1,5 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||
|
@ -10,6 +12,7 @@ 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 '../../data/enums/search_option.dart';
|
||||
|
||||
class GalleryViewModel extends BaseViewModel {
|
||||
GalleryViewModel({
|
||||
|
@ -30,6 +33,13 @@ class GalleryViewModel extends BaseViewModel {
|
|||
final ValueNotifier<bool> _isDisplayingPressingPrompt = ValueNotifier(true);
|
||||
ValueListenable<bool> get isDisplayingPressingPrompt => _isDisplayingPressingPrompt;
|
||||
|
||||
final ValueNotifier<bool> _isSearchingNotifier = ValueNotifier(false);
|
||||
ValueListenable<bool> get isSearchingListenable => _isSearchingNotifier;
|
||||
final ValueNotifier<SearchOption> _searchOptionNotifier = ValueNotifier(SearchOption.web);
|
||||
ValueListenable<SearchOption> get searchOptionListenable => _searchOptionNotifier;
|
||||
final ValueNotifier<List<ImageModel>> _imageSearchResultsNotifier = ValueNotifier([]);
|
||||
ValueListenable<List<ImageModel>> get imageSearchResultsListenable => _imageSearchResultsNotifier;
|
||||
|
||||
@override
|
||||
Future<void> initialise(bool Function() mounted, [arguments]) async {
|
||||
super.initialise(mounted, arguments);
|
||||
|
@ -40,9 +50,44 @@ class GalleryViewModel extends BaseViewModel {
|
|||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> onSearchTermUpdate(String searchTerm) async {
|
||||
// If empty-string (from backspacing) -> reset state.
|
||||
if (searchTerm.isEmpty) {
|
||||
_imageSearchResultsNotifier.value = [];
|
||||
return;
|
||||
}
|
||||
|
||||
// Detached call to prevent UI blocking
|
||||
unawaited(_imagesService
|
||||
.searchImages(
|
||||
imageNamePart: searchTerm,
|
||||
searchOption: searchOptionListenable.value,
|
||||
)
|
||||
.then(
|
||||
(final fetchedImageModels) => _imageSearchResultsNotifier.value = fetchedImageModels));
|
||||
|
||||
// Force-update to trigger listening to `lastQueryResultDone()`.
|
||||
_imageSearchResultsNotifier.notifyListeners();
|
||||
}
|
||||
|
||||
void searchPressed() {
|
||||
// If transitioning from 'Searching', clear previous results immediately
|
||||
if (_isSearchingNotifier.value) _imageSearchResultsNotifier.value = [];
|
||||
|
||||
_isSearchingNotifier.value = !_isSearchingNotifier.value;
|
||||
}
|
||||
|
||||
Future<void> get lastQueryResultDone => _imagesService.lastQueryIsCompleted;
|
||||
|
||||
void onSearchOptionChanged(SearchOption? option) => _searchOptionNotifier.value = option!;
|
||||
|
||||
void onPromptPressed() => _isDisplayingPressingPrompt.value = false;
|
||||
|
||||
Iterable<ImageModel> get imageModels => _imagesService.imageModels;
|
||||
Future<void> get initImageFetchIsDone => _imagesService.initAwaiter;
|
||||
|
||||
double? downloadProgressValue({required DownloadProgress progress}) =>
|
||||
progress.totalSize != null ? progress.downloaded / progress.totalSize! : null;
|
||||
|
||||
void pushImageCarouselView(BuildContext context, {required ImageModel imageModel}) =>
|
||||
_navigationService.pushImageCarouselView(
|
||||
|
@ -53,7 +98,4 @@ class GalleryViewModel extends BaseViewModel {
|
|||
);
|
||||
|
||||
static GalleryViewModel get locate => Locator.locate();
|
||||
|
||||
double? downloadProgressValue({required DownloadProgress progress}) =>
|
||||
progress.totalSize != null ? progress.downloaded / progress.totalSize! : null;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue