diff --git a/.gitmodules b/.gitmodules index e69de29..bc3df8d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "plugins/scribble"] + path = plugins/scribble + url = https://github.com/CopyPaste-Orbital2022/scribble.git diff --git a/lib/core/injections/injection.config.dart b/lib/core/injections/injection.config.dart index 312767e..add4fe0 100644 --- a/lib/core/injections/injection.config.dart +++ b/lib/core/injections/injection.config.dart @@ -6,29 +6,36 @@ import 'package:get_it/get_it.dart' as _i1; import 'package:injectable/injectable.dart' as _i2; -import 'package:shared_preferences/shared_preferences.dart' as _i3; +import 'package:scribble/scribble.dart' as _i3; +import 'package:shared_preferences/shared_preferences.dart' as _i4; import '../../features/drawing/data/repositories/drawing_toolbar_repository_impl.dart' - as _i5; + as _i6; import '../../features/drawing/domain/repositories/i_drawing_toolbar_repository.dart' - as _i4; -import '../../features/drawing/presentation/bloc/drawing_bloc.dart' as _i6; -import 'shared_preferences.dart' as _i7; // ignore_for_file: unnecessary_lambdas + as _i5; +import '../../features/drawing/presentation/bloc/drawing_bloc.dart' as _i7; +import 'scribble_injection.dart' as _i8; +import 'shared_preferences.dart' as _i9; // ignore_for_file: unnecessary_lambdas // ignore_for_file: lines_longer_than_80_chars /// initializes the registration of provided dependencies inside of [GetIt] Future<_i1.GetIt> $initGetIt(_i1.GetIt get, {String? environment, _i2.EnvironmentFilter? environmentFilter}) async { final gh = _i2.GetItHelper(get, environment, environmentFilter); + final scribbleInjectionModule = _$ScribbleInjectionModule(); final preferencesInjectionModule = _$PreferencesInjectionModule(); - await gh.factoryAsync<_i3.SharedPreferences>( + gh.lazySingleton<_i3.ScribbleNotifier>( + () => scribbleInjectionModule.notifier); + await gh.factoryAsync<_i4.SharedPreferences>( () => preferencesInjectionModule.prefs, preResolve: true); - gh.lazySingleton<_i4.IDrawingToolBarRepository>(() => - _i5.DrawingToolbarRepositoryImpl(prefs: get<_i3.SharedPreferences>())); - gh.lazySingleton<_i6.DrawingBloc>( - () => _i6.DrawingBloc(get<_i4.IDrawingToolBarRepository>())); + gh.lazySingleton<_i5.IDrawingToolBarRepository>(() => + _i6.DrawingToolbarRepositoryImpl(prefs: get<_i4.SharedPreferences>())); + gh.lazySingleton<_i7.DrawingBloc>(() => _i7.DrawingBloc( + get<_i5.IDrawingToolBarRepository>(), get<_i3.ScribbleNotifier>())); return get; } -class _$PreferencesInjectionModule extends _i7.PreferencesInjectionModule {} +class _$ScribbleInjectionModule extends _i8.ScribbleInjectionModule {} + +class _$PreferencesInjectionModule extends _i9.PreferencesInjectionModule {} diff --git a/lib/core/injections/injection.dart b/lib/core/injections/injection.dart index 1e2d621..d3998de 100644 --- a/lib/core/injections/injection.dart +++ b/lib/core/injections/injection.dart @@ -4,7 +4,7 @@ import 'injection.config.dart'; final getIt = GetIt.instance; -@injectableInit +@InjectableInit() Future configureInjection(String env) async { await $initGetIt(getIt, environment: env); } diff --git a/lib/core/injections/scribble_injection.dart b/lib/core/injections/scribble_injection.dart new file mode 100644 index 0000000..4c3323f --- /dev/null +++ b/lib/core/injections/scribble_injection.dart @@ -0,0 +1,8 @@ +import 'package:injectable/injectable.dart'; +import 'package:scribble/scribble.dart'; + +@module +abstract class ScribbleInjectionModule { + @lazySingleton + ScribbleNotifier get notifier => ScribbleNotifier(); +} diff --git a/lib/features/drawing/data/models/pen_state_model.dart b/lib/features/drawing/data/models/pen_state_model.dart index 042548c..c7eebbb 100644 --- a/lib/features/drawing/data/models/pen_state_model.dart +++ b/lib/features/drawing/data/models/pen_state_model.dart @@ -6,7 +6,7 @@ class PenStateModel { final int currentColorIdx; static const int defaultCurrentColorIdx = 0; final List colors; - static const List defaultColors = ["0x0000000000"]; + static const List defaultColors = ["0xFF00000000"]; final bool useStylus; static const bool defaultUseStylus = false; final double width; diff --git a/lib/features/drawing/domain/entities/pen_state.dart b/lib/features/drawing/domain/entities/pen_state.dart index 3d2a413..bc9db7e 100644 --- a/lib/features/drawing/domain/entities/pen_state.dart +++ b/lib/features/drawing/domain/entities/pen_state.dart @@ -22,3 +22,9 @@ class PenState with _$PenState { required double width, }) = _PenState; } + +extension PenStateX on PenState { + Color getCurrentColor() { + return colors[currentColorIdx]; + } +} diff --git a/lib/features/drawing/presentation/bloc/drawing_bloc.dart b/lib/features/drawing/presentation/bloc/drawing_bloc.dart index da810a9..ed64e96 100644 --- a/lib/features/drawing/presentation/bloc/drawing_bloc.dart +++ b/lib/features/drawing/presentation/bloc/drawing_bloc.dart @@ -1,17 +1,27 @@ import 'package:bloc/bloc.dart'; +import 'package:copypaste/features/drawing/domain/entities/pen_state.dart'; +import 'package:copypaste/features/drawing/domain/entities/selectable_tools.dart'; import 'package:copypaste/features/drawing/domain/repositories/i_drawing_toolbar_repository.dart'; import 'package:copypaste/features/drawing/presentation/bloc/index.dart'; import 'package:injectable/injectable.dart'; +import 'package:scribble/scribble.dart'; @LazySingleton() class DrawingBloc extends Bloc { final IDrawingToolBarRepository _repository; - DrawingBloc(this._repository) : super(DrawingStateX.initialState()) { + final ScribbleNotifier _notifier; + DrawingBloc(this._repository, this._notifier) + : super(DrawingStateX.initialState()) { on((event, emit) { _repository.saveCurrentTool(event.tool); emit( state.copyWith(currentTool: event.tool), ); + if (event.tool == DrawingTool.eraser) { + _notifier.setEraser(); + } else if (event.tool == DrawingTool.pen) { + _notifier.setPen(); + } else if (event.tool == DrawingTool.hand) {} }); } } diff --git a/lib/features/drawing/presentation/bloc/drawing_state.dart b/lib/features/drawing/presentation/bloc/drawing_state.dart index 0ddc7ac..ac0d700 100644 --- a/lib/features/drawing/presentation/bloc/drawing_state.dart +++ b/lib/features/drawing/presentation/bloc/drawing_state.dart @@ -4,6 +4,7 @@ import 'package:copypaste/features/drawing/domain/entities/pen_state.dart'; import 'package:copypaste/features/drawing/domain/entities/selectable_tools.dart'; import 'package:copypaste/features/drawing/domain/repositories/i_drawing_toolbar_repository.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; +import 'package:scribble/scribble.dart'; part 'drawing_state.freezed.dart'; diff --git a/lib/features/drawing/presentation/pages/drawing_page.dart b/lib/features/drawing/presentation/pages/drawing_page.dart new file mode 100644 index 0000000..6c7c494 --- /dev/null +++ b/lib/features/drawing/presentation/pages/drawing_page.dart @@ -0,0 +1,35 @@ +import 'package:copypaste/core/injections/injection.dart'; +import 'package:copypaste/features/drawing/domain/entities/selectable_tools.dart'; +import 'package:copypaste/features/drawing/presentation/bloc/index.dart'; +import 'package:copypaste/features/drawing/presentation/widgets/drawing_button_menu.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:scribble/scribble.dart'; + +class DrawingPage extends StatelessWidget { + const DrawingPage({Key? key}) : super(key: key); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + actions: const [ + DrawingButtonMenu(), + ], + ), + body: BlocProvider( + create: (context) => getIt(), + child: BlocConsumer( + builder: (context, state) { + return Scribble( + notifier: getIt(), + drawPen: state.currentTool == DrawingTool.pen, + drawEraser: state.currentTool == DrawingTool.eraser, + ); + }, + listener: (context, state) {}, + ), + ), + ); + } +} diff --git a/lib/main.dart b/lib/main.dart index c3120de..926fe12 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'package:copypaste/core/injections/injection.dart'; +import 'package:copypaste/features/drawing/presentation/pages/drawing_page.dart'; import 'package:copypaste/features/drawing/presentation/widgets/drawing_button_menu.dart'; import 'package:flutter/material.dart'; import 'package:injectable/injectable.dart'; @@ -16,108 +17,11 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( - title: 'Flutter Demo', + title: 'CopyPaste', theme: ThemeData( - // This is the theme of your application. - // - // Try running your application with "flutter run". You'll see the - // application has a blue toolbar. Then, without quitting the app, try - // changing the primarySwatch below to Colors.green and then invoke - // "hot reload" (press "r" in the console where you ran "flutter run", - // or simply save your changes to "hot reload" in a Flutter IDE). - // Notice that the counter didn't reset back to zero; the application - // is not restarted. primarySwatch: Colors.blue, ), - home: const MyHomePage(title: 'Flutter Demo Home Page'), - ); - } -} - -class MyHomePage extends StatefulWidget { - const MyHomePage({Key? key, required this.title}) : super(key: key); - - // This widget is the home page of your application. It is stateful, meaning - // that it has a State object (defined below) that contains fields that affect - // how it looks. - - // This class is the configuration for the state. It holds the values (in this - // case the title) provided by the parent (in this case the App widget) and - // used by the build method of the State. Fields in a Widget subclass are - // always marked "final". - - final String title; - - @override - State createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - int _counter = 0; - - void _incrementCounter() { - setState(() { - // This call to setState tells the Flutter framework that something has - // changed in this State, which causes it to rerun the build method below - // so that the display can reflect the updated values. If we changed - // _counter without calling setState(), then the build method would not be - // called again, and so nothing would appear to happen. - _counter++; - }); - } - - @override - Widget build(BuildContext context) { - // This method is rerun every time setState is called, for instance as done - // by the _incrementCounter method above. - // - // The Flutter framework has been optimized to make rerunning build methods - // fast, so that you can just rebuild anything that needs updating rather - // than having to individually change instances of widgets. - return Scaffold( - appBar: AppBar( - // Here we take the value from the MyHomePage object that was created by - // the App.build method, and use it to set our appbar title. - title: Text(widget.title), - actions: const [ - DrawingButtonMenu(), - ], - ), - body: Center( - // Center is a layout widget. It takes a single child and positions it - // in the middle of the parent. - child: Column( - // Column is also a layout widget. It takes a list of children and - // arranges them vertically. By default, it sizes itself to fit its - // children horizontally, and tries to be as tall as its parent. - // - // Invoke "debug painting" (press "p" in the console, choose the - // "Toggle Debug Paint" action from the Flutter Inspector in Android - // Studio, or the "Toggle Debug Paint" command in Visual Studio Code) - // to see the wireframe for each widget. - // - // Column has various properties to control how it sizes itself and - // how it positions its children. Here we use mainAxisAlignment to - // center the children vertically; the main axis here is the vertical - // axis because Columns are vertical (the cross axis would be - // horizontal). - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text( - 'You have pushed the button this many times:', - ), - Text( - '$_counter', - style: Theme.of(context).textTheme.headline4, - ), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: const Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. + home: const DrawingPage(), ); } } diff --git a/plugins/scribble b/plugins/scribble new file mode 160000 index 0000000..210f858 --- /dev/null +++ b/plugins/scribble @@ -0,0 +1 @@ +Subproject commit 210f8582adf62fd78a5445957bf87f925ed88599 diff --git a/pubspec.lock b/pubspec.lock index aa25c18..fe7422c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -488,10 +488,10 @@ packages: scribble: dependency: "direct main" description: - name: scribble - url: "https://pub.dartlang.org" - source: hosted - version: "0.4.0" + path: "plugins/scribble" + relative: true + source: path + version: "0.3.1" shared_preferences: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 695a192..d38f47a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -36,7 +36,8 @@ dependencies: freezed_annotation: ^2.0.3 get_it: ^7.2.0 injectable: ^1.5.3 - scribble: ^0.4.0 + scribble: + path: plugins/scribble shared_preferences: ^2.0.13 dev_dependencies: