-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from fga-eps-mds/fix#58/adiciona-command-ao-login
FEAT: adiciona o command ao login (#58)
- Loading branch information
Showing
5 changed files
with
316 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,52 @@ | ||
import 'package:aranduapp/core/log/Log.dart'; | ||
import 'package:aranduapp/core/state/command.dart'; | ||
import 'package:aranduapp/ui/navbar/view/navBarView.dart'; | ||
import 'package:async/async.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:local_auth/local_auth.dart'; | ||
import 'package:aranduapp/ui/login/service/LoginService.dart'; | ||
import 'package:aranduapp/ui/login/model/LoginRequest.dart'; | ||
|
||
class LoginViewModel extends ChangeNotifier { | ||
final BuildContext context; | ||
|
||
bool isLoading; | ||
late Command0<void> loginCommand; | ||
late Command0<void> validadeTokenCommand; | ||
|
||
final GlobalKey<FormState> formKey; | ||
final TextEditingController emailController; | ||
final TextEditingController passwordController; | ||
|
||
LoginViewModel(this.context) | ||
: isLoading = false, | ||
formKey = GlobalKey<FormState>(), | ||
LoginViewModel() | ||
: formKey = GlobalKey<FormState>(), | ||
emailController = TextEditingController(), | ||
passwordController = TextEditingController(); | ||
passwordController = TextEditingController() { | ||
|
||
Future<void> loginWithEmailAndPassword() async { | ||
// TODO use mutex to make this | ||
if (isLoading) { | ||
return; | ||
} | ||
|
||
try { | ||
isLoading = true; | ||
super.notifyListeners(); | ||
loginCommand = Command0<void>(loginWithEmailAndPassword); | ||
|
||
if (!formKey.currentState!.validate()) { | ||
throw Exception('Valores inválidos'); | ||
} | ||
validadeTokenCommand = Command0<void>(validateToken); | ||
validadeTokenCommand.execute(); | ||
} | ||
|
||
await LoginService.login( | ||
LoginRequest(emailController.text, passwordController.text)); | ||
} catch (e) { | ||
rethrow; | ||
} finally { | ||
isLoading = false; | ||
notifyListeners(); | ||
Future<Result<void>> loginWithEmailAndPassword() async { | ||
if (!formKey.currentState!.validate()) { | ||
return Result.error(Exception('Valores inválidos')); | ||
} | ||
|
||
await LoginService.login( | ||
LoginRequest(emailController.text, passwordController.text)); | ||
|
||
return Result.value(null); | ||
} | ||
|
||
Future<void> validateToken() async { | ||
Future<Result<void>> validateToken() async { | ||
await LoginService.validateToken(); | ||
|
||
return Result.value(null); | ||
} | ||
|
||
Future<bool> loginWithDeviceAuth() async { | ||
Log.d('init loginWithDeviceAuth'); | ||
return await LocalAuthentication() | ||
.authenticate(localizedReason: 'Toque com o dedo no sensor para logar'); | ||
} | ||
|
||
void goToHome() { | ||
try { | ||
if (context.mounted) { | ||
Navigator.of(context).pushReplacement( | ||
MaterialPageRoute( | ||
builder: (context) => const NavbarView(), | ||
), | ||
); | ||
} | ||
} catch (e) { | ||
Log.e(e); | ||
rethrow; | ||
} | ||
} | ||
} |
Oops, something went wrong.