feat(wyatt_app_template): use brickgen
This commit is contained in:
+59
@@ -0,0 +1,59 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:starting_template/data/models/integer_model.dart';
|
||||
import 'package:starting_template/domain/data_sources/local/counter_data_source.dart';
|
||||
import 'package:starting_template/domain/entities/integer.dart';
|
||||
import 'package:wyatt_architecture/wyatt_architecture.dart';
|
||||
|
||||
class CounterDataSourceImpl extends CounterDataSource {
|
||||
// Simulate external data processing
|
||||
String actual = '{"value": 0}';
|
||||
|
||||
@override
|
||||
Future<Integer> decrement(Integer value) async {
|
||||
final current =
|
||||
IntegerModel.fromJson(json.decode(actual) as Map<String, Object?>);
|
||||
|
||||
final newValue = current.value - value.value;
|
||||
if (newValue < 0) {
|
||||
throw ClientException("Counter can't be negative!");
|
||||
}
|
||||
|
||||
final newInteger = IntegerModel(value: newValue);
|
||||
actual = jsonEncode(newInteger.toJson());
|
||||
|
||||
return newInteger;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Integer> increment(Integer value) async {
|
||||
final current =
|
||||
IntegerModel.fromJson(json.decode(actual) as Map<String, Object?>);
|
||||
|
||||
final newValue = current.value + value.value;
|
||||
if (newValue < 0) {
|
||||
throw ClientException("Counter can't be negative!");
|
||||
}
|
||||
|
||||
final newInteger = IntegerModel(value: newValue);
|
||||
actual = jsonEncode(newInteger.toJson());
|
||||
|
||||
return newInteger;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Integer> getCurrent() async {
|
||||
final current =
|
||||
IntegerModel.fromJson(json.decode(actual) as Map<String, Object?>);
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Integer> reset() async {
|
||||
const newInteger = IntegerModel(value: 0);
|
||||
actual = jsonEncode(newInteger.toJson());
|
||||
|
||||
return newInteger;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
# just to keep empty folder in brick generation
|
||||
@@ -0,0 +1,15 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:starting_template/domain/entities/integer.dart';
|
||||
|
||||
part 'integer_model.freezed.dart';
|
||||
part 'integer_model.g.dart';
|
||||
|
||||
@freezed
|
||||
class IntegerModel extends Integer with _$IntegerModel {
|
||||
const factory IntegerModel({
|
||||
required int value,
|
||||
}) = _IntegerModel;
|
||||
|
||||
factory IntegerModel.fromJson(Map<String, Object?> json) =>
|
||||
_$IntegerModelFromJson(json);
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
// coverage:ignore-file
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
|
||||
|
||||
part of 'integer_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// FreezedGenerator
|
||||
// **************************************************************************
|
||||
|
||||
T _$identity<T>(T value) => value;
|
||||
|
||||
final _privateConstructorUsedError = UnsupportedError(
|
||||
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods');
|
||||
|
||||
IntegerModel _$IntegerModelFromJson(Map<String, dynamic> json) {
|
||||
return _IntegerModel.fromJson(json);
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
mixin _$IntegerModel {
|
||||
int get value => throw _privateConstructorUsedError;
|
||||
|
||||
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
|
||||
@JsonKey(ignore: true)
|
||||
$IntegerModelCopyWith<IntegerModel> get copyWith =>
|
||||
throw _privateConstructorUsedError;
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract class $IntegerModelCopyWith<$Res> {
|
||||
factory $IntegerModelCopyWith(
|
||||
IntegerModel value, $Res Function(IntegerModel) then) =
|
||||
_$IntegerModelCopyWithImpl<$Res, IntegerModel>;
|
||||
@useResult
|
||||
$Res call({int value});
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
class _$IntegerModelCopyWithImpl<$Res, $Val extends IntegerModel>
|
||||
implements $IntegerModelCopyWith<$Res> {
|
||||
_$IntegerModelCopyWithImpl(this._value, this._then);
|
||||
|
||||
// ignore: unused_field
|
||||
final $Val _value;
|
||||
// ignore: unused_field
|
||||
final $Res Function($Val) _then;
|
||||
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
$Res call({
|
||||
Object? value = null,
|
||||
}) {
|
||||
return _then(_value.copyWith(
|
||||
value: null == value
|
||||
? _value.value
|
||||
: value // ignore: cast_nullable_to_non_nullable
|
||||
as int,
|
||||
) as $Val);
|
||||
}
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract class _$$_IntegerModelCopyWith<$Res>
|
||||
implements $IntegerModelCopyWith<$Res> {
|
||||
factory _$$_IntegerModelCopyWith(
|
||||
_$_IntegerModel value, $Res Function(_$_IntegerModel) then) =
|
||||
__$$_IntegerModelCopyWithImpl<$Res>;
|
||||
@override
|
||||
@useResult
|
||||
$Res call({int value});
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
class __$$_IntegerModelCopyWithImpl<$Res>
|
||||
extends _$IntegerModelCopyWithImpl<$Res, _$_IntegerModel>
|
||||
implements _$$_IntegerModelCopyWith<$Res> {
|
||||
__$$_IntegerModelCopyWithImpl(
|
||||
_$_IntegerModel _value, $Res Function(_$_IntegerModel) _then)
|
||||
: super(_value, _then);
|
||||
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
$Res call({
|
||||
Object? value = null,
|
||||
}) {
|
||||
return _then(_$_IntegerModel(
|
||||
value: null == value
|
||||
? _value.value
|
||||
: value // ignore: cast_nullable_to_non_nullable
|
||||
as int,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
@JsonSerializable()
|
||||
class _$_IntegerModel implements _IntegerModel {
|
||||
const _$_IntegerModel({required this.value});
|
||||
|
||||
factory _$_IntegerModel.fromJson(Map<String, dynamic> json) =>
|
||||
_$$_IntegerModelFromJson(json);
|
||||
|
||||
@override
|
||||
final int value;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'IntegerModel(value: $value)';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(dynamic other) {
|
||||
return identical(this, other) ||
|
||||
(other.runtimeType == runtimeType &&
|
||||
other is _$_IntegerModel &&
|
||||
(identical(other.value, value) || other.value == value));
|
||||
}
|
||||
|
||||
@JsonKey(ignore: true)
|
||||
@override
|
||||
int get hashCode => Object.hash(runtimeType, value);
|
||||
|
||||
@JsonKey(ignore: true)
|
||||
@override
|
||||
@pragma('vm:prefer-inline')
|
||||
_$$_IntegerModelCopyWith<_$_IntegerModel> get copyWith =>
|
||||
__$$_IntegerModelCopyWithImpl<_$_IntegerModel>(this, _$identity);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return _$$_IntegerModelToJson(
|
||||
this,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class _IntegerModel implements IntegerModel {
|
||||
const factory _IntegerModel({required final int value}) = _$_IntegerModel;
|
||||
|
||||
factory _IntegerModel.fromJson(Map<String, dynamic> json) =
|
||||
_$_IntegerModel.fromJson;
|
||||
|
||||
@override
|
||||
int get value;
|
||||
@override
|
||||
@JsonKey(ignore: true)
|
||||
_$$_IntegerModelCopyWith<_$_IntegerModel> get copyWith =>
|
||||
throw _privateConstructorUsedError;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'integer_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
_$_IntegerModel _$$_IntegerModelFromJson(Map<String, dynamic> json) =>
|
||||
_$_IntegerModel(
|
||||
value: json['value'] as int,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$$_IntegerModelToJson(_$_IntegerModel instance) =>
|
||||
<String, dynamic>{
|
||||
'value': instance.value,
|
||||
};
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
import 'package:starting_template/domain/data_sources/local/counter_data_source.dart';
|
||||
import 'package:starting_template/domain/entities/integer.dart';
|
||||
import 'package:starting_template/domain/repositories/counter_repository.dart';
|
||||
import 'package:wyatt_architecture/wyatt_architecture.dart';
|
||||
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
|
||||
|
||||
class CounterRepositoryImpl extends CounterRepository {
|
||||
CounterRepositoryImpl({
|
||||
required CounterDataSource counterDataSource,
|
||||
}) : _counterDataSource = counterDataSource;
|
||||
|
||||
final CounterDataSource _counterDataSource;
|
||||
|
||||
@override
|
||||
FutureOrResult<Integer> decrement({Integer by = const Integer(1)}) =>
|
||||
Result.tryCatchAsync<Integer, AppException, AppException>(
|
||||
() async => _counterDataSource.decrement(by),
|
||||
(error) => error,
|
||||
);
|
||||
|
||||
@override
|
||||
FutureOrResult<Integer> increment({Integer by = const Integer(1)}) =>
|
||||
Result.tryCatchAsync<Integer, AppException, AppException>(
|
||||
() async => _counterDataSource.increment(by),
|
||||
(error) => error,
|
||||
);
|
||||
|
||||
@override
|
||||
FutureOrResult<Integer> getCurrent() =>
|
||||
Result.tryCatchAsync<Integer, AppException, AppException>(
|
||||
() async => _counterDataSource.getCurrent(),
|
||||
(error) => error,
|
||||
);
|
||||
|
||||
@override
|
||||
FutureOrResult<Integer> reset() =>
|
||||
Result.tryCatchAsync<Integer, AppException, AppException>(
|
||||
() async => _counterDataSource.reset(),
|
||||
(error) => error,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user