Skip to content

Commit 7183a57

Browse files
authored
Merge pull request #73 from Onix-Systems/feat/update_26
Feat/update 26
2 parents 999adec + cce3028 commit 7183a57

File tree

27 files changed

+253
-96
lines changed

27 files changed

+253
-96
lines changed

bricks/flutter_clean_base/hooks/post_gen.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const flavorizrInjectKey = '#{flavorizer_injection_config}';
1111
void run(HookContext context) async {
1212
name = context.vars['project_name'].toString().toSnakeCase;
1313

14+
1415
if (!context.vars['platforms'].contains('android')) {
1516
await Process.run('rm', ['-rf', '$name/android']);
1617
}
@@ -400,10 +401,10 @@ Future<void> injectFlavors(HookContext context) async {
400401
lines.add(' flavors:');
401402
for (String flavor in flavors) {
402403
final packageSuffix = flavor.toLowerCase() == 'prod' ? '' : '.$flavor';
403-
final nameSuffix = flavor.toLowerCase() == 'prod' ? '' : ' $flavor';
404+
final nameSuffix = flavor.toLowerCase() == 'prod' ? '' : ' ${flavor.toTitleCase}';
404405
lines.add(' $flavor:');
405406
lines.add(' app:');
406-
lines.add(' name: "$name$nameSuffix"');
407+
lines.add(' name: "${name.toTitleCase}$nameSuffix"');
407408
lines.add('');
408409
if(isAndroidEnabled){
409410
lines.add(' android:');
@@ -673,4 +674,6 @@ extension Case on String {
673674
String get toCamelCase => ReCase(this).camelCase;
674675

675676
String get toPascalCase => ReCase(this).pascalCase;
677+
678+
String get toTitleCase => ReCase(this).titleCase;
676679
}

lib/core/arch/bloc/base_bloc.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ abstract class BaseBloc<Event, State, SR> extends Bloc<Event, State>
1515
Stream<Failure> get failureStream => _errorStreamController.stream;
1616
Stream<bool> get progressStream => _progressStreamController.stream;
1717

18+
1819
BaseBloc(super.initialState) {
1920
_errorStreamController = StreamController<Failure>.broadcast();
2021
_progressStreamController = StreamController<bool>.broadcast();

lib/core/arch/data/local/prefs/base_preferences.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ class BasePreferences {
66
T result;
77

88
switch (defaultValue.runtimeType) {
9-
case String:
9+
case const (String):
1010
final value = prefs.getString(key) as T?;
1111
result = value ?? defaultValue;
1212
break;
13-
case bool:
13+
case const (bool):
1414
final value = prefs.getBool(key) as T?;
1515
result = value ?? defaultValue;
1616
break;
17-
case double:
17+
case const (double):
1818
final value = prefs.getDouble(key) as T?;
1919
result = value ?? defaultValue;
2020
break;
21-
case int:
21+
case const (int):
2222
final value = prefs.getInt(key) as T?;
2323
result = value ?? defaultValue;
2424
break;
@@ -31,16 +31,16 @@ class BasePreferences {
3131
Future<void> put<T>(String key, T value) async {
3232
final prefs = await SharedPreferences.getInstance();
3333
switch (value.runtimeType) {
34-
case String:
34+
case const (String):
3535
await prefs.setString(key, value as String);
3636
break;
37-
case bool:
37+
case const (bool):
3838
await prefs.setBool(key, value as bool);
3939
break;
40-
case double:
40+
case const (double):
4141
await prefs.setDouble(key, value as double);
4242
break;
43-
case int:
43+
case const (int):
4444
await prefs.setInt(key, value as int);
4545
break;
4646
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
abstract class Failure {}
2+
3+
4+
5+

lib/domain/entity/config/config.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class Config with _$Config {
3434
@Default(ProjectRouter.goRouter) ProjectRouter router,
3535
@Default(ProjectLocalization.intl) ProjectLocalization localization,
3636
@Default(ProjectTheming.manual) ProjectTheming theming,
37-
@Default(AppConsts.defaultSigningVars) List<String> signingVars,
37+
@Default(AppConsts.defaultSigningVars)
38+
List<String> signingVars,
3839
@Default({}) Set<Screen> screens,
3940
// ignore: invalid_annotation_target
4041
@JsonKey(includeFromJson: false, includeToJson: false)

lib/domain/entity/config/fingerprint.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ class Fingerprint with _$Fingerprint {
99
required FingerprintType type,
1010
required String value,
1111
}) = _Fingerprint;
12+
1213
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class JsonClassVariable {
2+
final String dartType;
3+
final String name;
4+
final bool nullable;
5+
6+
JsonClassVariable({
7+
required this.dartType,
8+
required this.name,
9+
this.nullable = true,
10+
});
11+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import 'package:onix_flutter_bricks/domain/entity/data_component/json_class_variable.dart';
2+
import 'package:onix_flutter_bricks/domain/service/base/class_builder/class_builder.dart';
3+
import 'package:onix_flutter_bricks/util/extension/codelines_extension.dart';
4+
import 'package:recase/recase.dart';
5+
6+
class JsonClassBuilder extends ClassBuilder {
7+
final String _className;
8+
final String _classNameSuffix;
9+
10+
Iterable<String> _imports = List.empty();
11+
12+
Iterable<String> _variableDeclarations = List.empty();
13+
Iterable<String> _baseConstructorProperties = List.empty();
14+
15+
bool _withToJson = true;
16+
17+
set withToJson(bool withToJson) => _withToJson = withToJson;
18+
19+
set imports(Iterable<String> imports) => _imports = imports;
20+
21+
set baseConstructorProperties(Iterable<String> baseConstructorProperties) =>
22+
_baseConstructorProperties = baseConstructorProperties;
23+
24+
set variableDeclarations(Iterable<String> variableDeclarations) =>
25+
_variableDeclarations = variableDeclarations;
26+
27+
JsonClassBuilder({
28+
required String className,
29+
String classNameSuffix = '',
30+
}) : _className = className,
31+
_classNameSuffix = classNameSuffix;
32+
33+
@override
34+
String build() {
35+
final importSuffix =
36+
_classNameSuffix.isNotEmpty ? '_${_classNameSuffix.snakeCase}' : '';
37+
final classPartImport = '${_className.snakeCase}$importSuffix';
38+
final classFullName =
39+
'${_className.pascalCase}${_classNameSuffix.pascalCase}';
40+
lines.add('import \'package:json_annotation/json_annotation.dart\';');
41+
lines.addAll(_imports);
42+
lines.addNewLine();
43+
lines.add('part \'$classPartImport.g.dart\';');
44+
lines.addNewLine();
45+
lines.add('@JsonSerializable()');
46+
lines.add('class $classFullName {');
47+
lines.addNewLine();
48+
lines.addAll(_variableDeclarations);
49+
lines.addNewLine();
50+
lines.add('const $classFullName({');
51+
lines.addAll(_baseConstructorProperties);
52+
lines.add('});');
53+
lines.addNewLine();
54+
lines.add(
55+
'factory $classFullName.fromJson(Map<String, dynamic> json) => _\$${classFullName}FromJson(json);');
56+
57+
if (_withToJson) {
58+
lines.addNewLine();
59+
lines.add(
60+
'Map<String, dynamic> toJson() => _\$${classFullName}ToJson(this);');
61+
}
62+
lines.add('}');
63+
lines.addNewLine();
64+
return super.build();
65+
}
66+
67+
static Iterable<String> variablesFromJsonVariable(
68+
Iterable<JsonClassVariable> input) {
69+
return input.map(
70+
(e) {
71+
final variableDeclaration = List<String>.empty(growable: true);
72+
variableDeclaration.add('@JsonKey(includeIfNull: false)');
73+
variableDeclaration.addNewLine();
74+
variableDeclaration
75+
.add('final ${e.dartType}${e.nullable ? '?' : ''} ${e.name};');
76+
return variableDeclaration.join('\n');
77+
},
78+
);
79+
}
80+
81+
static Iterable<String> constructorPropertiesFromJsonVariable(
82+
Iterable<JsonClassVariable> input) {
83+
return input.map(
84+
(e) {
85+
final prefix = e.nullable ? '' : 'required';
86+
return '$prefix this.${e.name},';
87+
},
88+
);
89+
}
90+
}

lib/domain/service/file_generator_service/data_component_generators/component_class_generator.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class ComponentClassGenerator implements BaseGenerationService<bool> {
2222
if (params is! DataComponentParams) {
2323
return false;
2424
}
25-
final codeLines = List<String>.empty(growable: true);
2625

2726
final sourceName = params.dataComponent.sourceName;
2827
final name = params.dataComponent.name;

lib/domain/service/file_generator_service/data_component_generators/request_generator.dart

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import 'dart:io';
33

44
import 'package:collection/collection.dart';
55
import 'package:onix_flutter_bricks/domain/entity/data_component/data_component.dart';
6+
import 'package:onix_flutter_bricks/domain/entity/data_component/json_class_variable.dart';
67
import 'package:onix_flutter_bricks/domain/repository/data_component_repository.dart';
78
import 'package:onix_flutter_bricks/domain/service/base/base_generation_service.dart';
8-
import 'package:onix_flutter_bricks/domain/service/base/class_builder/freezed_class_builder.dart';
9+
import 'package:onix_flutter_bricks/domain/service/base/class_builder/json_class_builder.dart';
910
import 'package:onix_flutter_bricks/domain/service/base/params/base_generation_params.dart';
1011
import 'package:onix_flutter_bricks/domain/service/file_generator_service/data_component_generators/params/data_component_params.dart';
1112
import 'package:onix_flutter_bricks/util/type_matcher.dart';
@@ -29,16 +30,25 @@ class RequestGenerator implements BaseGenerationService<bool> {
2930
params.dataComponent,
3031
);
3132

33+
final properties = _getProperties(params.dataComponent);
34+
35+
final variableDeclarations = JsonClassBuilder.variablesFromJsonVariable(
36+
properties,
37+
);
3238
final constructorProperties =
33-
_getConstructorProperties(params.dataComponent);
39+
JsonClassBuilder.constructorPropertiesFromJsonVariable(
40+
properties,
41+
);
3442

35-
final freezedClass = FreezedClassBuilder(
43+
final jsonClass = JsonClassBuilder(
3644
className: name,
3745
classNameSuffix: 'request',
3846
)
47+
..withToJson = true
3948
..imports = imports
49+
..variableDeclarations = variableDeclarations
4050
..baseConstructorProperties = constructorProperties;
41-
final fileContent = freezedClass.build();
51+
final fileContent = jsonClass.build();
4252

4353
final path = await Directory(
4454
'${params.projectPath}/${params.projectName}/lib/data/model/remote/${sourceName.isNotEmpty ? '${sourceName.snakeCase}/' : ''}${name.snakeCase}')
@@ -76,12 +86,17 @@ class RequestGenerator implements BaseGenerationService<bool> {
7686
return imports;
7787
}
7888

79-
Iterable<String> _getConstructorProperties(DataComponent dataComponent) {
80-
final constructorProperties = dataComponent.properties.map(
89+
Iterable<JsonClassVariable> _getProperties(
90+
DataComponent dataComponent,
91+
) {
92+
final properties = dataComponent.properties.map(
8193
(e) {
8294
String type = e.type;
83-
if (!TypeMatcher.isStandardType(TypeMatcher.getDartType(type)) &&
84-
!type.contains('dynamic')) {
95+
final isDynamic = type.contains('dynamic');
96+
final isStandardType = TypeMatcher.isStandardType(
97+
TypeMatcher.getDartType(type),
98+
);
99+
if (!isStandardType && !isDynamic) {
85100
final import = dataComponent.imports.firstWhereOrNull(
86101
(element) => element.pascalCase == type.pascalCase);
87102
final isEnum =
@@ -96,12 +111,16 @@ class RequestGenerator implements BaseGenerationService<bool> {
96111
if (e.isList) {
97112
type = 'List<${TypeMatcher.getDartType(type)}>';
98113
}
99-
final prefix =
100-
e.nullable ? '@JsonKey(includeIfNull: false)' : 'required';
114+
101115
final dartType = TypeMatcher.getDartType(type);
102-
return '$prefix $dartType${e.nullable ? '?' : ''} ${e.name},';
116+
117+
return JsonClassVariable(
118+
dartType: dartType,
119+
name: e.name,
120+
nullable: e.nullable,
121+
);
103122
},
104123
);
105-
return constructorProperties;
124+
return properties;
106125
}
107126
}

0 commit comments

Comments
 (0)