Skip to content

Commit

Permalink
Switched to a viewmodel for menu items
Browse files Browse the repository at this point in the history
  • Loading branch information
gus33000 committed Aug 6, 2019
1 parent 6393c57 commit 4e16297
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 87 deletions.
1 change: 1 addition & 0 deletions Chat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<DependentUpon>Shell.xaml</DependentUpon>
</Compile>
<Compile Include="ViewModels\AboutViewModel.cs" />
<Compile Include="ViewModels\ChatMenuItemViewModel.cs" />
<Compile Include="ViewModels\ComposeViewModel.cs" />
<Compile Include="ViewModels\ConversationViewModel.cs" />
<Compile Include="ViewModels\ShellViewModel.cs" />
Expand Down
65 changes: 37 additions & 28 deletions Controls/ChatMenuItemControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:media="using:Windows.UI.Xaml.Media"
mc:Ignorable="d"
Width="392"
d:DesignHeight="300"
d:DesignWidth="400">
d:DesignWidth="392">

<NavigationViewItem.ContextFlyout>
<MenuFlyout Placement="Top">
Expand Down Expand Up @@ -162,39 +163,47 @@
</Grid.ColumnDefinitions>

<!-- Profile Picture -->
<PersonPicture Grid.Row="0" x:Name="PeoplePic"
Grid.RowSpan="2"
Grid.Column="0"
Height="24"
Width="24"
VerticalAlignment="Center"
Margin="-6,0,12,0"/>
<PersonPicture Grid.Row="0"
x:Name="PeoplePic"
Grid.RowSpan="2"
Grid.Column="0"
Height="24"
Width="24"
VerticalAlignment="Center"
Contact="{x:Bind ViewModel.Contact, Mode=OneWay}"
Margin="-6,0,12,0"/>

<!-- Name -->
<TextBlock Grid.Row="0" x:Name="ChatName"
Grid.Column="1"
TextTrimming="CharacterEllipsis"
MaxLines="1"
Style="{ThemeResource FluentBaseTextStyle}" />
<TextBlock Grid.Row="0"
x:Name="ChatName"
Grid.Column="1"
TextTrimming="CharacterEllipsis"
Text="{x:Bind ViewModel.DisplayName, Mode=OneWay}"
MaxLines="1"
Style="{ThemeResource FluentBaseTextStyle}" />

<!-- Date -->
<TextBlock Grid.Row="0" x:Name="ChatDate"
Grid.Column="2"
VerticalAlignment="Center"
TextTrimming="CharacterEllipsis"
MaxLines="1"
Foreground="{ThemeResource SystemControlForegroundBaseMediumBrush}"
Style="{ThemeResource FluentCaptionTextStyle}" />
<TextBlock Grid.Row="0"
x:Name="ChatDate"
Grid.Column="2"
VerticalAlignment="Center"
TextTrimming="CharacterEllipsis"
MaxLines="1"
Text="{x:Bind ViewModel.TimeStamp, Mode=OneWay}"
Foreground="{ThemeResource SystemControlForegroundBaseMediumBrush}"
Style="{ThemeResource FluentCaptionTextStyle}" />

<!-- Content -->
<TextBlock Grid.Row="1" x:Name="ChatContent"
Grid.Column="1"
Grid.ColumnSpan="2"
Margin="0,4,0,0"
TextWrapping="Wrap"
TextTrimming="CharacterEllipsis"
MaxLines="3"
Style="{ThemeResource FluentSubbodyTextStyle}" />
<TextBlock Grid.Row="1"
x:Name="ChatContent"
Grid.Column="1"
Grid.ColumnSpan="2"
Margin="0,4,0,0"
TextWrapping="Wrap"
TextTrimming="CharacterEllipsis"
MaxLines="3"
Text="{x:Bind ViewModel.DisplayDescription, Mode=OneWay}"
Style="{ThemeResource FluentSubbodyTextStyle}" />

</Grid>
</NavigationViewItem>
45 changes: 8 additions & 37 deletions Controls/ChatMenuItemControl.xaml.cs
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();
}
}
}
2 changes: 1 addition & 1 deletion Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Identity
Name="LumiaWOA.Chat"
Publisher="CN=LumiaWOA"
Version="0.0.46.0" />
Version="0.0.47.0" />

<mp:PhoneIdentity PhoneProductId="d8719107-e9ed-450f-be3e-88003464d950" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

Expand Down
12 changes: 6 additions & 6 deletions Pages/ConversationPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Chat.Pages
public sealed partial class ConversationPage : Page
{
public ConversationViewModel ViewModel { get; } = new ConversationViewModel("");
private string convoid;
public string ConversationId;

public ConversationPage()
{
Expand Down Expand Up @@ -68,7 +68,7 @@ public ICommand StartCall
async () =>
{
var store = await ChatMessageManager.RequestStoreAsync();
await Launcher.LaunchUriAsync(new Uri("tel:" + (await store.GetConversationAsync(convoid)).Participants.First()));
await Launcher.LaunchUriAsync(new Uri("tel:" + (await store.GetConversationAsync(ConversationId)).Participants.First()));
});
}
return _startCall;
Expand All @@ -91,7 +91,7 @@ public ICommand SendReply
var smsDevice = ViewModel.CellularLines[CellularLineComboBox.SelectedIndex].device;
var store = await ChatMessageManager.RequestStoreAsync();
var result = await SmsUtils.SendTextMessageAsync(smsDevice, (await store.GetConversationAsync(convoid)).Participants.First(), ComposeTextBox.Text);
var result = await SmsUtils.SendTextMessageAsync(smsDevice, (await store.GetConversationAsync(ConversationId)).Participants.First(), ComposeTextBox.Text);
if (!result)
await new MessageDialog("We could not send one or some messages.", "Something went wrong").ShowAsync();
Expand All @@ -113,11 +113,11 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);

var args = e.Parameter as ChatConversation;
var args = e.Parameter as string;
if (args != null)
{
convoid = args.Id;
ViewModel.Initialize(convoid);
ConversationId = args;
ViewModel.Initialize(ConversationId);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Shell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public ICommand OpenAboutCommand
private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
if (args.SelectedItem != null)
MainFrame.Navigate(typeof(ConversationPage), (args.SelectedItem as ChatMenuItemControl).ChatConversation);
MainFrame.Navigate(typeof(ConversationPage), (args.SelectedItem as ChatMenuItemControl).ConversationId);
}
}
}
114 changes: 114 additions & 0 deletions ViewModels/ChatMenuItemViewModel.cs
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;
}
}
}
}
}
18 changes: 14 additions & 4 deletions ViewModels/ConversationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
using System.Threading.Tasks;
using Windows.ApplicationModel.Chat;
using Windows.ApplicationModel.Contacts;
using Windows.ApplicationModel.Core;
using Windows.Devices.Enumeration;
using Windows.Devices.Sms;
using Windows.UI.Core;

namespace Chat.ViewModels
{
Expand Down Expand Up @@ -119,14 +121,22 @@ private async Task<ObservableCollection<CellularLineControl>> GetSmsDevices()
return collection;
}

private void Store_StoreChanged(ChatMessageStore sender, ChatMessageStoreChangedEventArgs args)
private async void Store_StoreChanged(ChatMessageStore sender, ChatMessageStoreChangedEventArgs args)
{
switch (args.Kind)
{
case ChatStoreChangedEventKind.MessageCreated:
case ChatStoreChangedEventKind.MessageDeleted:
case ChatStoreChangedEventKind.MessageModified:
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;
}
}
Expand Down
Loading

0 comments on commit 4e16297

Please sign in to comment.