Skip to content

Commit 098bab7

Browse files
authored
fix(mobile): search page issues (#15804)
* fix: don't repeat search * fix: show snackbar for no result * fix: do not search on empty filter * chore: syling
1 parent 4fccc09 commit 098bab7

File tree

7 files changed

+159
-14
lines changed

7 files changed

+159
-14
lines changed

mobile/assets/i18n/en-US.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2+
"search_no_result": "No results found, try a different search term or combination",
3+
"search_no_more_result": "No more results",
24
"action_common_back": "Back",
35
"action_common_cancel": "Cancel",
46
"action_common_clear": "Clear",

mobile/lib/interfaces/person_api.interface.dart

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// ignore_for_file: public_member_api_docs, sort_constructors_first
2+
import 'dart:convert';
3+
14
abstract interface class IPersonApiRepository {
25
Future<List<Person>> getAll();
36
Future<Person> update(String id, {String? name});
@@ -6,10 +9,10 @@ abstract interface class IPersonApiRepository {
69
class Person {
710
Person({
811
required this.id,
12+
this.birthDate,
913
required this.isHidden,
1014
required this.name,
1115
required this.thumbnailPath,
12-
this.birthDate,
1316
this.updatedAt,
1417
});
1518

@@ -19,4 +22,80 @@ class Person {
1922
final String name;
2023
final String thumbnailPath;
2124
final DateTime? updatedAt;
25+
26+
@override
27+
String toString() {
28+
return 'Person(id: $id, birthDate: $birthDate, isHidden: $isHidden, name: $name, thumbnailPath: $thumbnailPath, updatedAt: $updatedAt)';
29+
}
30+
31+
Person copyWith({
32+
String? id,
33+
DateTime? birthDate,
34+
bool? isHidden,
35+
String? name,
36+
String? thumbnailPath,
37+
DateTime? updatedAt,
38+
}) {
39+
return Person(
40+
id: id ?? this.id,
41+
birthDate: birthDate ?? this.birthDate,
42+
isHidden: isHidden ?? this.isHidden,
43+
name: name ?? this.name,
44+
thumbnailPath: thumbnailPath ?? this.thumbnailPath,
45+
updatedAt: updatedAt ?? this.updatedAt,
46+
);
47+
}
48+
49+
Map<String, dynamic> toMap() {
50+
return <String, dynamic>{
51+
'id': id,
52+
'birthDate': birthDate?.millisecondsSinceEpoch,
53+
'isHidden': isHidden,
54+
'name': name,
55+
'thumbnailPath': thumbnailPath,
56+
'updatedAt': updatedAt?.millisecondsSinceEpoch,
57+
};
58+
}
59+
60+
factory Person.fromMap(Map<String, dynamic> map) {
61+
return Person(
62+
id: map['id'] as String,
63+
birthDate: map['birthDate'] != null
64+
? DateTime.fromMillisecondsSinceEpoch(map['birthDate'] as int)
65+
: null,
66+
isHidden: map['isHidden'] as bool,
67+
name: map['name'] as String,
68+
thumbnailPath: map['thumbnailPath'] as String,
69+
updatedAt: map['updatedAt'] != null
70+
? DateTime.fromMillisecondsSinceEpoch(map['updatedAt'] as int)
71+
: null,
72+
);
73+
}
74+
75+
String toJson() => json.encode(toMap());
76+
77+
factory Person.fromJson(String source) =>
78+
Person.fromMap(json.decode(source) as Map<String, dynamic>);
79+
80+
@override
81+
bool operator ==(covariant Person other) {
82+
if (identical(this, other)) return true;
83+
84+
return other.id == id &&
85+
other.birthDate == birthDate &&
86+
other.isHidden == isHidden &&
87+
other.name == name &&
88+
other.thumbnailPath == thumbnailPath &&
89+
other.updatedAt == updatedAt;
90+
}
91+
92+
@override
93+
int get hashCode {
94+
return id.hashCode ^
95+
birthDate.hashCode ^
96+
isHidden.hashCode ^
97+
name.hashCode ^
98+
thumbnailPath.hashCode ^
99+
updatedAt.hashCode;
100+
}
22101
}

mobile/lib/models/search/search_filter.model.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,23 @@ class SearchFilter {
255255
required this.mediaType,
256256
});
257257

258+
bool get isEmpty {
259+
return (context == null || (context != null && context!.isEmpty)) &&
260+
(filename == null || (filename!.isEmpty)) &&
261+
people.isEmpty &&
262+
location.country == null &&
263+
location.state == null &&
264+
location.city == null &&
265+
camera.make == null &&
266+
camera.model == null &&
267+
date.takenBefore == null &&
268+
date.takenAfter == null &&
269+
display.isNotInAlbum == false &&
270+
display.isArchive == false &&
271+
display.isFavorite == false &&
272+
mediaType == AssetType.other;
273+
}
274+
258275
SearchFilter copyWith({
259276
String? context,
260277
String? filename,

mobile/lib/pages/search/search.page.dart

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class SearchPage extends HookConsumerWidget {
4949
),
5050
);
5151

52-
final previousFilter = useState(filter.value);
52+
final previousFilter = useState<SearchFilter?>(null);
5353

5454
final peopleCurrentFilterWidget = useState<Widget?>(null);
5555
final dateRangeCurrentFilterWidget = useState<Widget?>(null);
@@ -60,19 +60,55 @@ class SearchPage extends HookConsumerWidget {
6060

6161
final isSearching = useState(false);
6262

63+
SnackBar searchInfoSnackBar(String message) {
64+
return SnackBar(
65+
content: Text(
66+
message,
67+
style: context.textTheme.labelLarge,
68+
),
69+
showCloseIcon: true,
70+
behavior: SnackBarBehavior.fixed,
71+
closeIconColor: context.colorScheme.onSurface,
72+
);
73+
}
74+
6375
search() async {
64-
if (prefilter == null && filter.value == previousFilter.value) return;
76+
if (filter.value.isEmpty) {
77+
return;
78+
}
79+
80+
if (prefilter == null && filter.value == previousFilter.value) {
81+
return;
82+
}
6583

6684
isSearching.value = true;
6785
ref.watch(paginatedSearchProvider.notifier).clear();
68-
await ref.watch(paginatedSearchProvider.notifier).search(filter.value);
86+
final hasResult = await ref
87+
.watch(paginatedSearchProvider.notifier)
88+
.search(filter.value);
89+
90+
if (!hasResult) {
91+
context.showSnackBar(
92+
searchInfoSnackBar('search_no_result'.tr()),
93+
);
94+
}
95+
6996
previousFilter.value = filter.value;
7097
isSearching.value = false;
7198
}
7299

73100
loadMoreSearchResult() async {
74101
isSearching.value = true;
75-
await ref.watch(paginatedSearchProvider.notifier).search(filter.value);
102+
final hasResult = await ref
103+
.watch(paginatedSearchProvider.notifier)
104+
.search(filter.value);
105+
106+
if (!hasResult) {
107+
context.showSnackBar(
108+
searchInfoSnackBar('search_no_more_result'.tr()),
109+
);
110+
}
111+
76112
isSearching.value = false;
77113
}
78114

@@ -596,10 +632,15 @@ class SearchPage extends HookConsumerWidget {
596632
),
597633
),
598634
),
599-
SearchResultGrid(
600-
onScrollEnd: loadMoreSearchResult,
601-
isSearching: isSearching.value,
602-
),
635+
if (isSearching.value)
636+
const Expanded(
637+
child: Center(child: CircularProgressIndicator.adaptive()),
638+
)
639+
else
640+
SearchResultGrid(
641+
onScrollEnd: loadMoreSearchResult,
642+
isSearching: isSearching.value,
643+
),
603644
],
604645
),
605646
);

mobile/lib/providers/search/paginated_search.provider.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,23 @@ class PaginatedSearchNotifier extends StateNotifier<SearchResult> {
1919
PaginatedSearchNotifier(this._searchService)
2020
: super(SearchResult(assets: [], nextPage: 1));
2121

22-
search(SearchFilter filter) async {
23-
if (state.nextPage == null) return;
22+
Future<bool> search(SearchFilter filter) async {
23+
if (state.nextPage == null) {
24+
return false;
25+
}
2426

2527
final result = await _searchService.search(filter, state.nextPage!);
2628

27-
if (result == null) return;
29+
if (result == null) {
30+
return false;
31+
}
2832

2933
state = SearchResult(
3034
assets: [...state.assets, ...result.assets],
3135
nextPage: result.nextPage,
3236
);
37+
38+
return true;
3339
}
3440

3541
clear() {

mobile/lib/services/search.service.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class SearchService {
101101
);
102102
}
103103

104-
if (response == null) {
104+
if (response == null || response.assets.items.isEmpty) {
105105
return null;
106106
}
107107

mobile/lib/widgets/search/search_filter/people_picker.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class PeoplePicker extends HookConsumerWidget {
2020
@override
2121
Widget build(BuildContext context, WidgetRef ref) {
2222
final formFocus = useFocusNode();
23-
final imageSize = 75.0;
23+
final imageSize = 60.0;
2424
final searchQuery = useState('');
2525
final people = ref.watch(getAllPeopleProvider);
2626
final headers = ApiService.getRequestHeaders();

0 commit comments

Comments
 (0)