Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using TBSMobileApplication.Views;
- using TBSMobileApplication.Data;
- [assembly: XamlCompilation (XamlCompilationOptions.Compile)]
- namespace TBSMobileApplication
- {
- public partial class App : Application
- {
- static TokenDatabaseController tokenDatabase;
- static UserDatabaseController userDatabase;
- public App ()
- {
- InitializeComponent();
- MainPage = new LoginPage();
- }
- protected override void OnStart ()
- {
- // Handle when your app starts
- }
- protected override void OnSleep ()
- {
- // Handle when your app sleeps
- }
- protected override void OnResume ()
- {
- // Handle when your app resumes
- }
- public static UserDatabaseController UserDatabase
- {
- get
- {
- if(userDatabase == null)
- {
- userDatabase = new UserDatabaseController();
- }
- return userDatabase;
- }
- }
- public static TokenDatabaseController TokenDatabase
- {
- get
- {
- if (tokenDatabase == null)
- {
- tokenDatabase = new TokenDatabaseController();
- }
- return tokenDatabase;
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using TBSMobileApplication.Models;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- namespace TBSMobileApplication.Views
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class LoginPage : ContentPage
- {
- public LoginPage ()
- {
- InitializeComponent ();
- }
- void LoginProcedure(object sender, EventArgs e)
- {
- User user = new User(entUser.Text, entPassword.Text);
- if (user.CheckInformation())
- {
- //DisplayAlert("Login Message", "Login Success", "Ok");
- try
- {
- App.UserDatabase.SaveUser(user);
- DisplayAlert("Database Message", "User Saved", "Ok");
- }
- catch(Exception ex)
- {
- DisplayAlert("Message", ex.Message, "Ok");
- }
- }
- else
- {
- DisplayAlert("Login Message", "Login Failed", "Ok");
- }
- }
- }
- }
- using SQLite;
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace TBSMobileApplication.Data
- {
- public interface ISQLite
- {
- SQLiteConnection GetConnection();
- }
- }
- using SQLite;
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace TBSMobileApplication.Models
- {
- public class User
- {
- [PrimaryKey, AutoIncrement]
- public int ContactID { get; set; }
- [Unique]
- public string UserID { get; set; }
- public string UserPassword { get; set; }
- public User() { }
- public User(string Username, string Password)
- {
- this.UserID = Username;
- this.UserPassword = Password;
- }
- public bool CheckInformation()
- {
- if(!this.UserID.Equals("") || !this.UserPassword.Equals(""))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using Android.App;
- using Android.Content;
- using Android.OS;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using TBSMobileApplication.Data;
- using TBSMobileApplication.Droid.Data;
- using Xamarin.Forms;
- [assembly: Dependency(typeof(SQLite_Android))]
- namespace TBSMobileApplication.Droid.Data
- {
- public class SQLite_Android : ISQLite
- {
- public SQLite_Android() { }
- public SQLite.SQLiteConnection GetConnection()
- {
- var DBFileName = "backend.db3";
- string DocumentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
- var path = Path.Combine(DocumentPath, DBFileName);
- var conn = new SQLite.SQLiteConnection(path);
- return conn;
- }
- }
- }
- using SQLite;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using TBSMobileApplication.Models;
- using Xamarin.Forms;
- namespace TBSMobileApplication.Data
- {
- public class UserDatabaseController
- {
- static object locker = new object();
- SQLiteConnection database;
- public UserDatabaseController()
- {
- database = DependencyService.Get<ISQLite>().GetConnection();
- database.CreateTable<User>();
- }
- public User GetUser()
- {
- lock (locker)
- {
- if(database.Table<User>().Count() == 0)
- {
- return null;
- }
- else
- {
- return database.Table<User>().First();
- }
- }
- }
- public int SaveUser(User user)
- {
- lock (locker)
- {
- if (user.ContactID != 0)
- {
- database.Update(user);
- return user.ContactID;
- }
- else
- {
- return database.Insert(user);
- }
- }
- }
- public int DeleteUser(int contactid)
- {
- lock (locker)
- {
- return database.Delete<User>(contactid);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment