Skip to content

Commit

Permalink
login
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrcode committed Oct 18, 2022
1 parent e915485 commit 261f513
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 125 deletions.
12 changes: 0 additions & 12 deletions lib/Controller/LoginController.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,4 @@ class LoginController extends Base {
prefs.setString('UsuarioApelido', '');
prefs.setString('token', '');
}

Future<String> getSenhaUsuarioLogado() async {
SharedPreferences prefs = await SharedPreferences.getInstance();

String? chaveCentralizador = prefs.getString('EmpresaChaveCentralizador');

LoginModel? login = await getLogin();

String senha = login!.senha.toString();

return '$senha';
}
}
35 changes: 29 additions & 6 deletions lib/Controller/UsuarioController.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:organizei/Model/Usuario/UsuarioModel.dart';
import 'package:organizei/Controller/Base/Base.dart';
import 'package:organizei/Repository/UsuarioRepository.dart';
import 'package:organizei/services/persistencia/login.configuracoes.dart';
import 'package:shared_preferences/shared_preferences.dart';

class UsuarioController extends Base {
UsuarioController(this.repository, this.context);
Expand All @@ -13,21 +14,26 @@ class UsuarioController extends Base {
var model = UsuarioModel();
var loginConfiguracoes = LoginConfiguracoes();

usuarioId(int? value) => model.id = value;
usuarioNome(String? value) => model.nome = value.toString();
usuarioApelido(String? value) => model.apelido = value.toString();
usuarioEmail(String? value) => model.email = value.toString();
usuarioSenha(String? value) => model.senha = value.toString();
usuarioNovaSenha(String? value) => model.novaSenha = value.toString();

var controllerNome = TextEditingController();
var controllerApelido = TextEditingController();
var controllerEmail = TextEditingController();
var controllerSenha = TextEditingController();
var controllerNovaSenha = TextEditingController();

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

SharedPreferences prefs = await SharedPreferences.getInstance();

formKey.currentState!.save();

try {
Expand Down Expand Up @@ -57,14 +63,31 @@ class UsuarioController extends Base {
}
});
} else {
/*return await repository.updateProduto(model).then((value) {
return await repository.updateUsuario(model).then((value) async {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(value.msg),
backgroundColor: value.valido ? Colors.green : Colors.red,
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),
));
return value.valido;
});*/
return false;

if (value.valido! == true) {
prefs.setString('UsuarioApelido', model.apelido ?? '');
return value.valido!;
} else {
return false;
}
});
}
} catch (e) {
print(e);
Expand Down
26 changes: 11 additions & 15 deletions lib/Model/API/APIModel.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import 'package:shared_preferences/shared_preferences.dart';

Future<String> getToken() async {
SharedPreferences prefs = await SharedPreferences.getInstance();

String? token = prefs.getString('UsuarioToken');
return token ?? '';
}

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";
String ApiUrl = "http://192.168.100.74:4444";

Future<Map<String, String>> getHeaders() async {
SharedPreferences prefs = await SharedPreferences.getInstance();

static final Future<String> _token = getToken();
String? token = prefs.getString('UsuarioToken');

static Map<String, String> headers = {
"Content-Type": "application/json",
"Authorization": "Bearer $_token"
};
Map<String, String> headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + token!
};
return headers;
}
}
13 changes: 8 additions & 5 deletions lib/Model/Usuario/UsuarioModel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class UsuarioModel {
String? apelido;
String? email;
String? senha;
String? novaSenha;
ResponseAPIModel? responseAPIModel;

UsuarioModel({
Expand All @@ -14,6 +15,7 @@ class UsuarioModel {
this.apelido,
this.email,
this.senha,
this.novaSenha,
});

UsuarioModel.fromJson(Map<String, dynamic> json) {
Expand All @@ -26,11 +28,12 @@ class UsuarioModel {

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['nome'] = this.nome;
data['apelido'] = this.apelido;
data['email'] = this.email;
data['senha'] = this.senha;
data['id'] = id;
data['nome'] = nome;
data['apelido'] = apelido;
data['email'] = email;
data['senha'] = senha;
data['novaSenha'] = novaSenha;

return data;
}
Expand Down
28 changes: 16 additions & 12 deletions lib/Repository/AtividadeRepository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class AtividadeRepository {
"prioridade": model.prioridade,
};

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

return ResponseAPIModel.fromJson(jsonDecode(response.body));
}
Expand All @@ -34,8 +36,8 @@ class AtividadeRepository {
};

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

return ResponseAPIModel.fromJson(jsonDecode(response.body));
Expand All @@ -48,40 +50,42 @@ class AtividadeRepository {
};

final response = await http.put(
Uri.parse(ApiModel.ApiUrl +
Uri.parse(ApiModel().ApiUrl +
'/atividades/' +
model.id.toString() +
'/concluir'),
headers: ApiModel.headers,
headers: await ApiModel().getHeaders(),
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);
Uri.parse(ApiModel().ApiUrl + '/atividades/' + model.id.toString()),
headers: await ApiModel().getHeaders());

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

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

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

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

return jsonMap;
}

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

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

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

Expand Down
21 changes: 11 additions & 10 deletions lib/Repository/HabitoRepository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class HabitoRepository {
"dataFinal": model.dataFinal,
};

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

return ResponseAPIModel.fromJson(jsonDecode(response.body));
}
Expand All @@ -32,8 +32,8 @@ class HabitoRepository {
};

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

return ResponseAPIModel.fromJson(jsonDecode(response.body));
Expand All @@ -46,29 +46,30 @@ class HabitoRepository {
};

final response = await http.put(
Uri.parse(ApiModel.ApiUrl +
Uri.parse(ApiModel().ApiUrl +
'/habitos/' +
model.idRotina.toString() +
'/concluir'),
headers: ApiModel.headers,
headers: await ApiModel().getHeaders(),
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);
Uri.parse(ApiModel().ApiUrl + '/habitos/' + model.id.toString()),
headers: await ApiModel().getHeaders());

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

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

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

Map<String, dynamic> jsonMap = jsonDecode(response.body);
List<HabitoModel> listaHabitos = (jsonMap['habitos'] as List)
Expand Down
8 changes: 4 additions & 4 deletions lib/Repository/ItemRepository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ class ItemRepository {

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

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

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

return ResponseAPIModel.fromJson(jsonDecode(response.body));
Expand Down
28 changes: 16 additions & 12 deletions lib/Repository/LembreteRepository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class LembreteRepository {
"cor": model.cor,
};

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

return ResponseAPIModel.fromJson(jsonDecode(response.body));
}
Expand All @@ -30,26 +32,27 @@ class LembreteRepository {
};

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

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

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

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

Future<List<LembreteModel>> getLembretes() async {
Uri url = Uri.parse(ApiModel.ApiUrl + '/lembretes');
Uri url = Uri.parse(ApiModel().ApiUrl + '/lembretes');

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

Map<String, dynamic> jsonMap = jsonDecode(response.body);
List<LembreteModel> listaLembretes = (jsonMap['lembretes'] as List)
Expand All @@ -66,21 +69,22 @@ class LembreteRepository {
};

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

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

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

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

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

Expand Down
Loading

0 comments on commit 261f513

Please sign in to comment.