flutter-freaccess-hub/lib/pages/preferences_settings_page/preferences_settings_model....

776 lines
33 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:share_plus/share_plus.dart';
class PreferencesPageModel with ChangeNotifier {
final unfocusNode = FocusNode();
Future<void> toggleFingerprint(BuildContext context) async {
// AppState().checkBiometrics()
// .then((value) => AppState().authenticateBiometric()
// .then( (value) {
// AppState().fingerprint = !AppState().fingerprint;
// })
// .whenComplete(() => notifyListeners()));
if (AppState().fingerprint) {
AppState().fingerprint = false;
AppState().deleteFingerprintPass();
notifyListeners();
} else {
await showModalBottomSheet(
isScrollControlled: true,
backgroundColor: Colors.transparent,
useSafeArea: true,
context: context,
builder: (context) {
return Padding(
padding: MediaQuery.viewInsetsOf(context),
child: PassKeyTemplateWidget(
toggleActionStatus: (key) async {
AppState().fingerprintPass = key;
AppState().fingerprint = true;
},
),
);
},
).then((value) {
if (value) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
enText: 'Fingerprint changed successfully',
ptText: 'Impressão digital alterada com sucesso',
),
style: TextStyle(color: FlutterFlowTheme.of(context).info)),
backgroundColor: FlutterFlowTheme.of(context).success,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
}
}).catchError((err, stack) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
ptText: 'Erro ao alterar impressão digital',
enText: 'Error changing fingerprint',
),
style: TextStyle(color: FlutterFlowTheme.of(context).info)),
backgroundColor: FlutterFlowTheme.of(context).error,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
}).whenComplete(() => notifyListeners());
}
}
void enablePerson(BuildContext context) {
notifyListeners();
Share.share(
FFLocalizations.of(context).getVariableText(
ptText:
'Este é o meu identificador de acesso: ${AppState().userDevUUID}',
enText: 'This is my access identifier: ${AppState().userDevUUID}',
),
);
}
void toggleNotify(BuildContext context) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
backgroundColor: FlutterFlowTheme.of(context).primaryBackground,
title: Text(
FFLocalizations.of(context).getVariableText(
enText: AppState().notify
? 'Disable Access Notification'
: 'Access Notification',
ptText: AppState().notify
? 'Desativar notificação de acesso'
: 'Notificação de acesso'),
),
content: Text(
FFLocalizations.of(context).getVariableText(
ptText: AppState().notify
? 'Tem certeza que deseja desativar as suas notificações de acesso?'
: 'Tem certeza que deseja receber as suas notificações de acesso?',
enText: AppState().notify
? 'Are you sure you want to disable your access notifications?'
: 'Are you sure you want to receive your access notifications?'),
),
actions: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: [
FFButtonWidget(
text: FFLocalizations.of(context).getVariableText(
enText: 'No',
ptText: 'Não',
),
onPressed: () {
context.pop();
},
options: FFButtonOptions(
elevation: 0,
width: MediaQuery.of(context).size.width * 0.3,
height: MediaQuery.of(context).size.height * 0.05,
color: FlutterFlowTheme.of(context).primaryBackground,
textStyle: TextStyle(
color: FlutterFlowTheme.of(context).primaryText,
),
borderSide: BorderSide(
color: FlutterFlowTheme.of(context).primaryBackground,
width: 1,
),
borderRadius: BorderRadius.circular(10)),
),
FFButtonWidget(
onPressed: () async {
try {
PhpGroup.changeNotifica
.call(
userUUID: AppState().userUUID,
devUUID: AppState().devUUID,
cliID: AppState().cliUUID,
atividade: 'updVisitado',
notifica: AppState().notify ? 'S' : 'N',
)
.then((value) {
if (value.jsonBody['error'] == false) {
context.pop();
AppState().notify = !AppState().notify;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
ptText:
'Notificação alterada com sucesso',
enText:
'Notification changed successfully',
),
style: TextStyle(
color:
FlutterFlowTheme.of(context).info)),
backgroundColor:
FlutterFlowTheme.of(context).success,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
ptText: 'Erro ao alterar notificação',
enText: 'Error changing notification',
),
style: TextStyle(
color:
FlutterFlowTheme.of(context).info)),
backgroundColor:
FlutterFlowTheme.of(context).error,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
}
}).catchError((err) {
context.pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
enText: 'Error changing notification',
ptText: 'Erro ao alterar notificação',
),
style: TextStyle(
color:
FlutterFlowTheme.of(context).info)),
backgroundColor:
FlutterFlowTheme.of(context).error,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
}).whenComplete(() => notifyListeners());
} on Exception catch (e) {
context.pop();
}
},
text: FFLocalizations.of(context).getVariableText(
enText: 'Yes',
ptText: 'Sim',
),
options: FFButtonOptions(
elevation: 0,
width: MediaQuery.of(context).size.width * 0.3,
height: MediaQuery.of(context).size.height * 0.05,
color: FlutterFlowTheme.of(context).primaryBackground,
textStyle: TextStyle(
color: FlutterFlowTheme.of(context).primaryText,
),
borderSide: BorderSide(
color: FlutterFlowTheme.of(context).primaryBackground,
width: 1,
),
borderRadius: BorderRadius.circular(10),
),
),
],
),
],
);
});
notifyListeners();
}
void localUnlink(BuildContext context) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
backgroundColor: FlutterFlowTheme.of(context).primaryBackground,
title: Text(
FFLocalizations.of(context).getVariableText(
enText: 'Unlink device',
ptText: 'Desvincular dispositivo',
),
),
content: Text(
FFLocalizations.of(context).getVariableText(
enText: 'Are you sure you want to unlink this device?',
ptText: 'Tem certeza que deseja desvincular este dispositivo?',
),
),
actions: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: [
FFButtonWidget(
text: FFLocalizations.of(context).getVariableText(
enText: 'Cancel',
ptText: 'Cancelar',
),
onPressed: () {
context.pop();
},
options: FFButtonOptions(
width: MediaQuery.of(context).size.width * 0.3,
height: MediaQuery.of(context).size.height * 0.05,
color: FlutterFlowTheme.of(context).primaryBackground,
textStyle: TextStyle(
color: FlutterFlowTheme.of(context).primaryText,
),
borderSide: BorderSide(
color: FlutterFlowTheme.of(context).primaryBackground,
width: 1,
),
borderRadius: BorderRadius.circular(10)),
),
FFButtonWidget(
onPressed: () async {
try {
await PhpGroup.resopndeVinculo
.call(
userUUID: AppState().userUUID,
devUUID: AppState().devUUID,
cliID: AppState().cliUUID,
tarefa: 'I',
)
.then((value) {
if (value.jsonBody['error'] == false) {
AppState().deleteCliUUID();
AppState().deleteLocal();
AppState().deleteOwnerUUID();
context.pop();
context.go(
'/homePage',
extra: <String, dynamic>{
kTransitionInfoKey: const TransitionInfo(
hasTransition: true,
transitionType: PageTransitionType.scale,
alignment: Alignment.bottomCenter,
),
},
);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
enText: 'Device unlinked successfully',
ptText:
'Dispositivo desvinculado com sucesso',
),
style: TextStyle(
color:
FlutterFlowTheme.of(context).info)),
backgroundColor:
FlutterFlowTheme.of(context).success,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
}
})
// ignore: body_might_complete_normally_catch_error
.catchError((err, stack) {
context.pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
enText: 'Error unlinking device',
ptText: 'Erro ao desvincular dispositivo',
),
style: TextStyle(
color:
FlutterFlowTheme.of(context).info)),
backgroundColor:
FlutterFlowTheme.of(context).error,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
});
// notifyListeners();
} catch (err, stack) {
context.pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
enText: 'Error unlinking device',
ptText: 'Erro ao desvincular dispositivo',
),
style: TextStyle(
color: FlutterFlowTheme.of(context).info)),
backgroundColor: FlutterFlowTheme.of(context).error,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
}
},
text: FFLocalizations.of(context).getVariableText(
enText: 'Unlink',
ptText: 'Desvincular',
),
options: FFButtonOptions(
width: MediaQuery.of(context).size.width * 0.3,
height: MediaQuery.of(context).size.height * 0.05,
color: FlutterFlowTheme.of(context).primaryBackground,
textStyle: TextStyle(
color: FlutterFlowTheme.of(context).primaryText,
),
borderSide: BorderSide(
color: FlutterFlowTheme.of(context).primaryBackground,
width: 1,
),
borderRadius: BorderRadius.circular(10),
),
),
],
),
],
);
});
// notifyListeners();
}
void deleteAccount(BuildContext context) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
backgroundColor: FlutterFlowTheme.of(context).primaryBackground,
title: Text(
FFLocalizations.of(context).getVariableText(
enText: 'Delete account',
ptText: 'Deletar conta',
),
),
content: Text(
FFLocalizations.of(context).getVariableText(
enText: 'Are you sure you want to delete your account?',
ptText: 'Tem certeza que deseja deletar sua conta?',
),
),
actions: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: [
FFButtonWidget(
onPressed: () => context.pop(), // Navigator.pop(context),
options: FFButtonOptions(
width: MediaQuery.of(context).size.width * 0.3,
height: MediaQuery.of(context).size.height * 0.05,
color: FlutterFlowTheme.of(context).primaryBackground,
textStyle: TextStyle(
color: FlutterFlowTheme.of(context).primaryText,
),
borderSide: BorderSide(
color: FlutterFlowTheme.of(context).primaryBackground,
width: 1,
),
borderRadius: BorderRadius.circular(10),
),
text: FFLocalizations.of(context).getVariableText(
enText: 'Cancel',
ptText: 'Cancelar',
),
),
FFButtonWidget(
onPressed: () async {
try {
await PhpGroup.deleteAccount
.call(
devUUID: AppState().devUUID,
userUUID: AppState().userUUID,
)
.then((value) {
if (value.jsonBody['error'] == false) {
AppState().deleteAll();
AppState().isLogged = false;
context.go(
'/welcomePage',
extra: <String, dynamic>{
kTransitionInfoKey: const TransitionInfo(
hasTransition: true,
transitionType: PageTransitionType.scale,
alignment: Alignment.bottomCenter,
),
},
);
}
}).catchError((err) {
log(err.toString());
context.pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
enText: 'Error deleting account',
ptText: 'Erro ao deletar conta',
),
style: TextStyle(
color:
FlutterFlowTheme.of(context).info)),
backgroundColor:
FlutterFlowTheme.of(context).error,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
});
notifyListeners();
} catch (err, stack) {
context.pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
enText: 'Error deleting account',
ptText: 'Erro ao deletar conta',
),
style: TextStyle(
color: FlutterFlowTheme.of(context).info)),
backgroundColor: FlutterFlowTheme.of(context).error,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
}
},
options: FFButtonOptions(
width: MediaQuery.of(context).size.width * 0.3,
height: MediaQuery.of(context).size.height * 0.05,
color: FlutterFlowTheme.of(context).primaryBackground,
textStyle: TextStyle(
color: FlutterFlowTheme.of(context).primaryText,
),
borderSide: BorderSide(
color: FlutterFlowTheme.of(context).primaryBackground,
width: 1,
),
borderRadius: BorderRadius.circular(10),
),
text: FFLocalizations.of(context).getVariableText(
enText: 'Delete',
ptText: 'Deletar',
),
),
],
),
],
);
});
notifyListeners();
}
Future<void> togglePass(BuildContext context) async {
// if (AppState().pass) {
// AppState().pass = false;
// AppState().deleteAccessPass();
// notifyListeners();
// } else {
await showModalBottomSheet(
isScrollControlled: true,
backgroundColor: Colors.transparent,
useSafeArea: true,
context: context,
builder: (context) {
return Padding(
padding: MediaQuery.viewInsetsOf(context),
child: PassKeyTemplateWidget(
toggleActionStatus: (key) async {
AppState().accessPass = key;
notifyListeners();
await PhpGroup.changePass
.call(
userUUID: AppState().userUUID,
devUUID: AppState().devUUID,
cliID: AppState().cliUUID,
atividade: 'updVisitado',
newSenha: AppState().accessPass,
)
.then((value) {
// var error = jsonDecode(value.jsonBody['error'].toString());
if (jsonDecode(value.jsonBody['error'].toString()) == false) {
AppState().pass = true;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
enText: 'Access password changed successfully',
ptText: 'Senha de acesso alterada com sucesso',
),
style: TextStyle(
color: FlutterFlowTheme.of(context).info)),
backgroundColor: FlutterFlowTheme.of(context).success,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
ptText: 'Erro ao alterar senha de acesso',
enText: 'Error changing access password',
),
style: TextStyle(
color: FlutterFlowTheme.of(context).info)),
backgroundColor: FlutterFlowTheme.of(context).error,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
// AppState().pass = false;
}
}).catchError((error, StackTrace) {
// AppState().pass = false;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
ptText: 'Erro ao alterar senha de acesso',
enText: 'Error changing access password',
),
style: TextStyle(
color: FlutterFlowTheme.of(context).info)),
backgroundColor: FlutterFlowTheme.of(context).error,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
}).whenComplete(() => notifyListeners());
},
),
);
},
);
// }
}
Future<void> togglePanic(BuildContext context) 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 {
AppState().panicPass = key;
notifyListeners();
await PhpGroup.changePanic
.call(
userUUID: AppState().userUUID,
devUUID: AppState().devUUID,
cliID: AppState().cliUUID,
atividade: 'updVisitado',
newSenhaPanico: AppState().panicPass,
)
.then((value) {
AppState().panic = true;
if (jsonDecode(value.jsonBody['error'].toString()) == false) {
AppState().panic = true;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
enText: 'Panic password changed successfully',
ptText: 'Senha de pânico alterada com sucesso',
),
style: TextStyle(
color: FlutterFlowTheme.of(context).info)),
backgroundColor: FlutterFlowTheme.of(context).success,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
ptText: 'Erro ao alterar senha de pânico',
enText: 'Error changing panic password',
),
style: TextStyle(
color: FlutterFlowTheme.of(context).info)),
backgroundColor: FlutterFlowTheme.of(context).error,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
// AppState().panic = false;
}
}).catchError((e, s) {
// AppState().panic = false;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
FFLocalizations.of(context).getVariableText(
ptText: 'Erro ao alterar senha de pânico',
enText: 'Error changing panic password',
),
style: TextStyle(
color: FlutterFlowTheme.of(context).info)),
backgroundColor: FlutterFlowTheme.of(context).error,
duration: const Duration(seconds: 3),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
);
}).whenComplete(() => notifyListeners());
},
),
);
},
);
}
void logout(BuildContext context) async {
showAlertDialog(
context,
'Logout',
FFLocalizations.of(context).getVariableText(
enText: 'Are you sure you want to logout?',
ptText: 'Tem certeza',
), () async {
AppState().deleteAll();
// setState(() {});
context.go(
'/welcomePage',
extra: <String, dynamic>{
kTransitionInfoKey: const TransitionInfo(
hasTransition: true,
transitionType: PageTransitionType.scale,
alignment: Alignment.bottomCenter,
),
},
);
});
}
@override
void dispose() {
unfocusNode.dispose();
super.dispose();
}
}