Skip to content

Commit

Permalink
Improved navigation view model caching, fixed handling the device con…
Browse files Browse the repository at this point in the history
…nection in DirectMethodView
  • Loading branch information
kgalic committed Sep 30, 2019
1 parent a23297c commit 7e3de7b
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,17 @@ private void SendDeviceConnectionUpdatedMessage()

private async Task ReceiveCloudMessages(CancellationToken cancellationToken)
{
while(!cancellationToken.IsCancellationRequested)
while(true)
{
var cloudToDeviceMessage = await _deviceClient.ReceiveAsync(_cancellationToken);
if (cancellationToken.IsCancellationRequested)
{
break;
}
if (cloudToDeviceMessage == null)
{
continue;
}
if (cloudToDeviceMessage.BodyStream != null)
{
var streamReader = new StreamReader(cloudToDeviceMessage.BodyStream);
Expand All @@ -185,6 +193,8 @@ private async Task ReceiveCloudMessages(CancellationToken cancellationToken)
_consoleLoggerService.Log(value: message,
logType: ConsoleLogTypes.C2DCommunication);
}

await _deviceClient.CompleteAsync(cloudToDeviceMessage);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CloudToDeviceCommunicationViewModel : MvxViewModel
{
#region Fields

private MvxSubscriptionToken _deviceStatusChangedMessageToken;
private MvxSubscriptionToken _outputChangedMessageToken;

private string _messageOutput = string.Empty;

Expand All @@ -20,7 +20,7 @@ public class CloudToDeviceCommunicationViewModel : MvxViewModel

public CloudToDeviceCommunicationViewModel(IMvxMessenger messageService)
{
messageService.Subscribe<CloudMessageReceivedMessage>(HandleCloudToDeviceMessage);
_outputChangedMessageToken = messageService.Subscribe<CloudMessageReceivedMessage>(HandleCloudToDeviceMessage);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class DirectMethodCommunicationViewModel : MvxViewModel

private ObservableCollection<DirectMethodSettingViewItem> _directMethodSettingViewItems;

private MvxSubscriptionToken _deviceConnectionStatusChangedMessageToken;
private MvxSubscriptionToken _directMethodStatusChangedMessageToken;

private string _directMethodEntry;
Expand All @@ -42,6 +43,7 @@ public DirectMethodCommunicationViewModel(IDeviceService deviceService,
_deviceSettingDataService = deviceSettingDataService;

_directMethodStatusChangedMessageToken = messageService.Subscribe<DirectMethodStatusUpdatedMessage>(HandleDirectMethodStatusChanged);
_deviceConnectionStatusChangedMessageToken = messageService.Subscribe<DeviceConnectionChangedMessage>(HandleDeviceConnectionStatus);
}

public override Task Initialize()
Expand Down Expand Up @@ -184,8 +186,9 @@ private DirectMethodSettingViewItem CreateDirectMethodViewItem(DirectMethodSetti
await _deviceService.UnregisterDirectMethodAsync(directMethodViewItem.DirectMethodSetting.DirectMethodName);
}
});
var removeCommand = new MvxCommand<DirectMethodSettingViewItem>((item) =>
var removeCommand = new MvxCommand<DirectMethodSettingViewItem>(async (item) =>
{
await _deviceService.UnregisterDirectMethodAsync(item.DirectMethodSetting.DirectMethodName);
_directMethodSettingViewItems.Remove(item);
});
directMethodViewItem.RemoveCommand = removeCommand;
Expand All @@ -194,6 +197,24 @@ private DirectMethodSettingViewItem CreateDirectMethodViewItem(DirectMethodSetti
return directMethodViewItem;
}

private void HandleDeviceConnectionStatus(DeviceConnectionChangedMessage message)
{
if (message.IsConnected)
{
Status = string.Empty;
}
else
{
foreach(var viewItem in ViewItems)
{
if (!viewItem.IsEnabled)
{
viewItem.RegisterCommand.Execute();
}
}
}
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
// if cache mode is 'Required' then View Model
// should be retrieved instead of creating a new one
var cachedViewModel = ViewModel;
base.OnNavigatedTo(e);
if (cachedViewModel != null)
if (cachedViewModel == null)
{
ViewModel = cachedViewModel;
base.OnNavigatedTo(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
// if cache mode is 'Required' then View Model
// should be retrieved instead of creating a new one
var cachedViewModel = ViewModel as DirectMethodCommunicationViewModel;
base.OnNavigatedTo(e);
if (cachedViewModel != null)
if (cachedViewModel == null)
{
cachedViewModel.Initialize(); //forcing
ViewModel = cachedViewModel;
base.OnNavigatedTo(e);
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions DeviceSimulator/Views/Device/HomeView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
// if cache mode is 'Required' then View Model
// should be retrieved instead of creating a new one
var cachedViewModel = ViewModel;
base.OnNavigatedTo(e);
if (cachedViewModel != null)
if (cachedViewModel == null)
{
ViewModel = cachedViewModel;
base.OnNavigatedTo(e);
}
}

Expand Down
10 changes: 5 additions & 5 deletions DeviceSimulator/Views/EventGrid/EventGridView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MvvmCross.Platforms.Uap.Views;
using MessagePublisher.Core;
using MvvmCross.Platforms.Uap.Views;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -32,11 +33,10 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
// if cache mode is 'Required' then View Model
// should be retrieved instead of creating a new one
var cachedViewModel = ViewModel;
base.OnNavigatedTo(e);
if (cachedViewModel != null)
var cachedViewModel = ViewModel as EventGridViewModel;
if (cachedViewModel == null)
{
ViewModel = cachedViewModel;
base.OnNavigatedTo(e);
}
}

Expand Down
11 changes: 2 additions & 9 deletions DeviceSimulator/Views/ServiceBus/ServiceBusView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,9 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
// if cache mode is 'Required' then View Model
// should be retrieved instead of creating a new one
var cachedViewModel = ViewModel as ServiceBusViewModel;
var selectedIndex = 0;
if (cachedViewModel != null)
if (cachedViewModel == null)
{
selectedIndex = cachedViewModel.ServiceBusEntityOptionsSelectedIndexId;
}
base.OnNavigatedTo(e);
if (cachedViewModel != null)
{
cachedViewModel.ServiceBusEntityOptionsSelectedIndexId = selectedIndex;
ViewModel = cachedViewModel;
base.OnNavigatedTo(e);
}
}

Expand Down

0 comments on commit 7e3de7b

Please sign in to comment.