forked from jafar260698/E-IMZO-INTEGRATION
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login_viewmodel.dart
155 lines (136 loc) · 4.21 KB
/
login_viewmodel.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import 'dart:async';
import 'dart:io';
import 'package:device_apps/device_apps.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';
import 'package:url_launcher/url_launcher.dart';
import 'apiservice/api_provider.dart';
import 'crc32.dart';
import 'crypto/gost_hash.dart';
import 'model/deep_link_response.dart';
class LoginViewModel extends BaseViewModel{
BuildContext context;
var _apiProvider = ApiProvider();
var _sendController = new TextEditingController();
TextEditingController get sendController => _sendController;
var _checkController = new TextEditingController();
TextEditingController get checkController => _checkController;
bool _isLoading = false;
bool get isLoading => _isLoading;
bool _isDeepLinkMessageVisible = false;
bool get isDeepLinkMessageVisible => _isDeepLinkMessageVisible;
bool _isVisible = false;
bool get isVisible => _isVisible;
Color _color=Colors.green;
Color get color=>_color;
String documentId;
String siteId;
String challange;
String responseSend="";
String successMessage="";
bool _successStatus = false;
void setContext(BuildContext context) {
this.context = context;
}
Future<void> callbackNewImzo() async {
String direct = "uz.yt.idcard.eimzo";
if (Platform.isAndroid||Platform.isIOS) {
bool isInstalled = await DeviceApps.isAppInstalled(direct);
if (isInstalled != false) {
send();
} else {
String url = "https://play.google.com/store/apps/details?id=$direct";
if (await canLaunch(url))
await launch(url);
else
throw 'Could not launch $url';
}
}
}
void send() {
_isLoading = true;
notifyListeners();
_apiProvider
.getDeepLink(_sendController.text.toString())
.then((value) => {
if (value != null)
{
print("Response Jafar $value"),
crc32Function(value),
}
}).catchError((onError) {
responseSend="Xatolik $onError";
_color=Colors.red;
_isVisible=true;
print(onError);
}).whenComplete(() {
_isLoading = false;
notifyListeners();
});
}
void crc32Function(DeepLinkResponse response) {
documentId = response.documentId;
challange = response.challange;
siteId = response.siteId;
responseSend="DokumentId: $documentId \n SiteID: $siteId\n Challange: $challange";
_color=Colors.green;
_isVisible=true;
notifyListeners();
}
void deepLink(){
_successStatus=false;
notifyListeners();
print("///////////\nsiteId: $siteId \n documentId: $documentId \n///////////");
print("///////////\nchallange: $challange \n challange hash: ${GostHash.hashGost(challange)}\n///////////");
var docHash = GostHash.hashGost(challange);
var code = siteId + documentId + docHash;
var crc32 = Crc32.calcHex(code);
code += crc32;
print("Deep Code $code");
_launchURL(code);
}
_launchURL(String code) async {
var _deepLink = 'eimzo://sign?qc=$code';
await canLaunch(_deepLink)
? launch(_deepLink)
: throw 'Could not launch $_deepLink';
recursion(102);
}
void recursion(int i) {
print(i);
if (!_successStatus) {
if (i > 0) {
checkStatus();
new Timer(Duration(seconds: 3), () => {i -= 3, recursion(i)});
} else {
print("Tizimga kirish ma'lumotlari yangilandi. Boshqadan urinib ko'ring");
return;
}
} else {
i = 0;
print("Muvaffaqiyatli bajarildi");
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("Muvaffaqiyatli bajarildi"),
));
successMessage="Muvaffaqiyatli bajarildi";
_isDeepLinkMessageVisible=true;
notifyListeners();
// getUserDataByDocumentID();
return;
}
}
void checkStatus() {
_apiProvider.checkStatus(documentId,_checkController.text.toString()).then((value) {
if (value.status == 1) {
_successStatus = true;
}
}).catchError((onError) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("Error $onError"),
));
successMessage="Error $onError";
print("Error $onError");
});
}
}