Skip to content

Commit

Permalink
Exernalize multiselect state
Browse files Browse the repository at this point in the history
  • Loading branch information
matthinc committed Oct 8, 2022
1 parent 6b84534 commit 3c807ae
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
40 changes: 22 additions & 18 deletions mobile/lib/modules/home/ui/asset_grid/immich_asset_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class ImmichAssetGridState extends State<ImmichAssetGrid> {
ItemPositionsListener.create();

bool _scrolling = false;
bool _multiselect = false;
Set<String> _selectedAssets = HashSet();

List<AssetResponseDto> get _assets {
Expand All @@ -46,18 +45,16 @@ class ImmichAssetGridState extends State<ImmichAssetGrid> {
.toSet();
}

void _callSelectionListener() {
widget.listener?.call(_multiselect, _getSelectedAssets());
void _callSelectionListener(bool selectionActive) {
widget.listener?.call(selectionActive, _getSelectedAssets());
}

void _selectAssets(List<AssetResponseDto> assets) {
setState(() {
for (var e in assets) {
_selectedAssets.add(e.id);
}

_multiselect = true;
_callSelectionListener();
_callSelectionListener(true);
});
}

Expand All @@ -66,26 +63,20 @@ class ImmichAssetGridState extends State<ImmichAssetGrid> {
for (var e in assets) {
_selectedAssets.remove(e.id);
}

if (_selectedAssets.isEmpty) {
_multiselect = false;
}

_callSelectionListener();
_callSelectionListener(_selectedAssets.isNotEmpty);
});
}

void _deselectAll() {
setState(() {
_multiselect = false;
_selectedAssets.clear();
});

_callSelectionListener();
_callSelectionListener(false);
}

bool _allAssetsSelected(List<AssetResponseDto> assets) {
return _multiselect &&
return widget.selectionActive &&
assets.firstWhereOrNull((e) => !_selectedAssets.contains(e.id)) == null;
}

Expand All @@ -104,7 +95,7 @@ class ImmichAssetGridState extends State<ImmichAssetGrid> {
return ThumbnailImage(
asset: asset,
assetList: _assets,
multiselectEnabled: _multiselect,
multiselectEnabled: widget.selectionActive,
isSelected: _selectedAssets.contains(asset.id),
onSelect: () => _selectAssets([asset]),
onDeselect: () => _deselectAssets([asset]),
Expand Down Expand Up @@ -137,7 +128,7 @@ class ImmichAssetGridState extends State<ImmichAssetGrid> {
BuildContext context, String title, List<AssetResponseDto> assets) {
return DailyTitleText(
isoDate: title,
multiselectEnabled: _multiselect,
multiselectEnabled: widget.selectionActive,
onSelect: () => _selectAssets(assets),
onDeselect: () => _deselectAssets(assets),
selected: _allAssetsSelected(assets),
Expand Down Expand Up @@ -227,12 +218,23 @@ class ImmichAssetGridState extends State<ImmichAssetGrid> {
);
}


@override
void didUpdateWidget(ImmichAssetGrid oldWidget) {
super.didUpdateWidget(oldWidget);
if (!widget.selectionActive) {
setState(() {
_selectedAssets.clear();
});
}
}

@override
Widget build(BuildContext context) {
return Stack(
children: [
_buildAssetGrid(),
if (_multiselect) _buildMultiSelectIndicator(),
if (widget.selectionActive) _buildMultiSelectIndicator(),
],
);
}
Expand All @@ -244,6 +246,7 @@ class ImmichAssetGrid extends StatefulWidget {
final double margin;
final bool showStorageIndicator;
final ImmichAssetGridSelectionListener? listener;
final bool selectionActive;

ImmichAssetGrid({
super.key,
Expand All @@ -252,6 +255,7 @@ class ImmichAssetGrid extends StatefulWidget {
required this.showStorageIndicator,
this.listener,
this.margin = 5.0,
this.selectionActive = false
});

@override
Expand Down
3 changes: 3 additions & 0 deletions mobile/lib/modules/home/views/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ class HomePage extends HookConsumerWidget {

void onShareAssets() {
ref.watch(shareServiceProvider).shareAssets(selection.value.toList());
multiselectEnabled.value = false;
}

void onDelete() {
ref.watch(assetProvider.notifier).deleteAssets(selection.value);
multiselectEnabled.value = false;
}

return SafeArea(
Expand Down Expand Up @@ -82,6 +84,7 @@ class HomePage extends HookConsumerWidget {
showStorageIndicator: appSettingService
.getSetting(AppSettingsEnum.storageIndicator),
listener: selectionListener,
selectionActive: multiselectEnabled.value,
),
),
if (multiselectEnabled.value) ...[
Expand Down

0 comments on commit 3c807ae

Please sign in to comment.