From 8190c0358a98bbe613dda0a1ca14d8f089d403cd Mon Sep 17 00:00:00 2001 From: Lenkomotive Date: Sat, 19 Oct 2024 17:20:56 +0200 Subject: [PATCH] PAINTROID-454: Flutter: Add Layers - select only one --- .../state/layer_menu_state_data.freezed.dart | 17 +++++--- .../state/layer_menu_state_provider.dart | 43 +++++++++---------- .../components/layer_menu/layer_menu.dart | 3 +- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/lib/core/providers/state/layer_menu_state_data.freezed.dart b/lib/core/providers/state/layer_menu_state_data.freezed.dart index 02214554..6f8b938d 100644 --- a/lib/core/providers/state/layer_menu_state_data.freezed.dart +++ b/lib/core/providers/state/layer_menu_state_data.freezed.dart @@ -17,7 +17,8 @@ final _privateConstructorUsedError = UnsupportedError( /// @nodoc mixin _$LayerMenuStateData { bool get isVisible => throw _privateConstructorUsedError; - List get layer => throw _privateConstructorUsedError; + + List get layers => throw _privateConstructorUsedError; @JsonKey(ignore: true) $LayerMenuStateDataCopyWith get copyWith => @@ -29,6 +30,7 @@ abstract class $LayerMenuStateDataCopyWith<$Res> { factory $LayerMenuStateDataCopyWith( LayerMenuStateData value, $Res Function(LayerMenuStateData) then) = _$LayerMenuStateDataCopyWithImpl<$Res, LayerMenuStateData>; + @useResult $Res call({bool isVisible, List layer}); } @@ -40,6 +42,7 @@ class _$LayerMenuStateDataCopyWithImpl<$Res, $Val extends LayerMenuStateData> // ignore: unused_field final $Val _value; + // ignore: unused_field final $Res Function($Val) _then; @@ -55,7 +58,7 @@ class _$LayerMenuStateDataCopyWithImpl<$Res, $Val extends LayerMenuStateData> : isVisible // ignore: cast_nullable_to_non_nullable as bool, layer: null == layer - ? _value.layer + ? _value.layers : layer // ignore: cast_nullable_to_non_nullable as List, ) as $Val); @@ -68,6 +71,7 @@ abstract class _$$LayerMenuStateDataImplCopyWith<$Res> factory _$$LayerMenuStateDataImplCopyWith(_$LayerMenuStateDataImpl value, $Res Function(_$LayerMenuStateDataImpl) then) = __$$LayerMenuStateDataImplCopyWithImpl<$Res>; + @override @useResult $Res call({bool isVisible, List layer}); @@ -110,8 +114,9 @@ class _$LayerMenuStateDataImpl implements _LayerMenuStateData { @override final bool isVisible; final List _layer; + @override - List get layer { + List get layers { if (_layer is EqualUnmodifiableListView) return _layer; // ignore: implicit_dynamic_type return EqualUnmodifiableListView(_layer); @@ -119,7 +124,7 @@ class _$LayerMenuStateDataImpl implements _LayerMenuStateData { @override String toString() { - return 'LayerMenuStateData(isVisible: $isVisible, layer: $layer)'; + return 'LayerMenuStateData(isVisible: $isVisible, layer: $layers)'; } @override @@ -151,8 +156,10 @@ abstract class _LayerMenuStateData implements LayerMenuStateData { @override bool get isVisible; + @override - List get layer; + List get layers; + @override @JsonKey(ignore: true) _$$LayerMenuStateDataImplCopyWith<_$LayerMenuStateDataImpl> get copyWith => diff --git a/lib/core/providers/state/layer_menu_state_provider.dart b/lib/core/providers/state/layer_menu_state_provider.dart index 36fd32cd..bf4a5591 100644 --- a/lib/core/providers/state/layer_menu_state_provider.dart +++ b/lib/core/providers/state/layer_menu_state_provider.dart @@ -17,7 +17,7 @@ class LayerMenuStateProvider extends _$LayerMenuStateProvider { layer: [ LayerStateData( key: ValueKey(uuid.v4()), - isSelected: false, + isSelected: true, isVisible: true, opacity: 1.0, ), @@ -31,7 +31,7 @@ class LayerMenuStateProvider extends _$LayerMenuStateProvider { void hide() => state = state.copyWith(isVisible: false); void reorder(int oldIndex, int newIndex) { - List layerList = List.from(state.layer); + List layerList = List.from(state.layers); if (oldIndex < newIndex) { newIndex -= 1; } @@ -41,31 +41,18 @@ class LayerMenuStateProvider extends _$LayerMenuStateProvider { } void toggleSelection(Key? layerKey) { - final layers = state.layer; - - final selectedCount = layers.where((layer) => layer.isSelected).length; - final unselectedCount = layers.length - selectedCount; - - final updatedLayerList = layers.map((layer) { + final updatedLayerList = state.layers.map((layer) { if (layer.key == layerKey) { - if (!layer.isSelected) { - if (unselectedCount <= 1) { - return layer; - } else { - return layer.copyWith(isSelected: true); - } - } else { - return layer.copyWith(isSelected: false); - } + return layer.copyWith(isSelected: true); } - return layer; + return layer.copyWith(isSelected: false); }).toList(); state = state.copyWith(layer: updatedLayerList); } void toggleLayerVisibility(Key? layerKey) { - final updatedLayerList = state.layer.map((layer) { + final updatedLayerList = state.layers.map((layer) { if (layer.key == layerKey) { return layer.copyWith(isVisible: !layer.isVisible); } @@ -75,7 +62,7 @@ class LayerMenuStateProvider extends _$LayerMenuStateProvider { } void updateLayerOpacity(Key? layerKey, double opacity) { - final updatedLayerList = state.layer.map((layer) { + final updatedLayerList = state.layers.map((layer) { if (layer.key == layerKey) { return layer.copyWith(opacity: opacity); } @@ -85,18 +72,28 @@ class LayerMenuStateProvider extends _$LayerMenuStateProvider { } void addLayer() { + // deselect all layers + final updatedLayerList = state.layers.map((layer) { + return layer.copyWith(isSelected: false); + }).toList(); final newLayer = LayerStateData( key: ValueKey(uuid.v4()), - isSelected: false, + isSelected: true, isVisible: true, opacity: 1.0, ); - state = state.copyWith(layer: [...state.layer, newLayer]); + updatedLayerList.add(newLayer); + state = state.copyWith(layer: updatedLayerList); } void deleteLayer() { + if (state.layers.length == 1) return; final updatedLayerList = - state.layer.where((layer) => !layer.isSelected).toList(); + state.layers.where((layer) => !layer.isSelected).toList(); + final lastIndex = updatedLayerList.length - 1; + updatedLayerList[lastIndex] = + updatedLayerList[lastIndex].copyWith(isSelected: true); + state = state.copyWith(layer: updatedLayerList); } } diff --git a/lib/ui/pages/workspace_page/components/layer_menu/layer_menu.dart b/lib/ui/pages/workspace_page/components/layer_menu/layer_menu.dart index 1cb57666..554f981b 100644 --- a/lib/ui/pages/workspace_page/components/layer_menu/layer_menu.dart +++ b/lib/ui/pages/workspace_page/components/layer_menu/layer_menu.dart @@ -13,7 +13,7 @@ class LayerMenu extends ConsumerWidget { layerMenuStateProvider.select((state) => state.isVisible), ); final layers = ref.watch( - layerMenuStateProvider.select((state) => state.layer), + layerMenuStateProvider.select((state) => state.layers), ); return Align( @@ -51,6 +51,7 @@ class LayerMenu extends ConsumerWidget { ), Flexible( child: ReorderableListView( + reverse: true, padding: const EdgeInsets.fromLTRB(8, 0, 8, 0), onReorder: (int oldIndex, int newIndex) { ref.read(layerMenuStateProvider.notifier).reorder(