diff options
author | Donatas Adamonis <dona0169@edu.easj.dk> | 2017-05-24 18:47:38 +0200 |
---|---|---|
committer | Donatas Adamonis <dona0169@edu.easj.dk> | 2017-05-24 18:47:38 +0200 |
commit | 47b566edf0a16f0d2593cbb81e04fedaab83118d (patch) | |
tree | 7d365b8f4cd12d6f2014239b6a282290a4f2c2c9 | |
parent | de05092ce474a8727ed2ec575822c6a43f50af8c (diff) |
code optimization
11 files changed, 122 insertions, 73 deletions
diff --git a/ApartmentManager/ApartmentManager/App.xaml.cs b/ApartmentManager/ApartmentManager/App.xaml.cs index 297bf9d..140776a 100644 --- a/ApartmentManager/ApartmentManager/App.xaml.cs +++ b/ApartmentManager/ApartmentManager/App.xaml.cs @@ -30,7 +30,7 @@ namespace ApartmentManager public App() { this.InitializeComponent(); - this.Suspending += OnSuspending; + this.Suspending += OnSuspending; } /// <summary> diff --git a/ApartmentManager/ApartmentManager/AppShell.xaml.cs b/ApartmentManager/ApartmentManager/AppShell.xaml.cs index dec5cf7..b1a88c6 100644 --- a/ApartmentManager/ApartmentManager/AppShell.xaml.cs +++ b/ApartmentManager/ApartmentManager/AppShell.xaml.cs @@ -6,6 +6,7 @@ using System.Linq; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; +using ApartmentManager.Singletons; namespace ApartmentManager { diff --git a/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs b/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs index 095ee73..e59d75f 100644 --- a/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs +++ b/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs @@ -12,6 +12,7 @@ using Newtonsoft.Json; using Windows.Storage; using ApartmentManager.Common; using Windows.Storage.Pickers; +using ApartmentManager.View; namespace ApartmentManager.Handler { @@ -31,7 +32,7 @@ namespace ApartmentManager.Handler public void GetApartment() { - string serializedApartment = ApiClient.GetData("api/Apartments/" + ApartmentViewModel.ApartmentNumber); + string serializedApartment = ApiClient.GetData("api/Apartments/" + ApartmentViewModel.UserSingleton.CurrentUser.ApartmentId); Apartment apartment = JsonConvert.DeserializeObject<Apartment>(serializedApartment); ApartmentViewModel.CatalogSingleton.Apartment = apartment; @@ -43,7 +44,7 @@ namespace ApartmentManager.Handler public void GetApartmentResidents() { Resident resident = new Resident(); - resident.ApartmentId = ApartmentViewModel.ApartmentNumber; + resident.ApartmentId = ApartmentViewModel.UserSingleton.CurrentUser.ApartmentId; var residentsFromDatabase = ApiClient.GetData("api/ApartmentResidents/" + resident.ApartmentId); IEnumerable<Resident> residentlist = JsonConvert.DeserializeObject<IEnumerable<Resident>>(residentsFromDatabase); @@ -56,20 +57,15 @@ namespace ApartmentManager.Handler } } - + /////////////////////////////////////////////////////////////////////////////////////////////////// public void CreateResident() { try { Resident resident = new Resident(); - resident.ApartmentId = ApartmentViewModel.ApartmentNumber; - resident.FirstName = ApartmentViewModel.NewResident.FirstName; - resident.LastName = ApartmentViewModel.NewResident.LastName; - resident.BirthDate = ApartmentViewModel.NewResident.BirthDate; - resident.Email = ApartmentViewModel.NewResident.Email; - resident.Picture = ApartmentViewModel.NewResident.Picture; - resident.Phone = ApartmentViewModel.NewResident.Phone; - + resident = ApartmentViewModel.NewResident; + resident.ApartmentId = ApartmentViewModel.UserSingleton.CurrentUser.ApartmentId; + ApiClient.PostData("api/residents/", resident); var residentsFromDatabase = ApiClient.GetData("api/ApartmentResidents/" + resident.ApartmentId); @@ -87,21 +83,15 @@ namespace ApartmentManager.Handler new MessageDialog(e.Message).ShowAsync(); } } - + /////////////////////////////////////////////////////////////////////////////////////////////////// public void DeleteResident() { try { Resident resident = new Resident(); - resident.ResidentId = ApartmentViewModel.NewResident.ResidentId; - resident.ApartmentId = ApartmentViewModel.ApartmentNumber; - resident.FirstName = ApartmentViewModel.NewResident.FirstName; - resident.LastName = ApartmentViewModel.NewResident.LastName; - resident.BirthDate = ApartmentViewModel.NewResident.BirthDate; - resident.Email = ApartmentViewModel.NewResident.Email; - resident.Picture = ApartmentViewModel.NewResident.Picture; - resident.Phone = ApartmentViewModel.NewResident.Phone; - + resident = ApartmentViewModel.NewResident; + resident.ApartmentId = ApartmentViewModel.UserSingleton.CurrentUser.ApartmentId; + ApiClient.DeleteData("api/residents/" + resident.ResidentId); var residentsFromDatabase = ApiClient.GetData("api/ApartmentResidents/" + resident.ApartmentId); @@ -119,20 +109,14 @@ namespace ApartmentManager.Handler new MessageDialog(e.Message).ShowAsync(); } } - + /////////////////////////////////////////////////////////////////////////////////////////////////// public void UpdateResident() { try { Resident resident = new Resident(); - resident.ResidentId = ApartmentViewModel.NewResident.ResidentId; - resident.ApartmentId = ApartmentViewModel.ApartmentNumber; - resident.FirstName = ApartmentViewModel.NewResident.FirstName; - resident.LastName = ApartmentViewModel.NewResident.LastName; - resident.BirthDate = ApartmentViewModel.NewResident.BirthDate; - resident.Email = ApartmentViewModel.NewResident.Email; - resident.Picture = ApartmentViewModel.NewResident.Picture; - resident.Phone = ApartmentViewModel.NewResident.Phone; + resident = ApartmentViewModel.NewResident; + resident.ApartmentId = ApartmentViewModel.UserSingleton.CurrentUser.ApartmentId; ApiClient.PutData("api/residents/" + resident.ResidentId, resident); var residentsFromDatabase = ApiClient.GetData("api/ApartmentResidents/" + resident.ApartmentId); @@ -150,7 +134,7 @@ namespace ApartmentManager.Handler new MessageDialog(e.Message).ShowAsync(); } } - + /////////////////////////////////////////////////////////////////////////////////////////////////// public async void UploadResidentPhoto() { try @@ -173,22 +157,20 @@ namespace ApartmentManager.Handler try { ApartmentViewModel.UserSingleton.CurrentUser.Picture = await ImgurPhotoUploader.UploadPhotoAsync(); - var tmp = ApartmentViewModel.UserSingleton.CurrentUser; - ApartmentViewModel.UserSingleton.CurrentUser = new User(); - ApartmentViewModel.UserSingleton.CurrentUser = tmp; + } catch (Exception e) { } } - + /////////////////////////////////////////////////////////////////////////////////////////////////// public void UpdateUser() { try { User user = new User(); user = ApartmentViewModel.UserSingleton.CurrentUser; - ApiClient.PutData("api/users/" + user.Username, user); + ApiClient.PutData("api/users/" + user.Username, user); } catch (Exception e) { @@ -201,7 +183,7 @@ namespace ApartmentManager.Handler public void GetApartmentDefects() { Defect defect = new Defect(); - defect.ApartmentId = ApartmentViewModel.ApartmentNumber; + defect.ApartmentId = ApartmentViewModel.UserSingleton.CurrentUser.ApartmentId; var defectsFromDatabase = ApiClient.GetData("api/ApartmentDefects/" + defect.ApartmentId); IEnumerable<Defect> defecttlist = JsonConvert.DeserializeObject<IEnumerable<Defect>>(defectsFromDatabase); @@ -228,12 +210,11 @@ namespace ApartmentManager.Handler ApartmentViewModel.NewResident = new Resident(); foreach (var defect2 in defecttlist) { - ApartmentViewModel.CatalogSingleton.Defects.Add(defect2); } ApartmentViewModel.CatalogSingleton.DefectPictures.Clear(); } - + /////////////////////////////////////////////////////////////////////////////////////////////////// public void DeleteDefectPicture() { ApartmentViewModel.CatalogSingleton.DefectPictures.Remove(ApartmentViewModel.SelectedDefectPicture); @@ -245,25 +226,23 @@ namespace ApartmentManager.Handler ApartmentViewModel.SelectedDefectPicture = new DefectPicture(); ApartmentViewModel.SelectedDefectPicture.Picture = await ImgurPhotoUploader.UploadPhotoAsync(); - ApartmentViewModel.CatalogSingleton.DefectPictures.Add(ApartmentViewModel.SelectedDefectPicture); - + ApartmentViewModel.CatalogSingleton.DefectPictures.Add(ApartmentViewModel.SelectedDefectPicture); } catch (Exception e) { } } + /////////////////////////////////////////////////////////////////////////////////////////////////// public void CreateDefect() - { - try - { + { Defect defect = new Defect(); defect = ApartmentViewModel.NewDefect; - defect.ApartmentId = ApartmentViewModel.ApartmentNumber; + defect.ApartmentId = ApartmentViewModel.UserSingleton.CurrentUser.ApartmentId; defect.Status = "New"; defect.UploadDate = DateTime.Now; ApiClient.PostData("api/defects/", defect); - defect.DefectId = Int32.Parse(ApartmentViewModel.ServerResponse); + defect.DefectId = ApartmentViewModel.ServerResponse; foreach (var picture in ApartmentViewModel.CatalogSingleton.DefectPictures) { picture.DefectId = defect.DefectId; @@ -271,14 +250,17 @@ namespace ApartmentManager.Handler } GetApartmentDefects(); - - } - catch (Exception e) - { - new MessageDialog(e.Message).ShowAsync(); - } + } - + /////////////////////////////////////////////////////////////////////////////////////////////////// + public bool CreateDefect_CanExecute() + { + if (string.IsNullOrEmpty(ApartmentViewModel.NewDefect.Description) || string.IsNullOrEmpty(ApartmentViewModel.NewDefect.Name)) + return false; + else + return true; + } + /////////////////////////////////////////////////////////////////////////////////////////////////// public void GetDefectInfo() { var id = ApartmentViewModel.NewDefect.DefectId; @@ -300,9 +282,10 @@ namespace ApartmentManager.Handler ApartmentViewModel.CatalogSingleton.DefectPictures2.Add(asd); } + } + } + /////////////////////////////////////////////////////////////////////////////////////////////////// - } - } } } diff --git a/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs b/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs index e952e0b..65efeca 100644 --- a/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs +++ b/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs @@ -2,6 +2,8 @@ using ApartmentManager.Persistency; using Newtonsoft.Json; using System; +using ApartmentManager.Singletons; +using ApartmentManager.ViewModel; namespace ApartmentManager.Handler { @@ -16,7 +18,9 @@ namespace ApartmentManager.Handler if (user.Password == password) { UserSingleton.Instance.CurrentUser = user; - } + + + } else throw new Exception("Wrong password!"); } else throw new Exception("Wrong username!"); diff --git a/ApartmentManager/ApartmentManager/Model/User.cs b/ApartmentManager/ApartmentManager/Model/User.cs index 42964af..8543833 100644 --- a/ApartmentManager/ApartmentManager/Model/User.cs +++ b/ApartmentManager/ApartmentManager/Model/User.cs @@ -1,13 +1,16 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using Windows.UI.Xaml.Controls; +using ApartmentManager.Annotations; namespace ApartmentManager.Model { - public class User + public class User: INotifyPropertyChanged { public string Username { get; set; } public int ApartmentId { get; set; } @@ -18,7 +21,7 @@ namespace ApartmentManager.Model public DateTime BirthDate { get; set; } public string Phone { get; set; } public string Email { get; set; } - public string Picture { get; set; } + private string _picture { get; set; } public DateTime? MoveInDate { get; set; } public DateTime? MoveOutDate { get; set; } @@ -33,10 +36,29 @@ namespace ApartmentManager.Model this.Email = Email; } + public string Picture + { + get { return _picture; } + set + { + _picture = value; + OnPropertyChanged(nameof(Picture)); + } + } public override string ToString() { return string.Format($"First name {FirstName} Last name {LastName} Phone {Phone}"); } + + + public event PropertyChangedEventHandler PropertyChanged; + + [NotifyPropertyChangedInvocator] + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + + } } } diff --git a/ApartmentManager/ApartmentManager/Persistency/ApiClient.cs b/ApartmentManager/ApartmentManager/Persistency/ApiClient.cs index 8ae0e1c..78a5a55 100644 --- a/ApartmentManager/ApartmentManager/Persistency/ApiClient.cs +++ b/ApartmentManager/ApartmentManager/Persistency/ApiClient.cs @@ -3,6 +3,7 @@ using System; using System.Net.Http; using System.Net.Http.Headers; using System.Text; +using ApartmentManager.Model; using ApartmentManager.ViewModel; namespace ApartmentManager.Persistency @@ -68,11 +69,15 @@ namespace ApartmentManager.Persistency { string serializedData = JsonConvert.SerializeObject(objectToPost); StringContent content = new StringContent(serializedData, Encoding.UTF8, "application/json"); - var response = client.PostAsync(url, content).Result.Headers.Location.AbsolutePath.Remove(0,13); - ApartmentViewModel.ServerResponse = response; + var response = new HttpResponseMessage(); + response = client.PostAsync(url, content).Result; + var def = JsonConvert.DeserializeObject<Defect>(response.Content.ReadAsStringAsync().Result); + //var response = client.PostAsync(url, content).Result.Headers.Location.AbsolutePath.Remove(0,13); + ApartmentViewModel.ServerResponse = def.DefectId; } catch (Exception) { + } } } diff --git a/ApartmentManager/ApartmentManager/Singletons/CatalogSingleton.cs b/ApartmentManager/ApartmentManager/Singletons/CatalogSingleton.cs index 62bbae3..050987d 100644 --- a/ApartmentManager/ApartmentManager/Singletons/CatalogSingleton.cs +++ b/ApartmentManager/ApartmentManager/Singletons/CatalogSingleton.cs @@ -1,10 +1,13 @@ using System; using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Runtime.CompilerServices; +using ApartmentManager.Annotations; using ApartmentManager.Model; namespace ApartmentManager.Singletons { - public class CatalogSingleton + public class CatalogSingleton : INotifyPropertyChanged { private static CatalogSingleton instance = new CatalogSingleton(); @@ -13,7 +16,7 @@ namespace ApartmentManager.Singletons public Apartment Apartment { get; set; } public ObservableCollection<Resident> Residents { get; set; } - public ObservableCollection<Defect> Defects { get; set; } + public ObservableCollection<Defect> defects { get; set; } public ObservableCollection<DefectPicture> DefectPictures { get; set; } public ObservableCollection<DefectPicture> DefectPictures2 { get; set; } public Defect Defect { get; set; } @@ -22,9 +25,27 @@ namespace ApartmentManager.Singletons { Residents = new ObservableCollection<Resident>(); - Defects = new ObservableCollection<Defect>(); + defects = new ObservableCollection<Defect>(); DefectPictures = new ObservableCollection<DefectPicture>(); DefectPictures2 = new ObservableCollection<DefectPicture>(); } + public ObservableCollection<Defect> Defects + { + get => this.defects; + set + { + this.defects = value; + OnPropertyChanged(nameof(Defect)); + + } + } + + public event PropertyChangedEventHandler PropertyChanged; + + [NotifyPropertyChangedInvocator] + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } } } diff --git a/ApartmentManager/ApartmentManager/View/ApartmentNewDefect.xaml.cs b/ApartmentManager/ApartmentManager/View/ApartmentNewDefect.xaml.cs index ba7139d..aed6e6e 100644 --- a/ApartmentManager/ApartmentManager/View/ApartmentNewDefect.xaml.cs +++ b/ApartmentManager/ApartmentManager/View/ApartmentNewDefect.xaml.cs @@ -29,12 +29,16 @@ namespace ApartmentManager.View this.InitializeComponent(); vm = new ApartmentViewModel(); DataContext = vm; + } private void Navigate(object sender, RoutedEventArgs e) { vm.CreateDefect.Execute(null); - Frame.Navigate(typeof(ApartmentDefectPage)); + if (vm.CreateDefect.CanExecute(null)) + { + Frame.Navigate(typeof(ApartmentDefectPage)); + } } } } diff --git a/ApartmentManager/ApartmentManager/ViewModel/ApartmentViewModel.cs b/ApartmentManager/ApartmentManager/ViewModel/ApartmentViewModel.cs index f932f18..618f27e 100644 --- a/ApartmentManager/ApartmentManager/ViewModel/ApartmentViewModel.cs +++ b/ApartmentManager/ApartmentManager/ViewModel/ApartmentViewModel.cs @@ -21,14 +21,15 @@ namespace ApartmentManager.ViewModel public CatalogSingleton CatalogSingleton { get; set; } public UserSingleton UserSingleton { get; set; } + private User _newUser; private Resident _newResident; private Defect _newDefect; private DefectPicture _selectedDefectPicture; - public static int ApartmentNumber { get; set; } - public static string ServerResponse { get; set; } + + public static int ServerResponse { get; set; } public ICommand CreateResidentCommand { get; set; } public ICommand DeleteResidentCommand { get; set; } @@ -51,7 +52,7 @@ namespace ApartmentManager.ViewModel ApartmentHandler = new ApartmentHandler(this); CatalogSingleton = CatalogSingleton.Instance; UserSingleton = UserSingleton.Instance; - ApartmentNumber = UserSingleton.CurrentUser.ApartmentId; + ////////// User relay commands////////// UpdateUser = new RelayCommand(ApartmentHandler.UpdateUser); UploadUserPhoto = new RelayCommand(ApartmentHandler.UploadUserPhoto); @@ -63,12 +64,11 @@ namespace ApartmentManager.ViewModel ////////// Defect relay commands////////// UploadDefectPicture = new RelayCommand(ApartmentHandler.UploadDefectPhoto); DeleteDefectPicture = new RelayCommand(ApartmentHandler.DeleteDefectPicture); - CreateDefect = new RelayCommand(ApartmentHandler.CreateDefect); + CreateDefect = new RelayCommand(ApartmentHandler.CreateDefect, ApartmentHandler.CreateDefect_CanExecute); DefectInfo = new RelayCommand(ApartmentHandler.GetDefectInfo); - ApartmentHandler.GetApartmentResidents(); - ApartmentHandler.GetApartment(); - ApartmentHandler.GetApartmentDefects(); + } + public Defect NewDefect { diff --git a/ApartmentManager/ApartmentManager/ViewModel/BoardMemberViewModel.cs b/ApartmentManager/ApartmentManager/ViewModel/BoardMemberViewModel.cs index fc2c838..db80503 100644 --- a/ApartmentManager/ApartmentManager/ViewModel/BoardMemberViewModel.cs +++ b/ApartmentManager/ApartmentManager/ViewModel/BoardMemberViewModel.cs @@ -9,6 +9,7 @@ using System.Windows.Input; using ApartmentManager.Annotations; using ApartmentManager.Common; using ApartmentManager.Model; +using ApartmentManager.Singletons; namespace ApartmentManager.ViewModel { diff --git a/ApartmentManager/ApartmentManager/ViewModel/LoginViewModel.cs b/ApartmentManager/ApartmentManager/ViewModel/LoginViewModel.cs index 5f3c1b7..0031061 100644 --- a/ApartmentManager/ApartmentManager/ViewModel/LoginViewModel.cs +++ b/ApartmentManager/ApartmentManager/ViewModel/LoginViewModel.cs @@ -7,6 +7,7 @@ using System.Windows.Input; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; +using ApartmentManager.Singletons; namespace ApartmentManager.ViewModel { @@ -69,7 +70,14 @@ namespace ApartmentManager.ViewModel // 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 appShell.AppFrame.Navigate(typeof(ApartmentPage)); + 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 |