Skip to content

Commit

Permalink
refactor!: Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
intex-weinmann committed Oct 10, 2023
1 parent fff24a5 commit e41dde2
Show file tree
Hide file tree
Showing 40 changed files with 800 additions and 683 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dart.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Flutter build
name: flutter build

on:
push:
Expand Down
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v0.2.0

- Code cleanup

## v0.1.6

- disable controls when convert images.
Expand Down
4 changes: 2 additions & 2 deletions lib/core/design/theme_icons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ class ThemeIcons {

static IconData get newPosition => FontAwesomeIcons.plus;
static IconData get newspaper => FontAwesomeIcons.newspaper;
static IconData get noObscure => FontAwesomeIcons.eye;
static IconData get noObscure => FontAwesomeIcons.eyeSlash;
static IconData get notifications => FontAwesomeIcons.bell;

static IconData get obscure => FontAwesomeIcons.eyeSlash;
static IconData get obscure => FontAwesomeIcons.eye;
static IconData get on => FontAwesomeIcons.toggleOn;
static IconData get off => FontAwesomeIcons.toggleOff;

Expand Down
8 changes: 4 additions & 4 deletions lib/core/lib/trace.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'logger.dart';

typedef F<T, R> = R Function(T t);

T trace<T>(T t, [dynamic prefix = 'xxx']) {
T trace<T>(T t, [dynamic prefix = 'trace']) {
Log.less("trace $prefix: [${t.runtimeType}]: $t");
return t;
}

T trace2<T, R>(T t, F<T, R> f, [dynamic prefix = 'xxx']) {
typedef F<T, R> = R Function(T t);

T trace2<T, R>(T t, F<T, R> f, [dynamic prefix = 'trace']) {
final r = f(t);
Log.less("trace $prefix: [${r.runtimeType}]: $r");
return t;
Expand Down
29 changes: 23 additions & 6 deletions lib/core/reactive/bloc.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:document_scanner/core/lib/optional.dart';
import 'package:document_scanner/core/reactive/form.dart';
import 'package:equatable/equatable.dart';
Expand All @@ -10,7 +12,10 @@ part 'state.dart';
abstract class ReactiveBloc<Param extends Equatable> extends Cubit<ReactiveState<Param>> implements ReactiveBlocForm {
FormGroup group = FormGroup({});

Future<void> loading();
final _completer = Completer<bool>();
Future<bool> get loaded => _completer.future;

Future<void> loading() async {}

ReactiveBloc({required Param parameter}) : super(InitReactiveState(parameter: parameter)) {
group.addAll(form);
Expand All @@ -21,7 +26,9 @@ abstract class ReactiveBloc<Param extends Equatable> extends Cubit<ReactiveState
await Future<void>.delayed(const Duration());
try {
emit(LoadingReactiveState.newWith(other: state));
loading().then((value) => emit(LoadedReactiveState.newWith(other: state)));
loading()
.then((value) => emit(LoadedReactiveState.newWith(other: state)))
.then((value) => _completer.complete(true));
} on Exception catch (e, s) {
emit(LoadFailureReactiveState.newWith(other: state, failureResponse: ErrorValue(e, s)));
}
Expand All @@ -31,12 +38,22 @@ abstract class ReactiveBloc<Param extends Equatable> extends Cubit<ReactiveState
emit(SubmittingReactiveState.newWith(other: state));
}

emitLoading() {
emit(LoadingReactiveState.newWith(other: state));
emitProgress({
FormControl<int>? progress,
int max = 0,
}) {
emit(ProgressReactiveState.newWith(other: state, progress: progress, max: max));
}

emitProgressSuccess({String? successResponse, Param? parameter}) {
emit(ProgressCloseReactiveState(parameter: parameter ?? state.parameter, successResponse: successResponse));
}

emitLoaded({Param? parameter}) {
emit(LoadedReactiveState(parameter: parameter ?? state.parameter));
emitProgressFailure({required ErrorValue failureResponse, Param? parameter}) {
emit(ProgressFailureReactiveState(
parameter: parameter ?? state.parameter,
failureResponse: failureResponse,
));
}

emitSuccess({required String successResponse, Param? parameter}) {
Expand Down
64 changes: 64 additions & 0 deletions lib/core/reactive/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,22 @@ class InitReactiveState<StateParam extends Equatable> extends ReactiveState<Stat
}

class LoadingReactiveState<StateParam extends Equatable> extends ReactiveState<StateParam> {
final FormControl<int>? progress;

const LoadingReactiveState({
required super.parameter,
this.progress,
}) : super(
successResponse: null,
failureResponse: null,
);

LoadingReactiveState.newWith({
required ReactiveState<StateParam> other,
FormControl<int>? progress,
}) : this(
parameter: other.parameter,
progress: progress,
);
}

Expand Down Expand Up @@ -75,6 +80,65 @@ class LoadFailureReactiveState<StateParam extends Equatable> extends ReactiveSta
);
}

class ProgressReactiveState<StateParam extends Equatable> extends ReactiveState<StateParam> {
final FormControl<int>? progress;
final int max;

const ProgressReactiveState({
required super.parameter,
this.progress,
this.max = 0,
}) : assert(progress == null || max > 0, "progress and max have to set both calculate the progress"),
super(
successResponse: null,
failureResponse: null,
);

ProgressReactiveState.newWith({
required ReactiveState<StateParam> other,
FormControl<int>? progress,
int max = -1,
}) : this(
parameter: other.parameter,
progress: progress,
max: max,
);
}

class ProgressCloseReactiveState<StateParam extends Equatable> extends ReactiveState<StateParam> {
const ProgressCloseReactiveState({
required super.parameter,
required super.successResponse,
}) : super(
failureResponse: null,
);

ProgressCloseReactiveState.newWith({
required ReactiveState<StateParam> other,
required String? successResponse,
}) : this(
successResponse: successResponse,
parameter: other.parameter,
);
}

class ProgressFailureReactiveState<StateParam extends Equatable> extends ReactiveState<StateParam> {
const ProgressFailureReactiveState({
required super.failureResponse,
required super.parameter,
}) : super(
successResponse: null,
);

ProgressFailureReactiveState.newWith({
required ErrorValue? failureResponse,
required ReactiveState<StateParam> other,
}) : this(
failureResponse: failureResponse,
parameter: other.parameter,
);
}

class UpdateReactiveState<StateParam extends Equatable> extends ReactiveState<StateParam> {
const UpdateReactiveState({
required super.parameter,
Expand Down
2 changes: 2 additions & 0 deletions lib/core/service_locator/service_locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'package:document_scanner/scanner/domain/usecases/export_attachment.dart'
import 'package:document_scanner/scanner/domain/usecases/export_database.dart';
import 'package:document_scanner/scanner/domain/usecases/import_database.dart';
import 'package:document_scanner/scanner/domain/usecases/load_list_items.dart';
import 'package:document_scanner/scanner/domain/usecases/read_file.dart';
import 'package:document_scanner/scanner/domain/usecases/read_files.dart';
import 'package:document_scanner/scanner/domain/usecases/rotate_image.dart';
import 'package:document_scanner/scanner/domain/usecases/store_list_items.dart';
Expand All @@ -43,6 +44,7 @@ Future<GetIt> initServiceLocator() async {
sl.registerSingletonAsync<ImageConverter>(() async => ImageConverterImpl());

// use cases
sl.registerLazySingleton<ReadFile>(() => ReadFileUseCase());
sl.registerLazySingleton<ReadFiles>(() => ReadFilesUseCase());
sl.registerLazySingleton<RotateImage>(() => RotateImageUseCase());
sl.registerLazySingleton<ConvertImage>(() => ConvertImageUseCase());
Expand Down
2 changes: 1 addition & 1 deletion lib/core/toaster/signal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ String showSnackBarSignal(BuildContext context, String hint, String message, [Li
}

WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: ResponsiveWidthPadding(content),
backgroundColor: nord12AuroraOrange,
Expand Down
2 changes: 1 addition & 1 deletion lib/core/toaster/success.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ String showSnackBarSuccess(BuildContext context, String hint, String message, [L
}

WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: ResponsiveWidthPadding(content),
backgroundColor: Theme.of(context).colorScheme.secondary,
Expand Down
75 changes: 0 additions & 75 deletions lib/core/widgets/cropper/widget.dart

This file was deleted.

Loading

0 comments on commit e41dde2

Please sign in to comment.