Skip to content

Commit

Permalink
Aula 02 | DartWeek 4ª Edição 🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecastrosales authored Sep 15, 2021
2 parents a37eb1c + 87a1a01 commit 508e80f
Show file tree
Hide file tree
Showing 17 changed files with 344 additions and 3 deletions.
22 changes: 22 additions & 0 deletions lib/application/bindings/application_bindings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:get/get.dart';

import 'package:app_filmes/repositories/login/login_repository_impl.dart';
import 'package:app_filmes/repositories/login/login_repository.dart';
import 'package:app_filmes/services/login/login_service.dart';
import 'package:app_filmes/services/login/login_service_impl.dart';

class ApplicationBindings implements Bindings {
@override
void dependencies() {
Get.lazyPut<LoginRepository>(
() => LoginRepositoryImpl(),
fenix: true,
);
Get.lazyPut<LoginService>(
() => LoginServiceImpl(
loginRepository: Get.find(),
),
fenix: true,
);
}
}
23 changes: 23 additions & 0 deletions lib/application/ui/loader/loader_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:flutter/material.dart';

import 'package:get/get.dart';

mixin LoaderMixin on GetxController {
void loaderListener(RxBool loaderRx) {
ever<bool>(
loaderRx,
(loading) async {
if (loading) {
await Get.dialog(
const Center(
child: CircularProgressIndicator(),
),
barrierDismissible: false,
);
} else {
Get.back();
}
},
);
}
}
58 changes: 58 additions & 0 deletions lib/application/ui/messages/messages_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:flutter/material.dart';

import 'package:get/get.dart';

mixin MessagesMixin on GetxController {
void messageListener(Rxn<MessageModel> message) {
ever<MessageModel?>(
message,
(model) {
if (model != null) {
Get.snackbar(
model.title,
model.message,
backgroundColor: model.type.color(),
);
}
},
);
}
}

class MessageModel {
final String title;
final String message;
final MessageType type;

MessageModel({
required this.title,
required this.message,
required this.type,
});

MessageModel.error({
required this.title,
required this.message,
}) : type = MessageType.error;

MessageModel.info({
required this.title,
required this.message,
}) : type = MessageType.info;
}

enum MessageType {
error,
info,
}

extension MessageTypeExtension on MessageType {
Color color() {
switch (this) {
case MessageType.error:
return Colors.red[600] ?? Colors.red;
case MessageType.info:
return Colors.blue[200] ?? Colors.blue;
}
}
}
4 changes: 4 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:get/get.dart';

import 'application/bindings/application_bindings.dart';
import 'modules/login/login_module.dart';
import 'modules/splash/splash_module.dart';

Future<void> main() async {
Expand All @@ -26,8 +28,10 @@ class MyApp extends StatelessWidget {
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialBinding: ApplicationBindings(),
getPages: [
...SplashModule().routers,
...LoginModule().routers,
],
);
}
Expand Down
14 changes: 14 additions & 0 deletions lib/modules/login/login_bindings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:get/get.dart';

import 'login_controller.dart';

class LoginBindings implements Bindings {
@override
void dependencies() {
Get.lazyPut(
() => LoginController(
loginService: Get.find(),
),
);
}
}
44 changes: 44 additions & 0 deletions lib/modules/login/login_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:get/get.dart';

import 'package:app_filmes/services/login/login_service.dart';
import 'package:app_filmes/application/ui/loader/loader_mixin.dart';
import 'package:app_filmes/application/ui/messages/messages_mixin.dart';

class LoginController extends GetxController with LoaderMixin, MessagesMixin {
final LoginService _loginService;
final loading = false.obs;
final message = Rxn<MessageModel>();

LoginController({
required LoginService loginService,
}) : _loginService = loginService;

@override
void onInit() {
super.onInit();
loaderListener(loading);
messageListener(message);
}

Future<void> login() async {
try {
loading(true);
await _loginService.login();
loading(false);
message(
MessageModel.info(
title: 'Success',
message: 'Login with Success',
),
);
} catch (e, s) {
print(e);
print(s);
loading(false);
MessageModel.error(
title: 'Error',
message: 'Login with Error',
);
}
}
}
16 changes: 16 additions & 0 deletions lib/modules/login/login_module.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:get/get.dart';

import 'package:app_filmes/application/modules/module.dart';
import 'login_bindings.dart';
import 'login_page.dart';

class LoginModule implements Module {
@override
List<GetPage> routers = [
GetPage(
name: '/login',
page: () => const LoginPage(),
binding: LoginBindings(),
)
];
}
49 changes: 49 additions & 0 deletions lib/modules/login/login_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:flutter/material.dart';

import 'package:flutter_signin_button/flutter_signin_button.dart';
import 'package:get/get.dart';

import 'login_controller.dart';

class LoginPage extends GetView<LoginController> {
const LoginPage({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
alignment: Alignment.topCenter,
children: [
Image.asset(
'assets/images/background.png',
width: Get.width,
height: Get.height,
fit: BoxFit.cover,
),
Container(color: Colors.black45),
Padding(
padding: const EdgeInsets.only(top: 80),
child: Column(
children: [
Image.asset('assets/images/logo.png'),
const SizedBox(height: 50),
SizedBox(
width: Get.width * .9,
height: 42,
child: SignInButton(
Buttons.Google,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(100),
),
text: 'Entrar com o Google',
onPressed: () => controller.login(),
),
),
],
),
),
],
),
);
}
}
12 changes: 12 additions & 0 deletions lib/modules/splash/splash_bindings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:get/get.dart';

import 'splash_controller.dart';

class SplashBindings implements Bindings {
@override
void dependencies() {
Get.put(
SplashController(),
);
}
}
8 changes: 7 additions & 1 deletion lib/modules/splash/splash_controller.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import 'package:get/get.dart';

class SplashController extends GetxController {}
class SplashController extends GetxController {
@override
void onReady() {
super.onReady();
Get.offAllNamed('/login');
}
}
2 changes: 2 additions & 0 deletions lib/modules/splash/splash_module.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:get/get.dart';

import 'package:app_filmes/application/modules/module.dart';
import 'splash_bindings.dart';
import 'splash_page.dart';

class SplashModule implements Module {
Expand All @@ -9,6 +10,7 @@ class SplashModule implements Module {
GetPage(
name: '/',
page: () => const SplashPage(),
binding: SplashBindings(),
),
];
}
5 changes: 5 additions & 0 deletions lib/repositories/login/login_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:firebase_auth/firebase_auth.dart';

abstract class LoginRepository {
Future<UserCredential> login();
}
22 changes: 22 additions & 0 deletions lib/repositories/login/login_repository_impl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';

import 'login_repository.dart';

class LoginRepositoryImpl implements LoginRepository {
@override
// ignore: override_on_non_overriding_member
Future<UserCredential> login() async {
final googleUser = await GoogleSignIn().signIn();
final googleAuth = await googleUser?.authentication;

if (googleAuth != null) {
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
return FirebaseAuth.instance.signInWithCredential(credential);
}
throw Exception('Erro ao realizar login com Google');
}
}
5 changes: 5 additions & 0 deletions lib/services/login/login_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:firebase_auth/firebase_auth.dart';

abstract class LoginService {
Future<UserCredential> login();
}
15 changes: 15 additions & 0 deletions lib/services/login/login_service_impl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:firebase_auth/firebase_auth.dart';

import 'package:app_filmes/repositories/login/login_repository.dart';
import 'login_service.dart';

class LoginServiceImpl implements LoginService {
final LoginRepository _loginRepository;

LoginServiceImpl({
required LoginRepository loginRepository,
}) : _loginRepository = loginRepository;

@override
Future<UserCredential> login() => _loginRepository.login();
}
Loading

0 comments on commit 508e80f

Please sign in to comment.