Skip to content

Commit

Permalink
feat: init to work with abstractions and complexities for ensure and …
Browse files Browse the repository at this point in the history
…improve flexibility
  • Loading branch information
felipecastrosales committed May 8, 2024
1 parent 4abde1e commit b4ef9bd
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 60 deletions.
36 changes: 18 additions & 18 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ include: package:flutter_lints/flutter.yaml
# - lib/app/core/l10n/localizations/** # Generated file
# - lib/data/services/firebase/firebase_set_defaults.dart # To use dynamic type (because really need it)

# linter:
# rules:
# - always_declare_return_types
# - always_use_package_imports
# - avoid_dynamic_calls
# - avoid_relative_lib_imports
# - implementation_imports
# - require_trailing_commas
# - sized_box_for_whitespace
# - sized_box_shrink_expand
# - sort_child_properties_last
# - sort_constructors_first
# - prefer_const_constructors
# - prefer_const_constructors_in_immutables
# - prefer_const_literals_to_create_immutables
# - prefer_const_declarations
# - prefer_final_fields
# - prefer_single_quotes
linter:
rules:
- always_declare_return_types
- always_use_package_imports
- avoid_dynamic_calls
- avoid_relative_lib_imports
- implementation_imports
- require_trailing_commas
- sized_box_for_whitespace
- sized_box_shrink_expand
- sort_child_properties_last
- sort_constructors_first
- prefer_const_constructors
- prefer_const_constructors_in_immutables
- prefer_const_literals_to_create_immutables
- prefer_const_declarations
- prefer_final_fields
- prefer_single_quotes

# dart_code_metrics:
# metrics:
Expand Down
1 change: 1 addition & 0 deletions lib/app/features/contact/data/data.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'repositories/repositories.dart';
export 'results/results.dart';
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:developer';

import 'package:dio/dio.dart';
import 'package:site/app/core/result/result.dart';

import 'package:site/app/features/contact/contact.dart';
import 'package:site/data/constants/constants_api.dart';
Expand All @@ -13,18 +14,21 @@ class ContactRepositoryImpl implements ContactRepository {
final Dio _httpClient;

@override
Future sendMail({
required Contact contact,
Future<Result<ContactFailedResult, ContactAnswer>> sendMail({
required ContactModel contact,
}) async {
try {
final response = await _httpClient.post(
ConstantsAPI.apiSendMail,
data: Contact.toJson(contact),
data: ContactUser.toJson(contact),
);

return response.data;
// return response.data;
return Success(ContactAnswer.fromJson(response.data));
} catch (e, s) {
log('Error: $e', stackTrace: s);

return const Failure(ContactFailedResult.unknown);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum ContactFailedResult {
tooManyRequests,
unauthorized,
unknown,
}
1 change: 1 addition & 0 deletions lib/app/features/contact/data/results/results.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'contact_failed_result.dart';
23 changes: 0 additions & 23 deletions lib/app/features/contact/domain/models/contact.dart

This file was deleted.

26 changes: 26 additions & 0 deletions lib/app/features/contact/domain/models/contact_answer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:site/app/features/contact/domain/models/contact_model.dart';

class ContactAnswer extends ContactModel {
factory ContactAnswer.fromJson(Map<String, dynamic> json) {
return ContactAnswer(
name: json['sender_name'],
email: json['source_email'],
message: json['template_body'],
subject: json['template_subject'],
response: json['response'],
status: json['status'],
);
}

ContactAnswer({
required super.name,
required super.email,
required super.message,
required super.subject,
required this.response,
required this.status,
});

final String response;
final String status;
}
10 changes: 10 additions & 0 deletions lib/app/features/contact/domain/models/contact_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
abstract class ContactModel {
ContactModel({
required this.name,
required this.email,
required this.message,
required this.subject,
});

final String name, email, message, subject;
}
24 changes: 24 additions & 0 deletions lib/app/features/contact/domain/models/contact_user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'dart:convert';

import 'package:site/app/features/contact/contact.dart';

class ContactUser extends ContactModel {
ContactUser({
required super.name,
required super.email,
required super.message,
required super.subject,
});

static Map<String, dynamic> toJson(ContactModel contact) {
return {
'sender_name': contact.name,
'source_email': contact.email,
'template_subject': contact.subject,
'template_body': contact.message,
};
}

static String toJsonString(ContactModel contact) =>
jsonEncode(toJson(contact));
}
4 changes: 3 additions & 1 deletion lib/app/features/contact/domain/models/models.dart
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export 'contact.dart';
export 'contact_answer.dart';
export 'contact_user.dart';
export 'contact_model.dart';
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'package:site/app/features/contact/domain/models/contact.dart';
import 'package:site/app/core/result/result.dart';
import 'package:site/app/features/contact/contact.dart';

abstract class ContactRepository {
Future sendMail({required Contact contact});
Future<Result<ContactFailedResult, ContactAnswer>> sendMail({
required ContactModel contact,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ContactController extends ChangeNotifier {
final ContactRepositoryImpl _contactRepository;

Future<void> sendMail({
required Contact contact,
required ContactModel contact,
}) {
return _contactRepository.sendMail(
contact: contact,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ContactCubit extends Cubit<ContactState> {
final ContactRepositoryImpl _contactRepository;

Future<void> sendMail({
required Contact contact,
required ContactModel contact,
}) async {
emit(const ContactLoading());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class ContactSuccess extends ContactState {
required this.message,
});

final Contact contact;
final ContactModel contact;
final String message;

@override
Expand All @@ -41,7 +41,7 @@ final class ContactError extends ContactState {
required this.message,
});

final Contact contact;
final ContactModel contact;
final String message;

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import 'package:flutter/material.dart';

import 'package:site/app/core/injections/injections.dart';
import 'package:site/app/core/l10n/l10n.dart';
import 'package:site/app/features/contact/domain/models/contact.dart';
import 'package:site/app/features/contact/presentation/controller/contact_controller.dart';
import 'package:site/app/features/contact/presentation/widgets/widgets.dart';
import 'package:site/app/features/contact/contact.dart';
import 'package:site/app/utils/contact_validators.dart';
import 'package:site/app/widgets/buttons/buttons.dart';
import 'package:site/app/widgets/dividers/dividers.dart';
import 'package:site/app/features/contact/data/repositories/contact_repository_impl.dart';

class CustomForm extends StatelessWidget {
CustomForm({
Expand Down Expand Up @@ -83,7 +80,7 @@ class CustomForm extends StatelessWidget {
onPressed: onPressed,
onLongPress: () {
_contactController?.sendMail(
contact: Contact(
contact: ContactUser(
name: 'felipe sales',
email: '[email protected]',
message: 'teste de mensagem',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ContactWidget extends StatelessWidget {
}

_contactController?.sendMail(
contact: Contact(
contact: ContactUser(
name: nameController.text,
email: emailController.text.trim(),
message: messageController.text,
Expand Down
4 changes: 2 additions & 2 deletions test/utils/fixtures/app_fixtures.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:site/app/features/contact/domain/models/contact.dart';
import 'package:site/app/features/contact/contact.dart';

class AppFixtures {
final tContact = Contact(
final tContact = ContactUser(
name: 'felipecastrosales',
email: '[email protected]',
message: 'Hello, World!',
Expand Down

0 comments on commit b4ef9bd

Please sign in to comment.