diff options
author | marcinzelent <marcin@zelent.net> | 2017-05-27 15:07:24 +0200 |
---|---|---|
committer | marcinzelent <marcin@zelent.net> | 2017-05-27 15:07:24 +0200 |
commit | 2a77dfb29886a2e6a778e49bb3eff3516689288a (patch) | |
tree | c0a7d3e047b5128401ecb368435543e5821f1465 | |
parent | ab9b9a75ec6abfd85f7ccae6aee08ec9e9e35497 (diff) |
Changed login system to vm-handler pattern.
-rw-r--r-- | ApartmentManager/ApartmentManager/Handler/LoginHandler.cs | 100 | ||||
-rw-r--r-- | ApartmentManager/ApartmentManager/ViewModel/LoginViewModel.cs | 96 |
2 files changed, 96 insertions, 100 deletions
diff --git a/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs b/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs index 65efeca..d0088d9 100644 --- a/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs +++ b/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs @@ -2,34 +2,114 @@ using ApartmentManager.Persistency; using Newtonsoft.Json; using System; -using ApartmentManager.Singletons; using ApartmentManager.ViewModel; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using ApartmentManager.View; +using Windows.UI.Popups; +using ApartmentManager.Singletons; namespace ApartmentManager.Handler { - public static class LoginHandler + public class LoginHandler { - public static void LogIn(string username, string password) + + private LoginViewModel _vm; + + public LoginHandler(LoginViewModel vm) + { + _vm = vm; + } + + public void LogIn() { - string serializedUser = ApiClient.GetData($"api/Users/{username}"); + try + { + string serializedUser = ApiClient.GetData($"api/Users/{_vm.Username}"); if (serializedUser != null) { User user = JsonConvert.DeserializeObject<User>(serializedUser); - if (user.Password == password) + if (user.Password == _vm.Password) { UserSingleton.Instance.CurrentUser = user; - - - } + NavigateToMainPage(); + } else throw new Exception("Wrong password!"); } else throw new Exception("Wrong username!"); - + } + catch (Exception ex) + { + var msg = new MessageDialog(ex.Message).ShowAsync(); + } } - public static void LogOut() + public void LogOut() { UserSingleton.Instance.CurrentUser = null; + NavigateToLoginPage(); + } + + private void NavigateToMainPage() + { + AppShell appShell = Window.Current.Content as AppShell; + + // Do not repeat app initialization when the Window already has content, + // just ensure that the window is active + if (appShell == null) + { + // Create a AppShell to act as the navigation context and navigate to the first page + appShell = new AppShell(); + + // Set the default language + appShell.Language = Windows.Globalization.ApplicationLanguages.Languages[0]; + + } + + // Place our app shell in the current Window + Window.Current.Content = appShell; + + if (appShell.AppFrame.Content == null) + { + // When the navigation stack isn't restored, navigate to the first page + // suppressing the initial entrance animation. + if (UserSingleton.Instance.CurrentUser.IsBm) + { + BmViewModel bvm = new BmViewModel(); + bvm.BmHandler.GetApartments(); + appShell.AppFrame.Navigate(typeof(BmMainPage)); + } + else + { + ApartmentViewModel avm = new ApartmentViewModel(); + avm.ApartmentHandler.GetApartmentResidents(); + avm.ApartmentHandler.GetApartment(); + avm.ApartmentHandler.GetApartmentDefects(); + appShell.AppFrame.Navigate(typeof(ApartmentPage)); + } + } + + // Ensure the current window is active + Window.Current.Activate(); + } + + private void NavigateToLoginPage() + { + Frame rootFrame = Window.Current.Content as Frame; + + // Do not repeat app initialization when the Window already has content, + // just ensure that the window is active + if (rootFrame == null) + { + // Create a Frame to act as the navigation context and navigate to the first page + rootFrame = new Frame(); + + // Place the frame in the current Window + Window.Current.Content = rootFrame; + } + + rootFrame.Navigate(typeof(LoginPage)); + Window.Current.Activate(); } } } diff --git a/ApartmentManager/ApartmentManager/ViewModel/LoginViewModel.cs b/ApartmentManager/ApartmentManager/ViewModel/LoginViewModel.cs index 0031061..82b7f97 100644 --- a/ApartmentManager/ApartmentManager/ViewModel/LoginViewModel.cs +++ b/ApartmentManager/ApartmentManager/ViewModel/LoginViewModel.cs @@ -1,106 +1,22 @@ using ApartmentManager.Common; using ApartmentManager.Handler; -using ApartmentManager.Model; -using ApartmentManager.View; -using System; using System.Windows.Input; -using Windows.UI.Popups; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; -using ApartmentManager.Singletons; namespace ApartmentManager.ViewModel { public class LoginViewModel { - public static string Username { get; set; } - public static string Password { get; set; } + public string Username { get; set; } + public string Password { get; set; } public ICommand LogInCommand { get; set; } public ICommand LogOutCommand { get; set; } + private LoginHandler loginHandler; public LoginViewModel() { - LogInCommand = new RelayCommand(LogIn); - LogOutCommand = new RelayCommand(LogOut); - } - - private void LogIn() - { - try - { - LoginHandler.LogIn(Username, Password); - NavigateToMainPage(); - } - catch(Exception ex) - { - var msg = new MessageDialog(ex.Message); - msg.ShowAsync(); - } - } - - private void LogOut() - { - LoginHandler.LogOut(); - NavigateToLoginPage(); - Username = null; - Password = null; - } - - private void NavigateToMainPage() - { - AppShell appShell = Window.Current.Content as AppShell; - - // Do not repeat app initialization when the Window already has content, - // just ensure that the window is active - if (appShell == null) - { - // Create a AppShell to act as the navigation context and navigate to the first page - appShell = new AppShell(); - - // Set the default language - appShell.Language = Windows.Globalization.ApplicationLanguages.Languages[0]; - - } - - // Place our app shell in the current Window - Window.Current.Content = appShell; - - if (appShell.AppFrame.Content == null) - { - // When the navigation stack isn't restored, navigate to the first page - // suppressing the initial entrance animation. - if (UserSingleton.Instance.CurrentUser.IsBm) appShell.AppFrame.Navigate(typeof(BoardMembersMainPage)); - else - { - ApartmentViewModel asd = new ApartmentViewModel(); - asd.ApartmentHandler.GetApartmentResidents(); - asd.ApartmentHandler.GetApartment(); - asd.ApartmentHandler.GetApartmentDefects(); - appShell.AppFrame.Navigate(typeof(ApartmentPage)); - } - } - - // Ensure the current window is active - Window.Current.Activate(); - } - - private void NavigateToLoginPage() - { - Frame rootFrame = Window.Current.Content as Frame; - - // Do not repeat app initialization when the Window already has content, - // just ensure that the window is active - if (rootFrame == null) - { - // Create a Frame to act as the navigation context and navigate to the first page - rootFrame = new Frame(); - - // Place the frame in the current Window - Window.Current.Content = rootFrame; - } - - rootFrame.Navigate(typeof(LoginPage)); - Window.Current.Activate(); + loginHandler = new LoginHandler(this); + LogInCommand = new RelayCommand(loginHandler.LogIn); + LogOutCommand = new RelayCommand(loginHandler.LogOut); } } } |