Skip to content

Commit

Permalink
PAINTROID-454: Flutter: Add Layers - select only one
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenkomotive committed Oct 19, 2024
1 parent 6eb0be0 commit 8190c03
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
17 changes: 12 additions & 5 deletions lib/core/providers/state/layer_menu_state_data.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ final _privateConstructorUsedError = UnsupportedError(
/// @nodoc
mixin _$LayerMenuStateData {
bool get isVisible => throw _privateConstructorUsedError;
List<LayerStateData> get layer => throw _privateConstructorUsedError;

List<LayerStateData> get layers => throw _privateConstructorUsedError;

@JsonKey(ignore: true)
$LayerMenuStateDataCopyWith<LayerMenuStateData> get copyWith =>
Expand All @@ -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<LayerStateData> layer});
}
Expand All @@ -40,6 +42,7 @@ class _$LayerMenuStateDataCopyWithImpl<$Res, $Val extends LayerMenuStateData>

// ignore: unused_field
final $Val _value;

// ignore: unused_field
final $Res Function($Val) _then;

Expand All @@ -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<LayerStateData>,
) as $Val);
Expand All @@ -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<LayerStateData> layer});
Expand Down Expand Up @@ -110,16 +114,17 @@ class _$LayerMenuStateDataImpl implements _LayerMenuStateData {
@override
final bool isVisible;
final List<LayerStateData> _layer;

@override
List<LayerStateData> get layer {
List<LayerStateData> get layers {
if (_layer is EqualUnmodifiableListView) return _layer;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_layer);
}

@override
String toString() {
return 'LayerMenuStateData(isVisible: $isVisible, layer: $layer)';
return 'LayerMenuStateData(isVisible: $isVisible, layer: $layers)';
}

@override
Expand Down Expand Up @@ -151,8 +156,10 @@ abstract class _LayerMenuStateData implements LayerMenuStateData {

@override
bool get isVisible;

@override
List<LayerStateData> get layer;
List<LayerStateData> get layers;

@override
@JsonKey(ignore: true)
_$$LayerMenuStateDataImplCopyWith<_$LayerMenuStateDataImpl> get copyWith =>
Expand Down
43 changes: 20 additions & 23 deletions lib/core/providers/state/layer_menu_state_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class LayerMenuStateProvider extends _$LayerMenuStateProvider {
layer: [
LayerStateData(
key: ValueKey(uuid.v4()),
isSelected: false,
isSelected: true,
isVisible: true,
opacity: 1.0,
),
Expand All @@ -31,7 +31,7 @@ class LayerMenuStateProvider extends _$LayerMenuStateProvider {
void hide() => state = state.copyWith(isVisible: false);

void reorder(int oldIndex, int newIndex) {
List<LayerStateData> layerList = List.from(state.layer);
List<LayerStateData> layerList = List.from(state.layers);
if (oldIndex < newIndex) {
newIndex -= 1;
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 8190c03

Please sign in to comment.