Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Handle search back gesture state

ashilknb6d9d5ffe8b5
diff --git a/mobile/apps/photos/lib/ui/tabs/albums_tab.dart b/mobile/apps/photos/lib/ui/tabs/albums_tab.dart
index 43357c33da..e6a223eefc 100644
--- a/mobile/apps/photos/lib/ui/tabs/albums_tab.dart
+++ b/mobile/apps/photos/lib/ui/tabs/albums_tab.dart
@@ -43,10 +43,14 @@ class AlbumsTab extends StatefulWidget {
super.key,
this.selectedAlbums,
this.isSearchActiveNotifier,
+ this.isSearchFieldFocusedNotifier,
+ this.isSearchFieldNotEmptyNotifier,
});
final SelectedAlbums? selectedAlbums;
final ValueNotifier<bool>? isSearchActiveNotifier;
+ final ValueNotifier<bool>? isSearchFieldFocusedNotifier;
+ final ValueNotifier<bool>? isSearchFieldNotEmptyNotifier;
@override
State<AlbumsTab> createState() => _AlbumsTabState();
@@ -162,19 +166,39 @@ class _AlbumsTabState extends State<AlbumsTab>
_tabChangedEvent = Bus.instance.on<TabChangedEvent>().listen(
_handleTabChanged,
);
+ _searchFocusNode.addListener(_handleSearchFocusChanged);
widget.isSearchActiveNotifier?.addListener(_handleSearchStateChanged);
_syncSearchNotifier(_isSearchActive);
+ _syncSearchFieldFocusNotifier(_searchFocusNode.hasFocus);
+ _syncSearchFieldTextNotifier(_hasSearchQuery);
}
@override
void didUpdateWidget(covariant AlbumsTab oldWidget) {
super.didUpdateWidget(oldWidget);
- if (oldWidget.isSearchActiveNotifier == widget.isSearchActiveNotifier) {
- return;
+ if (oldWidget.isSearchActiveNotifier != widget.isSearchActiveNotifier) {
+ oldWidget.isSearchActiveNotifier?.removeListener(
+ _handleSearchStateChanged,
+ );
+ widget.isSearchActiveNotifier?.addListener(_handleSearchStateChanged);
+ _syncSearchNotifier(_isSearchActive);
+ }
+ if (oldWidget.isSearchFieldFocusedNotifier !=
+ widget.isSearchFieldFocusedNotifier) {
+ _syncSearchFieldFocusNotifier(
+ false,
+ oldWidget.isSearchFieldFocusedNotifier,
+ );
+ _syncSearchFieldFocusNotifier(_searchFocusNode.hasFocus);
+ }
+ if (oldWidget.isSearchFieldNotEmptyNotifier !=
+ widget.isSearchFieldNotEmptyNotifier) {
+ _syncSearchFieldTextNotifier(
+ false,
+ oldWidget.isSearchFieldNotEmptyNotifier,
+ );
+ _syncSearchFieldTextNotifier(_hasSearchQuery);
}
- oldWidget.isSearchActiveNotifier?.removeListener(_handleSearchStateChanged);
- widget.isSearchActiveNotifier?.addListener(_handleSearchStateChanged);
- _syncSearchNotifier(_isSearchActive);
}
void _handleSearchStateChanged() {
@@ -189,12 +213,34 @@ class _AlbumsTabState extends State<AlbumsTab>
}
}
+ void _handleSearchFocusChanged() {
+ _syncSearchFieldFocusNotifier(_searchFocusNode.hasFocus);
+ }
+
void _syncSearchNotifier(bool isSearchActive) {
final notifier = widget.isSearchActiveNotifier;
if (notifier == null || notifier.value == isSearchActive) return;
notifier.value = isSearchActive;
}
+ void _syncSearchFieldFocusNotifier(
+ bool hasFocus, [
+ ValueNotifier<bool>? notifier,
+ ]) {
+ final focusNotifier = notifier ?? widget.isSearchFieldFocusedNotifier;
+ if (focusNotifier == null || focusNotifier.value == hasFocus) return;
+ focusNotifier.value = hasFocus;
+ }
+
+ void _syncSearchFieldTextNotifier(
+ bool hasText, [
+ ValueNotifier<bool>? notifier,
+ ]) {
+ final textNotifier = notifier ?? widget.isSearchFieldNotEmptyNotifier;
+ if (textNotifier == null || textNotifier.value == hasText) return;
+ textNotifier.value = hasText;
+ }
+
Future<void> _loadAll() async {
if (!Configuration.instance.hasConfiguredAccount()) {
_enteCollections.value = <Collection>[];
@@ -287,6 +333,7 @@ class _AlbumsTabState extends State<AlbumsTab>
_isSearchActive = false;
_searchQuery = "";
});
+ _syncSearchFieldTextNotifier(false);
if (syncNotifier) {
_syncSearchNotifier(false);
}
@@ -690,6 +737,9 @@ class _AlbumsTabState extends State<AlbumsTab>
_loggedOutEvent.cancel();
_tabChangedEvent.cancel();
widget.isSearchActiveNotifier?.removeListener(_handleSearchStateChanged);
+ _syncSearchFieldFocusNotifier(false);
+ _syncSearchFieldTextNotifier(false);
+ _searchFocusNode.removeListener(_handleSearchFocusChanged);
_debouncer.cancelDebounceTimer();
_searchController.dispose();
_searchFocusNode.dispose();
@@ -796,6 +846,9 @@ class _AlbumsTabState extends State<AlbumsTab>
setState(() {
_searchQuery = value;
});
+ _syncSearchFieldTextNotifier(
+ value.trim().isNotEmpty,
+ );
},
),
),
diff --git a/mobile/apps/photos/lib/ui/tabs/home_widget.dart b/mobile/apps/photos/lib/ui/tabs/home_widget.dart
index b151acc6e8..420efadc48 100644
--- a/mobile/apps/photos/lib/ui/tabs/home_widget.dart
+++ b/mobile/apps/photos/lib/ui/tabs/home_widget.dart
@@ -21,6 +21,7 @@ import "package:photos/events/account_configured_event.dart";
import "package:photos/events/app_mode_changed_event.dart";
import "package:photos/events/backup_folders_updated_event.dart";
import "package:photos/events/christmas_banner_event.dart";
+import "package:photos/events/clear_and_unfocus_search_bar_event.dart";
import "package:photos/events/homepage_swipe_to_select_in_progress_event.dart";
import "package:photos/events/opened_settings_event.dart";
import "package:photos/events/permission_granted_event.dart";
@@ -101,8 +102,6 @@ class HomeWidget extends StatefulWidget {
class _HomeWidgetState extends State<HomeWidget> {
static const _feedTab = FeedScreen(showBackButton: false);
- static const _searchTab = SearchTab();
-
final _logger = Logger("HomeWidgetState");
final _selectedAlbums = SelectedAlbums();
final _selectedFiles = SelectedFiles();
@@ -128,6 +127,14 @@ class _HomeWidgetState extends State<HomeWidget> {
final ValueNotifier<bool> _isAlbumsSearchActiveNotifier = ValueNotifier<bool>(
false,
);
+ final ValueNotifier<bool> _isAlbumsSearchFieldFocusedNotifier =
+ ValueNotifier<bool>(false);
+ final ValueNotifier<bool> _isAlbumsSearchFieldNotEmptyNotifier =
+ ValueNotifier<bool>(false);
+ final ValueNotifier<bool> _isSearchTabSearchFieldFocusedNotifier =
+ ValueNotifier<bool>(false);
+ final ValueNotifier<bool> _isSearchTabSearchFieldNotEmptyNotifier =
+ ValueNotifier<bool>(false);
final ValueNotifier<bool> _swipeToSelectInProgressNotifier =
ValueNotifier<bool>(false);
@@ -547,6 +554,10 @@ class _HomeWidgetState extends State<HomeWidget> {
_intentDataStreamSubscription?.cancel();
isOnSearchTabNotifier.dispose();
_isAlbumsSearchActiveNotifier.dispose();
+ _isAlbumsSearchFieldFocusedNotifier.dispose();
+ _isAlbumsSearchFieldNotEmptyNotifier.dispose();
+ _isSearchTabSearchFieldFocusedNotifier.dispose();
+ _isSearchTabSearchFieldNotEmptyNotifier.dispose();
_pageController.dispose();
_publicAlbumLinkSubscription?.cancel();
_authDeepLinkSubscription?.cancel();
@@ -843,15 +854,28 @@ class _HomeWidgetState extends State<HomeWidget> {
_selectedAlbums.clearAll();
return;
}
- if (_isAlbumsSearchActiveNotifier.value) {
- if (MediaQuery.viewInsetsOf(context).bottom > 0) {
+ if (_isAlbumsSearchFieldFocusedNotifier.value) {
+ if (_isAlbumsSearchActiveNotifier.value) {
+ _isAlbumsSearchActiveNotifier.value = false;
+ } else {
FocusManager.instance.primaryFocus?.unfocus();
- return;
}
+ return;
+ }
+ if (_isAlbumsSearchFieldNotEmptyNotifier.value) {
+ if (_isAlbumsSearchActiveNotifier.value) {
+ _isAlbumsSearchActiveNotifier.value = false;
+ }
+ return;
+ }
+ if (_isAlbumsSearchActiveNotifier.value) {
_isAlbumsSearchActiveNotifier.value = false;
- Bus.instance.fire(
- TabChangedEvent(0, TabChangedEventSource.backButton),
- );
+ }
+ }
+ if (_selectedTabIndex == 3) {
+ if (_isSearchTabSearchFieldFocusedNotifier.value ||
+ _isSearchTabSearchFieldNotEmptyNotifier.value) {
+ Bus.instance.fire(ClearAndUnfocusSearchBar());
return;
}
}
@@ -1049,10 +1073,23 @@ class _HomeWidgetState extends State<HomeWidget> {
selectedAlbums: _selectedAlbums,
isSearchActiveNotifier:
_isAlbumsSearchActiveNotifier,
+ isSearchFieldFocusedNotifier:
+ _isAlbumsSearchFieldFocusedNotifier,
+ isSearchFieldNotEmptyNotifier:
+ _isAlbumsSearchFieldNotEmptyNotifier,
),
),
_buildTabHeroMode(2, selectedTabIndex, _feedTab),
- _buildTabHeroMode(3, selectedTabIndex, _searchTab),
+ _buildTabHeroMode(
+ 3,
+ selectedTabIndex,
+ SearchTab(
+ isSearchFieldFocusedNotifier:
+ _isSearchTabSearchFieldFocusedNotifier,
+ isSearchFieldNotEmptyNotifier:
+ _isSearchTabSearchFieldNotEmptyNotifier,
+ ),
+ ),
],
);
},
diff --git a/mobile/apps/photos/lib/ui/viewer/search/search_widget.dart b/mobile/apps/photos/lib/ui/viewer/search/search_widget.dart
index c5967e5ef5..4c575ab6a7 100644
--- a/mobile/apps/photos/lib/ui/viewer/search/search_widget.dart
+++ b/mobile/apps/photos/lib/ui/viewer/search/search_widget.dart
@@ -17,7 +17,14 @@ import "package:photos/services/search_service.dart";
import "package:photos/ui/viewer/search/search_suffix_icon_widget.dart";
class SearchWidget extends StatefulWidget {
- const SearchWidget({super.key});
+ const SearchWidget({
+ super.key,
+ this.isSearchFieldFocusedNotifier,
+ this.isSearchFieldNotEmptyNotifier,
+ });
+
+ final ValueNotifier<bool>? isSearchFieldFocusedNotifier;
+ final ValueNotifier<bool>? isSearchFieldNotEmptyNotifier;
@override
State<SearchWidget> createState() => SearchWidgetState();
@@ -51,6 +58,7 @@ class SearchWidgetState extends State<SearchWidget> {
super.initState();
focusNode = FocusNode();
focusNode.addListener(() {
+ _syncSearchFieldFocusNotifier();
if (mounted) {
setState(() {});
}
@@ -93,15 +101,41 @@ class SearchWidgetState extends State<SearchWidget> {
//Populate the serach tab with the latest query when coming back
//to the serach tab.
textController.text = query;
+ _syncSearchFieldTextNotifier();
_clearAndUnfocusSearchBar = Bus.instance
.on<ClearAndUnfocusSearchBar>()
.listen((event) {
textController.clear();
+ _syncSearchFieldTextNotifier(
+ widget.isSearchFieldNotEmptyNotifier,
+ false,
+ );
focusNode.unfocus();
});
}
+ @override
+ void didUpdateWidget(covariant SearchWidget oldWidget) {
+ super.didUpdateWidget(oldWidget);
+ if (oldWidget.isSearchFieldFocusedNotifier !=
+ widget.isSearchFieldFocusedNotifier) {
+ _syncSearchFieldFocusNotifier(
+ oldWidget.isSearchFieldFocusedNotifier,
+ false,
+ );
+ _syncSearchFieldFocusNotifier();
+ }
+ if (oldWidget.isSearchFieldNotEmptyNotifier !=
+ widget.isSearchFieldNotEmptyNotifier) {
+ _syncSearchFieldTextNotifier(
+ oldWidget.isSearchFieldNotEmptyNotifier,
+ false,
+ );
+ _syncSearchFieldTextNotifier();
+ }
+ }
+
@override
void didChangeDependencies() {
super.didChangeDependencies();
@@ -117,6 +151,8 @@ class SearchWidgetState extends State<SearchWidget> {
@override
void dispose() {
+ _syncSearchFieldFocusNotifier(widget.isSearchFieldFocusedNotifier, false);
+ _syncSearchFieldTextNotifier(widget.isSearchFieldNotEmptyNotifier, false);
_debouncer.cancelDebounceTimer();
focusNode.dispose();
_tabChangedEvent?.cancel();
@@ -127,7 +163,32 @@ class SearchWidgetState extends State<SearchWidget> {
super.dispose();
}
+ void _syncSearchFieldFocusNotifier([
+ ValueNotifier<bool>? notifier,
+ bool? isFocused,
+ ]) {
+ final focusNotifier = notifier ?? widget.isSearchFieldFocusedNotifier;
+ final hasFocus = isFocused ?? focusNode.hasFocus;
+ if (focusNotifier == null || focusNotifier.value == hasFocus) {
+ return;
+ }
+ focusNotifier.value = hasFocus;
+ }
+
+ void _syncSearchFieldTextNotifier([
+ ValueNotifier<bool>? notifier,
+ bool? hasText,
+ ]) {
+ final textNotifier = notifier ?? widget.isSearchFieldNotEmptyNotifier;
+ final isNotEmpty = hasText ?? textController.text.trim().isNotEmpty;
+ if (textNotifier == null || textNotifier.value == isNotEmpty) {
+ return;
+ }
+ textNotifier.value = isNotEmpty;
+ }
+
Future<void> textControllerListener() async {
+ _syncSearchFieldTextNotifier();
isLoading.value = true;
_debouncer.run(() async {
if (mounted) {
diff --git a/mobile/apps/photos/lib/ui/viewer/search_tab/search_tab.dart b/mobile/apps/photos/lib/ui/viewer/search_tab/search_tab.dart
index c382a5b9e9..04060ae633 100644
--- a/mobile/apps/photos/lib/ui/viewer/search_tab/search_tab.dart
+++ b/mobile/apps/photos/lib/ui/viewer/search_tab/search_tab.dart
@@ -26,7 +26,14 @@ import "package:photos/ui/viewer/search_tab/people_section.dart";
import "package:photos/ui/wrapped/wrapped_discovery_section.dart";
class SearchTab extends StatefulWidget {
- const SearchTab({super.key});
+ const SearchTab({
+ super.key,
+ this.isSearchFieldFocusedNotifier,
+ this.isSearchFieldNotEmptyNotifier,
+ });
+
+ final ValueNotifier<bool>? isSearchFieldFocusedNotifier;
+ final ValueNotifier<bool>? isSearchFieldNotEmptyNotifier;
@override
State<SearchTab> createState() => _SearchTabState();
@@ -68,7 +75,14 @@ class _SearchTabState extends State<SearchTab> {
children: [
ColoredBox(
color: headerColor,
- child: const SafeArea(bottom: false, child: SearchWidget()),
+ child: SafeArea(
+ bottom: false,
+ child: SearchWidget(
+ isSearchFieldFocusedNotifier: widget.isSearchFieldFocusedNotifier,
+ isSearchFieldNotEmptyNotifier:
+ widget.isSearchFieldNotEmptyNotifier,
+ ),
+ ),
),
Expanded(
child: AllSectionsExamplesProvider(