Skip to content

Commit

Permalink
worked on the drawing menubar
Browse files Browse the repository at this point in the history
  • Loading branch information
wxxedu committed May 15, 2022
1 parent 7c756c7 commit f0e8075
Show file tree
Hide file tree
Showing 23 changed files with 636 additions and 883 deletions.
7 changes: 5 additions & 2 deletions lib/core/injections/injection.config.dart

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,7 +1,7 @@
import 'package:copypaste/features/drawing/domain/entities/eraser_entity.dart';
import 'package:copypaste/features/drawing/domain/entities/eraser_state.dart';
import 'package:equatable/equatable.dart';

class EraserModel extends Equatable {
class EraserStateModel extends Equatable {
/// storage
static const String prefix = "eraser";
static const double defaultRadius = 5.0;
Expand All @@ -12,22 +12,22 @@ class EraserModel extends Equatable {
final bool isPartial;

/// Represents an eraser in the datasource
const EraserModel({
const EraserStateModel({
required this.radius,
required this.isPartial,
});

/// converts to the domain layer
EraserEntity toDomain() {
return EraserEntity(
EraserState toDomain() {
return EraserState(
radius: radius,
isPartial: isPartial,
);
}

/// converts back to from the domain layer
static EraserModel fromDomain(EraserEntity eraserEntity) {
return EraserModel(
static EraserStateModel fromDomain(EraserState eraserEntity) {
return EraserStateModel(
radius: eraserEntity.radius,
isPartial: eraserEntity.isPartial,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:ui';

import 'package:copypaste/features/drawing/domain/entities/pen_entity.dart';
import 'package:copypaste/features/drawing/domain/entities/pen_state.dart';

class PenModel {
class PenStateModel {
final int currentColorIdx;
static const int defaultCurrentColorIdx = 0;
final List<String> colors;
Expand All @@ -12,16 +12,16 @@ class PenModel {
final double width;
static const double defaultWidth = 1.0;

PenModel({
PenStateModel({
required this.currentColorIdx,
required this.colors,
required this.useStylus,
required this.width,
});

/// converts to domain
PenEntity toDomain() {
return PenEntity(
PenState toDomain() {
return PenState(
currentColorIdx: currentColorIdx,
colors: _stringsToColors(colors),
useStylus: useStylus,
Expand All @@ -30,8 +30,8 @@ class PenModel {
}

/// converts from domain
static PenModel fromDomain(PenEntity penEntity) {
return PenModel(
static PenStateModel fromDomain(PenState penEntity) {
return PenStateModel(
currentColorIdx: penEntity.currentColorIdx,
colors: _colorsToStrings(penEntity.colors),
useStylus: penEntity.useStylus,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'package:copypaste/features/drawing/data/models/eraser_model.dart';
import 'package:copypaste/features/drawing/data/models/pen_model.dart';
import 'package:copypaste/features/drawing/data/models/eraser_state_model.dart';
import 'package:copypaste/features/drawing/data/models/pen_state_model.dart';
import 'package:copypaste/features/drawing/domain/entities/selectable_tools.dart';
import 'package:injectable/injectable.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:copypaste/features/drawing/domain/entities/eraser_entity.dart';
import 'package:copypaste/features/drawing/domain/entities/pen_entity.dart';
import 'package:copypaste/features/drawing/domain/entities/eraser_state.dart';
import 'package:copypaste/features/drawing/domain/entities/pen_state.dart';
import 'package:copypaste/features/drawing/domain/repositories/i_drawing_toolbar_repository.dart';
import 'package:copypaste/core/extensions/shared_preferences_extension.dart';

Expand All @@ -23,62 +24,81 @@ class DrawingToolbarRepositoryImpl implements IDrawingToolBarRepository {
required this.prefs,
});

// ! below are some HUGELY INEFFICIENT code
// ! fix this later
// TODO: Fix the inefficient code below later -> replace with constant
// TODO: (continued) key instead of the current functions
@override
EraserEntity getEraserEntity() {
DrawingTool getCurrentTool() {
final String selectableToolKey = generateKey(
featurePrefix: featurePrefix,
modelPrefix: DrawingToolX.modelKey,
propertyName: "current-tool");
return DrawingToolX.fromString(
prefs.getElseSet(selectableToolKey, DrawingTool.hand.name));
}

@override
void saveCurrentTool(DrawingTool tool) {
final String selectableToolKey = generateKey(
featurePrefix: featurePrefix,
modelPrefix: DrawingToolX.modelKey,
propertyName: "current-tool");
prefs.setString(selectableToolKey, tool.name);
}

@override
EraserState getEraserState() {
final String radiusKey = generateKey(
featurePrefix: featurePrefix,
modelPrefix: EraserModel.prefix,
modelPrefix: EraserStateModel.prefix,
propertyName: "radius");
final String isPartialKey = generateKey(
featurePrefix: featurePrefix,
modelPrefix: EraserModel.prefix,
modelPrefix: EraserStateModel.prefix,
propertyName: "is-partial",
);
final double radius =
prefs.getElseSet(radiusKey, EraserModel.defaultRadius);
prefs.getElseSet(radiusKey, EraserStateModel.defaultRadius);
final bool isPartial =
prefs.getElseSet(isPartialKey, EraserModel.defaultIsPartial);
return EraserModel(radius: radius, isPartial: isPartial).toDomain();
prefs.getElseSet(isPartialKey, EraserStateModel.defaultIsPartial);
return EraserStateModel(radius: radius, isPartial: isPartial).toDomain();
}

@override
PenEntity getPenEntity() {
PenState getPenState() {
final String currentColorIdxKey = generateKey(
featurePrefix: featurePrefix,
modelPrefix: PenModel.prefix,
modelPrefix: PenStateModel.prefix,
propertyName: "current-color-index",
);
final String colorsKey = generateKey(
featurePrefix: featurePrefix,
modelPrefix: PenModel.prefix,
modelPrefix: PenStateModel.prefix,
propertyName: "colors",
);
final String useStylusKey = generateKey(
featurePrefix: featurePrefix,
modelPrefix: PenModel.prefix,
modelPrefix: PenStateModel.prefix,
propertyName: "use-stylus",
);
final String widthKey = generateKey(
featurePrefix: featurePrefix,
modelPrefix: PenModel.prefix,
modelPrefix: PenStateModel.prefix,
propertyName: "width",
);
final int currentColorIdx = prefs.getElseSet(
currentColorIdxKey,
PenModel.defaultCurrentColorIdx,
PenStateModel.defaultCurrentColorIdx,
);
final List<String> colors = prefs.getElseSet(
colorsKey,
PenModel.defaultColors,
PenStateModel.defaultColors,
);
final bool useStylus = prefs.getElseSet(
useStylusKey,
PenModel.defaultUseStylus,
PenStateModel.defaultUseStylus,
);
final double width = prefs.getElseSet(widthKey, PenModel.defaultWidth);
return PenModel(
final double width = prefs.getElseSet(widthKey, PenStateModel.defaultWidth);
return PenStateModel(
currentColorIdx: currentColorIdx,
colors: colors,
useStylus: useStylus,
Expand All @@ -87,43 +107,43 @@ class DrawingToolbarRepositoryImpl implements IDrawingToolBarRepository {
}

@override
void saveEraserEntity(EraserEntity entity) {
void saveEraserState(EraserState state) {
final String radiusKey = generateKey(
featurePrefix: featurePrefix,
modelPrefix: EraserModel.prefix,
modelPrefix: EraserStateModel.prefix,
propertyName: "radius");
final String isPartialKey = generateKey(
featurePrefix: featurePrefix,
modelPrefix: EraserModel.prefix,
modelPrefix: EraserStateModel.prefix,
propertyName: "is-partial",
);
prefs.setDouble(radiusKey, entity.radius);
prefs.setBool(isPartialKey, entity.isPartial);
prefs.setDouble(radiusKey, state.radius);
prefs.setBool(isPartialKey, state.isPartial);
}

@override
void savePenEntity(PenEntity entity) {
void savePenState(PenState state) {
final String currentColorIdxKey = generateKey(
featurePrefix: featurePrefix,
modelPrefix: PenModel.prefix,
modelPrefix: PenStateModel.prefix,
propertyName: "current-color-index",
);
final String colorsKey = generateKey(
featurePrefix: featurePrefix,
modelPrefix: PenModel.prefix,
modelPrefix: PenStateModel.prefix,
propertyName: "colors",
);
final String useStylusKey = generateKey(
featurePrefix: featurePrefix,
modelPrefix: PenModel.prefix,
modelPrefix: PenStateModel.prefix,
propertyName: "use-stylus",
);
final String widthKey = generateKey(
featurePrefix: featurePrefix,
modelPrefix: PenModel.prefix,
modelPrefix: PenStateModel.prefix,
propertyName: "width",
);
PenModel model = PenModel.fromDomain(entity);
PenStateModel model = PenStateModel.fromDomain(state);
prefs.setInt(currentColorIdxKey, model.currentColorIdx);
prefs.setStringList(colorsKey, model.colors);
prefs.setBool(useStylusKey, model.useStylus);
Expand Down
11 changes: 0 additions & 11 deletions lib/features/drawing/domain/entities/eraser_entity.dart

This file was deleted.

16 changes: 16 additions & 0 deletions lib/features/drawing/domain/entities/eraser_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:freezed_annotation/freezed_annotation.dart';

part 'eraser_state.freezed.dart';

/// The state of the eraser
///
/// [radius] - the radius of the eraser
///
/// [isPartial] - if the eraser is a partial eraser or not
@freezed
class EraserState with _$EraserState {
const factory EraserState({
required double radius,
required bool isPartial,
}) = _EraserState;
}
Loading

0 comments on commit f0e8075

Please sign in to comment.