Guest User

Untitled

a guest
Aug 8th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.44 KB | None | 0 0
  1. using SQLite;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. namespace TBSMobileApplication.Data
  7. {
  8. public interface ISQLiteDB
  9. {
  10. SQLiteAsyncConnection GetConnection();
  11. }
  12. }
  13.  
  14. using System;
  15. using System.Collections.Generic;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Text;
  19.  
  20. using Android.App;
  21. using Android.Content;
  22. using Android.OS;
  23. using Android.Runtime;
  24. using Android.Views;
  25. using Android.Widget;
  26. using SQLite;
  27. using TBSMobileApplication.Data;
  28. using TBSMobileApplication.Droid.Data;
  29. using Xamarin.Forms;
  30.  
  31. [assembly: Dependency(typeof(AndroidSQLiteDb))]
  32.  
  33. namespace TBSMobileApplication.Droid.Data
  34. {
  35. public class AndroidSQLiteDb : ISQLiteDB
  36. {
  37. public SQLiteAsyncConnection GetConnection()
  38. {
  39. var dbFileName = "backend.db3";
  40. var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
  41. var path = Path.Combine(documentsPath, dbFileName);
  42.  
  43. return new SQLiteAsyncConnection(path);
  44. }
  45. }
  46. }
  47.  
  48. using System;
  49. using System.Collections.Generic;
  50. using System.Linq;
  51. using System.Text;
  52. using System.Threading.Tasks;
  53. using TBSMobileApplication.Data;
  54. using TBSMobileApplication.ViewModel;
  55. using Xamarin.Forms;
  56. using Xamarin.Forms.Xaml;
  57.  
  58. namespace TBSMobileApplication.View
  59. {
  60. [XamlCompilation(XamlCompilationOptions.Compile)]
  61. public partial class LoginPage : ContentPage
  62. {
  63. public LoginPageViewModel loginModel;
  64. public LoginPage ()
  65. {
  66. InitializeComponent();
  67. OnAppearingAsync();
  68. BindingContext = new LoginPageViewModel();
  69. MessagingCenter.Subscribe<LoginPageViewModel,string>(this, "Login Alert", (sender,Username) =>
  70. {
  71. DisplayAlert("Login Error", "Please fill-up the form", "Ok");
  72. });
  73.  
  74. MessagingCenter.Subscribe<LoginPageViewModel, string>(this, "Connected", (sender, Username) =>
  75. {
  76. DisplayAlert("Connection Info", "Connected", "Ok");
  77. });
  78.  
  79. MessagingCenter.Subscribe<LoginPageViewModel, string>(this, "Not Connected", (sender, Username) =>
  80. {
  81. DisplayAlert("Connection Info", "Not Connected", "Ok");
  82. });
  83.  
  84. MessagingCenter.Subscribe<LoginPageViewModel, string>(this, "Http", (sender, Username) =>
  85. {
  86. DisplayAlert("Login Error", "Username or Password is incorrect", "Ok");
  87. });
  88. }
  89.  
  90. protected async Task OnAppearingAsync()
  91. {
  92. var db = DependencyService.Get<ISQLiteDB>();
  93. var conn = db.GetConnection();
  94.  
  95. if (conn != null)
  96. {
  97. await conn.CreateTableAsync<UserTable>();
  98. await conn.CreateTableAsync<ContactsTable>();
  99. await conn.CreateTableAsync<ActivityTable>();
  100. await conn.CreateTableAsync<CAFTable>();
  101. }
  102.  
  103. base.OnAppearing();
  104. }
  105. }
  106. }
  107.  
  108. using SQLite;
  109. using System;
  110. using System.Collections.Generic;
  111. using System.Text;
  112.  
  113. namespace TBSMobileApplication.Data
  114. {
  115. [Table("tblCaf")]
  116. public class CAFTable
  117. {
  118. [PrimaryKey, MaxLength(100)]
  119. public string CAFNo { get; set; }
  120. [MaxLength(100)]
  121. public string TempCAFNo { get; set; }
  122. [MaxLength(100)]
  123. public string EmployeeID { get; set; }
  124. public DateTime CAFDate { get; set; }
  125. [MaxLength(100)]
  126. public string CustomerID { get; set; }
  127. public DateTime StartTime { get; set; }
  128. public DateTime EndTime { get; set; }
  129. [MaxLength(20)]
  130. public string ActivityID { get; set; }
  131. public string Photo1 { get; set; }
  132. public string Photo2 { get; set; }
  133. public string Photo3 { get; set; }
  134. [MaxLength(1000)]
  135. public string Remarks { get; set; }
  136. [MaxLength(1000)]
  137. public string OtherConcern { get; set; }
  138. }
  139. }
  140.  
  141. using Newtonsoft.Json;
  142. using System;
  143. using System.Collections.Generic;
  144. using System.IO;
  145. using System.Linq;
  146. using System.Net;
  147. using System.Text;
  148. using System.Threading.Tasks;
  149. using TBSMobileApplication.Data;
  150. using TBSMobileApplication.ViewModel;
  151. using Xamarin.Forms;
  152. using Xamarin.Forms.Xaml;
  153.  
  154. namespace TBSMobileApplication.View
  155. {
  156. [XamlCompilation(XamlCompilationOptions.Compile)]
  157. public partial class DatabaseSyncPage : ContentPage
  158. {
  159. public DatabaseSyncPage (string contanctID, string host, string database)
  160. {
  161. InitializeComponent ();
  162.  
  163. InsertUserData(contanctID, host, database);
  164. InsertCAFData(contanctID, host, database);
  165. }
  166.  
  167. public class UserData
  168. {
  169. public string ContactID { get; set; }
  170. public string UserID { get; set; }
  171. public string UserPassword { get; set; }
  172. public string UserType { get; set; }
  173. }
  174.  
  175. //IMPORT USER DATA TO LOCAL DATABASE
  176. public void InsertUserData(string contact, string host, string database)
  177. {
  178. var link = "http://192.168.120.9:7777/TBS/mobile-get-data.php?Host=" + host + "&Database=" + database + "&Contact=" + contact + "&Sync=1";
  179. var request = HttpWebRequest.Create(string.Format(@link));
  180. request.ContentType = "application/json";
  181. request.Method = "GET";
  182.  
  183. using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  184. {
  185. if (response.StatusCode != HttpStatusCode.OK)
  186. {
  187. Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
  188. }
  189. else
  190. {
  191. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  192. {
  193. var content = reader.ReadToEnd();
  194.  
  195. if (content.Equals("[]") || string.IsNullOrWhiteSpace(content) || string.IsNullOrEmpty(content))
  196. {
  197. DisplayAlert("Sync Message", "No data found", "Ok");
  198. }
  199. else
  200. {
  201. var db = DependencyService.Get<ISQLiteDB>();
  202. var conn = db.GetConnection();
  203.  
  204. var result = JsonConvert.DeserializeObject<List<UserData>>(content);
  205. for (int i = 0; i < result.Count; i++)
  206. {
  207. var item = result[i];
  208. var contactID = item.ContactID;
  209. var userID = item.UserID;
  210. var userPassword = item.UserPassword;
  211. var userType = item.UserType;
  212.  
  213. var user = new UserTable { ContactID=contactID, UserID=userID, UserPassword=userPassword, UserType=userType};
  214.  
  215. conn.InsertAsync(user);
  216. }
  217. }
  218.  
  219. }
  220. }
  221. }
  222. }
  223.  
  224. public class CAFData
  225. {
  226. public string CAFNo { get; set; }
  227. public string EmployeeID { get; set; }
  228. public DateTime CAFDate { get; set; }
  229. public string CustomerID { get; set; }
  230. public DateTime StartTime { get; set; }
  231. public DateTime EndTime { get; set; }
  232. public string ActivityID { get; set; }
  233. public string Photo1 { get; set; }
  234. public string Photo2 { get; set; }
  235. public string Photo3 { get; set; }
  236. public string Remarks { get; set; }
  237. public string OtherConcern { get; set; }
  238. }
  239.  
  240. //IMPORT CAF TO LOCAL DATABASE
  241. public void InsertCAFData(string contact, string host, string database)
  242. {
  243. var link = "http://192.168.120.9:7777/TBS/mobile-get-data.php?Host=" + host + "&Database=" + database + "&Contact=" + contact + "&Sync=2";
  244. var request = HttpWebRequest.Create(string.Format(@link));
  245. request.ContentType = "application/json";
  246. request.Method = "GET";
  247.  
  248. using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  249. {
  250. if (response.StatusCode != HttpStatusCode.OK)
  251. {
  252. Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
  253. }
  254. else
  255. {
  256. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  257. {
  258. var content = reader.ReadToEnd();
  259.  
  260. if (content.Equals("[]") || string.IsNullOrWhiteSpace(content) || string.IsNullOrEmpty(content))
  261. {
  262. DisplayAlert("Sync Message", "No data found", "Ok");
  263. }
  264. else
  265. {
  266. var db = DependencyService.Get<ISQLiteDB>();
  267. var conn = db.GetConnection();
  268.  
  269. var result = JsonConvert.DeserializeObject<List<CAFData>>(content);
  270. for (int i = 0; i < result.Count; i++)
  271. {
  272. var item = result[i];
  273. var cafNo = item.CAFNo;
  274. var empID = item.EmployeeID;
  275. var cafDate = item.CAFDate;
  276. var customerID = item.CustomerID;
  277. var startTime = item.StartTime;
  278. var endTime = item.EndTime;
  279. var activityID = item.ActivityID;
  280. var photo1 = item.Photo1;
  281. var photo2 = item.Photo2;
  282. var photo3 = item.Photo3;
  283. var remarks = item.Remarks;
  284. var others = item.OtherConcern;
  285.  
  286. var caf = new CAFTable { CAFNo=cafNo, EmployeeID=empID, CAFDate=cafDate, CustomerID=customerID, StartTime=startTime, EndTime=endTime, ActivityID=activityID, Photo1=photo1, Photo2=photo2, Photo3=photo3, Remarks=remarks, OtherConcern=others };
  287.  
  288. conn.InsertAsync(caf);
  289. }
  290. }
  291.  
  292. }
  293. }
  294. }
  295. }
  296. }
  297. }
Add Comment
Please, Sign In to add comment