ui_service.dart 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import 'dart:convert';
  2. import 'package:another_flushbar/flushbar.dart';
  3. import 'package:auto_route/auto_route.dart';
  4. import 'package:easy_localization/easy_localization.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:lottie/lottie.dart';
  7. import 'package:page_transition/page_transition.dart';
  8. import 'package:telnow_mobile_new/src/injector/injector.dart';
  9. import 'package:telnow_mobile_new/src/layouts/auth/qr.dart';
  10. import 'package:telnow_mobile_new/src/layouts/mobile/history_forum.dart';
  11. import 'package:telnow_mobile_new/src/layouts/mobile/message_list.dart';
  12. import 'U.dart';
  13. import 'package:telnow_mobile_new/src/storage/sharedpreferences/shared_preferences_manager.dart';
  14. import 'package:telnow_mobile_new/src/api/jwt_token.dart';
  15. enum ErrorType {
  16. noInternet, serverError, connectionError, invalidAccount, expiredAccount
  17. }
  18. final SharedPreferencesManager _sharedPreferencesManager = locator<SharedPreferencesManager>();
  19. final JwtToken token = JwtToken();
  20. class UIService {
  21. static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
  22. static BuildContext? get context => navigatorKey.currentContext;
  23. static void goNotification(Map<String, dynamic> list) {
  24. final ctx = context;
  25. if (ctx == null) return;
  26. if (list['type'] == 'MESSAGE') {
  27. Navigator.push(
  28. ctx,
  29. PageTransition(
  30. type: PageTransitionType.rightToLeft,
  31. child: MobMessageListPage(null),
  32. ),
  33. );
  34. } else if (list['type'] == 'FORUM') {
  35. Navigator.push(
  36. ctx,
  37. PageTransition(
  38. type: PageTransitionType.rightToLeft,
  39. child: MobHistoryForumPage(
  40. data: jsonDecode(list['requestHistory']),
  41. ),
  42. ),
  43. );
  44. } else {
  45. final pid = U.getPidFromUrl(ctx.router.currentUrl);
  46. ctx.router.removeLast();
  47. ctx.navigateToPath("/app/$pid/menu/history");
  48. }
  49. }
  50. static void navigateTo(String routePath) {
  51. final ctx = context;
  52. if (ctx != null) {
  53. AutoRouter.of(ctx).navigatePath(routePath);
  54. }
  55. }
  56. static void navigateNamed(String routePath) {
  57. final ctx = context;
  58. if (ctx != null) {
  59. AutoRouter.of(ctx).removeLast();
  60. AutoRouter.of(ctx).navigatePath(routePath);
  61. }
  62. }
  63. static void showError(String message) {
  64. final ctx = context;
  65. if (ctx != null) {
  66. Flushbar(
  67. message: message,
  68. icon: Icon(
  69. Icons.info_outline,
  70. size: 28.0,
  71. color: Colors.red,
  72. ),
  73. duration: Duration(seconds: 5),
  74. flushbarPosition: FlushbarPosition.BOTTOM,
  75. margin: EdgeInsets.all(8),
  76. borderRadius: BorderRadius.all(Radius.circular(8)),
  77. ).show(ctx);
  78. }
  79. }
  80. static void showSuccess(message) {
  81. final ctx = context;
  82. if (ctx != null) {
  83. Flushbar(
  84. message: message,
  85. icon: Icon(
  86. Icons.info_outline,
  87. size: 28.0,
  88. color: Colors.green,
  89. ),
  90. duration: Duration(seconds: 5),
  91. flushbarPosition: FlushbarPosition.BOTTOM,
  92. margin: EdgeInsets.all(8),
  93. borderRadius: BorderRadius.all(Radius.circular(8)),
  94. ).show(ctx);
  95. }
  96. }
  97. static bool get isCurrentRouteInactive {
  98. final ctx = context;
  99. final route = ModalRoute.of(ctx ?? navigatorKey.currentState?.context ?? ctx!);
  100. return route?.isCurrent != true;
  101. }
  102. static void pop() {
  103. navigatorKey.currentState?.maybePop();
  104. }
  105. static void setLocale(code) {
  106. final ctx = context;
  107. if(ctx == null) return;
  108. ctx.setLocale(code);
  109. }
  110. static void showLoading({String? text, String? lottie}){
  111. final ctx = context;
  112. if(ctx == null) return;
  113. showDialog(
  114. context: ctx,
  115. barrierDismissible: false,
  116. barrierColor: lottie != null ? Colors.white : null,
  117. builder: (BuildContext context) {
  118. return WillPopScope(
  119. onWillPop: () => Future.value(false),
  120. child: Material(
  121. type: MaterialType.transparency,
  122. child: Center(
  123. child: Column(
  124. mainAxisSize: MainAxisSize.min,
  125. children: [
  126. lottie != null
  127. ? Lottie.asset('assets/image/lottie/$lottie', width: 250, height: 250, fit: BoxFit.fill)
  128. : CircularProgressIndicator(color: primaryColor),
  129. SizedBox(height: 5),
  130. Text(
  131. text ?? 'inProcess'.tr(),
  132. style: TextStyle(color: lottie != null ? Colors.black : Colors.white),
  133. ),
  134. ],
  135. ),
  136. ),
  137. ),
  138. );
  139. }
  140. );
  141. }
  142. static void closeLoading(){
  143. Navigator.of(navigatorKey.currentContext!, rootNavigator: true).pop();
  144. }
  145. static void handlingError(ErrorType type) {
  146. var data = [
  147. {
  148. 'image': 'NoInternet.png',
  149. 'title': 'noInternetTitle'.tr(),
  150. 'description': 'noInternetDesc'.tr()
  151. },
  152. {
  153. 'image': 'ErrorServer.png',
  154. 'title': 'errorServerTitle'.tr(),
  155. 'description': 'errorServerDesc'.tr()
  156. },
  157. {
  158. 'image': 'ErrorConnection.png',
  159. 'title': 'errorConnectTitle'.tr(),
  160. 'description': 'errorConnectDesc'.tr()
  161. },
  162. {
  163. 'image': 'ErrorAuth.png',
  164. 'title': 'invalidAccountTitle'.tr(),
  165. 'description': 'invalidAccountDesc'.tr()
  166. },
  167. {
  168. 'image': 'ErrorAuth.png',
  169. 'title': 'expAccountTitle'.tr(),
  170. 'description': 'expAccountDesc'.tr()
  171. }
  172. ];
  173. int idx = type.index;
  174. final ctx = context;
  175. if (ctx == null) return;
  176. void clearSharedPreferencesKey(){
  177. _sharedPreferencesManager.clearKey(SharedPreferencesManager.keyAccessCode);
  178. _sharedPreferencesManager.clearKey(SharedPreferencesManager.keySerialCode);
  179. _sharedPreferencesManager.clearKey(SharedPreferencesManager.keyAccessToken);
  180. _sharedPreferencesManager.clearKey(SharedPreferencesManager.keyRefreshToken);
  181. _sharedPreferencesManager.clearKey(SharedPreferencesManager.keyUsername);
  182. _sharedPreferencesManager.clearKey(SharedPreferencesManager.keyIsLogin);
  183. _sharedPreferencesManager.clearKey(SharedPreferencesManager.keyScoope);
  184. _sharedPreferencesManager.clearKey(SharedPreferencesManager.debugString);
  185. _sharedPreferencesManager.clearKey(SharedPreferencesManager.keyHistoryMark);
  186. }
  187. showModalBottomSheet<void>(
  188. context: ctx,
  189. backgroundColor: Colors.white,
  190. isDismissible: false,
  191. enableDrag: false,
  192. builder: (BuildContext context) {
  193. return SingleChildScrollView(
  194. child: Column(
  195. mainAxisSize: MainAxisSize.min,
  196. children: <Widget>[
  197. Container(
  198. padding: const EdgeInsets.fromLTRB(15, 10, 15, 0),
  199. alignment: Alignment.centerRight,
  200. child: GestureDetector(
  201. child: Icon(Icons.clear),
  202. onTap: () => Navigator.of(context).pop(),
  203. ),
  204. ),
  205. Container(
  206. width: 200,
  207. padding: const EdgeInsets.fromLTRB(15, 0, 15, 10),
  208. child: Image(
  209. image: AssetImage('assets/image/error/${data[idx]['image']!}'))),
  210. Padding(
  211. padding: const EdgeInsets.fromLTRB(15, 0, 15, 10),
  212. child: Text(data[idx]['title']!,
  213. style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
  214. textAlign: TextAlign.center),
  215. ),
  216. Padding(
  217. padding: const EdgeInsets.fromLTRB(15, 0, 15, 10),
  218. child: Text(data[idx]['description']!,
  219. style: TextStyle(fontSize: 14),
  220. textAlign: TextAlign.center),
  221. ),
  222. if (idx > 2)
  223. Padding(
  224. padding: const EdgeInsets.fromLTRB(15, 0, 15, 10),
  225. child: SizedBox(
  226. width: double.infinity,
  227. height: 45,
  228. child: TextButton(
  229. style: ButtonStyle(
  230. backgroundColor:
  231. WidgetStateProperty.all<Color>(primaryColor),
  232. shape: WidgetStateProperty.all<
  233. RoundedRectangleBorder>(
  234. RoundedRectangleBorder(
  235. borderRadius: BorderRadius.circular(5),
  236. ))),
  237. child: Text('logout'.tr().toUpperCase(),
  238. style: TextStyle(color: Colors.white),
  239. textAlign: TextAlign.center),
  240. onPressed: () {
  241. if (idx == 4) {
  242. clearSharedPreferencesKey();
  243. Navigator.pushAndRemoveUntil(
  244. context,
  245. MaterialPageRoute(
  246. builder: (context) => NewQrPage()),
  247. ModalRoute.withName("/home"));
  248. } else {
  249. var pid = U.getPidFromUrl(context.router.currentUrl);
  250. token.logout();
  251. context.router.removeLast();
  252. context.navigateToPath("/app/$pid/login");
  253. }
  254. },
  255. )),
  256. )
  257. ],
  258. ),
  259. );
  260. });
  261. }
  262. static String getCurrentUrl(){
  263. final ctx = context;
  264. if(ctx == null) return '';
  265. return ctx.router.currentUrl;
  266. }
  267. }