-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
523 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.