forked from immich-app/immich
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mobile): search page (immich-app#13833)
* fix(mobile): search page minor problems * fix: flashing between search * restore search size * remove print statement * linting
- Loading branch information
1 parent
d41c102
commit a9bc7c9
Showing
5 changed files
with
141 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import 'package:collection/collection.dart'; | ||
|
||
import 'package:immich_mobile/entities/asset.entity.dart'; | ||
|
||
class SearchResult { | ||
final List<Asset> assets; | ||
final int? nextPage; | ||
|
||
SearchResult({ | ||
required this.assets, | ||
this.nextPage, | ||
}); | ||
|
||
SearchResult copyWith({ | ||
List<Asset>? assets, | ||
int? nextPage, | ||
}) { | ||
return SearchResult( | ||
assets: assets ?? this.assets, | ||
nextPage: nextPage ?? this.nextPage, | ||
); | ||
} | ||
|
||
@override | ||
String toString() => 'SearchResult(assets: $assets, nextPage: $nextPage)'; | ||
|
||
@override | ||
bool operator ==(covariant SearchResult other) { | ||
if (identical(this, other)) return true; | ||
final listEquals = const DeepCollectionEquality().equals; | ||
|
||
return listEquals(other.assets, assets) && other.nextPage == nextPage; | ||
} | ||
|
||
@override | ||
int get hashCode => assets.hashCode ^ nextPage.hashCode; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 25 additions & 36 deletions
61
mobile/lib/providers/search/paginated_search.provider.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,51 @@ | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:immich_mobile/models/search/search_result.model.dart'; | ||
import 'package:immich_mobile/providers/asset_viewer/render_list.provider.dart'; | ||
import 'package:immich_mobile/widgets/asset_grid/asset_grid_data_structure.dart'; | ||
import 'package:immich_mobile/models/search/search_filter.model.dart'; | ||
import 'package:immich_mobile/services/search.service.dart'; | ||
import 'package:immich_mobile/entities/asset.entity.dart'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
|
||
part 'paginated_search.provider.g.dart'; | ||
|
||
@riverpod | ||
class PaginatedSearch extends _$PaginatedSearch { | ||
Future<List<Asset>?> _search(SearchFilter filter, int page) async { | ||
final service = ref.read(searchServiceProvider); | ||
final result = await service.search(filter, page); | ||
|
||
return result; | ||
} | ||
final paginatedSearchProvider = | ||
StateNotifierProvider<PaginatedSearchNotifier, SearchResult>( | ||
(ref) => PaginatedSearchNotifier(ref.watch(searchServiceProvider)), | ||
); | ||
|
||
@override | ||
Future<List<Asset>> build() async { | ||
return []; | ||
} | ||
class PaginatedSearchNotifier extends StateNotifier<SearchResult> { | ||
final SearchService _searchService; | ||
|
||
Future<List<Asset>> getNextPage(SearchFilter filter, int nextPage) async { | ||
state = const AsyncValue.loading(); | ||
PaginatedSearchNotifier(this._searchService) | ||
: super(SearchResult(assets: [], nextPage: 1)); | ||
|
||
final newState = await AsyncValue.guard(() async { | ||
final assets = await _search(filter, nextPage); | ||
search(SearchFilter filter) async { | ||
if (state.nextPage == null) return; | ||
|
||
if (assets != null) { | ||
return [...?state.value, ...assets]; | ||
} | ||
}); | ||
final result = await _searchService.search(filter, state.nextPage!); | ||
|
||
state = newState.valueOrNull == null | ||
? const AsyncValue.data([]) | ||
: AsyncValue.data(newState.value!); | ||
if (result == null) return; | ||
|
||
return newState.valueOrNull ?? []; | ||
state = SearchResult( | ||
assets: [...state.assets, ...result.assets], | ||
nextPage: result.nextPage, | ||
); | ||
} | ||
|
||
clear() { | ||
state = const AsyncValue.data([]); | ||
state = SearchResult(assets: [], nextPage: 1); | ||
} | ||
} | ||
|
||
@riverpod | ||
AsyncValue<RenderList> paginatedSearchRenderList( | ||
PaginatedSearchRenderListRef ref, | ||
) { | ||
final assets = ref.watch(paginatedSearchProvider).value; | ||
final result = ref.watch(paginatedSearchProvider); | ||
|
||
if (assets != null) { | ||
return ref.watch( | ||
renderListProviderWithGrouping( | ||
(assets, GroupAssetsBy.none), | ||
), | ||
); | ||
} else { | ||
return const AsyncValue.loading(); | ||
} | ||
return ref.watch( | ||
renderListProviderWithGrouping( | ||
(result.assets, GroupAssetsBy.none), | ||
), | ||
); | ||
} |
18 changes: 1 addition & 17 deletions
18
mobile/lib/providers/search/paginated_search.provider.g.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters