Skip to content

Commit

Permalink
More work on the view models
Browse files Browse the repository at this point in the history
  • Loading branch information
gus33000 committed Aug 6, 2019
1 parent 99df2a4 commit 62062de
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 40 deletions.
16 changes: 16 additions & 0 deletions Chat.Common/ContactUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ public class ContactInformation
public string PhoneNumberKind { get; set; }
}

public static async Task<Contact> GetMyself()
{
var store = await ContactManager.RequestStoreAsync();
if (store != null)
{
var contact = await store.GetMeContactAsync();

if (contact != null)
return contact;
}

Contact blankcontact = new Contact();
blankcontact.Phones.Add(new ContactPhone() { Number = "Me", Kind = ContactPhoneKind.Other });
return blankcontact;
}

public static async Task<Contact> BindPhoneNumberToGlobalContact(string phonenumber)
{
var store = await ContactManager.RequestStoreAsync();
Expand Down
1 change: 1 addition & 0 deletions Chat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
</Compile>
<Compile Include="ViewModels\AboutViewModel.cs" />
<Compile Include="ViewModels\ChatMenuItemViewModel.cs" />
<Compile Include="ViewModels\ChatMessageViewModel.cs" />
<Compile Include="ViewModels\ComposeViewModel.cs" />
<Compile Include="ViewModels\ConversationViewModel.cs" />
<Compile Include="ViewModels\ShellViewModel.cs" />
Expand Down
23 changes: 15 additions & 8 deletions Controls/ChatMessageViewControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,33 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignHeight="100"
d:DesignWidth="500">

<StackPanel Margin="8">
<Grid x:Name="ChatBubble" CornerRadius="8" MinWidth="100" MaxWidth="400" HorizontalAlignment="Left">
<Grid x:Name="BgColor" Background="{ThemeResource SystemAccentColor}"/>
<Grid Margin="8" HorizontalAlignment="{x:Bind ViewModel.Alignment, Mode=OneWay}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>

<PersonPicture Contact="{x:Bind ViewModel.Contact, Mode=OneWay}" Margin="0,0,16,0" Height="40" Width="40" VerticalAlignment="Top"/>

<Grid Grid.Column="1" x:Name="ChatBubble" CornerRadius="8" MinWidth="100" MaxWidth="400">
<Grid x:Name="BgColor" Background="{ThemeResource SystemAccentColor}" Opacity="0.5"/>

<Grid Padding="8">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<TextBlock Text="{x:Bind message.Body}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" Style="{ThemeResource FluentBodyTextStyle}" Foreground="White"/>
<TextBlock x:Name="DateTimeLabel" VerticalAlignment="Bottom" HorizontalAlignment="Right" Grid.Row="1" TextTrimming="CharacterEllipsis" MaxLines="1" Foreground="White" Style="{ThemeResource FluentCaptionTextStyle}" >
<TextBlock Text="{x:Bind ViewModel.MessageBody, Mode=OneWay}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" Style="{ThemeResource FluentBodyTextStyle}" Foreground="White" IsTextSelectionEnabled="True"/>
<TextBlock x:Name="DateTimeLabel" VerticalAlignment="Bottom" Text="{x:Bind ViewModel.TimeStamp, Mode=OneWay}" HorizontalAlignment="Right" Grid.Row="1" TextTrimming="CharacterEllipsis" MaxLines="1" Foreground="White" Opacity="0.75" Style="{ThemeResource FluentCaptionTextStyle}" >
<ToolTipService.ToolTip>
<ToolTip Content="{x:Bind message.LocalTimestamp.LocalDateTime}"/>
<ToolTip Content="{x:Bind ViewModel.TimeStamp, Mode=OneWay}"/>
</ToolTipService.ToolTip>
</TextBlock>
</Grid>
</Grid>
</StackPanel>
</Grid>
</UserControl>
21 changes: 7 additions & 14 deletions Controls/ChatMessageViewControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
using Chat.Helpers;
using Windows.ApplicationModel.Chat;
using Windows.UI.Xaml;
using Chat.ViewModels;
using Windows.UI.Xaml.Controls;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

namespace Chat.Controls
{
public sealed partial class ChatMessageViewControl : UserControl
{
private ChatMessage message;
public ChatMessageViewControl(ChatMessage message)
public ChatMessageViewModel ViewModel { get; } = new ChatMessageViewModel("");

public string messageId;
public ChatMessageViewControl(string messageId)
{
this.InitializeComponent();
this.message = message;
if (!message.IsIncoming)
{
ChatBubble.HorizontalAlignment = HorizontalAlignment.Right;
BgColor.Opacity = 0.75;
}
DateTimeLabel.Text = message.LocalTimestamp.ToLocalTime().ToRelativeString();
this.messageId = messageId;
ViewModel.Initialize(messageId);
}
}
}
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.48.0" />
Version="0.0.52.0" />

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

Expand Down
2 changes: 1 addition & 1 deletion Pages/ComposePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ComboBox x:Name="CellularLineComboBox" Grid.Column="3" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="0,4,0,8" SelectionChanged="CellularLineComboBox_SelectionChanged" Style="{ThemeResource FluentComboBoxStyle}" ItemsSource="{x:Bind ViewModel.CellularLines, Mode=OneWay}" SelectedIndex="0" BorderBrush="Transparent" Background="Transparent" PlaceholderText="No cellular device" BorderThickness="0"/>
<ComboBox x:Name="CellularLineComboBox" Grid.Column="3" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="0,4,0,8" SelectionChanged="CellularLineComboBox_SelectionChanged" Style="{ThemeResource FluentComboBoxStyle}" ItemsSource="{x:Bind ViewModel.CellularLines, Mode=OneWay}" SelectedItem="{x:Bind ViewModel.SelectedLine, Mode=TwoWay}" BorderBrush="Transparent" Background="Transparent" PlaceholderText="No cellular device" BorderThickness="0"/>
<Grid Grid.Row="1" VerticalAlignment="Center" Margin="0,0,16,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
Expand Down
2 changes: 1 addition & 1 deletion Pages/ComposePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public ICommand SendReply
try
{
SendButton.IsEnabled = false;
var smsDevice = ViewModel.CellularLines[CellularLineComboBox.SelectedIndex].device;
var smsDevice = ViewModel.SelectedLine.device;

var result = await SmsUtils.SendTextMessageAsync(smsDevice, ContactPickerBox.Text.Split(';'), ComposeTextBox.Text);
if (!result)
Expand Down
2 changes: 1 addition & 1 deletion Pages/ConversationPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<PersonPicture Width="50" Margin="16,8" x:Name="ConvoPic" Contact="{x:Bind ViewModel.Contact, Mode=OneWay}"/>
<TextBlock x:Name="ConvoTitle" Grid.Column="1" Style="{ThemeResource FluentTitleTextStyle}" VerticalAlignment="Center" Text="{x:Bind ViewModel.DisplayName, Mode=OneWay}"/>
<Button x:Name="CallButton" Grid.Column="2" Height="48" Width="48" Foreground="{ThemeResource SystemControlForegroundAccentBrush}" Style="{ThemeResource FluentDialogButtonBackStyle}" VerticalAlignment="Center" FontFamily="Segoe MDL2 Assets" Content="&#xE717;" BorderBrush="Transparent" BorderThickness="0" Command="{x:Bind StartCall}"/>
<ComboBox x:Name="CellularLineComboBox" Grid.Column="3" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="0,4,0,8" SelectionChanged="CellularLineComboBox_SelectionChanged" ItemsSource="{x:Bind ViewModel.CellularLines, Mode=OneWay}" Style="{ThemeResource FluentComboBoxStyle}" SelectedIndex="0" BorderBrush="Transparent" Background="Transparent" PlaceholderText="No cellular device" BorderThickness="0"/>
<ComboBox x:Name="CellularLineComboBox" Grid.Column="3" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="0,4,0,8" SelectionChanged="CellularLineComboBox_SelectionChanged" ItemsSource="{x:Bind ViewModel.CellularLines, Mode=OneWay}" Style="{ThemeResource FluentComboBoxStyle}" SelectedItem="{x:Bind ViewModel.SelectedLine, Mode=TwoWay}" BorderBrush="Transparent" Background="Transparent" PlaceholderText="No cellular device" BorderThickness="0"/>
</Grid>

<!-- Compose panel -->
Expand Down
2 changes: 1 addition & 1 deletion Pages/ConversationPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public ICommand SendReply
try
{
SendButton.IsEnabled = false;
var smsDevice = ViewModel.CellularLines[CellularLineComboBox.SelectedIndex].device;
var smsDevice = ViewModel.SelectedLine.device;

var store = await ChatMessageManager.RequestStoreAsync();
var result = await SmsUtils.SendTextMessageAsync(smsDevice, (await store.GetConversationAsync(ConversationId)).Participants.First(), ComposeTextBox.Text);
Expand Down
36 changes: 36 additions & 0 deletions Shell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,43 @@ public ICommand OpenAboutCommand
_openAboutCommand = new RelayCommand(
async () =>
{
#if !DEBUG
await new AboutContentDialog().ShowAsync();
#else
Windows.ApplicationModel.Chat.ChatMessageStore store = await Windows.ApplicationModel.Chat.ChatMessageManager.RequestStoreAsync();
string transportId = await Windows.ApplicationModel.Chat.ChatMessageManager.RegisterTransportAsync();
Windows.ApplicationModel.Chat.ChatMessage msg = new Windows.ApplicationModel.Chat.ChatMessage();

msg.Body = "Hello how are you?";

msg.TransportId = transportId;

msg.MessageOperatorKind = Windows.ApplicationModel.Chat.ChatMessageOperatorKind.Sms;
msg.Status = Windows.ApplicationModel.Chat.ChatMessageStatus.Sent;

bool alternate = new Random().Next(2) == 2;

msg.Recipients.Clear();
msg.From = "";

var offset = new DateTimeOffset(DateTime.Now);
msg.LocalTimestamp = offset;
msg.NetworkTimestamp = offset;

msg.IsIncoming = alternate;
if (msg.IsIncoming)
{
msg.From = "Random dude";
}
else
{
msg.Recipients.Add("Random dude");
}

alternate = !alternate;

await store.SaveMessageAsync(msg);
#endif
});
}
return _openAboutCommand;
Expand Down
19 changes: 18 additions & 1 deletion ViewModels/ChatMenuItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public async void Initialize(string ConvoId)
_store = await ChatMessageManager.RequestStoreAsync();
_conversationid = ConvoId;

var convo = await _store.GetConversationAsync(_conversationid);
if (convo == null)
{
_store.StoreChanged -= Store_StoreChanged;
return;
}

(Contact, DisplayName) = await GetContactInformation();
(DisplayDescription, TimeStamp) = await GetLastMessageInfo();

Expand Down Expand Up @@ -99,15 +106,25 @@ private async void Store_StoreChanged(ChatMessageStore sender, ChatMessageStoreC
var conversation = await _store.GetConversationAsync(args.Id);

if (conversation == null)
{
_store.StoreChanged -= Store_StoreChanged;
break;
}

(var str, var dt) = await GetLastMessageInfo();

await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
Initialize(_conversationid);
(DisplayDescription, TimeStamp) = (str, dt);
});
break;
}
case ChatStoreChangedEventKind.ConversationDeleted:
{
_store.StoreChanged -= Store_StoreChanged;
break;
}
}
}
}
Expand Down
115 changes: 115 additions & 0 deletions ViewModels/ChatMessageViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using Chat.Common;
using Chat.Helpers;
using System;
using System.Threading.Tasks;
using Windows.ApplicationModel.Chat;
using Windows.ApplicationModel.Contacts;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
using Windows.UI.Xaml;

namespace Chat.ViewModels
{
public class ChatMessageViewModel : Observable
{
private DateTime _timeStamp;
public DateTime TimeStamp
{
get { return _timeStamp; }
set { Set(ref _timeStamp, value); }
}

private string _messageBody;
public string MessageBody
{
get { return _messageBody; }
set { Set(ref _messageBody, value); }
}

private Contact _contact;
public Contact Contact
{
get { return _contact; }
set { Set(ref _contact, value); }
}

private HorizontalAlignment _alignment;
public HorizontalAlignment Alignment
{
get { return _alignment; }
set { Set(ref _alignment, value); }
}

private ChatMessageStore _store;
private string _messageid;


// Constructor
public ChatMessageViewModel(string MessageId)
{
Initialize(MessageId);
}

// Initialize Stuff
public async void Initialize(string MessageId)
{
if (string.IsNullOrEmpty(MessageId))
return;

_store = await ChatMessageManager.RequestStoreAsync();
_messageid = MessageId;

Contact = await GetContactInformation();
(MessageBody, TimeStamp, Alignment) = await GetMessageInfo();

_store.ChangeTracker.Enable();
_store.StoreChanged += Store_StoreChanged;
}

// Methods
private async Task<Contact> GetContactInformation()
{
var msg = await _store.GetMessageAsync(_messageid);

if (!msg.IsIncoming)
return await ContactUtils.GetMyself();

return await ContactUtils.BindPhoneNumberToGlobalContact(msg.From);
}

private async Task<(string, DateTime, HorizontalAlignment)> GetMessageInfo()
{
var msg = await _store.GetMessageAsync(_messageid);
var align = msg.IsIncoming ? HorizontalAlignment.Left : HorizontalAlignment.Right;
return (msg.Body, msg.LocalTimestamp.LocalDateTime, align);
}

private async void Store_StoreChanged(ChatMessageStore sender, ChatMessageStoreChangedEventArgs args)
{
switch (args.Kind)
{
case ChatStoreChangedEventKind.MessageDeleted:
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
MessageBody = "Deleted message";
});
_store.StoreChanged -= Store_StoreChanged;
break;
}
case ChatStoreChangedEventKind.MessageModified:
{
(var body, var ts, var align) = await GetMessageInfo();

await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
(MessageBody, TimeStamp) = (body, ts);
});
break;
}
}
}
}
}
10 changes: 10 additions & 0 deletions ViewModels/ComposeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ public ObservableCollection<CellularLineControl> CellularLines
set { Set(ref _cellularLines, value); }
}

private CellularLineControl _selectedLine;
public CellularLineControl SelectedLine
{
get { return _selectedLine; }
set { Set(ref _selectedLine, value); }
}

// Constructor
public ComposeViewModel()
{
Expand All @@ -28,6 +35,9 @@ public ComposeViewModel()
public async void Initialize()
{
CellularLines = await GetSmsDevices();

if (CellularLines.Count != 0)
SelectedLine = CellularLines[0];
}

// Methods
Expand Down
Loading

0 comments on commit 62062de

Please sign in to comment.