From 94f0e6e5fb382099e797031dd6a0be4f41eca118 Mon Sep 17 00:00:00 2001 From: Richard Bailey Date: Mon, 2 Oct 2017 12:56:41 +0200 Subject: [PATCH] - updated UniversalWindowsPlatform to v5.2.3 - started with implementation of MvvmLight in BindableBase #hacktoberfest --- ChatApp/ChatApp/Common/BindableBase.cs | 20 ++------------ .../ChatApp/ViewModels/ChatPageViewModel.cs | 27 ++++++++----------- .../ViewModels/ContactsPageViewModel.cs | 18 +++++-------- ChatApp/ChatApp/project.json | 3 ++- 4 files changed, 21 insertions(+), 47 deletions(-) diff --git a/ChatApp/ChatApp/Common/BindableBase.cs b/ChatApp/ChatApp/Common/BindableBase.cs index 07ee266..7af993d 100644 --- a/ChatApp/ChatApp/Common/BindableBase.cs +++ b/ChatApp/ChatApp/Common/BindableBase.cs @@ -1,27 +1,11 @@ - -using System; -using System.ComponentModel; -using System.Runtime.CompilerServices; +using GalaSoft.MvvmLight; namespace ChatApp.Common { /// /// Provides a standard change-notification implementation. /// - public abstract class BindableBase : INotifyPropertyChanged + public abstract class BindableBase : ViewModelBase { - public event PropertyChangedEventHandler PropertyChanged; - - public void OnPropertyChanged([CallerMemberName] string propertyName = null) => - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - - protected bool SetProperty(ref T storage, T value, - [CallerMemberName] String propertyName = null) - { - if (object.Equals(storage, value)) return false; - storage = value; - OnPropertyChanged(propertyName); - return true; - } } } diff --git a/ChatApp/ChatApp/ViewModels/ChatPageViewModel.cs b/ChatApp/ChatApp/ViewModels/ChatPageViewModel.cs index 2019f00..f50695c 100644 --- a/ChatApp/ChatApp/ViewModels/ChatPageViewModel.cs +++ b/ChatApp/ChatApp/ViewModels/ChatPageViewModel.cs @@ -1,14 +1,8 @@ - -using ChatApp.Commands; +using ChatApp.Commands; using ChatApp.Common; using ChatApp.Facades; using ChatApp.Models.Csharp; -using System; -using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows.Input; namespace ChatApp.ViewModels @@ -28,14 +22,17 @@ public class ChatPageViewModel : BindableBase public string Text { get { return _text; } - set { SetProperty(ref _text, value); } + set { base.Set(ref _text, value); } } + public ObservableCollection MessageThread { get { return _messageThread; } - private set { SetProperty(ref _messageThread, value); } + private set { base.Set(ref _messageThread, value); } } + public ICommand SendMessage { get; set; } + /// /// The chat correspondent. /// @@ -44,11 +41,9 @@ public Profile ChatCorrespondent get { return _chatCorrespondent; } set { - if(_chatCorrespondent == null || value.User.Id != _chatCorrespondent.User.Id) - { - SetProperty(ref _chatCorrespondent, value); - MessageThread = MessageFacade.GetMessages(); - } + if (_chatCorrespondent != null && value.User.Id == _chatCorrespondent.User.Id) return; + base.Set(ref _chatCorrespondent, value); + MessageThread = MessageFacade.GetMessages(); } } @@ -59,7 +54,7 @@ public ChatPageViewModel() } /// - /// Sets the message to text from the textbox. + /// Sets the message to text from the text box. /// /// internal void SetText(string text) @@ -67,4 +62,4 @@ internal void SetText(string text) Text = text; } } -} +} \ No newline at end of file diff --git a/ChatApp/ChatApp/ViewModels/ContactsPageViewModel.cs b/ChatApp/ChatApp/ViewModels/ContactsPageViewModel.cs index 4ffa18c..f141246 100644 --- a/ChatApp/ChatApp/ViewModels/ContactsPageViewModel.cs +++ b/ChatApp/ChatApp/ViewModels/ContactsPageViewModel.cs @@ -1,12 +1,7 @@ using ChatApp.Common; using ChatApp.Facades; using ChatApp.Models.Csharp; -using System; -using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace ChatApp.ViewModels { @@ -16,20 +11,19 @@ namespace ChatApp.ViewModels /// public class ContactsPageViewModel : BindableBase { - private string error; + private readonly string error; public static ObservableCollection _allFriends; public static ObservableCollection _recentConversations; - private ObservableCollection _friends; - public ObservableCollection AllFriends { get { return _allFriends; } } - public ObservableCollection RecentConversations { get { return _recentConversations; } } - public ObservableCollection Friends { get { return _friends; } } - + public ObservableCollection AllFriends => _allFriends; + public ObservableCollection RecentConversations => _recentConversations; + public ObservableCollection Friends { get; } + public ContactsPageViewModel() { _allFriends = ContactFacade.GetAllFriends(out error); _recentConversations = ContactFacade.GetRecentConversations(out error); - _friends = ContactFacade.GetFriendProfiles(); + Friends = ContactFacade.GetFriendProfiles(); } } } diff --git a/ChatApp/ChatApp/project.json b/ChatApp/ChatApp/project.json index c594939..0d9159a 100644 --- a/ChatApp/ChatApp/project.json +++ b/ChatApp/ChatApp/project.json @@ -1,6 +1,7 @@ { "dependencies": { - "Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0" + "Microsoft.NETCore.UniversalWindowsPlatform": "5.2.3", + "MvvmLightLibs": "5.3.0" }, "frameworks": { "uap10.0": {}