From 4f18fff36b11a6ebc9eb2233ef70ff15ee776631 Mon Sep 17 00:00:00 2001 From: ashilkn Date: Mon, 17 Jun 2024 08:16:31 +0530 Subject: [PATCH] [mob][photos] Assert or log depending on the context if inherited widget holding selection state is used in a wrong way --- .../ui/viewer/actions/file_selection_overlay_bar.dart | 6 +++++- .../lib/ui/viewer/gallery/state/selection_state.dart | 11 ++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/mobile/lib/ui/viewer/actions/file_selection_overlay_bar.dart b/mobile/lib/ui/viewer/actions/file_selection_overlay_bar.dart index 1570a5e272..0cfb39c51f 100644 --- a/mobile/lib/ui/viewer/actions/file_selection_overlay_bar.dart +++ b/mobile/lib/ui/viewer/actions/file_selection_overlay_bar.dart @@ -117,6 +117,10 @@ class _SelectAllButtonState extends State { @override Widget build(BuildContext context) { final selectionState = SelectionState.of(context); + assert( + selectionState != null, + "SelectionState not found in context, SelectionState should be an ancestor of FileSelectionOverlayBar", + ); final colorScheme = getEnteColorScheme(context); return GestureDetector( onTap: () { @@ -155,7 +159,7 @@ class _SelectAllButtonState extends State { ), const SizedBox(width: 4), ListenableBuilder( - listenable: selectionState.selectedFiles, + listenable: selectionState!.selectedFiles, builder: (context, _) { if (selectionState.selectedFiles.files.length == selectionState.allGalleryFiles?.length) { diff --git a/mobile/lib/ui/viewer/gallery/state/selection_state.dart b/mobile/lib/ui/viewer/gallery/state/selection_state.dart index a67ffd449f..762b14b94a 100644 --- a/mobile/lib/ui/viewer/gallery/state/selection_state.dart +++ b/mobile/lib/ui/viewer/gallery/state/selection_state.dart @@ -1,4 +1,5 @@ import "package:flutter/material.dart"; +import "package:logging/logging.dart"; import "package:photos/models/file/file.dart"; import "package:photos/models/selected_files.dart"; @@ -22,10 +23,14 @@ class SelectionState extends InheritedWidget { return context.dependOnInheritedWidgetOfExactType(); } - static SelectionState of(BuildContext context) { + static SelectionState? of(BuildContext context) { final SelectionState? result = maybeOf(context); - assert(result != null, 'No SelectionState found in context'); - return result!; + if (result == null) { + Logger("SelectionState").warning( + "No SelectionState found in context. Ignore this if file selection is disabled in the gallery used.", + ); + } + return result; } @override