Skip to content

Commit

Permalink
PAINTROID-454: Flutter: Add Layers - reorder positions, select, delet…
Browse files Browse the repository at this point in the history
…e, add layer
  • Loading branch information
Lenkomotive committed Oct 10, 2024
1 parent 80a1af5 commit bc56d56
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/core/providers/state/layer_menu_state_data.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:paintroid/core/providers/state/layer_state_data.dart';

part 'layer_menu_state_data.freezed.dart';

Expand All @@ -7,5 +8,6 @@ part 'layer_menu_state_data.freezed.dart';
class LayerMenuStateData with _$LayerMenuStateData {
const factory LayerMenuStateData({
required bool isVisible,
required List<LayerStateData> layer,
}) = _LayerMenuStateData;
}
41 changes: 33 additions & 8 deletions lib/core/providers/state/layer_menu_state_data.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final _privateConstructorUsedError = UnsupportedError(
/// @nodoc
mixin _$LayerMenuStateData {
bool get isVisible => throw _privateConstructorUsedError;
List<LayerStateData> get layer => throw _privateConstructorUsedError;

@JsonKey(ignore: true)
$LayerMenuStateDataCopyWith<LayerMenuStateData> get copyWith =>
Expand All @@ -29,7 +30,7 @@ abstract class $LayerMenuStateDataCopyWith<$Res> {
LayerMenuStateData value, $Res Function(LayerMenuStateData) then) =
_$LayerMenuStateDataCopyWithImpl<$Res, LayerMenuStateData>;
@useResult
$Res call({bool isVisible});
$Res call({bool isVisible, List<LayerStateData> layer});
}

/// @nodoc
Expand All @@ -46,12 +47,17 @@ class _$LayerMenuStateDataCopyWithImpl<$Res, $Val extends LayerMenuStateData>
@override
$Res call({
Object? isVisible = null,
Object? layer = null,
}) {
return _then(_value.copyWith(
isVisible: null == isVisible
? _value.isVisible
: isVisible // ignore: cast_nullable_to_non_nullable
as bool,
layer: null == layer
? _value.layer
: layer // ignore: cast_nullable_to_non_nullable
as List<LayerStateData>,
) as $Val);
}
}
Expand All @@ -64,7 +70,7 @@ abstract class _$$LayerMenuStateDataImplCopyWith<$Res>
__$$LayerMenuStateDataImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({bool isVisible});
$Res call({bool isVisible, List<LayerStateData> layer});
}

/// @nodoc
Expand All @@ -79,27 +85,41 @@ class __$$LayerMenuStateDataImplCopyWithImpl<$Res>
@override
$Res call({
Object? isVisible = null,
Object? layer = null,
}) {
return _then(_$LayerMenuStateDataImpl(
isVisible: null == isVisible
? _value.isVisible
: isVisible // ignore: cast_nullable_to_non_nullable
as bool,
layer: null == layer
? _value._layer
: layer // ignore: cast_nullable_to_non_nullable
as List<LayerStateData>,
));
}
}

/// @nodoc
class _$LayerMenuStateDataImpl implements _LayerMenuStateData {
const _$LayerMenuStateDataImpl({required this.isVisible});
const _$LayerMenuStateDataImpl(
{required this.isVisible, required final List<LayerStateData> layer})
: _layer = layer;

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

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

@override
Expand All @@ -108,11 +128,13 @@ class _$LayerMenuStateDataImpl implements _LayerMenuStateData {
(other.runtimeType == runtimeType &&
other is _$LayerMenuStateDataImpl &&
(identical(other.isVisible, isVisible) ||
other.isVisible == isVisible));
other.isVisible == isVisible) &&
const DeepCollectionEquality().equals(other._layer, _layer));
}

@override
int get hashCode => Object.hash(runtimeType, isVisible);
int get hashCode => Object.hash(
runtimeType, isVisible, const DeepCollectionEquality().hash(_layer));

@JsonKey(ignore: true)
@override
Expand All @@ -123,12 +145,15 @@ class _$LayerMenuStateDataImpl implements _LayerMenuStateData {
}

abstract class _LayerMenuStateData implements LayerMenuStateData {
const factory _LayerMenuStateData({required final bool isVisible}) =
_$LayerMenuStateDataImpl;
const factory _LayerMenuStateData(
{required final bool isVisible,
required final List<LayerStateData> layer}) = _$LayerMenuStateDataImpl;

@override
bool get isVisible;
@override
List<LayerStateData> get layer;
@override
@JsonKey(ignore: true)
_$$LayerMenuStateDataImplCopyWith<_$LayerMenuStateDataImpl> get copyWith =>
throw _privateConstructorUsedError;
Expand Down
49 changes: 45 additions & 4 deletions lib/core/providers/state/layer_menu_state_provider.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:paintroid/core/providers/state/layer_menu_state_data.dart';
import 'package:paintroid/core/providers/state/layer_state_data.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'layer_menu_state_provider.g.dart';
Expand All @@ -9,14 +10,54 @@ class LayerMenuStateProvider extends _$LayerMenuStateProvider {
LayerMenuStateData build() {
return const LayerMenuStateData(
isVisible: false,
layer: [
LayerStateData(id: 0, isSelected: false),
LayerStateData(id: 1, isSelected: false),
LayerStateData(id: 2, isSelected: false),
LayerStateData(id: 3, isSelected: false),
LayerStateData(id: 4, isSelected: false),
LayerStateData(id: 5, isSelected: false),
LayerStateData(id: 6, isSelected: false),
],
);
}

void toggleVisibility() {
state = state.copyWith(isVisible: !state.isVisible);
void toggleVisibility() =>
state = state.copyWith(isVisible: !state.isVisible);

void hide() => state = state.copyWith(isVisible: false);

void reorder(int oldIndex, int newIndex) {
List<LayerStateData> layerList = List.from(state.layer);
if (oldIndex < newIndex) {
newIndex -= 1;
}
final movedLayer = layerList.removeAt(oldIndex);
layerList.insert(newIndex, movedLayer);
state = state.copyWith(layer: layerList);
}

void toggleSelection(int layerId) {
final updatedLayerList = state.layer.map((layer) {
if (layer.id == layerId) {
return layer.copyWith(isSelected: !layer.isSelected);
}
return layer;
}).toList();
state = state.copyWith(layer: updatedLayerList);
}

void addLayer() {
final newLayer = LayerStateData(
id: state.layer.length + 1,
isSelected: false,
);
state = state.copyWith(layer: [...state.layer, newLayer]);
}

void hide() {
state = state.copyWith(isVisible: false);
void deleteLayer() {
final updatedLayerList =
state.layer.where((layer) => !layer.isSelected).toList();
state = state.copyWith(layer: updatedLayerList);
}
}
2 changes: 1 addition & 1 deletion lib/core/providers/state/layer_menu_state_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions lib/core/providers/state/layer_state_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:freezed_annotation/freezed_annotation.dart';

part 'layer_state_data.freezed.dart';

@immutable
@freezed
class LayerStateData with _$LayerStateData {
const factory LayerStateData({
required int id,
required bool isSelected,
}) = _LayerStateData;
}
152 changes: 152 additions & 0 deletions lib/core/providers/state/layer_state_data.freezed.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark

part of 'layer_state_data.dart';

// **************************************************************************
// FreezedGenerator
// **************************************************************************

T _$identity<T>(T value) => value;

final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');

/// @nodoc
mixin _$LayerStateData {
int get id => throw _privateConstructorUsedError;
bool get isSelected => throw _privateConstructorUsedError;

@JsonKey(ignore: true)
$LayerStateDataCopyWith<LayerStateData> get copyWith =>
throw _privateConstructorUsedError;
}

/// @nodoc
abstract class $LayerStateDataCopyWith<$Res> {
factory $LayerStateDataCopyWith(
LayerStateData value, $Res Function(LayerStateData) then) =
_$LayerStateDataCopyWithImpl<$Res, LayerStateData>;
@useResult
$Res call({int id, bool isSelected});
}

/// @nodoc
class _$LayerStateDataCopyWithImpl<$Res, $Val extends LayerStateData>
implements $LayerStateDataCopyWith<$Res> {
_$LayerStateDataCopyWithImpl(this._value, this._then);

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

@pragma('vm:prefer-inline')
@override
$Res call({
Object? id = null,
Object? isSelected = null,
}) {
return _then(_value.copyWith(
id: null == id
? _value.id
: id // ignore: cast_nullable_to_non_nullable
as int,
isSelected: null == isSelected
? _value.isSelected
: isSelected // ignore: cast_nullable_to_non_nullable
as bool,
) as $Val);
}
}

/// @nodoc
abstract class _$$LayerStateDataImplCopyWith<$Res>
implements $LayerStateDataCopyWith<$Res> {
factory _$$LayerStateDataImplCopyWith(_$LayerStateDataImpl value,
$Res Function(_$LayerStateDataImpl) then) =
__$$LayerStateDataImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({int id, bool isSelected});
}

/// @nodoc
class __$$LayerStateDataImplCopyWithImpl<$Res>
extends _$LayerStateDataCopyWithImpl<$Res, _$LayerStateDataImpl>
implements _$$LayerStateDataImplCopyWith<$Res> {
__$$LayerStateDataImplCopyWithImpl(
_$LayerStateDataImpl _value, $Res Function(_$LayerStateDataImpl) _then)
: super(_value, _then);

@pragma('vm:prefer-inline')
@override
$Res call({
Object? id = null,
Object? isSelected = null,
}) {
return _then(_$LayerStateDataImpl(
id: null == id
? _value.id
: id // ignore: cast_nullable_to_non_nullable
as int,
isSelected: null == isSelected
? _value.isSelected
: isSelected // ignore: cast_nullable_to_non_nullable
as bool,
));
}
}

/// @nodoc
class _$LayerStateDataImpl implements _LayerStateData {
const _$LayerStateDataImpl({required this.id, required this.isSelected});

@override
final int id;
@override
final bool isSelected;

@override
String toString() {
return 'LayerStateData(id: $id, isSelected: $isSelected)';
}

@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$LayerStateDataImpl &&
(identical(other.id, id) || other.id == id) &&
(identical(other.isSelected, isSelected) ||
other.isSelected == isSelected));
}

@override
int get hashCode => Object.hash(runtimeType, id, isSelected);

@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
_$$LayerStateDataImplCopyWith<_$LayerStateDataImpl> get copyWith =>
__$$LayerStateDataImplCopyWithImpl<_$LayerStateDataImpl>(
this, _$identity);
}

abstract class _LayerStateData implements LayerStateData {
const factory _LayerStateData(
{required final int id,
required final bool isSelected}) = _$LayerStateDataImpl;

@override
int get id;
@override
bool get isSelected;
@override
@JsonKey(ignore: true)
_$$LayerStateDataImplCopyWith<_$LayerStateDataImpl> get copyWith =>
throw _privateConstructorUsedError;
}
Loading

0 comments on commit bc56d56

Please sign in to comment.