Skip to content

Commit

Permalink
PAINTROID-679 Implement JSON Serialization via Code Generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenkomotive committed Jan 27, 2024
1 parent d18319e commit c48353a
Show file tree
Hide file tree
Showing 75 changed files with 893 additions and 2,254 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Protoc
uses: arduino/setup-protoc@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
- uses: subosito/[email protected]
with:
flutter-version: "3.10.5"
Expand Down
3 changes: 0 additions & 3 deletions generate_protos.sh

This file was deleted.

13 changes: 6 additions & 7 deletions packages/command/lib/command.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
library command;

export 'src/graphic/draw_path_command.dart';
export 'src/manager/sync_command_manager.dart';

export 'src/command_factory.dart';
export 'src/command_manager.dart';
export 'src/command.dart';
export 'src/graphic_command.dart';
export 'src/command_factory/command_factory.dart';
export 'src/command_implementation/command.dart';
export 'src/command_implementation/graphic/draw_path_command.dart';
export 'src/command_implementation/graphic/graphic_command.dart';
export 'src/command_manager/command_manager.dart';
export 'src/command_manager/sync_command_manager.dart';
4 changes: 2 additions & 2 deletions packages/command/lib/command_providers.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library command;

export 'src/command_factory_provider.dart';
export 'src/command_manager_provider.dart';
export 'src/command_factory/command_factory_provider.dart';
export 'src/command_manager/command_manager_provider.dart';
5 changes: 0 additions & 5 deletions packages/command/lib/src/command.dart

This file was deleted.

19 changes: 19 additions & 0 deletions packages/command/lib/src/command_implementation/command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:command/command.dart';
import 'package:equatable/equatable.dart';
import 'package:io_library/io_library.dart';

abstract class Command with EquatableMixin {
const Command();

Map<String, dynamic> toJson();

factory Command.fromJson(Map<String, dynamic> json) {
String type = json['type'] as String;
switch (type) {
case SerializerType.DRAW_PATH_COMMAND:
return DrawPathCommand.fromJson(json);
default:
return DrawPathCommand.fromJson(json);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'dart:ui';

import 'package:command/command.dart';
import 'package:component_library/component_library.dart';
import 'package:flutter/widgets.dart';
import 'package:io_library/io_library.dart';
import 'package:json_annotation/json_annotation.dart';

part 'draw_path_command.g.dart';

@JsonSerializable()
class DrawPathCommand extends GraphicCommand {
final String type;
final int version;

DrawPathCommand(
this.path,
super.paint, {
this.type = SerializerType.DRAW_PATH_COMMAND,
int? version,
}) : version = version ??
VersionStrategyManager.strategy.getDrawPathCommandVersion();

@PathWithActionHistoryConverter()
final PathWithActionHistory path;

@override
void call(Canvas canvas) {
canvas.drawPath(path, paint);
}

@override
List<Object?> get props => [paint, path, type];

@override
Map<String, dynamic> toJson() => _$DrawPathCommandToJson(this);

factory DrawPathCommand.fromJson(Map<String, dynamic> json) {
int version = json['version'] as int;

switch (version) {
case Version.v1:
return _$DrawPathCommandFromJson(json);
case Version.v2:
// For different versions of DrawPathCommand the deserialization
// has to be implemented manually.
// Autogenerated code can only be used for one version
default:
return _$DrawPathCommandFromJson(json);
}
}
}

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

Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'dart:ui';

import 'package:command/command.dart';
import 'package:io_library/io_library.dart';

abstract class GraphicCommand extends Command {
const GraphicCommand(this.paint);

@PaintConverter()
final Paint paint;

void call(Canvas canvas);
Expand Down
18 changes: 0 additions & 18 deletions packages/command/lib/src/graphic/draw_path_command.dart

This file was deleted.

5 changes: 5 additions & 0 deletions packages/command/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ dependencies:
flutter_riverpod: ^2.3.6
riverpod_annotation: ^2.1.1
equatable: ^2.0.3
json_annotation: ^4.8.1


# Internal packages
component_library:
path: ../component_library
io_library:
path: ../io_library

dev_dependencies:
flutter_test:
Expand All @@ -31,6 +35,7 @@ dev_dependencies:
riverpod_lint: ^1.3.2
build_runner: ^2.2.0
freezed: ^2.4.1
json_serializable: ^6.7.1

flutter:
uses-material-design: true
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import 'dart:ui';

import 'package:collection/collection.dart';
import 'package:io_library/io_library.dart';

class PathWithActionHistory extends Path {
PathWithActionHistory();

@PathActionConverter()
final actions = <PathAction>[];

@override
Expand All @@ -20,6 +26,25 @@ class PathWithActionHistory extends Path {
actions.add(const CloseAction());
super.close();
}

Map<String, dynamic> toJson() {
return const PathWithActionHistoryConverter().toJson(this);
}

factory PathWithActionHistory.fromJson(Map<String, dynamic> json) {
return const PathWithActionHistoryConverter().fromJson(json);
}

@override
bool operator ==(Object other) {
if (other is PathWithActionHistory) {
return const ListEquality<PathAction>().equals(actions, other.actions);
}
return false;
}

@override
int get hashCode => const ListEquality<PathAction>().hash(actions);
}

abstract class PathAction {
Expand All @@ -31,13 +56,35 @@ class MoveToAction extends PathAction {
final double y;

const MoveToAction(this.x, this.y);

@override
bool operator ==(Object other) {
if (other is MoveToAction) {
return x == other.x && y == other.y;
}
return false;
}

@override
int get hashCode => Object.hash(x, y);
}

class LineToAction extends PathAction {
final double x;
final double y;

const LineToAction(this.x, this.y);

@override
bool operator ==(Object other) {
if (other is LineToAction) {
return x == other.x && y == other.y;
}
return false;
}

@override
int get hashCode => Object.hash(x, y);
}

class CloseAction extends PathAction {
Expand Down
6 changes: 6 additions & 0 deletions packages/component_library/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ dependencies:
toast: ^0.3.0
logging: ^1.0.2

collection: ^1.17.1

# Internal packages:
io_library:
path: ../io_library

dev_dependencies:
flutter_test:
sdk: flutter
Expand Down
21 changes: 6 additions & 15 deletions packages/io_library/lib/io_library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,23 @@ library io_library;

export 'src/enums/image_format.dart';
export 'src/enums/image_location.dart';

export 'src/failure/load_image_failure.dart';
export 'src/failure/save_image_failure.dart';

export 'src/io_handler.dart';
export 'src/json_serialization/converter/paint_converter.dart';
export 'src/json_serialization/converter/path_action_converter.dart';
export 'src/json_serialization/converter/path_with_action_history_converter.dart';
export 'src/json_serialization/versioning/serializer_version.dart';
export 'src/json_serialization/versioning/version_strategy.dart';
export 'src/models/catrobat_image.dart';
export 'src/models/failure.dart';
export 'src/models/image_from_file.dart';
export 'src/models/image_meta_data.dart';
export 'src/models/loggable_mixin.dart';

export 'src/serialization/proto/protos.dart';
export 'src/serialization/serializer/command/graphic/draw_path_command_serializer.dart';
export 'src/serialization/serializer/graphic/paint_serializer.dart';
export 'src/serialization/serializer/graphic/path_serializer.dart';
export 'src/serialization/serializer/catrobat_image_serializer.dart';
export 'src/serialization/proto_serializer_with_versioning.dart';
export 'src/serialization/version_serializer.dart';

export 'src/service/file_service.dart';
export 'src/service/image_service.dart';
export 'src/service/permission_service.dart';
export 'src/service/photo_library_service.dart';

export 'src/ui/about_dialog.dart';
export 'src/ui/delete_project_dialog.dart';
export 'src/ui/discard_changes_dialog.dart';
Expand All @@ -34,10 +28,7 @@ export 'src/ui/load_image_dialog.dart';
export 'src/ui/overwrite_dialog.dart';
export 'src/ui/project_details_dialog.dart';
export 'src/ui/save_image_dialog.dart';

export 'src/usecase/load_image_from_file_manager.dart';
export 'src/usecase/load_image_from_photo_library.dart';
export 'src/usecase/save_as_catrobat_image.dart';
export 'src/usecase/save_as_raster_image.dart';

export 'src/io_handler.dart';
10 changes: 0 additions & 10 deletions packages/io_library/lib/serialization.dart

This file was deleted.

17 changes: 15 additions & 2 deletions packages/io_library/lib/src/io_handler.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:command/command_providers.dart';
import 'package:component_library/component_library.dart';
Expand Down Expand Up @@ -201,8 +203,19 @@ class IOHandler {
final canvasState = ref.read(canvasStateProvider);
final imgWidth = canvasState.size.width.toInt();
final imgHeight = canvasState.size.height.toInt();
final catrobatImage = CatrobatImage(
commands, imgWidth, imgHeight, canvasState.backgroundImage);
Uint8List? backgroundImageData;
if (canvasState.backgroundImage != null) {
final result = await ref
.read(IImageService.provider)
.exportAsPng(canvasState.backgroundImage!);
backgroundImageData =
result.unwrapOrElse((failure) => throw failure.message);
}

final String backgroundImageAsString =
backgroundImageData != null ? base64Encode(backgroundImageData) : '';
final catrobatImage =
CatrobatImage(commands, imgWidth, imgHeight, backgroundImageAsString);
final saveAsCatrobatImage = ref.read(SaveAsCatrobatImage.provider);
final result =
await saveAsCatrobatImage(imageData, catrobatImage, isAProject);
Expand Down
Loading

0 comments on commit c48353a

Please sign in to comment.