Guest User

Untitled

a guest
Jul 24th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using TBSMobileApplication.Data;
  7. using TBSMobileApplication.ViewModel;
  8. using Xamarin.Forms;
  9. using Xamarin.Forms.Xaml;
  10.  
  11. namespace TBSMobileApplication.View
  12. {
  13. [XamlCompilation(XamlCompilationOptions.Compile)]
  14. public partial class LoginPage : ContentPage
  15. {
  16. public LoginPageViewModel loginModel;
  17. public LoginPage ()
  18. {
  19. InitializeComponent ();
  20. BindingContext = new LoginPageViewModel();
  21. MessagingCenter.Subscribe<LoginPageViewModel,string>(this, "Login Alert", (sender,Username) =>
  22. {
  23. DisplayAlert("Login Error", "Please fill-up the form", "Ok");
  24. });
  25.  
  26. MessagingCenter.Subscribe<LoginPageViewModel, string>(this, "Connected", (sender, Username) =>
  27. {
  28. DisplayAlert("Connection Info", "Connected", "Ok");
  29. });
  30.  
  31. MessagingCenter.Subscribe<LoginPageViewModel, string>(this, "Not Connected", (sender, Username) =>
  32. {
  33. DisplayAlert("Connection Info", "Not Connected", "Ok");
  34. });
  35.  
  36. MessagingCenter.Subscribe<LoginPageViewModel, string>(this, "Http", (sender, Username) =>
  37. {
  38. DisplayAlert("Login Error", "Username or Password is incorrect", "Ok");
  39. });
  40.  
  41. entUsername.Completed += (object sender, EventArgs e) =>
  42. {
  43. entPassword.Focus();
  44. };
  45. }
  46.  
  47. protected async Task OnAppearingAsync()
  48. {
  49. var db = DependencyService.Get<ISQLiteDB>();
  50. var conn = db.GetConnection();
  51.  
  52. if (conn != null)
  53. {
  54. await conn.CreateTableAsync<UserTable>();
  55. await conn.CreateTableAsync<RetailerTable>();
  56. await conn.CreateTableAsync<ContactsTable>();
  57. }
  58.  
  59. base.OnAppearing();
  60. }
  61. }
  62. }
  63.  
  64. using System;
  65. using System.Collections.Generic;
  66. using System.ComponentModel;
  67. using System.IO;
  68. using System.Net;
  69. using System.Text;
  70. using System.Windows.Input;
  71. using TBSMobileApplication.View;
  72. using Xamarin.Essentials;
  73. using Xamarin.Forms;
  74.  
  75. namespace TBSMobileApplication.ViewModel
  76. {
  77. public class LoginPageViewModel : INotifyPropertyChanged
  78. {
  79. void OnProperyChanged(string PropertyName)
  80. {
  81. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
  82. }
  83.  
  84. public string username;
  85. public string password;
  86.  
  87. public string Username
  88. {
  89. get { return username; }
  90. set
  91. {
  92. username = value;
  93. OnProperyChanged(nameof(Username));
  94. }
  95. }
  96.  
  97. public string Password
  98. {
  99. get { return password; }
  100. set
  101. {
  102. password = value;
  103. OnProperyChanged(nameof(Password));
  104. }
  105. }
  106.  
  107. public ICommand LoginCommand { get; set; }
  108.  
  109. public LoginPageViewModel()
  110. {
  111. LoginCommand = new Command(OnLogin);
  112. }
  113.  
  114. public void OnLogin()
  115. {
  116. if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
  117. {
  118. MessagingCenter.Send(this, "Login Alert", Username);
  119. }
  120. else
  121. {
  122. var current = Connectivity.NetworkAccess;
  123.  
  124. if (current == NetworkAccess.Internet)
  125. {
  126. var link = "http://192.168.1.25:7777/TBS/test.php?User=" + Username + "&Password=" + Password;
  127. var request = HttpWebRequest.Create(string.Format(@link));
  128. request.ContentType = "application/json";
  129. request.Method = "GET";
  130.  
  131. using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  132. {
  133. if (response.StatusCode != HttpStatusCode.OK)
  134. {
  135. Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
  136. }
  137. else
  138. {
  139. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  140. {
  141. var content = reader.ReadToEnd();
  142.  
  143. if (content.Equals("[]") || string.IsNullOrWhiteSpace(content) || string.IsNullOrEmpty(content))
  144. {
  145. MessagingCenter.Send(this, "Http", Username);
  146. }
  147. else
  148. {
  149. App.Current.MainPage.Navigation.PushAsync(new DatabaseSyncPage(), true);
  150. }
  151. }
  152. }
  153. }
  154. }
  155. else
  156. {
  157. MessagingCenter.Send(this, "Not Connected", Username);
  158. }
  159. }
  160. }
  161.  
  162. public event PropertyChangedEventHandler PropertyChanged;
  163. }
  164. }
Add Comment
Please, Sign In to add comment