account.dart 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import 'package:auto_route/auto_route.dart';
  2. import 'package:easy_localization/easy_localization.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:provider/provider.dart';
  5. import 'package:telnow_mobile_new/main.dart';
  6. import 'package:telnow_mobile_new/src/api/api_auth_provider.dart';
  7. import 'package:telnow_mobile_new/src/api/jwt_token.dart';
  8. import 'package:telnow_mobile_new/src/layouts/components/template.dart';
  9. import 'package:telnow_mobile_new/src/utils/U.dart';
  10. import 'package:telnow_mobile_new/src/utils/provider.dart';
  11. class AccountFunction{
  12. final ApiAuthProvider apiAuthProvider = ApiAuthProvider();
  13. final NotificationClass notification = NotificationClass();
  14. final JwtToken token = JwtToken();
  15. getUser(BuildContext context) async {
  16. var res = await token.getUserData(context);
  17. if (res != null) {
  18. var ver = await apiAuthProvider.getDataNoAuth('/api/license');
  19. Provider.of<UserModule>(context, listen: false).setSerialNum(ver['serialNumber']);
  20. Provider.of<UserModule>(context, listen: false).setList(res);
  21. } else {
  22. Provider.of<UserModule>(context, listen: false).setResetData(true);
  23. }
  24. }
  25. switchLang(BuildContext context, code, user) async {
  26. showLoading(context);
  27. var res = await apiAuthProvider.patchData('/api/informants/' + user['id'].toString(), {'userId': user['userId'], 'language': code.toUpperCase()}, context);
  28. if (res != null) {
  29. closeLoading(context);
  30. context.setLocale(Locale(code));
  31. notification.startNotification(context);
  32. Navigator.of(context).pop();
  33. }
  34. else{
  35. closeLoading(context);
  36. }
  37. }
  38. Future<bool> setDndStatus(BuildContext context, bool dnd)async{
  39. var res = await apiAuthProvider.patchData('/api/informants/${Provider.of<UserModule>(context, listen: false).user()['id']}/setDnd', null, context, params: {'dnd': '${!dnd}'});
  40. if (res != null) {
  41. Provider.of<UserModule>(context, listen: false).setDndStatus(dnd);
  42. return true;
  43. }
  44. return false;
  45. }
  46. logoutAction(BuildContext context)async{
  47. showLoading(context, text: '');
  48. String pid = U.getPidFromUrl(context.router.currentUrl);
  49. try {
  50. var tkn = await U.getFcmToken();
  51. if (tkn == null) {
  52. Navigator.of(context).pop();
  53. closeLoading(context);
  54. token.logout();
  55. // context.router.removeLast();
  56. context.navigateNamedTo("/app/$pid/login");
  57. } else {
  58. var param = {'token': tkn, 'whiteListToken': false};
  59. var res = await apiAuthProvider.postData('/api/fcmTokens/remove/', null, param, context);
  60. if (res != null && res['success']) {
  61. Navigator.of(context).pop();
  62. closeLoading(context);
  63. token.logout();
  64. // context.router.removeLast();
  65. context.navigateNamedTo("/app/$pid/login");
  66. }
  67. else{
  68. Future.delayed(const Duration(seconds: 5), () => closeLoading(context));
  69. }
  70. }
  71. } catch(e) {
  72. print(e.toString());
  73. // context.router.removeLast();
  74. context.navigateNamedTo("/app/$pid/login");
  75. }
  76. }
  77. }