Guest User

Untitled

a guest
Jul 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. using System;
  2. using Xamarin.Forms;
  3. using Xamarin.Forms.Xaml;
  4. using TBSMobileApplication.Views;
  5. using TBSMobileApplication.Data;
  6.  
  7. [assembly: XamlCompilation (XamlCompilationOptions.Compile)]
  8. namespace TBSMobileApplication
  9. {
  10. public partial class App : Application
  11. {
  12. static TokenDatabaseController tokenDatabase;
  13. static UserDatabaseController userDatabase;
  14.  
  15. public App ()
  16. {
  17. InitializeComponent();
  18.  
  19. MainPage = new LoginPage();
  20. }
  21.  
  22. protected override void OnStart ()
  23. {
  24. // Handle when your app starts
  25. }
  26.  
  27. protected override void OnSleep ()
  28. {
  29. // Handle when your app sleeps
  30. }
  31.  
  32. protected override void OnResume ()
  33. {
  34. // Handle when your app resumes
  35. }
  36.  
  37. public static UserDatabaseController UserDatabase
  38. {
  39. get
  40. {
  41. if(userDatabase == null)
  42. {
  43. userDatabase = new UserDatabaseController();
  44. }
  45.  
  46. return userDatabase;
  47. }
  48. }
  49.  
  50. public static TokenDatabaseController TokenDatabase
  51. {
  52. get
  53. {
  54. if (tokenDatabase == null)
  55. {
  56. tokenDatabase = new TokenDatabaseController();
  57. }
  58.  
  59. return tokenDatabase;
  60. }
  61. }
  62. }
  63. }
  64.  
  65. using System;
  66. using System.Collections.Generic;
  67. using System.Linq;
  68. using System.Text;
  69. using System.Threading.Tasks;
  70. using TBSMobileApplication.Models;
  71. using Xamarin.Forms;
  72. using Xamarin.Forms.Xaml;
  73.  
  74. namespace TBSMobileApplication.Views
  75. {
  76. [XamlCompilation(XamlCompilationOptions.Compile)]
  77. public partial class LoginPage : ContentPage
  78. {
  79. public LoginPage ()
  80. {
  81. InitializeComponent ();
  82. }
  83.  
  84. void LoginProcedure(object sender, EventArgs e)
  85. {
  86. User user = new User(entUser.Text, entPassword.Text);
  87. if (user.CheckInformation())
  88. {
  89. //DisplayAlert("Login Message", "Login Success", "Ok");
  90. try
  91. {
  92. App.UserDatabase.SaveUser(user);
  93. DisplayAlert("Database Message", "User Saved", "Ok");
  94. }
  95. catch(Exception ex)
  96. {
  97. DisplayAlert("Message", ex.Message, "Ok");
  98. }
  99.  
  100. }
  101. else
  102. {
  103. DisplayAlert("Login Message", "Login Failed", "Ok");
  104. }
  105. }
  106. }
  107. }
  108.  
  109. using SQLite;
  110. using System;
  111. using System.Collections.Generic;
  112. using System.Text;
  113.  
  114. namespace TBSMobileApplication.Data
  115. {
  116. public interface ISQLite
  117. {
  118. SQLiteConnection GetConnection();
  119. }
  120. }
  121.  
  122. using SQLite;
  123. using System;
  124. using System.Collections.Generic;
  125. using System.Text;
  126.  
  127. namespace TBSMobileApplication.Models
  128. {
  129. public class User
  130. {
  131. [PrimaryKey, AutoIncrement]
  132. public int ContactID { get; set; }
  133. [Unique]
  134. public string UserID { get; set; }
  135. public string UserPassword { get; set; }
  136.  
  137. public User() { }
  138. public User(string Username, string Password)
  139. {
  140. this.UserID = Username;
  141. this.UserPassword = Password;
  142. }
  143.  
  144. public bool CheckInformation()
  145. {
  146. if(!this.UserID.Equals("") || !this.UserPassword.Equals(""))
  147. {
  148. return true;
  149. }
  150. else
  151. {
  152. return false;
  153. }
  154. }
  155. }
  156. }
  157.  
  158. using System;
  159. using System.Collections.Generic;
  160. using System.IO;
  161. using System.Linq;
  162. using System.Text;
  163.  
  164. using Android.App;
  165. using Android.Content;
  166. using Android.OS;
  167. using Android.Runtime;
  168. using Android.Views;
  169. using Android.Widget;
  170. using TBSMobileApplication.Data;
  171. using TBSMobileApplication.Droid.Data;
  172. using Xamarin.Forms;
  173.  
  174. [assembly: Dependency(typeof(SQLite_Android))]
  175.  
  176. namespace TBSMobileApplication.Droid.Data
  177. {
  178. public class SQLite_Android : ISQLite
  179. {
  180. public SQLite_Android() { }
  181. public SQLite.SQLiteConnection GetConnection()
  182. {
  183. var DBFileName = "backend.db3";
  184. string DocumentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
  185. var path = Path.Combine(DocumentPath, DBFileName);
  186. var conn = new SQLite.SQLiteConnection(path);
  187.  
  188. return conn;
  189. }
  190. }
  191. }
  192.  
  193. using SQLite;
  194. using System;
  195. using System.Collections.Generic;
  196. using System.Text;
  197. using TBSMobileApplication.Models;
  198. using Xamarin.Forms;
  199.  
  200. namespace TBSMobileApplication.Data
  201. {
  202. public class UserDatabaseController
  203. {
  204. static object locker = new object();
  205.  
  206. SQLiteConnection database;
  207.  
  208. public UserDatabaseController()
  209. {
  210. database = DependencyService.Get<ISQLite>().GetConnection();
  211. database.CreateTable<User>();
  212. }
  213.  
  214. public User GetUser()
  215. {
  216. lock (locker)
  217. {
  218. if(database.Table<User>().Count() == 0)
  219. {
  220. return null;
  221. }
  222. else
  223. {
  224. return database.Table<User>().First();
  225. }
  226. }
  227. }
  228.  
  229. public int SaveUser(User user)
  230. {
  231. lock (locker)
  232. {
  233. if (user.ContactID != 0)
  234. {
  235. database.Update(user);
  236. return user.ContactID;
  237. }
  238. else
  239. {
  240. return database.Insert(user);
  241. }
  242. }
  243. }
  244.  
  245. public int DeleteUser(int contactid)
  246. {
  247. lock (locker)
  248. {
  249. return database.Delete<User>(contactid);
  250. }
  251. }
  252. }
  253. }
Add Comment
Please, Sign In to add comment