GSI - Employe Self Service Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
11 KiB

2 months ago
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:connectivity_plus/connectivity_plus.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter/services.dart';
  8. import 'package:fluttertoast/fluttertoast.dart';
  9. import 'package:google_fonts/google_fonts.dart';
  10. import 'package:employee_selfservice_mobile/Screens/Login/login_screen.dart';
  11. import 'package:employee_selfservice_mobile/Screens/Splash/loginstatus_post.dart';
  12. import 'package:employee_selfservice_mobile/constants.dart';
  13. import 'package:internet_connection_checker/internet_connection_checker.dart';
  14. import 'package:lottie/lottie.dart';
  15. import 'package:shared_preferences/shared_preferences.dart';
  16. import '../Home/home_screen.dart';
  17. import '../Splash/background.dart';
  18. import 'package:url_launcher/url_launcher.dart';
  19. import 'dart:developer' as logDev;
  20. class Splash extends StatefulWidget {
  21. const Splash({Key? key}) : super(key: key);
  22. @override
  23. _SplashState createState() => _SplashState();
  24. }
  25. class _SplashState extends State<Splash> {
  26. late StreamSubscription subscription;
  27. bool isDeviceConnected = false;
  28. bool isAlertSet = false;
  29. @override
  30. void initState() {
  31. //getConnectivity();
  32. Timer(Duration(seconds: 10), () => loginStatus(context));
  33. super.initState();
  34. }
  35. /*getConnectivity() =>
  36. subscription = Connectivity().onConnectivityChanged.listen(
  37. (ConnectivityResult result) async {
  38. isDeviceConnected = await InternetConnectionChecker().hasConnection;
  39. if (!isDeviceConnected && isAlertSet == false) {
  40. showConnectivityDialogBox();
  41. setState(() => isAlertSet = true);
  42. }
  43. },
  44. );*/
  45. @override
  46. Widget build(BuildContext context) {
  47. return Scaffold(
  48. resizeToAvoidBottomInset: false,
  49. body: Stack(
  50. children: <Widget>[
  51. Background(),
  52. SplashScreen(),
  53. ],
  54. ));
  55. }
  56. /*showConnectivityDialogBox() =>
  57. showCupertinoDialog<String>(
  58. context: context,
  59. builder: (BuildContext context) =>
  60. CupertinoAlertDialog(
  61. title: const Text('No Connection'),
  62. content: const Text('Please check your internet connectivity'),
  63. actions: <Widget>[
  64. Column(
  65. children: [
  66. SizedBox(
  67. width: 250,
  68. height: 250,
  69. child: LottieBuilder.asset(
  70. //'assets/animation/animation_no_internet.json',
  71. 'assets/animation/animation_no_internet_3.json',
  72. repeat: true),
  73. ),
  74. ],
  75. ),
  76. TextButton(
  77. onPressed: () async {
  78. Navigator.pop(context, 'Cancel');
  79. setState(() => isAlertSet = false);
  80. isDeviceConnected =
  81. await InternetConnectionChecker().hasConnection;
  82. if (!isDeviceConnected && isAlertSet == false) {
  83. showConnectivityDialogBox();
  84. setState(() => isAlertSet = true);
  85. }
  86. },
  87. child: const Text('Retry'),
  88. ),
  89. ],
  90. ),
  91. );*/
  92. }
  93. class SplashScreen extends StatelessWidget {
  94. const SplashScreen({Key? key}) : super(key: key);
  95. @override
  96. Widget build(BuildContext context) {
  97. var size = MediaQuery.of(context).size;
  98. return Scaffold(
  99. body: Container(
  100. width: double.infinity,
  101. height: double.infinity,
  102. decoration: BoxDecoration(
  103. gradient: LinearGradient(
  104. begin: Alignment.topRight,
  105. end: Alignment.bottomRight,
  106. colors: [
  107. Color(0xFF4858A7),
  108. Color(0xFF4858A7),
  109. Color(0xFF6474C6),
  110. //Color(0xFF8C99DD),
  111. //Color(0xFFFFFFFF)
  112. ])),
  113. child: Center(
  114. child: Column(
  115. mainAxisAlignment: MainAxisAlignment.center,
  116. crossAxisAlignment: CrossAxisAlignment.center,
  117. children: [
  118. Container(
  119. /*decoration: BoxDecoration(
  120. shape: BoxShape.circle,
  121. color: Colors.white
  122. ),*/
  123. child: SizedBox(
  124. width: size.width * 0.55,
  125. //height: 250,
  126. child: Image.asset('assets/animation/iconapp2.gif')
  127. /*LottieBuilder.asset('assets/animation/animation_4.json',
  128. repeat: true, ),*/
  129. ),
  130. ),
  131. /* Text(appName,
  132. style: GoogleFonts.carterOne(fontSize: 25, color: Colors.white))*/
  133. ],
  134. ),
  135. ),
  136. ));
  137. }
  138. }
  139. Future<void> loginStatus(BuildContext context) async {
  140. final SharedPreferences prefs = await SharedPreferences.getInstance();
  141. String? version = prefs.getString('version');
  142. String? device = prefs.getString('device');
  143. String? session = prefs.getString('session');
  144. String? notif_token = prefs.getString('notif_token');
  145. if (session == null) {
  146. version = "1.0.0";
  147. session = "";
  148. notif_token = "";
  149. if (Platform.isAndroid){
  150. device = "android";
  151. } else if (Platform.isIOS){
  152. device = "ios";
  153. }
  154. }
  155. //logDev.log(session, name: "SESSIONNYA");
  156. LoginStatus_Post.connectToAPI(version!, device!, session, notif_token!).then((valueResult) async {
  157. Map<String, dynamic> object = json.decode(valueResult);
  158. if (object.containsKey("result").toString() == "true") {
  159. String status = object['result']['status'].toString();
  160. if (status == "success") {
  161. Fluttertoast.showToast(
  162. msg: "You are already logged in",
  163. toastLength: Toast.LENGTH_SHORT,
  164. gravity: ToastGravity.CENTER,
  165. timeInSecForIosWeb: 1,
  166. textColor: Colors.white,
  167. fontSize: 16.0);
  168. Navigator.pushReplacement(
  169. context, MaterialPageRoute(builder: (context) => HomeView()));
  170. } else if (status == "failed") {
  171. String message = object['result']['message'].toString();
  172. if (message == "Not Logged In") {
  173. Fluttertoast.showToast(
  174. msg: message,
  175. toastLength: Toast.LENGTH_SHORT,
  176. gravity: ToastGravity.CENTER,
  177. timeInSecForIosWeb: 1,
  178. textColor: Colors.white,
  179. fontSize: 16.0);
  180. Navigator.pushReplacement(
  181. context, MaterialPageRoute(builder: (context) => LoginView()));
  182. } else {
  183. String title = object['result']['title'].toString();
  184. String action = object['result']['action'].toString();
  185. String block = object['result']['block'].toString();
  186. if (block == "false") {
  187. Widget okButton = TextButton(
  188. child: Text(action),
  189. onPressed: () async {
  190. Uri url = Uri.parse(Uri.encodeFull("https://play.google.com/store/apps"));
  191. //final Uri uri = Uri(scheme: "https", host: "play.google.com");
  192. if(!await launchUrl(url, mode: LaunchMode.externalApplication)) {
  193. throw "Can not launch url";
  194. }
  195. },
  196. );
  197. Widget noButton = TextButton(
  198. child: Text("Later"),
  199. onPressed: () {
  200. if (session == ""){
  201. Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => LoginView()));
  202. } else {
  203. Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => HomeView()));
  204. }
  205. },
  206. );
  207. // set up the AlertDialog
  208. AlertDialog alert = AlertDialog(
  209. title: Text(appName),
  210. content: Text(title + "\n" + message),
  211. actions: [noButton, okButton],
  212. );
  213. // show the dialog
  214. showDialog(
  215. context: context,
  216. barrierDismissible: false,
  217. builder: (BuildContext context) {
  218. return alert;
  219. },
  220. );
  221. } else if (block == "true") {
  222. Widget okButton = TextButton(
  223. child: Text(action),
  224. onPressed: () async {
  225. if (action == "update") {
  226. _launchURL(Uri.parse("https://play.google.com/store/apps"));
  227. SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  228. Uri url = Uri.parse(Uri.encodeFull("https://play.google.com/store/apps"));
  229. //final Uri uri = Uri(scheme: "https", host: "play.google.com");
  230. if(!await launchUrl(url, mode: LaunchMode.externalApplication)) {
  231. throw "Can not launch url";
  232. }
  233. } else if (action == "close") {
  234. SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  235. }
  236. },
  237. );
  238. // set up the AlertDialog
  239. AlertDialog alert = AlertDialog(
  240. title: Text(appName),
  241. content: Text(title + "\n" + message),
  242. actions: [okButton],
  243. );
  244. // show the dialog
  245. showDialog(
  246. context: context,
  247. barrierDismissible: false,
  248. builder: (BuildContext context) {
  249. return alert;
  250. },
  251. );
  252. }
  253. }
  254. }
  255. } else {
  256. alertDialogFailedResponse(context);
  257. Fluttertoast.showToast(
  258. msg: "Server Response Error",
  259. toastLength: Toast.LENGTH_SHORT,
  260. gravity: ToastGravity.CENTER,
  261. timeInSecForIosWeb: 1,
  262. textColor: Colors.white,
  263. fontSize: 16.0);
  264. }
  265. });
  266. }
  267. _launchURL(Uri url) async {
  268. if (await canLaunchUrl(url)) {
  269. await launchUrl(url);
  270. } else {
  271. throw 'Could not launch $url';
  272. }
  273. }
  274. alertDialogFailedResponse(BuildContext context){
  275. Widget okButton = TextButton(
  276. child: Text("Refresh"),
  277. onPressed: () {
  278. Navigator.of(context, rootNavigator: true).pop();
  279. Navigator.pushReplacement(context, MaterialPageRoute(
  280. builder: (context) => SplashScreen()));
  281. },
  282. );
  283. Widget noButton = TextButton(
  284. child: Text("Back"),
  285. onPressed: () {
  286. Navigator.of(context, rootNavigator: true).pop();
  287. Navigator.pop(context);
  288. },
  289. );
  290. // set up the AlertDialog
  291. AlertDialog alert = AlertDialog(
  292. title: Text(appName),
  293. content: Text("Server Response Error"),
  294. actions: [
  295. noButton,
  296. okButton,
  297. ],
  298. );
  299. // show the dialog
  300. showDialog(
  301. context: context,
  302. barrierDismissible: false,
  303. builder: (BuildContext context) {
  304. return alert;
  305. },
  306. );
  307. }