Skip to content

Commit

Permalink
mexi nuns trem mona
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrcode committed Oct 5, 2022
1 parent 5ac03a0 commit d87af8e
Show file tree
Hide file tree
Showing 13 changed files with 523 additions and 89 deletions.
189 changes: 189 additions & 0 deletions lib/Controller/HabitoController.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:organizei/Model/Habito/HabitoModel.dart';
import 'package:organizei/Controller/Base/Base.dart';
import 'package:organizei/Repository/HabitoRepository.dart';
import 'package:organizei/services/persistencia/login.configuracoes.dart';

class HabitoController extends Base {
HabitoController(this.repository, this.context);

final BuildContext context;
final HabitoRepository repository;
final formKey = GlobalKey<FormState>();
var model = HabitoModel();
//var loginConfiguracoes = LoginConfiguracoes();

habitoId(int? value) => model.id = value;
habitoNome(String? value) => model.nome = value.toString();
habitoDataehora(String? value) => model.data = value.toString();
habitoDias(List<dynamic>? value) => model.dias = value!.cast<String>();
habitoCor(String? value) => model.cor = value.toString();

var controllerNome = TextEditingController();
var controllerDias = TextEditingController();
var controllerPrioridade = TextEditingController();
var controllerCor = TextEditingController();
var controllerDataehora = TextEditingController();

Future<bool> saveHabito() async {
if (!formKey.currentState!.validate()) {
return false;
}

formKey.currentState!.save();

try {
if (model.id == null) {
return await repository.addHabito(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.updateHabito(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<List<HabitoModel>?> getHabitos() async {
return await repository.getHabitos();
}

Future<bool> concluirHabito(bool concluido) async {
try {
return await repository
.concluirHabito(model, concluido)
.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<bool> excluirHabito() async {
try {
return await repository.excluirHabito(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;
}
}
}
4 changes: 4 additions & 0 deletions lib/Controller/TarefaController.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ class TarefaController extends Base {
return await repository.getTarefas();
}

Future<Map<String, dynamic>> get() async {
return await repository.get();
}

Future<bool> concluirTarefa(bool concluido) async {
try {
return await repository
Expand Down
19 changes: 19 additions & 0 deletions lib/Model/Generica.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:organizei/Model/API/ResponseAPIModel.dart';

class GenericaModel {
String? tipo;

ResponseAPIModel? responseAPIModel;

GenericaModel({this.tipo});

GenericaModel.fromJson(Map<String, dynamic> json) {
tipo = json['tipo'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['tipo'] = this.tipo;
return data;
}
}
32 changes: 32 additions & 0 deletions lib/Model/Habito/HabitoModel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:organizei/Model/API/ResponseAPIModel.dart';
import 'package:organizei/Model/Generica.dart';

class HabitoModel extends GenericaModel {
int? id;
String? nome;
String? data;
List<dynamic>? dias;
String? cor;
ResponseAPIModel? responseAPIModel;

HabitoModel({this.id, this.nome, this.data, this.dias, this.cor});

HabitoModel.fromJson(Map<String, dynamic> json) {
id = int.parse(json['id']);
nome = json['nome'];
data = json['hora'];
dias = json['dias'];
cor = json['cor'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['nome'] = this.nome;
data['hora'] = this.data;
data['dias'] = this.dias;
data['cor'] = this.cor;

return data;
}
}
76 changes: 76 additions & 0 deletions lib/Repository/HabitoRepository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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/Habito/HabitoModel.dart';

class HabitoRepository {
Future<ResponseAPIModel> addHabito(HabitoModel model) async {
var json = {
"nome": model.nome,
"hora": model.data,
"dias": model.dias,
"cor": model.cor,
};

final response = await http.post(Uri.parse(ApiModel.ApiUrl + '/habitos'),
headers: ApiModel.headers, body: jsonEncode(json));

return ResponseAPIModel.fromJson(jsonDecode(response.body));
}

Future<ResponseAPIModel> updateHabito(HabitoModel model) async {
var json = {
"nome": model.nome,
"hora": model.data,
"dias": model.dias,
"cor": model.cor,
};

final response = await http.put(
Uri.parse(ApiModel.ApiUrl + '/habitos/' + model.id.toString()),
headers: ApiModel.headers,
body: jsonEncode(json));

return ResponseAPIModel.fromJson(jsonDecode(response.body));
}

Future<ResponseAPIModel> concluirHabito(
HabitoModel model, bool concluido) async {
var json = {
"concluido": concluido,
};

final response = await http.put(
Uri.parse(
ApiModel.ApiUrl + '/habitos/' + model.id.toString() + '/concluir'),
headers: ApiModel.headers,
body: jsonEncode(json));

return ResponseAPIModel.fromJson(jsonDecode(response.body));
}

Future<ResponseAPIModel> excluirHabito(HabitoModel model) async {
final response = await http.delete(
Uri.parse(ApiModel.ApiUrl + '/habitos/' + model.id.toString()),
headers: ApiModel.headers);

return ResponseAPIModel.fromJson(jsonDecode(response.body));
}

Future<List<HabitoModel>> getHabitos() async {
Uri _uriSearchProduto = Uri.parse(ApiModel.ApiUrl + '/habitos');

var _url = Uri.parse(_uriSearchProduto.toString());
final response = await http.get(_url, headers: ApiModel.headers);

Map<String, dynamic> jsonMap = jsonDecode(response.body);
List<HabitoModel> listaProdutos = (jsonMap['habitos'] as List)
.map((item) => HabitoModel.fromJson(item))
.toList();

return listaProdutos;
}
}
15 changes: 13 additions & 2 deletions lib/Repository/TarefaRepository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,21 @@ class TarefaRepository {
final response = await http.get(_url, headers: ApiModel.headers);

Map<String, dynamic> jsonMap = jsonDecode(response.body);
List<TarefaModel> listaProdutos = (jsonMap['tarefas'] as List)
List<TarefaModel> listaTarefas = (jsonMap['tarefas'] as List)
.map((item) => TarefaModel.fromJson(item))
.toList();

return listaProdutos;
return listaTarefas;
}

Future<Map<String, dynamic>> get() async {
Uri _uriSearchProduto = Uri.parse(ApiModel.ApiUrl + '/tarefas');

var _url = Uri.parse(_uriSearchProduto.toString());
final response = await http.get(_url, headers: ApiModel.headers);

Map<String, dynamic> jsonMap = jsonDecode(response.body);

return jsonMap;
}
}
Loading

0 comments on commit d87af8e

Please sign in to comment.