380 lines
12 KiB
Dart
380 lines
12 KiB
Dart
import 'dart:developer';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:hub/backend/api_requests/api_calls.dart';
|
|
import 'package:hub/components/templates_components/change_passs_qr_code_pass_key_template_component/change_pass_widget.dart';
|
|
import 'package:hub/flutter_flow/flutter_flow_theme.dart';
|
|
import 'package:hub/flutter_flow/flutter_flow_util.dart';
|
|
import 'package:hub/flutter_flow/flutter_flow_widgets.dart';
|
|
import 'package:hub/flutter_flow/nav/nav.dart';
|
|
import 'package:hub/shared/helpers/sqlite_storage_helper.dart';
|
|
import 'package:share_plus/share_plus.dart';
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
class PreferencesPageModel with ChangeNotifier {
|
|
final unfocusNode = FocusNode();
|
|
final SQLiteStorageHelper db = SQLiteStorageHelper();
|
|
bool fingerprint = false;
|
|
bool person = false;
|
|
bool notify = false;
|
|
bool access = false;
|
|
bool panic = false;
|
|
|
|
PreferencesPageModel() {
|
|
initVariables();
|
|
}
|
|
|
|
Future<void> enablePerson(BuildContext context) async {
|
|
final String userDevUUID = await db.get(key: 'userDevUUID', field: 'value');
|
|
notifyListeners();
|
|
Share.share(
|
|
FFLocalizations.of(context).getVariableText(
|
|
ptText: 'Este é o meu identificador de acesso: $userDevUUID',
|
|
enText: 'This is my access identifier: $userDevUUID',
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> toggleNotify(BuildContext context) async {
|
|
final String title = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Change notification',
|
|
ptText: 'Alterar notificação',
|
|
);
|
|
final String content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Are you sure you want to change your notification?',
|
|
ptText: 'Tem certeza que deseja alterar sua notificação?',
|
|
);
|
|
onConfirm() async {
|
|
String content;
|
|
try {
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Notification changed successfully',
|
|
ptText: 'Notificação alterada com sucesso',
|
|
);
|
|
context.pop();
|
|
_showSnackBar(context, content);
|
|
notify = await _toggleBoolInDb('notify');
|
|
notifyListeners();
|
|
} catch (err) {
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Error changing notification',
|
|
ptText: 'Erro ao alterar notificação',
|
|
);
|
|
context.pop();
|
|
_showSnackBar(context, content, isError: true);
|
|
}
|
|
}
|
|
|
|
showAlertDialog(context, title, content, onConfirm);
|
|
}
|
|
|
|
Future<void> toggleAccess(BuildContext context) async {
|
|
onChange(String key) async {
|
|
AppState().accessPass = key;
|
|
await PhpGroup.changePass
|
|
.call(
|
|
newSenha: key,
|
|
)
|
|
.then((value) async {
|
|
final String content;
|
|
if (jsonDecode(value.jsonBody['error'].toString()) == false) {
|
|
if (!access) access = await _toggleBoolInDb('access');
|
|
notifyListeners();
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Access pass changed successfully',
|
|
ptText: 'Senha de acesso alterada com sucesso',
|
|
);
|
|
_showSnackBar(context, content);
|
|
} else {
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
ptText: 'Erro ao alterar senha de acesso',
|
|
enText: 'Error changing access pass',
|
|
);
|
|
_showSnackBar(context, content, isError: true);
|
|
}
|
|
}).catchError((e, s) {
|
|
final String content = FFLocalizations.of(context).getVariableText(
|
|
ptText: 'Erro ao alterar senha de acesso',
|
|
enText: 'Error changing access pass',
|
|
);
|
|
_showSnackBar(context, content, isError: true);
|
|
}).whenComplete(() => notifyListeners());
|
|
}
|
|
|
|
_showPassKey(context, onChange);
|
|
}
|
|
|
|
Future<void> togglePanic(BuildContext context) async {
|
|
onChange(String key) async {
|
|
AppState().panicPass = key;
|
|
await PhpGroup.changePanic
|
|
.call(
|
|
newSenhaPanico: AppState().panicPass,
|
|
)
|
|
.then((value) async {
|
|
final String content;
|
|
if (jsonDecode(value.jsonBody['error'].toString()) == false) {
|
|
if (!panic) panic = await _toggleBoolInDb('panic');
|
|
notifyListeners();
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Panic password changed successfully',
|
|
ptText: 'Senha de pânico alterada com sucesso',
|
|
);
|
|
_showSnackBar(context, content);
|
|
} else {
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
ptText: 'Erro ao alterar senha de pânico',
|
|
enText: 'Error changing panic password',
|
|
);
|
|
_showSnackBar(context, content, isError: true);
|
|
}
|
|
}).catchError((e, s) {
|
|
final String content = FFLocalizations.of(context).getVariableText(
|
|
ptText: 'Erro ao alterar senha de pânico',
|
|
enText: 'Error changing panic password',
|
|
);
|
|
_showSnackBar(context, content, isError: true);
|
|
}).whenComplete(() => notifyListeners());
|
|
}
|
|
|
|
_showPassKey(context, onChange);
|
|
}
|
|
|
|
Future<void> toggleFingerprint(BuildContext context) async {
|
|
final String content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Fingerprint changed successfully',
|
|
ptText: 'Impressão digital alterada com sucesso',
|
|
);
|
|
|
|
onChange(String? key) async {
|
|
if (!fingerprint) AppState().fingerprintPass = key ?? '';
|
|
if (fingerprint) AppState().deleteFingerprintPass();
|
|
|
|
fingerprint = await _toggleBoolInDb('fingerprint');
|
|
notifyListeners();
|
|
|
|
_showSnackBar(context, content);
|
|
}
|
|
|
|
fingerprint ? onChange(null) : _showPassKey(context, onChange);
|
|
}
|
|
|
|
Future<void> deleteAccount(BuildContext context) async {
|
|
final String title = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Delete account',
|
|
ptText: 'Deletar conta',
|
|
);
|
|
final String content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Are you sure you want to delete your account?',
|
|
ptText: 'Tem certeza que deseja deletar sua conta?',
|
|
);
|
|
onConfirm() async {
|
|
String content;
|
|
try {
|
|
await PhpGroup.deleteAccount.call().then((value) {
|
|
if (value.jsonBody['error'] == false) {
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Account deleted successfully',
|
|
ptText: 'Conta deletada com sucesso',
|
|
);
|
|
|
|
AppState().deleteAll();
|
|
|
|
context.pop();
|
|
context.go(
|
|
'/welcomePage',
|
|
extra: <String, dynamic>{
|
|
kTransitionInfoKey: const TransitionInfo(
|
|
hasTransition: true,
|
|
transitionType: PageTransitionType.scale,
|
|
alignment: Alignment.bottomCenter,
|
|
),
|
|
},
|
|
);
|
|
}
|
|
}).catchError((err) {
|
|
context.pop();
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Error deleting account',
|
|
ptText: 'Erro ao deletar conta',
|
|
);
|
|
_showSnackBar(context, content);
|
|
});
|
|
notifyListeners();
|
|
} catch (err) {
|
|
context.pop();
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Error deleting account',
|
|
ptText: 'Erro ao deletar conta',
|
|
);
|
|
_showSnackBar(context, content, isError: true);
|
|
}
|
|
}
|
|
|
|
showAlertDialog(context, title, content, onConfirm);
|
|
}
|
|
|
|
void logout(BuildContext context) async {
|
|
final String title = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Logout',
|
|
ptText: 'Sair',
|
|
);
|
|
final String content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Are you sure you want to logout?',
|
|
ptText: 'Tem certeza que deseja sair?',
|
|
);
|
|
onConfirm() async {
|
|
AppState().deleteAll();
|
|
|
|
context.go(
|
|
'/welcomePage',
|
|
extra: <String, dynamic>{
|
|
kTransitionInfoKey: const TransitionInfo(
|
|
hasTransition: true,
|
|
transitionType: PageTransitionType.scale,
|
|
alignment: Alignment.bottomCenter,
|
|
),
|
|
},
|
|
);
|
|
}
|
|
|
|
showAlertDialog(context, title, content, onConfirm);
|
|
}
|
|
|
|
void localUnlink(BuildContext context) {
|
|
final String title = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Unlink device',
|
|
ptText: 'Desvincular dispositivo',
|
|
);
|
|
final String content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Are you sure you want to unlink your device?',
|
|
ptText: 'Tem certeza que deseja desvincular seu dispositivo?',
|
|
);
|
|
onConfirm() async {
|
|
String content;
|
|
try {
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Device unlinked successfully',
|
|
ptText: 'Dispositivo desvinculado com sucesso',
|
|
);
|
|
|
|
await PhpGroup.resopndeVinculo.call(tarefa: 'I').then((value) {
|
|
if (value.jsonBody['error'] == false) {
|
|
db.clear('cliName');
|
|
db.clear('cliUUID');
|
|
db.clear('ownerUUID');
|
|
|
|
context.pop();
|
|
|
|
context.go(
|
|
'/homePage',
|
|
extra: <String, dynamic>{
|
|
kTransitionInfoKey: const TransitionInfo(
|
|
hasTransition: true,
|
|
transitionType: PageTransitionType.scale,
|
|
alignment: Alignment.bottomCenter,
|
|
),
|
|
},
|
|
);
|
|
|
|
_showSnackBar(context, content);
|
|
}
|
|
}).catchError((err, stack) {
|
|
context.pop();
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Error unlinking device',
|
|
ptText: 'Erro ao desvincular dispositivo',
|
|
);
|
|
_showSnackBar(context, content, isError: true);
|
|
});
|
|
} catch (err, stack) {
|
|
context.pop();
|
|
log(err.toString(), stackTrace: stack);
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Error unlinking device',
|
|
ptText: 'Erro ao desvincular dispositivo',
|
|
);
|
|
_showSnackBar(context, content, isError: true);
|
|
}
|
|
}
|
|
|
|
showAlertDialog(context, title, content, onConfirm);
|
|
}
|
|
|
|
Future<void> initVariables() async {
|
|
fingerprint = await _getBoolFromDb('fingerprint');
|
|
person = await _getBoolFromDb('person');
|
|
notify = await _getBoolFromDb('notify');
|
|
access = await _getBoolFromDb('pass');
|
|
panic = await _getBoolFromDb('panic');
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<bool> _getBoolFromDb(String key) async {
|
|
final value = await db.get(key: key, field: 'value');
|
|
return value.toString() == 'true';
|
|
}
|
|
|
|
Future<bool> _toggleBoolInDb(String key) async {
|
|
final currentValue = await _getBoolFromDb(key);
|
|
final newValue = !currentValue;
|
|
await db.update(key, newValue.toString(), 'util');
|
|
return newValue;
|
|
}
|
|
|
|
void _showAlertDialog(BuildContext context, String title, String content,
|
|
Function() onConfirm) {
|
|
showAlertDialog(
|
|
context,
|
|
'Delete account',
|
|
'Are you sure you want to delete your account?',
|
|
onConfirm(),
|
|
);
|
|
}
|
|
|
|
void _showSnackBar(BuildContext context, String text,
|
|
{bool isError = false}) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
text,
|
|
style: TextStyle(color: FlutterFlowTheme.of(context).info),
|
|
),
|
|
backgroundColor: isError
|
|
? FlutterFlowTheme.of(context).error
|
|
: FlutterFlowTheme.of(context).success,
|
|
duration: const Duration(seconds: 3),
|
|
behavior: SnackBarBehavior.floating,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(30),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showPassKey(
|
|
BuildContext context, void Function(String) onChange) async {
|
|
await showModalBottomSheet(
|
|
isScrollControlled: true,
|
|
backgroundColor: Colors.transparent,
|
|
useSafeArea: true,
|
|
context: context,
|
|
builder: (context) {
|
|
return Padding(
|
|
padding: MediaQuery.viewInsetsOf(context),
|
|
child: PassKeyTemplateWidget(
|
|
toggleActionStatus: (key) async {
|
|
onChange.call(key);
|
|
},
|
|
),
|
|
);
|
|
},
|
|
).catchError((err, stack) {
|
|
final String content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Error changing key',
|
|
ptText: 'Erro ao alterar senha',
|
|
);
|
|
_showSnackBar(context, content, isError: true);
|
|
}).whenComplete(() => notifyListeners());
|
|
}
|
|
}
|