Skip to content

Commit

Permalink
Projeto Atividade
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrcode committed Oct 17, 2022
1 parent 0641e17 commit 1e185b4
Show file tree
Hide file tree
Showing 15 changed files with 732 additions and 20 deletions.
167 changes: 167 additions & 0 deletions lib/Controller/AtividadeController.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:organizei/Model/Atividade/AtividadeModel.dart';
import 'package:organizei/Controller/Base/Base.dart';
import 'package:organizei/Repository/AtividadeRepository.dart';
import 'package:organizei/services/persistencia/login.configuracoes.dart';

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

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

atividadeId(int? value) => model.id = value;
atividadeNome(String? value) => model.nome = value.toString();
atividadeDataInical(String? value) => model.dataInicial = value.toString();
atividadeObservacao(String? value) => model.observacao = value.toString();
atividadeDataFinal(String? value) => model.dataFinal = value.toString();
atividadeCor(String? value) => model.cor = value.toString();
atividadePrioridade(String? value) => model.prioridade = value.toString();

var controllerNome = TextEditingController();
var controllerObservacao = TextEditingController();
var controllerPrioridade = TextEditingController();
var controllerCor = TextEditingController();
var controllerDataIncial = TextEditingController();
var controllerDataFinal = TextEditingController();

Future<bool> saveAtividade(Function? addAtividade) async {
if (!formKey.currentState!.validate()) {
return false;
}

formKey.currentState!.save();

if (addAtividade != null) {
addAtividade(model);
}

try {
if (model.id != null) {
return await repository.updateAtividade(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 true;
}
} catch (e) {
print(e);
return false;
}
}

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

Future<bool> concluirAtividade(bool concluido) async {
try {
return await repository
.concluirAtividade(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> excluirAtividade() async {
try {
return await repository.excluirAtividade(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;
}
}
}
2 changes: 2 additions & 0 deletions lib/Controller/ProjetoController.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:organizei/Model/Atividade/AtividadeModel.dart';
import 'package:organizei/Model/Projeto/ProjetoModel.dart';
import 'package:organizei/Controller/Base/Base.dart';
import 'package:organizei/Repository/ProjetoRepository.dart';
Expand All @@ -22,6 +23,7 @@ class ProjetoController extends Base {
projetoObservacao(String? value) => model.observacao = value.toString();
projetoDataFinal(String? value) => model.dataFinal = value.toString();
projetoCor(String? value) => model.cor = value.toString();
projetoAtividades(List<AtividadeModel?>? value) => model.atividades = value;

var controllerNome = TextEditingController();
var controllerObservacao = TextEditingController();
Expand Down
1 change: 0 additions & 1 deletion lib/Controller/TarefaController.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class TarefaController extends Base {

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

Expand Down
50 changes: 50 additions & 0 deletions lib/Model/Atividade/AtividadeModel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:organizei/Model/API/ResponseAPIModel.dart';

class AtividadeModel {
int? id;
int? idProjeto;
String? nome;
String? dataInicial;
String? observacao;
String? dataFinal;
String? cor;
String? prioridade;
bool? concluido;
ResponseAPIModel? responseAPIModel;

AtividadeModel({
this.id,
this.nome,
this.dataInicial,
this.observacao,
this.dataFinal,
this.cor,
this.prioridade,
this.concluido,
});

AtividadeModel.fromJson(Map<String, dynamic> json) {
id = int.parse(json['id']);
nome = json['nome'];
dataInicial = json['dataInicial'];
observacao = json['observacao'];
dataFinal = json['dataFinal'];
cor = json['cor'];
prioridade = json['prioridade'];
concluido = json['concluido'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = id;
data['nome'] = nome;
data['dataInicial'] = dataInicial;
data['observacao'] = observacao;
data['dataFinal'] = dataFinal;
data['cor'] = cor;
data['prioridade'] = prioridade;
data['concluido'] = concluido;

return data;
}
}
4 changes: 0 additions & 4 deletions lib/Model/Lista/ListaModel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ class ListaModel {
}).toList();

itens = listaItens;

// _controllers.map((item) {
// listaitens.add(ItemModel(nome: (item.text)));
// }).toList();
}

Map<String, dynamic> toJson() {
Expand Down
24 changes: 23 additions & 1 deletion lib/Model/Projeto/ProjetoModel.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:organizei/Model/API/ResponseAPIModel.dart';
import 'package:organizei/Model/Atividade/AtividadeModel.dart';

class ProjetoModel {
int? id;
Expand All @@ -7,6 +8,8 @@ class ProjetoModel {
String? observacao;
String? dataFinal;
String? cor;
String? progresso = '0%';
List<AtividadeModel?>? atividades;
ResponseAPIModel? responseAPIModel;

ProjetoModel(
Expand All @@ -15,7 +18,9 @@ class ProjetoModel {
this.dataInicial,
this.observacao,
this.dataFinal,
this.cor});
this.cor,
this.atividades,
this.progresso});

ProjetoModel.fromJson(Map<String, dynamic> json) {
id = int.parse(json['id']);
Expand All @@ -24,6 +29,23 @@ class ProjetoModel {
observacao = json['observacao'];
dataFinal = json['dataFinal'];
cor = json['cor'];
progresso = json['progresso'];

List<AtividadeModel> listaAtividades = [];
json['atividade'].map((item) {
listaAtividades.add(AtividadeModel(
id: int.parse(item['id']),
nome: item['nome'],
dataInicial: item['dataInicial'],
dataFinal: item['dataFinal'],
observacao: item['observacao'],
cor: item['cor'],
prioridade: item['prioridade'],
concluido: item['concluido'],
));
}).toList();

atividades = listaAtividades;
}

Map<String, dynamic> toJson() {
Expand Down
90 changes: 90 additions & 0 deletions lib/Repository/AtividadeRepository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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/Atividade/AtividadeModel.dart';

class AtividadeRepository {
Future<ResponseAPIModel> addAtividade(AtividadeModel model) async {
var json = {
"nome": model.nome,
"dataInicial": model.dataInicial,
"observacao": model.observacao,
"dataFinal": model.dataFinal,
"cor": model.cor,
"prioridade": model.prioridade,
};

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

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

Future<ResponseAPIModel> updateAtividade(AtividadeModel model) async {
var json = {
"nome": model.nome,
"dataInicial": model.dataInicial,
"observacao": model.observacao,
"dataFinal": model.dataFinal,
"cor": model.cor,
"prioridade": model.prioridade,
};

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

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

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

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

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

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

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

Future<Map<String, dynamic>> getAtividades() async {
Uri url = Uri.parse(ApiModel.ApiUrl + '/atividades');

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

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

return jsonMap;
}

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

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

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

return jsonMap;
}
}
Loading

0 comments on commit 1e185b4

Please sign in to comment.