Skip to content

Commit

Permalink
Finished refactoring MainViewModel
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Hofmann <oss@hoffe.org>
  • Loading branch information
hoffe86 committed Dec 21, 2023
1 parent 09b1c6d commit bfcc99b
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 32 deletions.
41 changes: 41 additions & 0 deletions src/openHAB.Core/Messages/DataOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

/// <summary>
/// Represents the state of a data operation.
/// </summary>
public enum OperationState
{
/// <summary>
/// Represents no operation state.
/// </summary>
None,

/// <summary>
/// Represents the start of a data operation.
/// </summary>
Started,

/// <summary>
/// Represents the completion of a data operation.
/// </summary>
Completed
}

/// <summary>
/// Represents a data operation.
/// </summary>
public class DataOperation
{
/// <summary>
/// Initializes a new instance of the <see cref="DataOperation"/> class with the specified state.
/// </summary>
/// <param name="state">The state of the data operation.</param>
public DataOperation(OperationState state = OperationState.None)
{
State = state;
}

/// <summary>
/// Gets or sets the state of the data operation.
/// </summary>
public OperationState State { get; set; }
}
28 changes: 28 additions & 0 deletions src/openHAB.Core/Messages/WigetNavigation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using openHAB.Core.Model;

/// <summary>
/// Represents a navigation between two OpenHAB widgets.
/// </summary>
public class WigetNavigation
{
/// <summary>
/// Initializes a new instance of the <see cref="WigetNavigation"/> class.
/// </summary>
/// <param name="originWidget">The origin widget.</param>
/// <param name="targetWidget">The target widget.</param>
public WigetNavigation(OpenHABWidget originWidget, OpenHABWidget targetWidget)
{
OriginWidget = originWidget;
TargetWidget = targetWidget;
}

/// <summary>
/// Gets or sets the origin widget.
/// </summary>
public OpenHABWidget OriginWidget { get; set; }

/// <summary>
/// Gets or sets the target widget.
/// </summary>
public OpenHABWidget TargetWidget { get; set; }
}
14 changes: 6 additions & 8 deletions src/openHAB.Windows/View/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
xmlns:media="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:model="using:openHAB.Core.Model"
mc:Ignorable="d"
Name="Page">65417
Name="Page">
<Page.Resources>
<Style TargetType="GridViewItem">
<Setter Property="HorizontalContentAlignment"
Expand Down Expand Up @@ -108,11 +108,11 @@
Text="{Binding Vm.SelectedSitemap.Label, ElementName=Page, Mode=OneWay}">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<core:InvokeCommandAction Command="{Binding Vm.NavigateToSitemapRoot, ElementName=Page, Mode=OneWay}" />
<core:InvokeCommandAction Command="{Binding Vm.SelectedSitemap.NavigateToSitemapRoot, ElementName=Page, Mode=OneWay}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors></TextBlock>
<Button Margin="10,0,0,0"
Command="{Binding Vm.ReloadSitemapCommand, ElementName=Page}">
Command="{Binding Vm.SelectedSitemap.ReloadSitemapCommand, ElementName=Page}">
<SymbolIcon Symbol="Refresh"
Foreground="Black"
VerticalAlignment="Center" />
Expand Down Expand Up @@ -140,7 +140,7 @@
<DataTemplate x:Key="NavHeaderTemplate">
<StackPanel Orientation="Horizontal"
Margin="10">
<Button Command="{Binding Vm.ReloadSitemapCommand, ElementName=Page}">
<Button Command="{Binding Vm.SelectedSitemap.ReloadSitemapCommand, ElementName=Page}">
<SymbolIcon Symbol="Refresh"
Foreground="Black"
VerticalAlignment="Center" />
Expand Down Expand Up @@ -229,9 +229,7 @@
Style="{StaticResource FlyoutPickerTitleTextBlockStyle}" />
</StackPanel>
</muxc:NavigationView.PaneCustomContent>
<muxc:NavigationView.FooterMenuItems>

</muxc:NavigationView.FooterMenuItems>
<muxc:NavigationView.FooterMenuItems></muxc:NavigationView.FooterMenuItems>
<Grid x:Name="MainContentGrid"
Margin="35,0,0,0"
VerticalAlignment="Stretch"
Expand All @@ -251,7 +249,7 @@
IsTapEnabled="True"
ItemClick="MasterListView_OnItemClick"
ItemTemplateSelector="{StaticResource WidgetTemplateSelector}"
ItemsSource="{x:Bind Vm.CurrentWidgets, Mode=OneWay}"
ItemsSource="{x:Bind Vm.SelectedSitemap.CurrentWidgets, Mode=OneWay}"
SelectionMode="None">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
Expand Down
6 changes: 5 additions & 1 deletion src/openHAB.Windows/View/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ protected override void OnNavigatedFrom(NavigationEventArgs e)
}

#pragma warning disable S1172 // Unused method parameters should be removed

private async Task ShowErrorMessage(object recipient, FireErrorMessage message)
#pragma warning restore S1172 // Unused method parameters should be removed
{
Expand Down Expand Up @@ -99,6 +100,7 @@ await App.DispatcherQueue.EnqueueAsync(() =>
}

#pragma warning disable S1172 // Unused method parameters should be removed

private async Task ShowInfoMessage(object recipient, FireInfoMessage msg)
#pragma warning restore S1172 // Unused method parameters should be removed
{
Expand All @@ -110,9 +112,11 @@ private async Task ShowInfoMessage(object recipient, FireInfoMessage msg)
case MessageType.NotConfigured:
message = AppResources.Values.GetString("MessageNotConfigured");
break;

case MessageType.NotReachable:
message = AppResources.Values.GetString("MessagesNotReachable");
break;

default:
message = "Message not defined";
break;
Expand Down Expand Up @@ -148,7 +152,7 @@ private void SitemapNavigation_SelectionChanged(
private void BreadcrumbBar_ItemClicked(BreadcrumbBar sender, BreadcrumbBarItemClickedEventArgs args)
{
OpenHABWidget widget = args.Item as OpenHABWidget;
this.Vm.WidgetGoBack(widget);
this.Vm.SelectedSitemap.WidgetGoBack(widget);
}
}
}
4 changes: 2 additions & 2 deletions src/openHAB.Windows/View/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<ListViewItem x:Uid="MenuLogsView">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<core:NavigateToPageAction TargetPage="OpenHAB.Windows.View.LogViewerPage" />
<core:NavigateToPageAction TargetPage="openHAB.Windows.View.LogViewerPage" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</ListViewItem>
Expand All @@ -106,7 +106,7 @@
Icon="Cancel">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<core:NavigateToPageAction TargetPage="OpenHAB.Windows.View.MainPage" />
<core:NavigateToPageAction TargetPage="openHAB.Windows.View.MainPage" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</AppBarButton>
Expand Down
35 changes: 32 additions & 3 deletions src/openHAB.Windows/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public MainViewModel(IOpenHAB openHabsdk, ISettingsService settingsService, ILog
_openHabsdk = openHabsdk;
_settingsService = settingsService;
_breadcrumbItems = new ObservableCollection<OpenHABWidget>();

StrongReferenceMessenger.Default.Register<DataOperation>(this, (obj, operation)
=> DataOperationState(obj, operation));

StrongReferenceMessenger.Default.Register<WigetNavigation>(this, (obj, operation)
=> WidgetNavigatedEvent(obj, operation));
}

/// <summary>
Expand Down Expand Up @@ -126,10 +132,9 @@ public SitemapViewModel SelectedSitemap
}
else
{
_selectedSitemap.SetWidgetsOnScreen(SelectedSitemap.Widgets);
_selectedSitemap?.SetWidgetsOnScreen(SelectedSitemap.Widgets);
}

SelectedWidget = null;
SelectedMenuItem = value;
}
}
Expand Down Expand Up @@ -207,7 +212,6 @@ await App.DispatcherQueue.EnqueueAsync(() =>
{
IsDataLoading = true;
Sitemaps?.Clear();
SelectedSitemap = null;
});

Settings settings = _settingsService.Load();
Expand Down Expand Up @@ -318,5 +322,30 @@ private void OpenLastOrDefaultSitemap()
}

#endregion

#region Events

private void DataOperationState(object obj, DataOperation operation)
{
switch (operation.State)
{
case OperationState.Started:
IsDataLoading = true;
break;

case OperationState.Completed:
IsDataLoading = false;
break;
}
}

private void WidgetNavigatedEvent(object obj, WigetNavigation operation)
{
BreadcrumbItems.Clear();
BreadcrumbItems.AddRange(WidgetNavigationService.Widgets);
OnPropertyChanged(nameof(BreadcrumbItems));
}

#endregion
}
}
49 changes: 31 additions & 18 deletions src/openHAB.Windows/ViewModel/SitemapViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
Expand Down Expand Up @@ -44,7 +45,7 @@ public SitemapViewModel(OpenHABSitemap model, ServerInfo serverInfo)
: base(model)
{
_serverInfo = serverInfo;
_widgets = new ObservableCollection<OpenHABWidget>(model.Widgets);
_widgets = new ObservableCollection<OpenHABWidget>(model.Widgets ?? new List<OpenHABWidget>());
_openHabsdk = DIService.Instance.GetService<IOpenHAB>();

_currentWidgets = new ObservableCollection<OpenHABWidget>();
Expand All @@ -54,7 +55,10 @@ public SitemapViewModel(OpenHABSitemap model, ServerInfo serverInfo)

StrongReferenceMessenger.Default.Register<TriggerCommandMessage>(this, async (recipient, msg)
=> await TriggerItemCommand(recipient, msg).ConfigureAwait(false));
}

StrongReferenceMessenger.Default.Register<DataOperation>(this, (obj, operation)
=> DataOperationState(obj, operation));
}

#endregion

Expand Down Expand Up @@ -141,7 +145,7 @@ public ObservableCollection<OpenHABWidget> Widgets

private bool CanExecuteReloadSitemapCommand(object arg)
{
return !IsDataLoading;
return !_canExecuteReloadSitemap;
}

private async void ExecuteReloadSitemapCommand(object obj)
Expand All @@ -154,6 +158,7 @@ private async void ExecuteReloadSitemapCommand(object obj)
#region Navigate To Sitemap Root Command

private ActionCommand _navigateToSitemapRootCommand;
private bool _canExecuteReloadSitemap;

public ActionCommand NavigateToSitemapRoot => _navigateToSitemapRootCommand ?? (_navigateToSitemapRootCommand = new ActionCommand(ExecuteNavigateToSitemapRootCommand, CanExecuteNavigateToSitemapRootCommand));

Expand All @@ -166,9 +171,9 @@ private void ExecuteNavigateToSitemapRootCommand(object obj)
{
WidgetNavigationService.ClearWidgetNavigation();
SetWidgetsOnScreen(Widgets);
SelectedWidget = null;

BreadcrumbItems.Clear();
OnPropertyChanged(nameof(BreadcrumbItems));
StrongReferenceMessenger.Default.Send<WigetNavigation>(new WigetNavigation(SelectedWidget, null));
}

#endregion
Expand All @@ -192,6 +197,19 @@ private async Task TriggerItemCommand(object recipient, TriggerCommandMessage me
}
}

private void DataOperationState(object obj, DataOperation operation)
{
switch (operation.State)
{
case OperationState.Started:
_canExecuteReloadSitemap = true;
break;
case OperationState.Completed:
_canExecuteReloadSitemap = false;
break;
}
}

#endregion

/// <summary>
Expand All @@ -210,14 +228,14 @@ public async Task LoadWidgets(OpenHABVersion version)
public async Task LoadWidgets()
{
CurrentWidgets.Clear();
IsDataLoading = true;
StrongReferenceMessenger.Default.Send<DataOperation>(new DataOperation(OperationState.Started));

await this.LoadWidgets(_serverInfo.Version).ConfigureAwait(false);

await App.DispatcherQueue.EnqueueAsync(() =>
{
SetWidgetsOnScreen(this.Widgets);
IsDataLoading = false;
StrongReferenceMessenger.Default.Send<DataOperation>(new DataOperation(OperationState.Completed));
});
}

Expand All @@ -226,12 +244,12 @@ public async Task ReloadSitemap()
await App.DispatcherQueue.EnqueueAsync(async () =>
{
CurrentWidgets?.Clear();
IsDataLoading = true;
StrongReferenceMessenger.Default.Send<DataOperation>(new DataOperation(OperationState.Started));

if (SelectedWidget != null)
{
await LoadWidgets().ConfigureAwait(false);
OpenHABWidget widget = FindWidget(SelectedWidget.WidgetId, SelectedSitemap.Widgets);
OpenHABWidget widget = FindWidget(SelectedWidget.WidgetId, Widgets);
if (widget != null)
{
await OnWidgetClickedAsync(widget);
Expand All @@ -247,9 +265,8 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
await LoadWidgets().ConfigureAwait(false);
}

IsDataLoading = false;
StrongReferenceMessenger.Default.Send<DataOperation>(new DataOperation(OperationState.Completed));
ReloadSitemapCommand.InvokeCanExecuteChanged(null);
RefreshCommand.InvokeCanExecuteChanged(null);
});
}

Expand All @@ -268,10 +285,7 @@ public void WidgetGoBack(OpenHABWidget widget)
SelectedWidget = widget;

SetWidgetsOnScreen(widget.LinkedPage.Widgets);

BreadcrumbItems.Clear();
BreadcrumbItems.AddRange(WidgetNavigationService.Widgets);
OnPropertyChanged(nameof(BreadcrumbItems));
StrongReferenceMessenger.Default.Send<WigetNavigation>(new WigetNavigation(lastWidget, widget));
}

private OpenHABWidget FindWidget(string widgetId, ICollection<OpenHABWidget> widgets)
Expand Down Expand Up @@ -304,6 +318,7 @@ private async Task OnWidgetClickedAsync(OpenHABWidget widget)
{
await App.DispatcherQueue.EnqueueAsync(() =>
{
OpenHABWidget lastWidget = SelectedWidget;
SelectedWidget = widget;
if (SelectedWidget.LinkedPage == null || !SelectedWidget.LinkedPage.Widgets.Any())
{
Expand All @@ -313,9 +328,7 @@ await App.DispatcherQueue.EnqueueAsync(() =>
Subtitle = SelectedWidget.Label;

WidgetNavigationService.Navigate(SelectedWidget);

BreadcrumbItems.Clear();
BreadcrumbItems.AddRange(WidgetNavigationService.Widgets);
StrongReferenceMessenger.Default.Send<WigetNavigation>(new WigetNavigation(lastWidget, widget));

SetWidgetsOnScreen(SelectedWidget?.LinkedPage?.Widgets);
});
Expand Down

0 comments on commit bfcc99b

Please sign in to comment.