π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Simplify search back handling state
ashilknc8c2d36d5cf5
diff --git a/mobile/apps/photos/lib/ui/tabs/albums_tab.dart b/mobile/apps/photos/lib/ui/tabs/albums_tab.dartindex e6a223eefc..d0b2990403 100644--- a/mobile/apps/photos/lib/ui/tabs/albums_tab.dart+++ b/mobile/apps/photos/lib/ui/tabs/albums_tab.dart@@ -43,14 +43,12 @@ class AlbumsTab extends StatefulWidget {super.key,this.selectedAlbums,this.isSearchActiveNotifier,- this.isSearchFieldFocusedNotifier,- this.isSearchFieldNotEmptyNotifier,+ this.shouldConsumeBackNotifier,});final SelectedAlbums? selectedAlbums;final ValueNotifier<bool>? isSearchActiveNotifier;- final ValueNotifier<bool>? isSearchFieldFocusedNotifier;- final ValueNotifier<bool>? isSearchFieldNotEmptyNotifier;+ final ValueNotifier<bool>? shouldConsumeBackNotifier;@overrideState<AlbumsTab> createState() => _AlbumsTabState();@@ -169,8 +167,7 @@ class _AlbumsTabState extends State<AlbumsTab>_searchFocusNode.addListener(_handleSearchFocusChanged);widget.isSearchActiveNotifier?.addListener(_handleSearchStateChanged);_syncSearchNotifier(_isSearchActive);- _syncSearchFieldFocusNotifier(_searchFocusNode.hasFocus);- _syncSearchFieldTextNotifier(_hasSearchQuery);+ _syncSearchBackNotifier();}@override@@ -183,21 +180,10 @@ class _AlbumsTabState extends State<AlbumsTab>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);+ if (oldWidget.shouldConsumeBackNotifier !=+ widget.shouldConsumeBackNotifier) {+ _syncSearchBackNotifier(false, oldWidget.shouldConsumeBackNotifier);+ _syncSearchBackNotifier();}}@@ -214,7 +200,7 @@ class _AlbumsTabState extends State<AlbumsTab>}void _handleSearchFocusChanged() {- _syncSearchFieldFocusNotifier(_searchFocusNode.hasFocus);+ _syncSearchBackNotifier();}void _syncSearchNotifier(bool isSearchActive) {@@ -223,22 +209,15 @@ class _AlbumsTabState extends State<AlbumsTab>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, [+ void _syncSearchBackNotifier([+ bool? shouldConsumeBack,ValueNotifier<bool>? notifier,]) {- final textNotifier = notifier ?? widget.isSearchFieldNotEmptyNotifier;- if (textNotifier == null || textNotifier.value == hasText) return;- textNotifier.value = hasText;+ final backNotifier = notifier ?? widget.shouldConsumeBackNotifier;+ final shouldConsume =+ shouldConsumeBack ?? (_searchFocusNode.hasFocus || _hasSearchQuery);+ if (backNotifier == null || backNotifier.value == shouldConsume) return;+ backNotifier.value = shouldConsume;}Future<void> _loadAll() async {@@ -323,6 +302,7 @@ class _AlbumsTabState extends State<AlbumsTab>_searchController.clear();_searchFocusNode.unfocus();if (!_isSearchActive && _searchQuery.isEmpty) {+ _syncSearchBackNotifier(false);if (syncNotifier) {_syncSearchNotifier(false);}@@ -333,7 +313,7 @@ class _AlbumsTabState extends State<AlbumsTab>_isSearchActive = false;_searchQuery = "";});- _syncSearchFieldTextNotifier(false);+ _syncSearchBackNotifier(false);if (syncNotifier) {_syncSearchNotifier(false);}@@ -737,8 +717,7 @@ class _AlbumsTabState extends State<AlbumsTab>_loggedOutEvent.cancel();_tabChangedEvent.cancel();widget.isSearchActiveNotifier?.removeListener(_handleSearchStateChanged);- _syncSearchFieldFocusNotifier(false);- _syncSearchFieldTextNotifier(false);+ _syncSearchBackNotifier(false);_searchFocusNode.removeListener(_handleSearchFocusChanged);_debouncer.cancelDebounceTimer();_searchController.dispose();@@ -846,9 +825,7 @@ class _AlbumsTabState extends State<AlbumsTab>setState(() {_searchQuery = value;});- _syncSearchFieldTextNotifier(- value.trim().isNotEmpty,- );+ _syncSearchBackNotifier();},),),diff --git a/mobile/apps/photos/lib/ui/tabs/home_widget.dart b/mobile/apps/photos/lib/ui/tabs/home_widget.dartindex 420efadc48..a89202624c 100644--- a/mobile/apps/photos/lib/ui/tabs/home_widget.dart+++ b/mobile/apps/photos/lib/ui/tabs/home_widget.dart@@ -127,13 +127,9 @@ class _HomeWidgetState extends State<HomeWidget> {final ValueNotifier<bool> _isAlbumsSearchActiveNotifier = ValueNotifier<bool>(false,);- final ValueNotifier<bool> _isAlbumsSearchFieldFocusedNotifier =+ final ValueNotifier<bool> _shouldAlbumsSearchConsumeBackNotifier =ValueNotifier<bool>(false);- final ValueNotifier<bool> _isAlbumsSearchFieldNotEmptyNotifier =- ValueNotifier<bool>(false);- final ValueNotifier<bool> _isSearchTabSearchFieldFocusedNotifier =- ValueNotifier<bool>(false);- final ValueNotifier<bool> _isSearchTabSearchFieldNotEmptyNotifier =+ final ValueNotifier<bool> _shouldSearchTabSearchConsumeBackNotifier =ValueNotifier<bool>(false);final ValueNotifier<bool> _swipeToSelectInProgressNotifier =ValueNotifier<bool>(false);@@ -554,10 +550,8 @@ class _HomeWidgetState extends State<HomeWidget> {_intentDataStreamSubscription?.cancel();isOnSearchTabNotifier.dispose();_isAlbumsSearchActiveNotifier.dispose();- _isAlbumsSearchFieldFocusedNotifier.dispose();- _isAlbumsSearchFieldNotEmptyNotifier.dispose();- _isSearchTabSearchFieldFocusedNotifier.dispose();- _isSearchTabSearchFieldNotEmptyNotifier.dispose();+ _shouldAlbumsSearchConsumeBackNotifier.dispose();+ _shouldSearchTabSearchConsumeBackNotifier.dispose();_pageController.dispose();_publicAlbumLinkSubscription?.cancel();_authDeepLinkSubscription?.cancel();@@ -854,18 +848,8 @@ class _HomeWidgetState extends State<HomeWidget> {_selectedAlbums.clearAll();return;}- if (_isAlbumsSearchFieldFocusedNotifier.value) {- if (_isAlbumsSearchActiveNotifier.value) {- _isAlbumsSearchActiveNotifier.value = false;- } else {- FocusManager.instance.primaryFocus?.unfocus();- }- return;- }- if (_isAlbumsSearchFieldNotEmptyNotifier.value) {- if (_isAlbumsSearchActiveNotifier.value) {- _isAlbumsSearchActiveNotifier.value = false;- }+ if (_shouldAlbumsSearchConsumeBackNotifier.value) {+ _isAlbumsSearchActiveNotifier.value = false;return;}if (_isAlbumsSearchActiveNotifier.value) {@@ -873,8 +857,7 @@ class _HomeWidgetState extends State<HomeWidget> {}}if (_selectedTabIndex == 3) {- if (_isSearchTabSearchFieldFocusedNotifier.value ||- _isSearchTabSearchFieldNotEmptyNotifier.value) {+ if (_shouldSearchTabSearchConsumeBackNotifier.value) {Bus.instance.fire(ClearAndUnfocusSearchBar());return;}@@ -1073,10 +1056,8 @@ class _HomeWidgetState extends State<HomeWidget> {selectedAlbums: _selectedAlbums,isSearchActiveNotifier:_isAlbumsSearchActiveNotifier,- isSearchFieldFocusedNotifier:- _isAlbumsSearchFieldFocusedNotifier,- isSearchFieldNotEmptyNotifier:- _isAlbumsSearchFieldNotEmptyNotifier,+ shouldConsumeBackNotifier:+ _shouldAlbumsSearchConsumeBackNotifier,),),_buildTabHeroMode(2, selectedTabIndex, _feedTab),@@ -1084,10 +1065,8 @@ class _HomeWidgetState extends State<HomeWidget> {3,selectedTabIndex,SearchTab(- isSearchFieldFocusedNotifier:- _isSearchTabSearchFieldFocusedNotifier,- isSearchFieldNotEmptyNotifier:- _isSearchTabSearchFieldNotEmptyNotifier,+ shouldConsumeBackNotifier:+ _shouldSearchTabSearchConsumeBackNotifier,),),],diff --git a/mobile/apps/photos/lib/ui/viewer/search/search_widget.dart b/mobile/apps/photos/lib/ui/viewer/search/search_widget.dartindex 4c575ab6a7..94490ad609 100644--- a/mobile/apps/photos/lib/ui/viewer/search/search_widget.dart+++ b/mobile/apps/photos/lib/ui/viewer/search/search_widget.dart@@ -17,14 +17,9 @@ 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,- this.isSearchFieldFocusedNotifier,- this.isSearchFieldNotEmptyNotifier,- });+ const SearchWidget({super.key, this.shouldConsumeBackNotifier});- final ValueNotifier<bool>? isSearchFieldFocusedNotifier;- final ValueNotifier<bool>? isSearchFieldNotEmptyNotifier;+ final ValueNotifier<bool>? shouldConsumeBackNotifier;@overrideState<SearchWidget> createState() => SearchWidgetState();@@ -58,7 +53,7 @@ class SearchWidgetState extends State<SearchWidget> {super.initState();focusNode = FocusNode();focusNode.addListener(() {- _syncSearchFieldFocusNotifier();+ _syncSearchBackNotifier();if (mounted) {setState(() {});}@@ -101,38 +96,24 @@ class SearchWidgetState extends State<SearchWidget> {//Populate the serach tab with the latest query when coming back//to the serach tab.textController.text = query;- _syncSearchFieldTextNotifier();+ _syncSearchBackNotifier();_clearAndUnfocusSearchBar = Bus.instance.on<ClearAndUnfocusSearchBar>().listen((event) {textController.clear();- _syncSearchFieldTextNotifier(- widget.isSearchFieldNotEmptyNotifier,- false,- );focusNode.unfocus();+ _syncSearchBackNotifier(false);});}@overridevoid 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();+ if (oldWidget.shouldConsumeBackNotifier !=+ widget.shouldConsumeBackNotifier) {+ _syncSearchBackNotifier(false, oldWidget.shouldConsumeBackNotifier);+ _syncSearchBackNotifier();}}@@ -151,8 +132,7 @@ class SearchWidgetState extends State<SearchWidget> {@overridevoid dispose() {- _syncSearchFieldFocusNotifier(widget.isSearchFieldFocusedNotifier, false);- _syncSearchFieldTextNotifier(widget.isSearchFieldNotEmptyNotifier, false);+ _syncSearchBackNotifier(false);_debouncer.cancelDebounceTimer();focusNode.dispose();_tabChangedEvent?.cancel();@@ -163,32 +143,22 @@ 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([+ void _syncSearchBackNotifier([+ bool? shouldConsumeBack,ValueNotifier<bool>? notifier,- bool? hasText,]) {- final textNotifier = notifier ?? widget.isSearchFieldNotEmptyNotifier;- final isNotEmpty = hasText ?? textController.text.trim().isNotEmpty;- if (textNotifier == null || textNotifier.value == isNotEmpty) {+ final backNotifier = notifier ?? widget.shouldConsumeBackNotifier;+ final shouldConsume =+ shouldConsumeBack ??+ (focusNode.hasFocus || textController.text.trim().isNotEmpty);+ if (backNotifier == null || backNotifier.value == shouldConsume) {return;}- textNotifier.value = isNotEmpty;+ backNotifier.value = shouldConsume;}Future<void> textControllerListener() async {- _syncSearchFieldTextNotifier();+ _syncSearchBackNotifier();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.dartindex 04060ae633..0e8ac10b0a 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,14 +26,9 @@ 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,- this.isSearchFieldFocusedNotifier,- this.isSearchFieldNotEmptyNotifier,- });+ const SearchTab({super.key, this.shouldConsumeBackNotifier});- final ValueNotifier<bool>? isSearchFieldFocusedNotifier;- final ValueNotifier<bool>? isSearchFieldNotEmptyNotifier;+ final ValueNotifier<bool>? shouldConsumeBackNotifier;@overrideState<SearchTab> createState() => _SearchTabState();@@ -78,9 +73,7 @@ class _SearchTabState extends State<SearchTab> {child: SafeArea(bottom: false,child: SearchWidget(- isSearchFieldFocusedNotifier: widget.isSearchFieldFocusedNotifier,- isSearchFieldNotEmptyNotifier:- widget.isSearchFieldNotEmptyNotifier,+ shouldConsumeBackNotifier: widget.shouldConsumeBackNotifier,),),),