256 lines
10 KiB
Dart
256 lines
10 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_util.dart';
|
|
import 'package:hub/flutter_flow/nav/nav.dart';
|
|
import 'package:hub/shared/helpers/base_storage.dart';
|
|
import 'package:hub/shared/helpers/storage_helper.dart';
|
|
import 'package:hub/shared/services/authentication/authentication_service.dart';
|
|
import 'package:hub/shared/services/localization/localization_service.dart';
|
|
import 'package:share_plus/share_plus.dart';
|
|
|
|
import '../../shared/utils/snackbar_util.dart';
|
|
|
|
class PreferencesPageModel with ChangeNotifier {
|
|
final unfocusNode = FocusNode();
|
|
|
|
|
|
late bool isFingerprint = false;
|
|
late bool isPerson = false;
|
|
late bool isNotify = false;
|
|
late bool isAccess = false;
|
|
late bool isPanic = false;
|
|
|
|
Future<void> _initialize() async {
|
|
isFingerprint = await StorageHelper.instance.get(SQLiteStorageKey.fingerprint.value, Storage.SQLiteStorage) == 'true';
|
|
isPerson = await StorageHelper.instance.get(SQLiteStorageKey.person.value, Storage.SQLiteStorage) == 'true';
|
|
isNotify = await StorageHelper.instance.get(SQLiteStorageKey.notify.value, Storage.SQLiteStorage) == 'true';
|
|
isAccess = await StorageHelper.instance.get(SQLiteStorageKey.access.value, Storage.SQLiteStorage) == 'true';
|
|
isPanic = await StorageHelper.instance.get(SQLiteStorageKey.panic.value, Storage.SQLiteStorage) == 'true';
|
|
notifyListeners();
|
|
}
|
|
|
|
PreferencesPageModel() {
|
|
_initialize();
|
|
}
|
|
|
|
Future<void> enablePerson(BuildContext context) async {
|
|
final String userDevUUID = (await StorageHelper.instance.get(SQLiteStorageKey.userDevUUID.value, Storage.SQLiteStorage)) ?? '';
|
|
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;
|
|
String value = !isNotify ? 'N' : 'S';
|
|
await PhpGroup.changeNotifica.call(notifica: value).then((value) async {
|
|
if (value.jsonBody['error'] == false) {
|
|
await StorageHelper.instance.set(SQLiteStorageKey.notify.value, isNotify ? 'false' : 'true',Storage.SQLiteStorage);
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Notification changed successfully',
|
|
ptText: 'Notificação alterada com sucesso',
|
|
);
|
|
notifyListeners();
|
|
SnackBarUtil.showSnackBar(context, content);
|
|
} else {
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Error changing notification',
|
|
ptText: 'Erro ao alterar notificação',
|
|
);
|
|
SnackBarUtil.showSnackBar(context, content, isError: true);
|
|
}
|
|
}).catchError((e, s) {
|
|
log('toggleNotify', error: e, stackTrace: s);
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Error changing notification',
|
|
ptText: 'Erro ao alterar notificação',
|
|
);
|
|
SnackBarUtil.showSnackBar(context, content, isError: true);
|
|
})
|
|
.then((_) async => isNotify = await StorageHelper.instance.get(SQLiteStorageKey.notify.value, Storage.SQLiteStorage) == 'true')
|
|
.whenComplete(() => notifyListeners());
|
|
context.pop();
|
|
}
|
|
|
|
showAlertDialog(context, title, content, onConfirm);
|
|
}
|
|
|
|
Future<void> toggleAccess(BuildContext context) async {
|
|
onChange(String key) async {
|
|
await StorageHelper.instance.set(SecureStorageKey.accessPass.value, key, Storage.SecureStorage);
|
|
await PhpGroup.changePass.call(newSenha: key).then((value) async {
|
|
final String content;
|
|
if (jsonDecode(value.jsonBody['error'].toString()) == false) {
|
|
await StorageHelper.instance.set(SQLiteStorageKey.access.value, isAccess ? 'false' : 'true', Storage.SQLiteStorage);
|
|
notifyListeners();
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Access pass changed successfully',
|
|
ptText: 'Senha de acesso alterada com sucesso',
|
|
);
|
|
SnackBarUtil.showSnackBar(context, content);
|
|
} else {
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
ptText: 'Erro ao alterar senha de acesso',
|
|
enText: 'Error changing access pass',
|
|
);
|
|
SnackBarUtil.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',
|
|
);
|
|
SnackBarUtil.showSnackBar(context, content, isError: true);
|
|
})
|
|
.then((_) async => isAccess = await StorageHelper.instance.get(SQLiteStorageKey.access.value, Storage.SQLiteStorage) == 'true')
|
|
.whenComplete(() => notifyListeners());
|
|
}
|
|
|
|
_showPassKey(context, onChange);
|
|
}
|
|
|
|
Future<void> togglePanic(BuildContext context) async {
|
|
onChange(String key) async {
|
|
await StorageHelper.instance.set(SecureStorageKey.panicPass.value, key, Storage.SecureStorage);
|
|
await PhpGroup.changePanic
|
|
.call(newSenhaPanico: key)
|
|
.then((value) async {
|
|
final String content;
|
|
if (jsonDecode(value.jsonBody['error'].toString()) == false) {
|
|
await StorageHelper.instance.set(SQLiteStorageKey.panic.value, isPanic ? 'false' : 'true', Storage.SQLiteStorage);
|
|
notifyListeners();
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Panic password changed successfully',
|
|
ptText: 'Senha de pânico alterada com sucesso',
|
|
);
|
|
SnackBarUtil.showSnackBar(context, content);
|
|
} else {
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
ptText: 'Erro ao alterar senha de pânico',
|
|
enText: 'Error changing panic password',
|
|
);
|
|
SnackBarUtil.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',
|
|
);
|
|
SnackBarUtil.showSnackBar(context, content, isError: true);
|
|
})
|
|
.then((_) async => isPanic = await StorageHelper.instance.get(SQLiteStorageKey.panic.value, Storage.SQLiteStorage) == '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 {
|
|
isFingerprint = !isFingerprint;
|
|
await StorageHelper.instance.set(SecureStorageKey.fingerprintPass.value, key ?? '', Storage.SecureStorage);
|
|
await StorageHelper.instance.set(SQLiteStorageKey.fingerprint.value, isFingerprint ? 'true' : 'false', Storage.SQLiteStorage);
|
|
notifyListeners();
|
|
SnackBarUtil.showSnackBar(context, content);
|
|
isFingerprint = await StorageHelper.instance.get(SQLiteStorageKey.fingerprint.value, Storage.SQLiteStorage) == 'true';
|
|
|
|
}
|
|
|
|
isFingerprint ? 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 => AuthenticationService.deleteAccount(context);
|
|
|
|
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 => AuthenticationService.signOut(context);
|
|
|
|
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 => LocalizationService.unlinkLocal(context);
|
|
|
|
showAlertDialog(context, title, content, onConfirm);
|
|
}
|
|
|
|
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',
|
|
);
|
|
SnackBarUtil.showSnackBar(context, content, isError: true);
|
|
}).whenComplete(() => notifyListeners());
|
|
}
|
|
|
|
|
|
|
|
}
|