JLChen
2020-04-15 19545253f1977af3ef84b301183c2e12107983c9
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using Newtonsoft.Json.Linq;
using Shared.SimpleControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
 
namespace Shared.SimpleControl.Phone
{
    public class AccountLogin : Dialog
    {
        public void LoginViewShow ()
        {
            var loginView = new LoginView ();
            this.AddChidren (loginView);
            loginView.LoginViewShow (this);
        }
    }
 
    public class LoginView : FrameLayout
    {
 
        public void LoginViewShow (Dialog dialog = null)
        {
            string folderID = "";
            var bodyView = new FrameLayout () {
                BackgroundImagePath = "Logo/Register_bg.png"
            };
            AddChidren (bodyView);
 
            FrameLayout logoView = new FrameLayout () {
                Height = Application.GetRealHeight (400),
            };
            bodyView.AddChidren (logoView);
 
 
            var btnQRCodeLoginType = new Button () {
                Width = Application.GetMinRealAverage (500),
                Height = Application.GetMinRealAverage (85),
                Gravity = Gravity.CenterHorizontal,
                Y =  Application.GetRealHeight (560),
                Text = "QR-Code",
                TextAlignment = TextAlignment.Center,
                BackgroundColor = SkinStyle.Current.MainColor,
                SelectedBackgroundColor = SkinStyle.Current.SelectedColor,
                TextSize = 15,
                TextColor = SkinStyle.Current.TextColor1,
                Radius = 5,
                BorderColor = SkinStyle.Current.Transparent,
                BorderWidth = 0,
            };
            bodyView.AddChidren (btnQRCodeLoginType);
 
            btnQRCodeLoginType.MouseUpEventHandler += (sender, e) => {
                com.hdl.on.Scan.OpenScan ((scanMAC) => {
                    folderID = scanMAC;
                    double fId = 0;
                    double.TryParse (folderID, out fId);
                    if (fId == 0) {
                        new Alert ("", "Please enter the correct code.", "Close").Show ();
                    } else {
                        DownloadData (folderID, dialog);
                    }
                });
            };
            var btnLoginCode = new Button () {
                Width = Application.GetMinRealAverage (500),
                Height = Application.GetMinRealAverage (85),
                Gravity = Gravity.CenterHorizontal,
                Y = btnQRCodeLoginType.Bottom + Application.GetRealHeight (70),
                Text = "Verification Code",
                TextAlignment = TextAlignment.Center,
                BackgroundColor = SkinStyle.Current.MainColor,
                SelectedBackgroundColor = SkinStyle.Current.SelectedColor,
                TextSize = 15,
                TextColor = SkinStyle.Current.TextColor1,
                Radius = 5,
                BorderColor = SkinStyle.Current.Transparent,
                BorderWidth = 0,
            };
            bodyView.AddChidren (btnLoginCode);
 
            btnLoginCode.MouseUpEventHandler += (sender, e) => {
                if (btnLoginCode.Text == "Verification Code") {
                    btnQRCodeLoginType.RemoveFromParent ();
                    btnQRCodeLoginType.Y = btnLoginCode.Bottom + Application.GetRealHeight (20);
                    btnQRCodeLoginType.BackgroundColor = 0x00ffffff;
                    btnQRCodeLoginType.Text = "User QR-Code";
                    bodyView.AddChidren (btnQRCodeLoginType);
 
                    btnLoginCode.BackgroundColor = 0xFFCDAD7D;
                    btnLoginCode.Radius = (uint)Application.GetRealHeight (43);
 
 
                    var etCode = new EditText () {
                        Width = Application.GetMinRealAverage (500),
                        Height = Application.GetMinRealAverage (85),
                        X = Application.GetRealWidth (70),
                        Y = Application.GetRealHeight (560),
                        Radius = (uint)Application.GetRealHeight (2),
                        PlaceholderText = "Please entry verification code",
                        PlaceholderTextColor = 0xFF4A4A4A,
                        TextAlignment = TextAlignment.Center,
                        BorderColor = 0xFF4A4A4A,
                        BorderWidth = (uint)Application.GetRealHeight (2),
                    };
                    bodyView.AddChidren (etCode);
 
                    etCode.Foucs = true;
                    etCode.TextChangeEventHandler += (sender1, e1) => {
                        folderID = etCode.Text.Trim ();
                    };
                    etCode.EditorEnterAction += (obj) => {
                        Application.HideSoftInput ();
                    };
 
                    btnLoginCode.Text = "Download";
                } else {
                    double fId = 0;
                    double.TryParse (folderID, out fId);
                    if (fId == 0) {
                        new Alert ("", "Please enter the correct code.", "Close").Show ();
                    } else {
                        DownloadData (folderID, dialog);
                    }
                }
            };
        }
 
        void DownloadData(string folderID, Dialog dialog = null)
        {
            Application.RunOnMainThread (() => {
                MainPage.Loading.Start ("Please wait...");
            });
            new System.Threading.Thread (() => {
                try {
                    var webClient = new WebClient ();
                    string url = $"https://developer.hdlcontrol.com/api/GetFolderByFolderID/?folderID={folderID}";
                    string responseString = null;
                    responseString = Encoding.UTF8.GetString (webClient.DownloadData (url));
                    var revertObj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack> (responseString);
                    if (revertObj.StateCode == "SUCCESS") {
                        IO.FileUtils.DeleteAllFile ();
                        var jt = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DemoBackup>> (revertObj.ResponseData.ToString ());
                        int index = 0;
                        foreach (var file in jt) {
                            if (file.Name == ".linphonerc" || file.Name == "error.wav" || file.Name == "jpush_stat_cache.json" || file.Name == "linphone -history.db" ||
                                file.Name == "linphone -log-history.db" || file.Name == "linphone1.log" || file.Name == "linphonerc" || file.Name == "wakeup_cache.json"
                                || file.Name == "linphone-history.db" || file.Name == "linphone-log-history.db") {
                                Console.WriteLine ("'");
                                continue;
                            }
                            var ss = CommonPage.MyEncodingUTF8.GetString (file.DetailByte);
                            IO.FileUtils.WriteFileByBytes (file.Name, file.DetailByte);
                            index++;
                            Application.RunOnMainThread (() => {
                                int pro = (int)(index * 1.0 / jt.Count * 100);
                                MainPage.Loading.Text = pro.ToString () + "%";
                            });
                        }
                        Application.RunOnMainThread (() => {
                            MainPage.LoginUser = new UserInfo ();
                            MainPage.LoginUser.SaveUserInfo ();
                            MainPage.Loading.Hide ();
                            Room.InitAllRoom ();
                            UserMiddle.InitHomePageView ();
                            if (dialog != null) {
                                dialog.Close ();
                            }
                        });
                    } else {
                        Application.RunOnMainThread (() => {
                            new Alert ("", "Please enter the correct code.", "Close").Show ();
                        });
                    }
                } catch (Exception ex) {
                    Console.WriteLine (ex.Message);
                } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                    });
                }
            }) { IsBackground = true }.Start ();
        }
 
    }
 
 
    public class DemoBackup
    {
        public string Name;
 
        public byte [] DetailByte;
           
    }
}