2024-01-05 07:20:55 +02:00
|
|
|
import 'package:auto_route/auto_route.dart';
|
2022-07-07 20:40:54 +02:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2022-03-03 00:44:24 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2023-11-09 18:19:53 +02:00
|
|
|
import 'package:immich_mobile/extensions/build_context_extensions.dart';
|
2022-03-03 00:44:24 +02:00
|
|
|
import 'package:immich_mobile/modules/search/providers/search_page_state.provider.dart';
|
2022-03-16 17:19:31 +02:00
|
|
|
import 'package:immich_mobile/modules/search/providers/search_result_page.provider.dart';
|
2022-03-03 00:44:24 +02:00
|
|
|
import 'package:immich_mobile/modules/search/ui/search_suggestion_list.dart';
|
2024-01-05 23:23:58 +02:00
|
|
|
import 'package:immich_mobile/shared/ui/asset_grid/multiselect_grid.dart';
|
2022-11-11 19:52:02 +02:00
|
|
|
import 'package:immich_mobile/shared/ui/immich_loading_indicator.dart';
|
2022-03-03 00:44:24 +02:00
|
|
|
|
2023-03-28 17:34:06 +02:00
|
|
|
class SearchType {
|
|
|
|
SearchType({required this.isClip, required this.searchTerm});
|
|
|
|
|
|
|
|
final bool isClip;
|
|
|
|
final String searchTerm;
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchType _getSearchType(String searchTerm) {
|
|
|
|
if (searchTerm.startsWith('m:')) {
|
|
|
|
return SearchType(isClip: false, searchTerm: searchTerm.substring(2));
|
|
|
|
} else {
|
|
|
|
return SearchType(isClip: true, searchTerm: searchTerm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 00:44:24 +02:00
|
|
|
class SearchResultPage extends HookConsumerWidget {
|
2023-03-28 17:34:06 +02:00
|
|
|
const SearchResultPage({
|
|
|
|
Key? key,
|
|
|
|
required this.searchTerm,
|
|
|
|
}) : super(key: key);
|
2022-03-03 00:44:24 +02:00
|
|
|
|
|
|
|
final String searchTerm;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
|
|
final searchTermController = useTextEditingController(text: "");
|
|
|
|
final isNewSearch = useState(false);
|
|
|
|
final currentSearchTerm = useState(searchTerm);
|
|
|
|
|
2022-08-03 22:36:12 +02:00
|
|
|
FocusNode? searchFocusNode;
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
useEffect(
|
|
|
|
() {
|
|
|
|
searchFocusNode = FocusNode();
|
|
|
|
|
2023-03-28 17:34:06 +02:00
|
|
|
var searchType = _getSearchType(searchTerm);
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
Future.delayed(
|
|
|
|
Duration.zero,
|
2023-03-28 17:34:06 +02:00
|
|
|
() => ref
|
|
|
|
.read(searchResultPageProvider.notifier)
|
|
|
|
.search(searchType.searchTerm, clipEnable: searchType.isClip),
|
2022-07-13 14:23:48 +02:00
|
|
|
);
|
2022-08-03 22:36:12 +02:00
|
|
|
return () => searchFocusNode?.dispose();
|
2022-07-13 14:23:48 +02:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
2022-03-03 00:44:24 +02:00
|
|
|
|
2024-01-05 23:23:58 +02:00
|
|
|
Future<void> onSearchSubmitted(String newSearchTerm) {
|
2022-03-03 00:44:24 +02:00
|
|
|
debugPrint("Re-Search with $newSearchTerm");
|
2022-08-03 22:36:12 +02:00
|
|
|
searchFocusNode?.unfocus();
|
2022-03-03 00:44:24 +02:00
|
|
|
isNewSearch.value = false;
|
|
|
|
currentSearchTerm.value = newSearchTerm;
|
2023-03-28 17:34:06 +02:00
|
|
|
var searchType = _getSearchType(newSearchTerm);
|
2024-01-05 23:23:58 +02:00
|
|
|
return ref
|
2023-03-28 17:34:06 +02:00
|
|
|
.watch(searchResultPageProvider.notifier)
|
|
|
|
.search(searchType.searchTerm, clipEnable: searchType.isClip);
|
2022-03-03 00:44:24 +02:00
|
|
|
}
|
|
|
|
|
2022-11-21 14:13:14 +02:00
|
|
|
buildTextField() {
|
2022-03-03 00:44:24 +02:00
|
|
|
return TextField(
|
|
|
|
controller: searchTermController,
|
|
|
|
focusNode: searchFocusNode,
|
|
|
|
autofocus: false,
|
|
|
|
onTap: () {
|
|
|
|
searchTermController.clear();
|
|
|
|
ref.watch(searchPageStateProvider.notifier).setSearchTerm("");
|
2022-08-03 22:36:12 +02:00
|
|
|
searchFocusNode?.requestFocus();
|
2022-03-03 00:44:24 +02:00
|
|
|
},
|
|
|
|
textInputAction: TextInputAction.search,
|
|
|
|
onSubmitted: (searchTerm) {
|
|
|
|
if (searchTerm.isNotEmpty) {
|
|
|
|
searchTermController.clear();
|
2022-11-21 14:13:14 +02:00
|
|
|
onSearchSubmitted(searchTerm);
|
2022-03-03 00:44:24 +02:00
|
|
|
} else {
|
|
|
|
isNewSearch.value = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onChanged: (value) {
|
|
|
|
ref.watch(searchPageStateProvider.notifier).setSearchTerm(value);
|
|
|
|
},
|
2022-07-07 20:40:54 +02:00
|
|
|
decoration: InputDecoration(
|
|
|
|
hintText: 'search_result_page_new_search_hint'.tr(),
|
2022-07-13 14:23:48 +02:00
|
|
|
enabledBorder: const UnderlineInputBorder(
|
2022-03-03 00:44:24 +02:00
|
|
|
borderSide: BorderSide(color: Colors.transparent),
|
|
|
|
),
|
2022-07-13 14:23:48 +02:00
|
|
|
focusedBorder: const UnderlineInputBorder(
|
2022-03-03 00:44:24 +02:00
|
|
|
borderSide: BorderSide(color: Colors.transparent),
|
|
|
|
),
|
2023-03-28 17:34:06 +02:00
|
|
|
hintStyle: TextStyle(
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
fontSize: 16.0,
|
2023-11-09 18:19:53 +02:00
|
|
|
color: context.isDarkTheme
|
|
|
|
? Colors.grey[500]
|
|
|
|
: Colors.black.withOpacity(0.5),
|
2023-03-28 17:34:06 +02:00
|
|
|
),
|
2022-03-03 00:44:24 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-21 14:13:14 +02:00
|
|
|
buildChip() {
|
2022-03-03 00:44:24 +02:00
|
|
|
return Chip(
|
|
|
|
label: Wrap(
|
|
|
|
spacing: 5,
|
2022-05-30 00:32:30 +02:00
|
|
|
runAlignment: WrapAlignment.center,
|
|
|
|
crossAxisAlignment: WrapCrossAlignment.center,
|
|
|
|
alignment: WrapAlignment.center,
|
2022-03-03 00:44:24 +02:00
|
|
|
children: [
|
2022-05-30 00:32:30 +02:00
|
|
|
Text(
|
|
|
|
currentSearchTerm.value,
|
2022-06-22 07:23:35 +02:00
|
|
|
style: TextStyle(
|
2023-11-09 18:19:53 +02:00
|
|
|
color: context.primaryColor,
|
2022-07-13 14:23:48 +02:00
|
|
|
fontSize: 13,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
),
|
2022-05-30 00:32:30 +02:00
|
|
|
maxLines: 1,
|
2022-03-03 00:44:24 +02:00
|
|
|
),
|
|
|
|
Icon(
|
|
|
|
Icons.close_rounded,
|
2023-11-09 18:19:53 +02:00
|
|
|
color: context.primaryColor,
|
2022-03-03 00:44:24 +02:00
|
|
|
size: 20,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2023-11-09 18:19:53 +02:00
|
|
|
backgroundColor: context.primaryColor.withAlpha(50),
|
2022-03-03 00:44:24 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-05 23:23:58 +02:00
|
|
|
Future<void> refresh() async => onSearchSubmitted(currentSearchTerm.value);
|
|
|
|
|
2022-11-21 14:13:14 +02:00
|
|
|
buildSearchResult() {
|
2024-01-05 23:23:58 +02:00
|
|
|
final searchResultPageState = ref.watch(searchResultPageProvider);
|
2022-09-30 11:05:54 +02:00
|
|
|
|
2022-03-03 00:44:24 +02:00
|
|
|
if (searchResultPageState.isError) {
|
2023-03-15 23:29:07 +02:00
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.all(12),
|
|
|
|
child: const Text("common_server_error").tr(),
|
|
|
|
);
|
2022-03-03 00:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (searchResultPageState.isLoading) {
|
2022-11-11 19:52:02 +02:00
|
|
|
return const Center(child: ImmichLoadingIndicator());
|
2022-03-03 00:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (searchResultPageState.isSuccess) {
|
2024-01-05 23:23:58 +02:00
|
|
|
return MultiselectGrid(
|
|
|
|
renderListProvider: searchRenderListProvider,
|
|
|
|
archiveEnabled: true,
|
|
|
|
deleteEnabled: true,
|
|
|
|
editEnabled: true,
|
|
|
|
favoriteEnabled: true,
|
|
|
|
stackEnabled: false,
|
|
|
|
onRefresh: refresh,
|
|
|
|
);
|
2022-03-03 00:44:24 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 03:08:49 +02:00
|
|
|
return const SizedBox();
|
2022-03-03 00:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
leading: IconButton(
|
|
|
|
splashRadius: 20,
|
|
|
|
onPressed: () {
|
|
|
|
if (isNewSearch.value) {
|
|
|
|
isNewSearch.value = false;
|
|
|
|
} else {
|
2024-01-05 07:20:55 +02:00
|
|
|
context.popRoute(true);
|
2022-03-03 00:44:24 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.arrow_back_ios_rounded),
|
|
|
|
),
|
|
|
|
title: GestureDetector(
|
2022-07-13 14:23:48 +02:00
|
|
|
onTap: () {
|
|
|
|
isNewSearch.value = true;
|
2022-08-03 22:36:12 +02:00
|
|
|
searchFocusNode?.requestFocus();
|
2022-07-13 14:23:48 +02:00
|
|
|
},
|
2022-11-21 14:13:14 +02:00
|
|
|
child: isNewSearch.value ? buildTextField() : buildChip(),
|
2022-07-13 14:23:48 +02:00
|
|
|
),
|
2022-03-03 00:44:24 +02:00
|
|
|
centerTitle: false,
|
|
|
|
),
|
|
|
|
body: GestureDetector(
|
|
|
|
onTap: () {
|
2022-08-03 22:36:12 +02:00
|
|
|
if (searchFocusNode != null) {
|
|
|
|
searchFocusNode?.unfocus();
|
|
|
|
}
|
|
|
|
|
2022-03-03 00:44:24 +02:00
|
|
|
ref.watch(searchPageStateProvider.notifier).disableSearch();
|
|
|
|
},
|
|
|
|
child: Stack(
|
|
|
|
children: [
|
2022-11-21 14:13:14 +02:00
|
|
|
buildSearchResult(),
|
2022-07-01 03:08:49 +02:00
|
|
|
if (isNewSearch.value)
|
2022-11-21 14:13:14 +02:00
|
|
|
SearchSuggestionList(onSubmitted: onSearchSubmitted),
|
2022-03-03 00:44:24 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|