account.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'package:auto_route/auto_route.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:provider/provider.dart';
  4. import 'package:telnow_mobile_new/main.dart';
  5. import 'package:telnow_mobile_new/src/api/api_auth_provider.dart';
  6. import 'package:telnow_mobile_new/src/api/jwt_token.dart';
  7. import 'package:telnow_mobile_new/src/layouts/components/template.dart';
  8. import 'package:telnow_mobile_new/src/storage/sharedpreferences/shared_preferences_manager.dart';
  9. import 'package:telnow_mobile_new/src/utils/C.dart';
  10. import 'package:telnow_mobile_new/src/utils/U.dart';
  11. import 'package:telnow_mobile_new/src/utils/provider.dart';
  12. import 'package:telnow_mobile_new/src/utils/ui_service.dart';
  13. class AccountFunction{
  14. final ApiAuthProvider apiAuthProvider = ApiAuthProvider();
  15. final NotificationClass notification = NotificationClass();
  16. final JwtToken token = JwtToken();
  17. final SharedPreferencesManager _sharedPreferencesManager = SharedPreferencesManager();
  18. getUser(BuildContext context) async {
  19. final userModule = Provider.of<UserModule>(context, listen: false);
  20. var res = await token.getUserData();
  21. if (res != null) {
  22. var ver = await apiAuthProvider.getDataNoAuth('/api/license');
  23. userModule.setSerialNum(ver['serialNumber']);
  24. userModule.setList(res);
  25. } else {
  26. userModule.setResetData(true);
  27. }
  28. }
  29. switchLang(code, user) async {
  30. UIService.showLoading();
  31. var locale = 'id';
  32. if(code is int){
  33. var vl = U.retValidLang();
  34. locale = vl[code+1];
  35. code = code.toString();
  36. } else {
  37. locale = code;
  38. }
  39. try{
  40. Map<String, dynamic> p;
  41. if(await U.isCompatibleWith(VersionKey.multiBahasa)){
  42. p = {'userId': user['userId'], 'language': 'ID', '_language': code.toUpperCase()};
  43. } else {
  44. p = {'userId': user['userId'], 'language': code.toUpperCase()};
  45. }
  46. var res = await apiAuthProvider.patchData('/api/informants/${user['id']}', p);
  47. if (res != null) {
  48. UIService.closeLoading();
  49. UIService.setLocale(Locale(locale));
  50. // context.setLocale(Locale(locale));
  51. notification.startNotification(code: code);
  52. } else {
  53. UIService.closeLoading();
  54. }
  55. }catch(e){
  56. UIService.closeLoading();
  57. }
  58. }
  59. Future<bool> setDndStatus(String id, bool dnd)async{
  60. var res = await apiAuthProvider.patchData('/api/informants/$id/setDnd', null, params: {'dnd': '${!dnd}'});
  61. if (res != null) {
  62. // Provider.of<UserModule>(context, listen: false).setDndStatus(dnd);
  63. return true;
  64. }
  65. return false;
  66. }
  67. logoutAction(BuildContext context)async{
  68. showLoading(context, text: '');
  69. String pid = U.getPidFromUrl(context.router.currentUrl);
  70. void loggingOut(){
  71. Navigator.of(context).pop();
  72. UIService.closeLoading();
  73. token.logout();
  74. context.navigateToPath("/app/$pid/login");
  75. }
  76. try {
  77. var tkn = _sharedPreferencesManager.getString(SharedPreferencesManager.keyFcmToken);
  78. if (tkn == null)
  79. {
  80. loggingOut();
  81. }
  82. else
  83. {
  84. var param = {'token': tkn, 'whiteListToken': false};
  85. var res = await apiAuthProvider.postData('/api/fcmTokens/remove/', null, param);
  86. if (res != null && res['success'])
  87. {
  88. loggingOut();
  89. }
  90. else
  91. {
  92. Future.delayed(const Duration(seconds: 5), () => UIService.closeLoading());
  93. }
  94. }
  95. } catch(e) {
  96. debugPrint(e.toString());
  97. UIService.navigateNamed("/app/$pid/login");
  98. }
  99. }
  100. }