From 261f5132b7e653a3494696356cd2cd32d94238cc Mon Sep 17 00:00:00 2001 From: wjrcode Date: Tue, 18 Oct 2022 00:10:48 -0300 Subject: [PATCH] login --- lib/Controller/LoginController.dart | 12 ------ lib/Controller/UsuarioController.dart | 35 +++++++++++++++--- lib/Model/API/APIModel.dart | 26 ++++++------- lib/Model/Usuario/UsuarioModel.dart | 13 ++++--- lib/Repository/AtividadeRepository.dart | 28 ++++++++------ lib/Repository/HabitoRepository.dart | 21 ++++++----- lib/Repository/ItemRepository.dart | 8 ++-- lib/Repository/LembreteRepository.dart | 28 ++++++++------ lib/Repository/ListaRepository.dart | 17 +++++---- lib/Repository/LoginRepository.dart | 18 +++++---- lib/Repository/ProjetoRepository.dart | 30 ++++++++------- lib/Repository/TarefaRepository.dart | 30 ++++++++------- lib/Repository/UsuarioRepository.dart | 21 ++++++++++- .../dialogs/login/entrarDialog.dart | 1 + .../dialogs/login/usuarioCadastroDialog.dart | 37 ++++++++++++++++--- 15 files changed, 200 insertions(+), 125 deletions(-) diff --git a/lib/Controller/LoginController.dart b/lib/Controller/LoginController.dart index 8c61597..fa70f5e 100644 --- a/lib/Controller/LoginController.dart +++ b/lib/Controller/LoginController.dart @@ -104,16 +104,4 @@ class LoginController extends Base { prefs.setString('UsuarioApelido', ''); prefs.setString('token', ''); } - - Future getSenhaUsuarioLogado() async { - SharedPreferences prefs = await SharedPreferences.getInstance(); - - String? chaveCentralizador = prefs.getString('EmpresaChaveCentralizador'); - - LoginModel? login = await getLogin(); - - String senha = login!.senha.toString(); - - return '$senha'; - } } diff --git a/lib/Controller/UsuarioController.dart b/lib/Controller/UsuarioController.dart index 3902aa5..ad00def 100644 --- a/lib/Controller/UsuarioController.dart +++ b/lib/Controller/UsuarioController.dart @@ -3,6 +3,7 @@ import 'package:organizei/Model/Usuario/UsuarioModel.dart'; import 'package:organizei/Controller/Base/Base.dart'; import 'package:organizei/Repository/UsuarioRepository.dart'; import 'package:organizei/services/persistencia/login.configuracoes.dart'; +import 'package:shared_preferences/shared_preferences.dart'; class UsuarioController extends Base { UsuarioController(this.repository, this.context); @@ -13,21 +14,26 @@ class UsuarioController extends Base { var model = UsuarioModel(); var loginConfiguracoes = LoginConfiguracoes(); + usuarioId(int? value) => model.id = value; usuarioNome(String? value) => model.nome = value.toString(); usuarioApelido(String? value) => model.apelido = value.toString(); usuarioEmail(String? value) => model.email = value.toString(); usuarioSenha(String? value) => model.senha = value.toString(); + usuarioNovaSenha(String? value) => model.novaSenha = value.toString(); var controllerNome = TextEditingController(); var controllerApelido = TextEditingController(); var controllerEmail = TextEditingController(); var controllerSenha = TextEditingController(); + var controllerNovaSenha = TextEditingController(); Future saveUsuario() async { if (!formKey.currentState!.validate()) { return false; } + SharedPreferences prefs = await SharedPreferences.getInstance(); + formKey.currentState!.save(); try { @@ -57,14 +63,31 @@ class UsuarioController extends Base { } }); } else { - /*return await repository.updateProduto(model).then((value) { + return await repository.updateUsuario(model).then((value) async { ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text(value.msg), - backgroundColor: value.valido ? Colors.green : Colors.red, + elevation: 6.0, + behavior: SnackBarBehavior.floating, + shape: RoundedRectangleBorder( + side: const BorderSide(color: Colors.black, width: 3), + borderRadius: BorderRadius.circular(16), + ), + content: Text(value.msg!, + style: const TextStyle( + color: Colors.black, + fontWeight: FontWeight.w500, + )), + backgroundColor: value.valido! + ? const Color(0xFF74C198) + : const Color(0xFFEF7E69), )); - return value.valido; - });*/ - return false; + + if (value.valido! == true) { + prefs.setString('UsuarioApelido', model.apelido ?? ''); + return value.valido!; + } else { + return false; + } + }); } } catch (e) { print(e); diff --git a/lib/Model/API/APIModel.dart b/lib/Model/API/APIModel.dart index 38d0ce3..9aaacba 100644 --- a/lib/Model/API/APIModel.dart +++ b/lib/Model/API/APIModel.dart @@ -1,21 +1,17 @@ import 'package:shared_preferences/shared_preferences.dart'; -Future getToken() async { - SharedPreferences prefs = await SharedPreferences.getInstance(); - - String? token = prefs.getString('UsuarioToken'); - return token ?? ''; -} - class ApiModel { - static const ApiUrl = "http://10.0.0.91:4444"; - //static const ApiUrl = "http://192.168.100.74:4444"; - //static const ApiUrl = "http://192.168.100.82:4444"; + String ApiUrl = "http://192.168.100.74:4444"; + + Future> getHeaders() async { + SharedPreferences prefs = await SharedPreferences.getInstance(); - static final Future _token = getToken(); + String? token = prefs.getString('UsuarioToken'); - static Map headers = { - "Content-Type": "application/json", - "Authorization": "Bearer $_token" - }; + Map headers = { + "Content-Type": "application/json", + "Authorization": "Bearer " + token! + }; + return headers; + } } diff --git a/lib/Model/Usuario/UsuarioModel.dart b/lib/Model/Usuario/UsuarioModel.dart index 15014bd..5ce3b7e 100644 --- a/lib/Model/Usuario/UsuarioModel.dart +++ b/lib/Model/Usuario/UsuarioModel.dart @@ -6,6 +6,7 @@ class UsuarioModel { String? apelido; String? email; String? senha; + String? novaSenha; ResponseAPIModel? responseAPIModel; UsuarioModel({ @@ -14,6 +15,7 @@ class UsuarioModel { this.apelido, this.email, this.senha, + this.novaSenha, }); UsuarioModel.fromJson(Map json) { @@ -26,11 +28,12 @@ class UsuarioModel { Map toJson() { final Map data = new Map(); - data['id'] = this.id; - data['nome'] = this.nome; - data['apelido'] = this.apelido; - data['email'] = this.email; - data['senha'] = this.senha; + data['id'] = id; + data['nome'] = nome; + data['apelido'] = apelido; + data['email'] = email; + data['senha'] = senha; + data['novaSenha'] = novaSenha; return data; } diff --git a/lib/Repository/AtividadeRepository.dart b/lib/Repository/AtividadeRepository.dart index ad83472..e83ebe6 100644 --- a/lib/Repository/AtividadeRepository.dart +++ b/lib/Repository/AtividadeRepository.dart @@ -17,8 +17,10 @@ class AtividadeRepository { "prioridade": model.prioridade, }; - final response = await http.post(Uri.parse(ApiModel.ApiUrl + '/atividades'), - headers: ApiModel.headers, body: jsonEncode(json)); + final response = await http.post( + Uri.parse(ApiModel().ApiUrl + '/atividades'), + headers: await ApiModel().getHeaders(), + body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); } @@ -34,8 +36,8 @@ class AtividadeRepository { }; final response = await http.put( - Uri.parse(ApiModel.ApiUrl + '/atividades/' + model.id.toString()), - headers: ApiModel.headers, + Uri.parse(ApiModel().ApiUrl + '/atividades/' + model.id.toString()), + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); @@ -48,11 +50,11 @@ class AtividadeRepository { }; final response = await http.put( - Uri.parse(ApiModel.ApiUrl + + Uri.parse(ApiModel().ApiUrl + '/atividades/' + model.id.toString() + '/concluir'), - headers: ApiModel.headers, + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); @@ -60,17 +62,18 @@ class AtividadeRepository { Future excluirAtividade(AtividadeModel model) async { final response = await http.delete( - Uri.parse(ApiModel.ApiUrl + '/atividades/' + model.id.toString()), - headers: ApiModel.headers); + Uri.parse(ApiModel().ApiUrl + '/atividades/' + model.id.toString()), + headers: await ApiModel().getHeaders()); return ResponseAPIModel.fromJson(jsonDecode(response.body)); } Future> getAtividades() async { - Uri url = Uri.parse(ApiModel.ApiUrl + '/atividades'); + Uri url = Uri.parse(ApiModel().ApiUrl + '/atividades'); var _url = Uri.parse(url.toString()); - final response = await http.get(_url, headers: ApiModel.headers); + final response = + await http.get(_url, headers: await ApiModel().getHeaders()); Map jsonMap = jsonDecode(response.body); @@ -78,10 +81,11 @@ class AtividadeRepository { } Future> get() async { - Uri url = Uri.parse(ApiModel.ApiUrl + '/atividades'); + Uri url = Uri.parse(ApiModel().ApiUrl + '/atividades'); var _url = Uri.parse(url.toString()); - final response = await http.get(_url, headers: ApiModel.headers); + final response = + await http.get(_url, headers: await ApiModel().getHeaders()); Map jsonMap = jsonDecode(response.body); diff --git a/lib/Repository/HabitoRepository.dart b/lib/Repository/HabitoRepository.dart index 33b17b6..47516b9 100644 --- a/lib/Repository/HabitoRepository.dart +++ b/lib/Repository/HabitoRepository.dart @@ -16,8 +16,8 @@ class HabitoRepository { "dataFinal": model.dataFinal, }; - final response = await http.post(Uri.parse(ApiModel.ApiUrl + '/habitos'), - headers: ApiModel.headers, body: jsonEncode(json)); + final response = await http.post(Uri.parse(ApiModel().ApiUrl + '/habitos'), + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); } @@ -32,8 +32,8 @@ class HabitoRepository { }; final response = await http.put( - Uri.parse(ApiModel.ApiUrl + '/habitos/' + model.id.toString()), - headers: ApiModel.headers, + Uri.parse(ApiModel().ApiUrl + '/habitos/' + model.id.toString()), + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); @@ -46,11 +46,11 @@ class HabitoRepository { }; final response = await http.put( - Uri.parse(ApiModel.ApiUrl + + Uri.parse(ApiModel().ApiUrl + '/habitos/' + model.idRotina.toString() + '/concluir'), - headers: ApiModel.headers, + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); @@ -58,17 +58,18 @@ class HabitoRepository { Future excluirHabito(HabitoModel model) async { final response = await http.delete( - Uri.parse(ApiModel.ApiUrl + '/habitos/' + model.id.toString()), - headers: ApiModel.headers); + Uri.parse(ApiModel().ApiUrl + '/habitos/' + model.id.toString()), + headers: await ApiModel().getHeaders()); return ResponseAPIModel.fromJson(jsonDecode(response.body)); } Future> getHabitos() async { - Uri url = Uri.parse(ApiModel.ApiUrl + '/habitos'); + Uri url = Uri.parse(ApiModel().ApiUrl + '/habitos'); var _url = Uri.parse(url.toString()); - final response = await http.get(_url, headers: ApiModel.headers); + final response = + await http.get(_url, headers: await ApiModel().getHeaders()); Map jsonMap = jsonDecode(response.body); List listaHabitos = (jsonMap['habitos'] as List) diff --git a/lib/Repository/ItemRepository.dart b/lib/Repository/ItemRepository.dart index 966ea93..5a222f1 100644 --- a/lib/Repository/ItemRepository.dart +++ b/lib/Repository/ItemRepository.dart @@ -14,8 +14,8 @@ class ItemRepository { final response = await http.put( Uri.parse( - ApiModel.ApiUrl + '/item/' + model.id.toString() + '/concluir'), - headers: ApiModel.headers, + ApiModel().ApiUrl + '/item/' + model.id.toString() + '/concluir'), + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); @@ -23,8 +23,8 @@ class ItemRepository { Future excluirItem(ItemModel model) async { final response = await http.delete( - Uri.parse(ApiModel.ApiUrl + '/item/' + model.id.toString()), - headers: ApiModel.headers, + Uri.parse(ApiModel().ApiUrl + '/item/' + model.id.toString()), + headers: await ApiModel().getHeaders(), ); return ResponseAPIModel.fromJson(jsonDecode(response.body)); diff --git a/lib/Repository/LembreteRepository.dart b/lib/Repository/LembreteRepository.dart index 8ac27c6..ac2d145 100644 --- a/lib/Repository/LembreteRepository.dart +++ b/lib/Repository/LembreteRepository.dart @@ -15,8 +15,10 @@ class LembreteRepository { "cor": model.cor, }; - final response = await http.post(Uri.parse(ApiModel.ApiUrl + '/lembretes'), - headers: ApiModel.headers, body: jsonEncode(json)); + final response = await http.post( + Uri.parse(ApiModel().ApiUrl + '/lembretes'), + headers: await ApiModel().getHeaders(), + body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); } @@ -30,8 +32,8 @@ class LembreteRepository { }; final response = await http.put( - Uri.parse(ApiModel.ApiUrl + '/lembretes/' + model.id.toString()), - headers: ApiModel.headers, + Uri.parse(ApiModel().ApiUrl + '/lembretes/' + model.id.toString()), + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); @@ -39,17 +41,18 @@ class LembreteRepository { Future excluirLembrete(LembreteModel model) async { final response = await http.delete( - Uri.parse(ApiModel.ApiUrl + '/lembretes/' + model.id.toString()), - headers: ApiModel.headers); + Uri.parse(ApiModel().ApiUrl + '/lembretes/' + model.id.toString()), + headers: await ApiModel().getHeaders()); return ResponseAPIModel.fromJson(jsonDecode(response.body)); } Future> getLembretes() async { - Uri url = Uri.parse(ApiModel.ApiUrl + '/lembretes'); + Uri url = Uri.parse(ApiModel().ApiUrl + '/lembretes'); var _url = Uri.parse(url.toString()); - final response = await http.get(_url, headers: ApiModel.headers); + final response = + await http.get(_url, headers: await ApiModel().getHeaders()); Map jsonMap = jsonDecode(response.body); List listaLembretes = (jsonMap['lembretes'] as List) @@ -66,21 +69,22 @@ class LembreteRepository { }; final response = await http.put( - Uri.parse(ApiModel.ApiUrl + + Uri.parse(ApiModel().ApiUrl + '/lembretes/' + model.id.toString() + '/concluir'), - headers: ApiModel.headers, + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); } Future> get() async { - Uri url = Uri.parse(ApiModel.ApiUrl + '/lembretes'); + Uri url = Uri.parse(ApiModel().ApiUrl + '/lembretes'); var _url = Uri.parse(url.toString()); - final response = await http.get(_url, headers: ApiModel.headers); + final response = + await http.get(_url, headers: await ApiModel().getHeaders()); Map jsonMap = jsonDecode(response.body); diff --git a/lib/Repository/ListaRepository.dart b/lib/Repository/ListaRepository.dart index e5d9b13..49027a6 100644 --- a/lib/Repository/ListaRepository.dart +++ b/lib/Repository/ListaRepository.dart @@ -12,8 +12,8 @@ class ListaRepository { Future addLista(ListaModel model) async { var json = {"nome": model.nome, "cor": model.cor, "itens": model.itens}; - final response = await http.post(Uri.parse(ApiModel.ApiUrl + '/listas'), - headers: ApiModel.headers, body: jsonEncode(json)); + final response = await http.post(Uri.parse(ApiModel().ApiUrl + '/listas'), + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); } @@ -22,8 +22,8 @@ class ListaRepository { var json = {"nome": model.nome, "cor": model.cor, "itens": model.itens}; final response = await http.put( - Uri.parse(ApiModel.ApiUrl + '/listas/' + model.id.toString()), - headers: ApiModel.headers, + Uri.parse(ApiModel().ApiUrl + '/listas/' + model.id.toString()), + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); @@ -31,17 +31,18 @@ class ListaRepository { Future excluirLista(ListaModel model) async { final response = await http.delete( - Uri.parse(ApiModel.ApiUrl + '/listas/' + model.id.toString()), - headers: ApiModel.headers); + Uri.parse(ApiModel().ApiUrl + '/listas/' + model.id.toString()), + headers: await ApiModel().getHeaders()); return ResponseAPIModel.fromJson(jsonDecode(response.body)); } Future> getListas() async { - Uri url = Uri.parse(ApiModel.ApiUrl + '/listas'); + Uri url = Uri.parse(ApiModel().ApiUrl + '/listas'); var _url = Uri.parse(url.toString()); - final response = await http.get(_url, headers: ApiModel.headers); + final response = + await http.get(_url, headers: await ApiModel().getHeaders()); Map jsonMap = jsonDecode(response.body); // List listaProdutos = (jsonMap['listas'] as List) diff --git a/lib/Repository/LoginRepository.dart b/lib/Repository/LoginRepository.dart index 0291e61..04ca902 100644 --- a/lib/Repository/LoginRepository.dart +++ b/lib/Repository/LoginRepository.dart @@ -8,17 +8,18 @@ import 'package:shared_preferences/shared_preferences.dart'; class LoginRepository { Future autenticar(LoginModel model) async { - var _url = Uri.parse(ApiModel.ApiUrl + '/signin'); + var _url = Uri.parse(ApiModel().ApiUrl + '/signin'); var json = { "email": model.usuario.toString().toLowerCase(), "senha": model.senha.toString().toLowerCase() - /*md5 - .convert(utf8.encode(model.senha.toString().toLowerCase())) - .toString(),*/ }; - final response = await http.post(_url, - headers: ApiModel.headers, body: jsonEncode(json)); + Map headers = { + "Content-Type": "application/json", + }; + + final response = + await http.post(_url, headers: headers, body: jsonEncode(json)); if (response.statusCode == 200) { SharedPreferences prefs = await SharedPreferences.getInstance(); @@ -39,9 +40,10 @@ class LoginRepository { String? token = prefs.getString('UsuarioToken'); print(token); - var _url = Uri.parse(ApiModel.ApiUrl + '/usuario/' + token!); + var _url = Uri.parse(ApiModel().ApiUrl + '/usuario/' + token!); - final response = await http.get(_url, headers: ApiModel.headers); + final response = + await http.get(_url, headers: await ApiModel().getHeaders()); UsuarioModel usuario = UsuarioModel.fromJson(jsonDecode(response.body)); diff --git a/lib/Repository/ProjetoRepository.dart b/lib/Repository/ProjetoRepository.dart index e860f0f..38e19c7 100644 --- a/lib/Repository/ProjetoRepository.dart +++ b/lib/Repository/ProjetoRepository.dart @@ -17,8 +17,8 @@ class ProjetoRepository { "atividades": model.atividades }; - final response = await http.post(Uri.parse(ApiModel.ApiUrl + '/projetos'), - headers: ApiModel.headers, body: jsonEncode(json)); + final response = await http.post(Uri.parse(ApiModel().ApiUrl + '/projetos'), + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); } @@ -34,8 +34,8 @@ class ProjetoRepository { }; final response = await http.put( - Uri.parse(ApiModel.ApiUrl + '/projetos/' + model.id.toString()), - headers: ApiModel.headers, + Uri.parse(ApiModel().ApiUrl + '/projetos/' + model.id.toString()), + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); @@ -48,9 +48,11 @@ class ProjetoRepository { }; final response = await http.put( - Uri.parse( - ApiModel.ApiUrl + '/projetos/' + model.id.toString() + '/concluir'), - headers: ApiModel.headers, + Uri.parse(ApiModel().ApiUrl + + '/projetos/' + + model.id.toString() + + '/concluir'), + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); @@ -58,17 +60,18 @@ class ProjetoRepository { Future excluirProjeto(ProjetoModel model) async { final response = await http.delete( - Uri.parse(ApiModel.ApiUrl + '/projetos/' + model.id.toString()), - headers: ApiModel.headers); + Uri.parse(ApiModel().ApiUrl + '/projetos/' + model.id.toString()), + headers: await ApiModel().getHeaders()); return ResponseAPIModel.fromJson(jsonDecode(response.body)); } Future> getProjetos() async { - Uri url = Uri.parse(ApiModel.ApiUrl + '/projetos'); + Uri url = Uri.parse(ApiModel().ApiUrl + '/projetos'); var _url = Uri.parse(url.toString()); - final response = await http.get(_url, headers: ApiModel.headers); + final response = + await http.get(_url, headers: await ApiModel().getHeaders()); Map jsonMap = jsonDecode(response.body); @@ -76,10 +79,11 @@ class ProjetoRepository { } Future> get() async { - Uri url = Uri.parse(ApiModel.ApiUrl + '/projetos'); + Uri url = Uri.parse(ApiModel().ApiUrl + '/projetos'); var _url = Uri.parse(url.toString()); - final response = await http.get(_url, headers: ApiModel.headers); + final response = + await http.get(_url, headers: await ApiModel().getHeaders()); Map jsonMap = jsonDecode(response.body); diff --git a/lib/Repository/TarefaRepository.dart b/lib/Repository/TarefaRepository.dart index 06521fe..5d62fb1 100644 --- a/lib/Repository/TarefaRepository.dart +++ b/lib/Repository/TarefaRepository.dart @@ -16,8 +16,8 @@ class TarefaRepository { "cor": model.cor, }; - final response = await http.post(Uri.parse(ApiModel.ApiUrl + '/tarefas'), - headers: ApiModel.headers, body: jsonEncode(json)); + final response = await http.post(Uri.parse(ApiModel().ApiUrl + '/tarefas'), + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); } @@ -32,8 +32,8 @@ class TarefaRepository { }; final response = await http.put( - Uri.parse(ApiModel.ApiUrl + '/tarefas/' + model.id.toString()), - headers: ApiModel.headers, + Uri.parse(ApiModel().ApiUrl + '/tarefas/' + model.id.toString()), + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); @@ -46,9 +46,11 @@ class TarefaRepository { }; final response = await http.put( - Uri.parse( - ApiModel.ApiUrl + '/tarefas/' + model.id.toString() + '/concluir'), - headers: ApiModel.headers, + Uri.parse(ApiModel().ApiUrl + + '/tarefas/' + + model.id.toString() + + '/concluir'), + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); @@ -56,17 +58,18 @@ class TarefaRepository { Future excluirTarefa(TarefaModel model) async { final response = await http.delete( - Uri.parse(ApiModel.ApiUrl + '/tarefas/' + model.id.toString()), - headers: ApiModel.headers); + Uri.parse(ApiModel().ApiUrl + '/tarefas/' + model.id.toString()), + headers: await ApiModel().getHeaders()); return ResponseAPIModel.fromJson(jsonDecode(response.body)); } Future> getTarefas() async { - Uri url = Uri.parse(ApiModel.ApiUrl + '/tarefas'); + Uri url = Uri.parse(ApiModel().ApiUrl + '/tarefas'); var _url = Uri.parse(url.toString()); - final response = await http.get(_url, headers: ApiModel.headers); + final response = + await http.get(_url, headers: await ApiModel().getHeaders()); Map jsonMap = jsonDecode(response.body); List listaTarefas = (jsonMap['tarefas'] as List) @@ -77,10 +80,11 @@ class TarefaRepository { } Future> get() async { - Uri url = Uri.parse(ApiModel.ApiUrl + '/tarefas'); + Uri url = Uri.parse(ApiModel().ApiUrl + '/tarefas'); var _url = Uri.parse(url.toString()); - final response = await http.get(_url, headers: ApiModel.headers); + final response = + await http.get(_url, headers: await ApiModel().getHeaders()); Map jsonMap = jsonDecode(response.body); diff --git a/lib/Repository/UsuarioRepository.dart b/lib/Repository/UsuarioRepository.dart index b806cac..e553508 100644 --- a/lib/Repository/UsuarioRepository.dart +++ b/lib/Repository/UsuarioRepository.dart @@ -17,8 +17,25 @@ class UsuarioRepository { "senha": model.senha, }; - final response = await http.post(Uri.parse(ApiModel.ApiUrl + '/usuarios'), - headers: ApiModel.headers, body: jsonEncode(json)); + final response = await http.post(Uri.parse(ApiModel().ApiUrl + '/usuarios'), + headers: await ApiModel().getHeaders(), body: jsonEncode(json)); + + return ResponseAPIModel.fromJson(jsonDecode(response.body)); + } + + Future updateUsuario(UsuarioModel model) async { + var json = { + "nome": model.nome, + "apelido": model.apelido, + "email": model.email, + "senha": model.senha, + "novaSenha": model.novaSenha + }; + + final response = await http.put( + Uri.parse(ApiModel().ApiUrl + '/usuarios/' + model.id.toString()), + headers: await ApiModel().getHeaders(), + body: jsonEncode(json)); return ResponseAPIModel.fromJson(jsonDecode(response.body)); } diff --git a/lib/components/dialogs/login/entrarDialog.dart b/lib/components/dialogs/login/entrarDialog.dart index b9f559d..c537186 100644 --- a/lib/components/dialogs/login/entrarDialog.dart +++ b/lib/components/dialogs/login/entrarDialog.dart @@ -65,6 +65,7 @@ Future entrar(BuildContext context) { await loginController.autentica(); if (succes) { + print('fkfjkl'); Navigator.of(context).push( MaterialPageRoute( builder: (BuildContext context) => diff --git a/lib/components/dialogs/login/usuarioCadastroDialog.dart b/lib/components/dialogs/login/usuarioCadastroDialog.dart index c3c0959..d19b72a 100644 --- a/lib/components/dialogs/login/usuarioCadastroDialog.dart +++ b/lib/components/dialogs/login/usuarioCadastroDialog.dart @@ -6,6 +6,7 @@ import 'package:organizei/components/botao.dart'; import 'package:organizei/components/dialog_personalizado.dart'; import 'package:organizei/components/dialogs/login/entrarDialog.dart'; import 'package:organizei/components/input.dart'; +import 'package:organizei/home_page.dart'; Future cadastrarUsuario(BuildContext context, {UsuarioModel? usuario, Function? fecharDialog}) { @@ -14,8 +15,23 @@ Future cadastrarUsuario(BuildContext context, if (usuario != null) { usuarioController.controllerNome.text = usuario.nome ?? ''; - usuarioController.controllerApelido.text = usuario.nome ?? ''; - usuarioController.controllerSenha.text = usuario.apelido ?? ''; + usuarioController.controllerApelido.text = usuario.apelido ?? ''; + usuarioController.controllerEmail.text = usuario.email ?? ''; + usuarioController.usuarioId(usuario.id); + } + + Widget _buildNovaSenha() { + return usuario != null + ? Padding( + padding: const EdgeInsets.only(bottom: 16), + child: input( + onSaved: usuarioController.usuarioNovaSenha, + textController: usuarioController.controllerNovaSenha, + label: 'nova senha', + senha: true), + //child: Input(label: 'senha'), + ) + : Container(); } return showDialog( @@ -71,10 +87,13 @@ Future cadastrarUsuario(BuildContext context, child: input( onSaved: usuarioController.usuarioSenha, textController: usuarioController.controllerSenha, - label: 'senha', + label: (usuario != null && usuario.id != null) + ? 'senha atual' + : 'senha', senha: true), //child: Input(label: 'senha'), ), + _buildNovaSenha(), Botao( texto: 'salvar', cor: const Color(0xFF6BC8E4), @@ -82,8 +101,16 @@ Future cadastrarUsuario(BuildContext context, bool succes = await usuarioController.saveUsuario(); if (succes == true) { - Navigator.pop(context); - entrar(context); + if (usuario == null) { + Navigator.pop(context); + entrar(context); + } else { + Navigator.pushAndRemoveUntil( + context, + MaterialPageRoute( + builder: (_) => const HomePage()), + (route) => false); + } } }, ),