-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switched to a viewmodel for menu items
- Loading branch information
Showing
9 changed files
with
192 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,27 @@ | ||
using Chat.Common; | ||
using Chat.Helpers; | ||
using Chat.ViewModels; | ||
using System; | ||
using System.Linq; | ||
using Windows.ApplicationModel.Chat; | ||
using Windows.ApplicationModel.Contacts; | ||
using Windows.UI.Xaml.Controls; | ||
|
||
namespace Chat.Controls | ||
{ | ||
public sealed partial class ChatMenuItemControl : NavigationViewItem | ||
{ | ||
private string DisplayMessage; | ||
private Contact Contact; | ||
private string DisplayDate; | ||
private string DisplayName; | ||
public ChatMenuItemViewModel ViewModel { get; } = new ChatMenuItemViewModel(""); | ||
public string ConversationId; | ||
|
||
public ChatConversation ChatConversation; | ||
|
||
public ChatMenuItemControl(ChatConversation ChatConversation) | ||
public ChatMenuItemControl(string ConversationId) | ||
{ | ||
this.InitializeComponent(); | ||
|
||
this.ChatConversation = ChatConversation; | ||
Load(); | ||
} | ||
|
||
private async void Load() | ||
{ | ||
var participant = ChatConversation.Participants.First(); | ||
var contact = await ContactUtils.BindPhoneNumberToGlobalContact(participant); | ||
|
||
var messageReader = ChatConversation.GetMessageReader(); | ||
var lastMessageId = ChatConversation.MostRecentMessageId; | ||
|
||
var messages = await messageReader.ReadBatchAsync(); | ||
|
||
var lastMessage = messages.Where(x => x.Id == lastMessageId).First(); | ||
|
||
DisplayMessage = lastMessage.Body; | ||
DisplayDate = lastMessage.LocalTimestamp.ToLocalTime().ToRelativeString(); | ||
Contact = contact; | ||
DisplayName = contact.DisplayName; | ||
|
||
ChatName.Text = DisplayName; | ||
ChatDate.Text = DisplayDate; | ||
PeoplePic.Contact = Contact; | ||
ChatContent.Text = DisplayMessage; | ||
this.ConversationId = ConversationId; | ||
ViewModel.Initialize(ConversationId); | ||
} | ||
|
||
private async void DeleteConvoButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) | ||
{ | ||
await ChatConversation.DeleteAsync(); | ||
var store = await ChatMessageManager.RequestStoreAsync(); | ||
await (await store.GetConversationAsync(ConversationId)).DeleteAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using Chat.Common; | ||
using Chat.Helpers; | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Windows.ApplicationModel.Chat; | ||
using Windows.ApplicationModel.Contacts; | ||
using Windows.ApplicationModel.Core; | ||
using Windows.UI.Core; | ||
|
||
namespace Chat.ViewModels | ||
{ | ||
public class ChatMenuItemViewModel : Observable | ||
{ | ||
|
||
private Contact _contact; | ||
public Contact Contact | ||
{ | ||
get { return _contact; } | ||
set { Set(ref _contact, value); } | ||
} | ||
|
||
private string _displayName; | ||
public string DisplayName | ||
{ | ||
get { return _displayName; } | ||
set { Set(ref _displayName, value); } | ||
} | ||
|
||
private string _displayDescription; | ||
public string DisplayDescription | ||
{ | ||
get { return _displayDescription; } | ||
set { Set(ref _displayDescription, value); } | ||
} | ||
|
||
private DateTime _timeStamp; | ||
public DateTime TimeStamp | ||
{ | ||
get { return _timeStamp; } | ||
set { Set(ref _timeStamp, value); } | ||
} | ||
|
||
private ChatMessageStore _store; | ||
private string _conversationid; | ||
|
||
// Constructor | ||
public ChatMenuItemViewModel(string ConvoId) | ||
{ | ||
Initialize(ConvoId); | ||
} | ||
|
||
|
||
// Initialize Stuff | ||
public async void Initialize(string ConvoId) | ||
{ | ||
if (string.IsNullOrEmpty(ConvoId)) | ||
return; | ||
|
||
_store = await ChatMessageManager.RequestStoreAsync(); | ||
_conversationid = ConvoId; | ||
|
||
(Contact, DisplayName) = await GetContactInformation(); | ||
(DisplayDescription, TimeStamp) = await GetLastMessageInfo(); | ||
|
||
_store.ChangeTracker.Enable(); | ||
_store.StoreChanged += Store_StoreChanged; | ||
} | ||
|
||
// Methods | ||
private async Task<(Contact, string)> GetContactInformation() | ||
{ | ||
var convo = await _store.GetConversationAsync(_conversationid); | ||
var contact = await ContactUtils.BindPhoneNumberToGlobalContact(convo.Participants.First()); | ||
|
||
return (contact, contact.DisplayName); | ||
} | ||
|
||
private async Task<(string, DateTime)> GetLastMessageInfo() | ||
{ | ||
var convo = await _store.GetConversationAsync(_conversationid); | ||
|
||
var messageReader = convo.GetMessageReader(); | ||
var lastMessageId = convo.MostRecentMessageId; | ||
|
||
var messages = await messageReader.ReadBatchAsync(); | ||
|
||
var lastMessage = messages.Where(x => x.Id == lastMessageId).First(); | ||
|
||
return (lastMessage.Body, lastMessage.LocalTimestamp.LocalDateTime); | ||
} | ||
|
||
private async void Store_StoreChanged(ChatMessageStore sender, ChatMessageStoreChangedEventArgs args) | ||
{ | ||
switch (args.Kind) | ||
{ | ||
case ChatStoreChangedEventKind.ConversationModified: | ||
{ | ||
var conversation = await _store.GetConversationAsync(args.Id); | ||
|
||
if (conversation == null) | ||
break; | ||
|
||
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, | ||
() => | ||
{ | ||
Initialize(_conversationid); | ||
}); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.