Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using TBSMobileApplication.Data;
- using TBSMobileApplication.ViewModel;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- namespace TBSMobileApplication.View
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class LoginPage : ContentPage
- {
- public LoginPageViewModel loginModel;
- public LoginPage ()
- {
- InitializeComponent ();
- BindingContext = new LoginPageViewModel();
- MessagingCenter.Subscribe<LoginPageViewModel,string>(this, "Login Alert", (sender,Username) =>
- {
- DisplayAlert("Login Error", "Please fill-up the form", "Ok");
- });
- MessagingCenter.Subscribe<LoginPageViewModel, string>(this, "Connected", (sender, Username) =>
- {
- DisplayAlert("Connection Info", "Connected", "Ok");
- });
- MessagingCenter.Subscribe<LoginPageViewModel, string>(this, "Not Connected", (sender, Username) =>
- {
- DisplayAlert("Connection Info", "Not Connected", "Ok");
- });
- MessagingCenter.Subscribe<LoginPageViewModel, string>(this, "Http", (sender, Username) =>
- {
- DisplayAlert("Login Error", "Username or Password is incorrect", "Ok");
- });
- entUsername.Completed += (object sender, EventArgs e) =>
- {
- entPassword.Focus();
- };
- }
- protected async Task OnAppearingAsync()
- {
- var db = DependencyService.Get<ISQLiteDB>();
- var conn = db.GetConnection();
- if (conn != null)
- {
- await conn.CreateTableAsync<UserTable>();
- await conn.CreateTableAsync<RetailerTable>();
- await conn.CreateTableAsync<ContactsTable>();
- }
- base.OnAppearing();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.IO;
- using System.Net;
- using System.Text;
- using System.Windows.Input;
- using TBSMobileApplication.View;
- using Xamarin.Essentials;
- using Xamarin.Forms;
- namespace TBSMobileApplication.ViewModel
- {
- public class LoginPageViewModel : INotifyPropertyChanged
- {
- void OnProperyChanged(string PropertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
- }
- public string username;
- public string password;
- public string Username
- {
- get { return username; }
- set
- {
- username = value;
- OnProperyChanged(nameof(Username));
- }
- }
- public string Password
- {
- get { return password; }
- set
- {
- password = value;
- OnProperyChanged(nameof(Password));
- }
- }
- public ICommand LoginCommand { get; set; }
- public LoginPageViewModel()
- {
- LoginCommand = new Command(OnLogin);
- }
- public void OnLogin()
- {
- if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
- {
- MessagingCenter.Send(this, "Login Alert", Username);
- }
- else
- {
- var current = Connectivity.NetworkAccess;
- if (current == NetworkAccess.Internet)
- {
- var link = "http://192.168.1.25:7777/TBS/test.php?User=" + Username + "&Password=" + Password;
- var request = HttpWebRequest.Create(string.Format(@link));
- request.ContentType = "application/json";
- request.Method = "GET";
- using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
- {
- if (response.StatusCode != HttpStatusCode.OK)
- {
- Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
- }
- else
- {
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- var content = reader.ReadToEnd();
- if (content.Equals("[]") || string.IsNullOrWhiteSpace(content) || string.IsNullOrEmpty(content))
- {
- MessagingCenter.Send(this, "Http", Username);
- }
- else
- {
- App.Current.MainPage.Navigation.PushAsync(new DatabaseSyncPage(), true);
- }
- }
- }
- }
- }
- else
- {
- MessagingCenter.Send(this, "Not Connected", Username);
- }
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- }
- }
Add Comment
Please, Sign In to add comment