Skip to content

Commit

Permalink
Item, Lista e limpeza
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrcode committed Oct 13, 2022
1 parent cff98d5 commit 59dc853
Show file tree
Hide file tree
Showing 8 changed files with 288 additions and 23 deletions.
146 changes: 146 additions & 0 deletions lib/Controller/ListaController.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:organizei/Model/Item/ItemModel.dart';
import 'package:organizei/Model/Lista/ListaModel.dart';
import 'package:organizei/Controller/Base/Base.dart';
import 'package:organizei/Repository/ListaRepository.dart';
import 'package:organizei/services/persistencia/login.configuracoes.dart';

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

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

listaId(int? value) => model.id = value;
listaNome(String? value) => model.nome = value.toString();
listaCor(String? value) => model.cor = value.toString();
listaItens(List<ItemModel?>? value) => model.itens = value;

var controllerNome = TextEditingController();
var controllerCor = TextEditingController();

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

formKey.currentState!.save();

try {
if (model.id == null) {
return await repository.addLista(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.updateLista(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<ListaModel>?> getListas() async {
return await repository.getListas();
}

Future<bool> excluirLista() async {
try {
return await repository.excluirLista(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;
}
}
}
19 changes: 0 additions & 19 deletions lib/Model/Generica.dart

This file was deleted.

3 changes: 1 addition & 2 deletions lib/Model/Habito/HabitoModel.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:organizei/Model/API/ResponseAPIModel.dart';
import 'package:organizei/Model/Generica.dart';

class HabitoModel extends GenericaModel {
class HabitoModel {
int? id;
int? idRotina;
String? nome;
Expand Down
29 changes: 29 additions & 0 deletions lib/Model/Item/ItemModel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:organizei/Model/API/ResponseAPIModel.dart';

class ItemModel {
int? id;
String? nome;
bool? concluido;
ResponseAPIModel? responseAPIModel;

ItemModel({
this.id,
this.nome,
this.concluido,
});

ItemModel.fromJson(Map<String, dynamic> json) {
id = int.parse(json['id']);
nome = json['nome'];
concluido = json['concluido'];
}

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

return data;
}
}
34 changes: 34 additions & 0 deletions lib/Model/Lista/ListaModel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:organizei/Model/API/ResponseAPIModel.dart';
import 'package:organizei/Model/Item/ItemModel.dart';

class ListaModel {
int? id;
String? nome;
String? cor;
List<ItemModel?>? itens;
ResponseAPIModel? responseAPIModel;

ListaModel({
this.id,
this.nome,
this.cor,
this.itens,
});

ListaModel.fromJson(Map<String, dynamic> json) {
id = int.parse(json['id']);
nome = json['nome'];
cor = json['cor'];
itens = json['itens'];
}

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

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

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

return listaProdutos;
return listaHabitos;
}
}
23 changes: 23 additions & 0 deletions lib/Repository/ItemRepository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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/Item/ItemModel.dart';

class ItemRepository {
Future<ResponseAPIModel> concluirItem(ItemModel model, bool concluido) async {
var json = {
"concluido": concluido,
};

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

return ResponseAPIModel.fromJson(jsonDecode(response.body));
}
}
53 changes: 53 additions & 0 deletions lib/Repository/ListaRepository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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/Lista/ListaModel.dart';

import '../Model/Item/ItemModel.dart';

class ListaRepository {
Future<ResponseAPIModel> 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));

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

Future<ResponseAPIModel> updateLista(ListaModel model) async {
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,
body: jsonEncode(json));

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

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

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

Future<List<ListaModel>> getListas() async {
Uri _uriSearchProduto = Uri.parse(ApiModel.ApiUrl + '/listas');

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

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

return listaProdutos;
}
}

0 comments on commit 59dc853

Please sign in to comment.