From 6c0fd28a39967fd0104aa6b1f51869e1c8504a8a Mon Sep 17 00:00:00 2001 From: wjrcode Date: Sat, 15 Oct 2022 17:37:39 -0300 Subject: [PATCH] Lembrete --- lib/Controller/LembreteController.dart | 151 ++++++++++++++++++ lib/Model/API/APIModel.dart | 4 +- lib/Model/Lembrete/LembreteModel.dart | 31 ++++ lib/Repository/LembreteRepository.dart | 72 +++++++++ .../lembretes/lembreteCadastroDialog.dart | 124 ++++++++++++++ .../dialogs/lembretes/lembreteDialog.dart | 119 ++++++++++++++ lib/components/navigation/menu.dart | 25 ++- 7 files changed, 516 insertions(+), 10 deletions(-) create mode 100644 lib/Controller/LembreteController.dart create mode 100644 lib/Model/Lembrete/LembreteModel.dart create mode 100644 lib/Repository/LembreteRepository.dart create mode 100644 lib/components/dialogs/lembretes/lembreteCadastroDialog.dart create mode 100644 lib/components/dialogs/lembretes/lembreteDialog.dart diff --git a/lib/Controller/LembreteController.dart b/lib/Controller/LembreteController.dart new file mode 100644 index 0000000..85a138e --- /dev/null +++ b/lib/Controller/LembreteController.dart @@ -0,0 +1,151 @@ +import 'dart:ffi'; + +import 'package:flutter/material.dart'; +import 'package:http/http.dart'; +import 'package:organizei/Model/Lembrete/LembreteModel.dart'; +import 'package:organizei/Controller/Base/Base.dart'; +import 'package:organizei/Repository/LembreteRepository.dart'; +import 'package:organizei/services/persistencia/login.configuracoes.dart'; + +class LembreteController extends Base { + LembreteController(this.repository, this.context); + + final BuildContext context; + final LembreteRepository repository; + final formKey = GlobalKey(); + var model = LembreteModel(); + //var loginConfiguracoes = LoginConfiguracoes(); + + lembreteId(int? value) => model.id = value; + lembreteNome(String? value) => model.nome = value.toString(); + lembreteDataehora(String? value) => model.data = value.toString(); + lembreteAniversario(bool? value) => model.eAniversario = value; + lembreteCor(String? value) => model.cor = value.toString(); + + var controllerNome = TextEditingController(); + var controllerCor = TextEditingController(); + var controllerDataehora = TextEditingController(); + + Future saveLembrete() async { + if (!formKey.currentState!.validate()) { + return false; + } + + formKey.currentState!.save(); + + try { + if (model.id == null) { + return await repository.addLembrete(model).then((value) async { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + 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), + ), + ); + + await Future.delayed(const Duration(milliseconds: 500)); + + if (value.valido!) { + return value.valido!; + } else { + return false; + } + }); + } else { + return await repository.updateLembrete(model).then((value) async { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + 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), + ), + ); + + await Future.delayed(const Duration(milliseconds: 500)); + + if (value.valido!) { + return value.valido!; + } else { + return false; + } + }); + } + } catch (e) { + print(e); + return false; + } + } + + Future?> getLembretes() async { + return await repository.getLembretes(); + } + + Future> get() async { + return await repository.get(); + } + + Future excluirLembrete() async { + try { + return await repository.excluirLembrete(model).then((value) async { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + 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), + ), + ); + + await Future.delayed(const Duration(milliseconds: 500)); + + if (value.valido!) { + return value.valido!; + } else { + return false; + } + }); + } catch (e) { + print(e); + return false; + } + } +} diff --git a/lib/Model/API/APIModel.dart b/lib/Model/API/APIModel.dart index fe198a3..005c58e 100644 --- a/lib/Model/API/APIModel.dart +++ b/lib/Model/API/APIModel.dart @@ -1,7 +1,7 @@ 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"; + static const ApiUrl = "http://192.168.100.74:4444"; + //static const ApiUrl = "http://192.168.100.82:4444"; static const String _token = "fd7bc88b7c0149adbc134d5d0a919814"; static const Map headers = { "Content-Type": "application/json", diff --git a/lib/Model/Lembrete/LembreteModel.dart b/lib/Model/Lembrete/LembreteModel.dart new file mode 100644 index 0000000..2eed199 --- /dev/null +++ b/lib/Model/Lembrete/LembreteModel.dart @@ -0,0 +1,31 @@ +import 'package:organizei/Model/API/ResponseAPIModel.dart'; + +class LembreteModel { + int? id; + String? nome; + String? data; + bool? eAniversario; + String? cor; + ResponseAPIModel? responseAPIModel; + + LembreteModel({this.id, this.nome, this.data, this.eAniversario, this.cor}); + + LembreteModel.fromJson(Map json) { + id = int.parse(json['id']); + nome = json['nome']; + data = json['data']; + eAniversario = json['eAniversario']; + cor = json['cor']; + } + + Map toJson() { + final Map data = Map(); + data['id'] = id; + data['nome'] = nome; + data['data'] = data; + data['eAniversario'] = eAniversario; + data['cor'] = cor; + + return data; + } +} diff --git a/lib/Repository/LembreteRepository.dart b/lib/Repository/LembreteRepository.dart new file mode 100644 index 0000000..d4b7195 --- /dev/null +++ b/lib/Repository/LembreteRepository.dart @@ -0,0 +1,72 @@ +import 'dart:convert'; + +import 'package:organizei/Model/API/APIModel.dart'; + +import 'package:http/http.dart' as http; +import 'package:organizei/Model/API/ResponseAPIModel.dart'; +import 'package:organizei/Model/Lembrete/LembreteModel.dart'; + +class LembreteRepository { + Future addLembrete(LembreteModel model) async { + var json = { + "nome": model.nome, + "data": model.data, + "eAniversario": model.eAniversario, + "cor": model.cor, + }; + + final response = await http.post(Uri.parse(ApiModel.ApiUrl + '/lembretes'), + headers: ApiModel.headers, body: jsonEncode(json)); + + return ResponseAPIModel.fromJson(jsonDecode(response.body)); + } + + Future updateLembrete(LembreteModel model) async { + var json = { + "nome": model.nome, + "data": model.data, + "eAniversario": model.eAniversario, + "cor": model.cor, + }; + + final response = await http.put( + Uri.parse(ApiModel.ApiUrl + '/lembretes/' + model.id.toString()), + headers: ApiModel.headers, + body: jsonEncode(json)); + + return ResponseAPIModel.fromJson(jsonDecode(response.body)); + } + + Future excluirLembrete(LembreteModel model) async { + final response = await http.delete( + Uri.parse(ApiModel.ApiUrl + '/lembretes/' + model.id.toString()), + headers: ApiModel.headers); + + return ResponseAPIModel.fromJson(jsonDecode(response.body)); + } + + Future> getLembretes() async { + Uri _uriSearchProduto = Uri.parse(ApiModel.ApiUrl + '/lembretes'); + + var _url = Uri.parse(_uriSearchProduto.toString()); + final response = await http.get(_url, headers: ApiModel.headers); + + Map jsonMap = jsonDecode(response.body); + List listaLembretes = (jsonMap['lembretes'] as List) + .map((item) => LembreteModel.fromJson(item)) + .toList(); + + return listaLembretes; + } + + Future> get() async { + Uri _uriSearchProduto = Uri.parse(ApiModel.ApiUrl + '/lembretes'); + + var _url = Uri.parse(_uriSearchProduto.toString()); + final response = await http.get(_url, headers: ApiModel.headers); + + Map jsonMap = jsonDecode(response.body); + + return jsonMap; + } +} diff --git a/lib/components/dialogs/lembretes/lembreteCadastroDialog.dart b/lib/components/dialogs/lembretes/lembreteCadastroDialog.dart new file mode 100644 index 0000000..cc163d6 --- /dev/null +++ b/lib/components/dialogs/lembretes/lembreteCadastroDialog.dart @@ -0,0 +1,124 @@ +import 'package:flutter/material.dart'; +import 'package:organizei/Model/Lembrete/LembreteModel.dart'; +import 'package:organizei/Repository/LembreteRepository.dart'; +import 'package:organizei/components/botao.dart'; +import 'package:organizei/components/dialog_personalizado.dart'; +import 'package:organizei/components/input.dart'; +import 'package:organizei/components/selectCor.dart'; +import 'package:organizei/components/selectData.dart'; +import 'package:organizei/components/selectPrioridade.dart'; +import '../../../Controller/LembreteController.dart'; + +Future criarLembrete(BuildContext context, + {LembreteModel? lembrete = null, Function? fecharDialog = null}) { + late LembreteController lembreteController; + + lembreteController = LembreteController(LembreteRepository(), context); + + late bool? eAniversario = false; + + if (lembrete != null) { + lembreteController.controllerNome.text = lembrete.nome ?? ''; + lembreteController.controllerDataehora.text = lembrete.data ?? ''; + lembreteController.lembreteAniversario(lembrete.eAniversario); + eAniversario = lembrete.eAniversario; + lembreteController.lembreteCor(lembrete.cor); + lembreteController.lembreteId(lembrete.id); + } + + return showDialog( + barrierDismissible: false, + barrierColor: Colors.white.withOpacity(0), + context: context, + builder: (context) { + return StatefulBuilder(builder: (context, StateSetter setState) { + return Scaffold( + backgroundColor: Colors.transparent, + body: SingleChildScrollView( + scrollDirection: Axis.vertical, + child: Material( + type: MaterialType.transparency, + child: Form( + key: lembreteController.formKey, + child: Padding( + padding: const EdgeInsets.only(top: 24.0), + child: ConstrainedBox( + constraints: BoxConstraints( + minHeight: MediaQuery.of(context).size.height, + ), + child: DialogPersonalizado( + nome: 'Lembrete', + child: [ + Padding( + padding: const EdgeInsets.only(bottom: 16), + child: input( + onSaved: lembreteController.lembreteNome, + textController: lembreteController.controllerNome, + label: 'nome', + ), + ), + SelectData( + controller: lembreteController.controllerDataehora, + onSaved: lembreteController.lembreteDataehora, + ), + Row( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + Padding( + padding: const EdgeInsets.only(bottom: 4.0), + child: Checkbox( + //tristate: true, + activeColor: const Color(0xFF6385C3), + side: const BorderSide( + width: 2, color: Colors.black), + value: eAniversario, + onChanged: (bool? value) async { + setState(() { + eAniversario = value; + }); + + lembreteController + .lembreteAniversario(eAniversario); + }, + ), + ), + const Padding( + padding: EdgeInsets.only(bottom: 16), + child: Text('é aniversário?'), + ), + ], + ), + SelectCor( + cor: lembreteController.lembreteCor, + corAtual: lembrete?.cor ?? ''), + Botao( + texto: 'Salvar', + cor: const Color(0xFF6385C3), + clicar: () async { + bool succes = + await lembreteController.saveLembrete(); + + if (succes == true) { + var nav = Navigator.of(context); + nav.pop(); + + if (lembrete?.id != null) { + //nav.pop(); + nav.pop(); + } + fecharDialog!(); + } + }, + ), + ], + ), + ), + ), + ), + ), + ), + ); + }); + }); +} diff --git a/lib/components/dialogs/lembretes/lembreteDialog.dart b/lib/components/dialogs/lembretes/lembreteDialog.dart new file mode 100644 index 0000000..a6e4fde --- /dev/null +++ b/lib/components/dialogs/lembretes/lembreteDialog.dart @@ -0,0 +1,119 @@ +import 'package:flutter/material.dart'; +import 'package:organizei/Model/Tarefa/TarefaModel.dart'; +import 'package:organizei/Repository/TarefaRepository.dart'; +import 'package:organizei/components/botao.dart'; +import 'package:organizei/components/dialog_personalizado.dart'; +import 'package:organizei/components/dialogs/tarefas/tarefaCadastroDialog.dart'; +import '../../../Controller/TarefaController.dart'; + +Future visualizarTarefa(BuildContext context, + {required TarefaModel tarefa, Function? fecharDialog = null}) { + late TarefaController tarefaController; + tarefaController = TarefaController(TarefaRepository(), context); + + tarefaController.tarefaId(tarefa.id); + + return showDialog( + barrierDismissible: false, + barrierColor: Colors.white.withOpacity(0), + context: context, + builder: (context) { + return StatefulBuilder(builder: (context, StateSetter setState) { + return Scaffold( + backgroundColor: Colors.transparent, + body: SingleChildScrollView( + scrollDirection: Axis.vertical, + child: Material( + type: MaterialType.transparency, + child: Form( + key: tarefaController.formKey, + child: Padding( + padding: const EdgeInsets.only(top: 24.0), + child: ConstrainedBox( + constraints: BoxConstraints( + minHeight: MediaQuery.of(context).size.height, + ), + child: DialogPersonalizado( + nome: tarefa.nome ?? '', + cor: tarefa.cor ?? '', + child: [ + Text(tarefa.observacao ?? ''), + Row( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + const Text( + 'data: ', + style: TextStyle( + color: Colors.black, + fontWeight: FontWeight.w700, + ), + ), + Text(tarefa.data ?? ''), + ], + ), + Row( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + const Text( + 'prioridade: ', + style: TextStyle( + color: Colors.black, + fontWeight: FontWeight.w700, + ), + ), + Text(tarefa.prioridade ?? ''), + ], + ), + Padding( + padding: const EdgeInsets.only(bottom: 16, top: 16), + child: Botao( + texto: 'Concluir', + cor: const Color(0xFF74C198), + clicar: () async { + bool succes = + await tarefaController.concluirTarefa(true); + + if (succes == true) { + Navigator.pop(context); + fecharDialog!(); + } + }, + ), + ), + Botao( + texto: 'Editar', + cor: const Color(0xFF6385C3), + clicar: () async { + criarTarefa(context, + tarefa: tarefa, fecharDialog: fecharDialog); + }, + ), + Padding( + padding: const EdgeInsets.only(bottom: 16, top: 16), + child: Botao( + texto: 'Excluir', + cor: const Color(0xFFEF7E69), + clicar: () async { + bool succes = + await tarefaController.excluirTarefa(); + + if (succes == true) { + Navigator.pop(context); + fecharDialog!(); + } + }, + ), + ), + ], + ), + ), + ), + ), + ), + ), + ); + }); + }); +} diff --git a/lib/components/navigation/menu.dart b/lib/components/navigation/menu.dart index cc3f20a..9b8d2f7 100644 --- a/lib/components/navigation/menu.dart +++ b/lib/components/navigation/menu.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:organizei/components/dialog_personalizado.dart'; +import 'package:organizei/components/dialogs/lembretes/lembreteCadastroDialog.dart'; import 'package:organizei/components/dialogs/listas/listaCadastroDialog.dart'; import 'package:organizei/components/dialogs/tarefas/tarefaCadastroDialog.dart'; import 'package:organizei/components/dialogs/habitos/habitoCadastroDialog.dart'; @@ -121,15 +122,23 @@ class _MenuState extends State { thickness: 3, color: Colors.black, ), - Row( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - 'criar lembrete', - style: TextStyle(fontSize: 24.0), + GestureDetector( + onTap: () { + criarLembrete(context, fecharDialog: widget.fecharDialog); + widget.fecharMenu!(); + }, + child: AbsorbPointer( + child: Row( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Text( + 'criar lembrete', + style: TextStyle(fontSize: 24.0), + ), + ], ), - ], + ), ), ], ),