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 Feb 3, 2024
1 parent d18319e commit d7fb50f
Show file tree
Hide file tree
Showing 94 changed files with 991 additions and 2,349 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.

15 changes: 8 additions & 7 deletions packages/command/lib/command.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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 'graphic_factory/graphic_factory.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';
export 'utils/path_with_action_history.dart';
5 changes: 3 additions & 2 deletions packages/command/lib/command_providers.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
library command;

export 'src/command_factory_provider.dart';
export 'src/command_manager_provider.dart';
export 'graphic_factory/graphic_factory_provider.dart';
export 'src/command_factory/command_factory_provider.dart';
export 'src/command_manager/command_manager_provider.dart';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:ui';

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

class GraphicFactory {
const GraphicFactory();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:component_library/component_library.dart';
import 'package:command/graphic_factory/graphic_factory.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'graphic_factory_provider.g.dart';
Expand Down
5 changes: 0 additions & 5 deletions packages/command/lib/src/command.dart

This file was deleted.

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

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

class CommandFactory {
const CommandFactory();
Expand Down
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,51 @@
import 'dart:ui';

import 'package:command/command.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.

92 changes: 92 additions & 0 deletions packages/command/lib/utils/path_with_action_history.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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
void moveTo(double x, double y) {
actions.add(MoveToAction(x, y));
super.moveTo(x, y);
}

@override
void lineTo(double x, double y) {
actions.add(LineToAction(x, y));
super.lineTo(x, y);
}

@override
void close() {
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 {
const PathAction();
}

class MoveToAction extends PathAction {
final double x;
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 {
const CloseAction();
}
6 changes: 6 additions & 0 deletions packages/command/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ dependencies:
flutter_riverpod: ^2.3.6
riverpod_annotation: ^2.1.1
equatable: ^2.0.3
json_annotation: ^4.8.1
collection: ^1.17.1


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

dev_dependencies:
flutter_test:
Expand All @@ -31,6 +36,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
3 changes: 1 addition & 2 deletions packages/command/test/unit/command_factory_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'dart:ui';

import 'package:flutter_test/flutter_test.dart';
import 'package:command/command.dart';
import 'package:component_library/component_library.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
late PathWithActionHistory testPath;
Expand Down
1 change: 0 additions & 1 deletion packages/command/test/unit/draw_path_command_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:ui';

import 'package:command/command.dart';
import 'package:component_library/component_library.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
Expand Down
11 changes: 2 additions & 9 deletions packages/component_library/lib/component_library.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
library component_library;

export 'src/components/imgs.dart';
export 'src/components/icon_svg.dart';
export 'src/components/bottom_nav_bar_icon.dart';
export 'src/components/custom_action_chip.dart';
export 'src/components/icon_button_with_label.dart';
export 'src/components/icon_svg.dart';
export 'src/components/imgs.dart';
export 'src/components/loading_overlay.dart';
export 'src/components/pop_menu_button.dart';

export 'src/models/graphic_factory.dart';
export 'src/models/path_with_action_history.dart';

export 'src/theme/color_schemes.dart';
export 'src/theme/styles.dart';

export 'src/utils/open_url.dart';
export 'src/utils/toast_utils.dart';

export 'src/graphic_factory_provider.dart';
Loading

0 comments on commit d7fb50f

Please sign in to comment.