2022-03-02 15:44:24 -07:00
|
|
|
import 'package:auto_route/auto_route.dart';
|
2022-07-07 11:40:54 -07:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2022-03-02 15:44:24 -07:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2022-04-02 10:31:53 -07:00
|
|
|
import 'package:flutter_spinkit/flutter_spinkit.dart';
|
2022-03-02 15:44:24 -07:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:immich_mobile/modules/home/ui/daily_title_text.dart';
|
|
|
|
import 'package:immich_mobile/modules/home/ui/draggable_scrollbar.dart';
|
|
|
|
import 'package:immich_mobile/modules/home/ui/image_grid.dart';
|
|
|
|
import 'package:immich_mobile/modules/home/ui/monthly_title_text.dart';
|
|
|
|
import 'package:immich_mobile/modules/search/providers/search_page_state.provider.dart';
|
2022-03-16 08:19:31 -07:00
|
|
|
import 'package:immich_mobile/modules/search/providers/search_result_page.provider.dart';
|
2022-03-02 15:44:24 -07:00
|
|
|
import 'package:immich_mobile/modules/search/ui/search_suggestion_list.dart';
|
2022-08-03 13:36:12 -07:00
|
|
|
import 'package:openapi/api.dart';
|
2022-03-02 15:44:24 -07:00
|
|
|
|
|
|
|
class SearchResultPage extends HookConsumerWidget {
|
2022-06-21 22:23:35 -07:00
|
|
|
const SearchResultPage({Key? key, required this.searchTerm})
|
|
|
|
: super(key: key);
|
2022-03-02 15:44:24 -07:00
|
|
|
|
|
|
|
final String searchTerm;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2022-06-21 22:23:35 -07:00
|
|
|
ScrollController scrollController = useScrollController();
|
2022-03-02 15:44:24 -07:00
|
|
|
final searchTermController = useTextEditingController(text: "");
|
|
|
|
final isNewSearch = useState(false);
|
|
|
|
final currentSearchTerm = useState(searchTerm);
|
|
|
|
|
2022-06-21 22:23:35 -07:00
|
|
|
final List<Widget> imageGridGroup = [];
|
|
|
|
|
2022-08-03 13:36:12 -07:00
|
|
|
FocusNode? searchFocusNode;
|
|
|
|
|
|
|
|
List<AssetResponseDto> sortedAssetList = [];
|
2022-03-02 15:44:24 -07:00
|
|
|
|
2022-07-13 05:23:48 -07:00
|
|
|
useEffect(
|
|
|
|
() {
|
|
|
|
searchFocusNode = FocusNode();
|
|
|
|
|
|
|
|
Future.delayed(
|
|
|
|
Duration.zero,
|
|
|
|
() => ref.read(searchResultPageProvider.notifier).search(searchTerm),
|
|
|
|
);
|
2022-08-03 13:36:12 -07:00
|
|
|
return () => searchFocusNode?.dispose();
|
2022-07-13 05:23:48 -07:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
2022-03-02 15:44:24 -07:00
|
|
|
|
|
|
|
_onSearchSubmitted(String newSearchTerm) {
|
|
|
|
debugPrint("Re-Search with $newSearchTerm");
|
2022-08-03 13:36:12 -07:00
|
|
|
searchFocusNode?.unfocus();
|
2022-03-02 15:44:24 -07:00
|
|
|
isNewSearch.value = false;
|
|
|
|
currentSearchTerm.value = newSearchTerm;
|
2022-03-16 08:19:31 -07:00
|
|
|
ref.watch(searchResultPageProvider.notifier).search(newSearchTerm);
|
2022-03-02 15:44:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_buildTextField() {
|
|
|
|
return TextField(
|
|
|
|
controller: searchTermController,
|
|
|
|
focusNode: searchFocusNode,
|
|
|
|
autofocus: false,
|
|
|
|
onTap: () {
|
|
|
|
searchTermController.clear();
|
|
|
|
ref.watch(searchPageStateProvider.notifier).setSearchTerm("");
|
2022-08-03 13:36:12 -07:00
|
|
|
searchFocusNode?.requestFocus();
|
2022-03-02 15:44:24 -07:00
|
|
|
},
|
|
|
|
textInputAction: TextInputAction.search,
|
|
|
|
onSubmitted: (searchTerm) {
|
|
|
|
if (searchTerm.isNotEmpty) {
|
|
|
|
searchTermController.clear();
|
|
|
|
_onSearchSubmitted(searchTerm);
|
|
|
|
} else {
|
|
|
|
isNewSearch.value = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onChanged: (value) {
|
|
|
|
ref.watch(searchPageStateProvider.notifier).setSearchTerm(value);
|
|
|
|
},
|
2022-07-07 11:40:54 -07:00
|
|
|
decoration: InputDecoration(
|
|
|
|
hintText: 'search_result_page_new_search_hint'.tr(),
|
2022-07-13 05:23:48 -07:00
|
|
|
enabledBorder: const UnderlineInputBorder(
|
2022-03-02 15:44:24 -07:00
|
|
|
borderSide: BorderSide(color: Colors.transparent),
|
|
|
|
),
|
2022-07-13 05:23:48 -07:00
|
|
|
focusedBorder: const UnderlineInputBorder(
|
2022-03-02 15:44:24 -07:00
|
|
|
borderSide: BorderSide(color: Colors.transparent),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_buildChip() {
|
|
|
|
return Chip(
|
|
|
|
label: Wrap(
|
|
|
|
spacing: 5,
|
2022-05-29 15:32:30 -07:00
|
|
|
runAlignment: WrapAlignment.center,
|
|
|
|
crossAxisAlignment: WrapCrossAlignment.center,
|
|
|
|
alignment: WrapAlignment.center,
|
2022-03-02 15:44:24 -07:00
|
|
|
children: [
|
2022-05-29 15:32:30 -07:00
|
|
|
Text(
|
|
|
|
currentSearchTerm.value,
|
2022-06-21 22:23:35 -07:00
|
|
|
style: TextStyle(
|
2022-07-13 05:23:48 -07:00
|
|
|
color: Theme.of(context).primaryColor,
|
|
|
|
fontSize: 13,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
),
|
2022-05-29 15:32:30 -07:00
|
|
|
maxLines: 1,
|
2022-03-02 15:44:24 -07:00
|
|
|
),
|
|
|
|
Icon(
|
|
|
|
Icons.close_rounded,
|
|
|
|
color: Theme.of(context).primaryColor,
|
|
|
|
size: 20,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
backgroundColor: Theme.of(context).primaryColor.withAlpha(50),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_buildSearchResult() {
|
2022-03-16 08:19:31 -07:00
|
|
|
var searchResultPageState = ref.watch(searchResultPageProvider);
|
2022-03-02 15:44:24 -07:00
|
|
|
var assetGroupByDateTime = ref.watch(searchResultGroupByDateTimeProvider);
|
|
|
|
|
|
|
|
if (searchResultPageState.isError) {
|
|
|
|
return const Text("Error");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (searchResultPageState.isLoading) {
|
2022-04-02 10:31:53 -07:00
|
|
|
return Center(
|
2022-07-13 05:23:48 -07:00
|
|
|
child: SpinKitDancingSquare(
|
|
|
|
color: Theme.of(context).primaryColor,
|
|
|
|
),
|
|
|
|
);
|
2022-03-02 15:44:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (searchResultPageState.isSuccess) {
|
|
|
|
if (searchResultPageState.searchResult.isNotEmpty) {
|
|
|
|
int? lastMonth;
|
2022-08-03 13:36:12 -07:00
|
|
|
// set sorted List
|
|
|
|
for (var group in assetGroupByDateTime.values) {
|
|
|
|
for (var value in group) {
|
|
|
|
sortedAssetList.add(value);
|
|
|
|
}
|
|
|
|
}
|
2022-03-02 15:44:24 -07:00
|
|
|
assetGroupByDateTime.forEach((dateGroup, immichAssetList) {
|
|
|
|
DateTime parseDateGroup = DateTime.parse(dateGroup);
|
|
|
|
int currentMonth = parseDateGroup.month;
|
|
|
|
|
|
|
|
if (lastMonth != null) {
|
|
|
|
if (currentMonth - lastMonth! != 0) {
|
2022-06-21 22:23:35 -07:00
|
|
|
imageGridGroup.add(
|
2022-03-02 15:44:24 -07:00
|
|
|
MonthlyTitleText(
|
|
|
|
isoDate: dateGroup,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 22:23:35 -07:00
|
|
|
imageGridGroup.add(
|
2022-03-02 15:44:24 -07:00
|
|
|
DailyTitleText(
|
|
|
|
isoDate: dateGroup,
|
|
|
|
assetGroup: immichAssetList,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2022-06-21 22:23:35 -07:00
|
|
|
imageGridGroup.add(
|
2022-08-03 13:36:12 -07:00
|
|
|
ImageGrid(
|
|
|
|
assetGroup: immichAssetList,
|
|
|
|
sortedAssetGroup: sortedAssetList,
|
|
|
|
),
|
2022-03-02 15:44:24 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
lastMonth = currentMonth;
|
|
|
|
});
|
|
|
|
|
|
|
|
return DraggableScrollbar.semicircle(
|
2022-08-15 16:53:30 -07:00
|
|
|
backgroundColor: Theme.of(context).hintColor,
|
2022-06-21 22:23:35 -07:00
|
|
|
controller: scrollController,
|
2022-03-02 15:44:24 -07:00
|
|
|
heightScrollThumb: 48.0,
|
|
|
|
child: CustomScrollView(
|
2022-06-21 22:23:35 -07:00
|
|
|
controller: scrollController,
|
|
|
|
slivers: [...imageGridGroup],
|
2022-03-02 15:44:24 -07:00
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return const Text("No assets found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 18:08:49 -07:00
|
|
|
return const SizedBox();
|
2022-03-02 15:44:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
leading: IconButton(
|
|
|
|
splashRadius: 20,
|
|
|
|
onPressed: () {
|
|
|
|
if (isNewSearch.value) {
|
|
|
|
isNewSearch.value = false;
|
|
|
|
} else {
|
|
|
|
AutoRouter.of(context).pop(true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.arrow_back_ios_rounded),
|
|
|
|
),
|
|
|
|
title: GestureDetector(
|
2022-07-13 05:23:48 -07:00
|
|
|
onTap: () {
|
|
|
|
isNewSearch.value = true;
|
2022-08-03 13:36:12 -07:00
|
|
|
searchFocusNode?.requestFocus();
|
2022-07-13 05:23:48 -07:00
|
|
|
},
|
|
|
|
child: isNewSearch.value ? _buildTextField() : _buildChip(),
|
|
|
|
),
|
2022-03-02 15:44:24 -07:00
|
|
|
centerTitle: false,
|
|
|
|
),
|
|
|
|
body: GestureDetector(
|
|
|
|
onTap: () {
|
2022-08-03 13:36:12 -07:00
|
|
|
if (searchFocusNode != null) {
|
|
|
|
searchFocusNode?.unfocus();
|
|
|
|
}
|
|
|
|
|
2022-03-02 15:44:24 -07:00
|
|
|
ref.watch(searchPageStateProvider.notifier).disableSearch();
|
|
|
|
},
|
|
|
|
child: Stack(
|
|
|
|
children: [
|
|
|
|
_buildSearchResult(),
|
2022-06-30 18:08:49 -07:00
|
|
|
if (isNewSearch.value)
|
|
|
|
SearchSuggestionList(onSubmitted: _onSearchSubmitted),
|
2022-03-02 15:44:24 -07:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|