Skip to content

Commit

Permalink
Move save state in current index cubit to allow undo/redo
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Jul 17, 2022
1 parent 4af73f9 commit b698e89
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 37 deletions.
26 changes: 12 additions & 14 deletions app/lib/bloc/document_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -440,18 +440,18 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
.createTemplate(current.document);

if (event.deleteDocument) {
emit(current.copyWith(
current.currentIndexCubit.setSaveState(
location:
AssetLocation(remote: remote?.identifier ?? '', path: '')));
AssetLocation(remote: remote?.identifier ?? '', path: ''));
}
});
on<DocumentPathChanged>((event, emit) {
final current = state;
if (current is! DocumentLoadSuccess) return;
if (!(current.embedding?.editable ?? true)) return;
emit(current.copyWith(
current.currentIndexCubit.setSaveState(
location: AssetLocation(
remote: current.location.remote, path: event.location)));
remote: current.location.remote, path: event.location));
});
on<AreaCreated>((event, emit) async {
final current = state;
Expand Down Expand Up @@ -507,8 +507,8 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
final current = state;
if (current is! DocumentLoadSuccess) return;
if (!(current.embedding?.editable ?? true)) return;
emit(current.copyWith(saved: true, location: event.location));
if (current.location.path == '') clearHistory();
current.currentIndexCubit
.setSaveState(saved: true, location: event.location);
});
}

Expand All @@ -524,29 +524,27 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
elements = List<Renderer<PadElement>>.from(elements)
..addAll(unbakedElements);
}
var nextState = current.copyWith(
saved: false,
document: current.document.copyWith(updatedAt: DateTime.now()),
);
emit(current);

if (unbakedElements == null) {
current.currentIndexCubit.unbake(
unbakedElements: List<Renderer<PadElement>>.from(elements)
..addAll(cameraViewport.bakedElements));
} else {
current.currentIndexCubit.withUnbaked(elements);
}

if (current.embedding != null) {
emit(nextState);
current.currentIndexCubit.setSaveState(saved: true);
return;
}
emit(nextState);
AssetLocation? path = current.location;
if (current.hasAutosave()) {
path = await nextState.save();
path = await current.save();
var currentState = state;
if (currentState is! DocumentLoadSuccess) return;
if (currentState.location.path == '' && state is DocumentLoadSuccess) {
emit(currentState.copyWith(location: path, saved: true));
current.currentIndexCubit.setSaveState(saved: true, location: path);
clearHistory();
}
}
Expand Down
22 changes: 12 additions & 10 deletions app/lib/bloc/document_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@ class DocumentLoadInProgress extends DocumentState {}

class DocumentLoadSuccess extends DocumentState {
final AppDocument document;
final AssetLocation location;
final StorageType storageType;
final String currentLayer;
final int currentAreaIndex;
final List<String> invisibleLayers;
final SettingsCubit settingsCubit;
final CurrentIndexCubit currentIndexCubit;
final Embedding? embedding;
final bool saved;

const DocumentLoadSuccess(this.document,
{required this.location,
DocumentLoadSuccess(this.document,
{AssetLocation? location,
this.storageType = StorageType.local,
this.saved = true,
bool saved = true,
required this.settingsCubit,
required this.currentIndexCubit,
this.currentAreaIndex = -1,
this.currentLayer = '',
this.embedding,
this.invisibleLayers = const []});
this.invisibleLayers = const []}) {
if (location != null) {
currentIndexCubit.setSaveState(location: location, saved: saved);
}
}

@override
List<Object?> get props => [
Expand All @@ -61,6 +63,9 @@ class DocumentLoadSuccess extends DocumentState {

List<Renderer<PadElement>> get renderers => currentIndexCubit.renderers;

AssetLocation get location => currentIndexCubit.state.location;
bool get saved => currentIndexCubit.state.saved;

Future<void> load() async {
final background = Renderer.fromInstance(document.background);
await background.setup(document);
Expand All @@ -74,22 +79,19 @@ class DocumentLoadSuccess extends DocumentState {
DocumentLoadSuccess copyWith({
AppDocument? document,
bool? editMode,
AssetLocation? location,
String? currentLayer,
int? currentAreaIndex,
bool? saved,
List<String>? invisibleLayers,
}) =>
DocumentLoadSuccess(
document ?? this.document,
location: location ?? this.location,
invisibleLayers: invisibleLayers ?? this.invisibleLayers,
currentLayer: currentLayer ?? this.currentLayer,
currentAreaIndex: currentAreaIndex ?? this.currentAreaIndex,
saved: saved ?? this.saved,
settingsCubit: settingsCubit,
embedding: embedding,
currentIndexCubit: currentIndexCubit,
location: location,
);

bool isLayerVisible(String layer) => !invisibleLayers.contains(layer);
Expand Down
7 changes: 7 additions & 0 deletions app/lib/cubits/current_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class CurrentIndex with _$CurrentIndex {
List<Rect>? temporarySelections,
@Default([]) List<int> pointers,
@Default(CameraViewport.unbaked()) CameraViewport cameraViewport,
@Default(AssetLocation(path: '')) AssetLocation location,
@Default(false) bool saved,
}) = _CurrentIndex;

bool get moveEnabled =>
Expand Down Expand Up @@ -331,4 +333,9 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
emit(state.copyWith(
cameraViewport: state.cameraViewport.withUnbaked(unbakedElements)));
}

void setSaveState({AssetLocation? location, bool? saved}) {
emit(state.copyWith(
location: location ?? state.location, saved: saved ?? state.saved));
}
}
70 changes: 63 additions & 7 deletions app/lib/cubits/current_index.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ mixin _$CurrentIndex {
List<Rect>? get temporarySelections => throw _privateConstructorUsedError;
List<int> get pointers => throw _privateConstructorUsedError;
CameraViewport get cameraViewport => throw _privateConstructorUsedError;
AssetLocation get location => throw _privateConstructorUsedError;
bool get saved => throw _privateConstructorUsedError;

@JsonKey(ignore: true)
$CurrentIndexCopyWith<CurrentIndex> get copyWith =>
Expand All @@ -52,7 +54,11 @@ abstract class $CurrentIndexCopyWith<$Res> {
List<Renderer>? temporaryForegrounds,
List<Rect>? temporarySelections,
List<int> pointers,
CameraViewport cameraViewport});
CameraViewport cameraViewport,
AssetLocation location,
bool saved});

$AssetLocationCopyWith<$Res> get location;
}

/// @nodoc
Expand All @@ -77,6 +83,8 @@ class _$CurrentIndexCopyWithImpl<$Res> implements $CurrentIndexCopyWith<$Res> {
Object? temporarySelections = freezed,
Object? pointers = freezed,
Object? cameraViewport = freezed,
Object? location = freezed,
Object? saved = freezed,
}) {
return _then(_value.copyWith(
index: index == freezed
Expand Down Expand Up @@ -127,8 +135,23 @@ class _$CurrentIndexCopyWithImpl<$Res> implements $CurrentIndexCopyWith<$Res> {
? _value.cameraViewport
: cameraViewport // ignore: cast_nullable_to_non_nullable
as CameraViewport,
location: location == freezed
? _value.location
: location // ignore: cast_nullable_to_non_nullable
as AssetLocation,
saved: saved == freezed
? _value.saved
: saved // ignore: cast_nullable_to_non_nullable
as bool,
));
}

@override
$AssetLocationCopyWith<$Res> get location {
return $AssetLocationCopyWith<$Res>(_value.location, (value) {
return _then(_value.copyWith(location: value));
});
}
}

/// @nodoc
Expand All @@ -150,7 +173,12 @@ abstract class _$$_CurrentIndexCopyWith<$Res>
List<Renderer>? temporaryForegrounds,
List<Rect>? temporarySelections,
List<int> pointers,
CameraViewport cameraViewport});
CameraViewport cameraViewport,
AssetLocation location,
bool saved});

@override
$AssetLocationCopyWith<$Res> get location;
}

/// @nodoc
Expand Down Expand Up @@ -178,6 +206,8 @@ class __$$_CurrentIndexCopyWithImpl<$Res>
Object? temporarySelections = freezed,
Object? pointers = freezed,
Object? cameraViewport = freezed,
Object? location = freezed,
Object? saved = freezed,
}) {
return _then(_$_CurrentIndex(
index == freezed
Expand Down Expand Up @@ -228,6 +258,14 @@ class __$$_CurrentIndexCopyWithImpl<$Res>
? _value.cameraViewport
: cameraViewport // ignore: cast_nullable_to_non_nullable
as CameraViewport,
location: location == freezed
? _value.location
: location // ignore: cast_nullable_to_non_nullable
as AssetLocation,
saved: saved == freezed
? _value.saved
: saved // ignore: cast_nullable_to_non_nullable
as bool,
));
}
}
Expand All @@ -244,7 +282,9 @@ class _$_CurrentIndex extends _CurrentIndex {
final List<Renderer>? temporaryForegrounds,
final List<Rect>? temporarySelections,
final List<int> pointers = const [],
this.cameraViewport = const CameraViewport.unbaked()})
this.cameraViewport = const CameraViewport.unbaked(),
this.location = const AssetLocation(path: ''),
this.saved = false})
: _foregrounds = foregrounds,
_selections = selections,
_temporaryForegrounds = temporaryForegrounds,
Expand Down Expand Up @@ -309,10 +349,16 @@ class _$_CurrentIndex extends _CurrentIndex {
@override
@JsonKey()
final CameraViewport cameraViewport;
@override
@JsonKey()
final AssetLocation location;
@override
@JsonKey()
final bool saved;

@override
String toString() {
return 'CurrentIndex(index: $index, handler: $handler, settingsCubit: $settingsCubit, transformCubit: $transformCubit, temporaryIndex: $temporaryIndex, temporaryHandler: $temporaryHandler, foregrounds: $foregrounds, selections: $selections, temporaryForegrounds: $temporaryForegrounds, temporarySelections: $temporarySelections, pointers: $pointers, cameraViewport: $cameraViewport)';
return 'CurrentIndex(index: $index, handler: $handler, settingsCubit: $settingsCubit, transformCubit: $transformCubit, temporaryIndex: $temporaryIndex, temporaryHandler: $temporaryHandler, foregrounds: $foregrounds, selections: $selections, temporaryForegrounds: $temporaryForegrounds, temporarySelections: $temporarySelections, pointers: $pointers, cameraViewport: $cameraViewport, location: $location, saved: $saved)';
}

@override
Expand Down Expand Up @@ -340,7 +386,9 @@ class _$_CurrentIndex extends _CurrentIndex {
.equals(other._temporarySelections, _temporarySelections) &&
const DeepCollectionEquality().equals(other._pointers, _pointers) &&
const DeepCollectionEquality()
.equals(other.cameraViewport, cameraViewport));
.equals(other.cameraViewport, cameraViewport) &&
const DeepCollectionEquality().equals(other.location, location) &&
const DeepCollectionEquality().equals(other.saved, saved));
}

@override
Expand All @@ -357,7 +405,9 @@ class _$_CurrentIndex extends _CurrentIndex {
const DeepCollectionEquality().hash(_temporaryForegrounds),
const DeepCollectionEquality().hash(_temporarySelections),
const DeepCollectionEquality().hash(_pointers),
const DeepCollectionEquality().hash(cameraViewport));
const DeepCollectionEquality().hash(cameraViewport),
const DeepCollectionEquality().hash(location),
const DeepCollectionEquality().hash(saved));

@JsonKey(ignore: true)
@override
Expand All @@ -375,7 +425,9 @@ abstract class _CurrentIndex extends CurrentIndex {
final List<Renderer>? temporaryForegrounds,
final List<Rect>? temporarySelections,
final List<int> pointers,
final CameraViewport cameraViewport}) = _$_CurrentIndex;
final CameraViewport cameraViewport,
final AssetLocation location,
final bool saved}) = _$_CurrentIndex;
const _CurrentIndex._() : super._();

@override
Expand Down Expand Up @@ -403,6 +455,10 @@ abstract class _CurrentIndex extends CurrentIndex {
@override
CameraViewport get cameraViewport;
@override
AssetLocation get location;
@override
bool get saved;
@override
@JsonKey(ignore: true)
_$$_CurrentIndexCopyWith<_$_CurrentIndex> get copyWith =>
throw _privateConstructorUsedError;
Expand Down
10 changes: 4 additions & 6 deletions app/lib/dialogs/file_system/menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ class FileSystemAssetMenu extends StatelessWidget {
var state = bloc.state;
if (state is! DocumentLoadSuccess) return;
if (document != null && state.location.path == path) {
bloc.clearHistory();
bloc.emit(state.copyWith(
state.currentIndexCubit.setSaveState(
location: AssetLocation(
remote: state.location.remote, path: path)));
remote: state.location.remote, path: path));
}
onRefreshed();
}
Expand Down Expand Up @@ -173,10 +172,9 @@ class FileSystemAssetMenu extends StatelessWidget {
var state = bloc.state;
if (state is! DocumentLoadSuccess) return;
if (state.location.path == asset.pathWithLeadingSlash) {
bloc.clearHistory();
bloc.emit(state.copyWith(
state.currentIndexCubit.setSaveState(
location: AssetLocation(
remote: state.location.remote, path: newPath)));
remote: state.location.remote, path: newPath));
}
}),
),
Expand Down
1 change: 1 addition & 0 deletions fastlane/metadata/android/en-US/changelogs/33.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
* Add stylus button input
* Add hex input to color dialog
* Add constraints to shape painter
* Move save state in current index cubit to allow undo/redo
* Fix bake issues
* Fix wrong properties when resetting back to hand painter
* Fix ignored properties in area painter

0 comments on commit b698e89

Please sign in to comment.