Skip to content

Commit

Permalink
progress on alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanyucel committed Jun 9, 2023
1 parent 4df2ccd commit 74d8e5e
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 4 deletions.
2 changes: 1 addition & 1 deletion App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public App()
private static IServiceProvider ConfigureServices()
{
var services = new ServiceCollection();
services.AddTransient<MainViewModel>();
services.AddSingleton<MainViewModel>();
services.AddSingleton<IWindowService, WindowService>();
services.AddSingleton<ILogService, LogService>();
services.AddSingleton<IWebService, WebService>();
Expand Down
7 changes: 7 additions & 0 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
d:DataContext="{d:DesignInstance Type=viewmodels:MainViewModel}"
mc:Ignorable="d" SizeToContent="WidthAndHeight"
Title="{Binding LastUpdate, StringFormat=SON GÜNCELLEME:{0}}">
<mah:MetroWindow.RightWindowCommands>
<mah:WindowCommands>
<Button ToolTip="Alarmlar" Command="{Binding ShowAlarmWindowCommand}">
<icons:PackIconMaterial Kind="Alarm" />
</Button>
</mah:WindowCommands>
</mah:MetroWindow.RightWindowCommands>
<mah:MetroWindow.Resources>
<converters:ChangeColorConverter x:Key="ChangeColorConverter" />
<converters:ChangeIconConverter x:Key="ChangeIconConverter" />
Expand Down
2 changes: 2 additions & 0 deletions models/Alarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
{
internal record struct Alarm
{
public string Id { get; private set; }
public string Code { get; private set; }
public double Value { get; private set; }
public bool IsHigh { get; private set; }
public bool IsEnabled { get; set; } = true;

public Alarm(string code, double value, bool isHigh)
{
Id = System.Guid.NewGuid().ToString();
Code = code;
Value = value;
IsHigh = isHigh;
Expand Down
3 changes: 3 additions & 0 deletions services/IWindowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ internal interface IWindowService
public void ShowMessage(string title, string message);
public void ShowError(string message);
public void ShowPreferenceWindow();

public void ShowAlarmSettingsWindow();
public void ShowAlarm(string title, string message);
}
}
24 changes: 23 additions & 1 deletion services/implementations/WindowService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
using System;
using DovizKuru.views;
using System;

namespace DovizKuru.services.implementations
{
internal class WindowService : IWindowService
{
public void ShowAlarm(string title, string message)
{
throw new NotImplementedException();
}

public void ShowAlarmSettingsWindow()
{
if (m_AlarmSettingsWindow == null)
{
m_AlarmSettingsWindow = new AlarmSettingsWindow();
m_AlarmSettingsWindow.Closed += (s, e) => m_AlarmSettingsWindow = null;
m_AlarmSettingsWindow.Show();
}
else
m_AlarmSettingsWindow.Activate();
}

public void ShowError(string message)
{
throw new NotImplementedException();
Expand All @@ -18,5 +36,9 @@ public void ShowPreferenceWindow()
{
throw new NotImplementedException();
}

#region Fields
private AlarmSettingsWindow? m_AlarmSettingsWindow;
#endregion
}
}
25 changes: 23 additions & 2 deletions viewmodels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using DovizKuru.services;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -12,18 +13,26 @@ namespace DovizKuru.viewmodels
internal class MainViewModel : ObservableObject
{
public IAsyncRelayCommand LoadPreferencesCommand { get => m_LoadPreferencesCommand; }
public IRelayCommand ShowAlarmWindowCommand { get => m_ShowAlarmWindowCommand; }
public IRelayCommand AddAlarmCommand { get => m_AddAlarmCommand; }
public IRelayCommand<string> RemoveAlarmCommand { get => m_RemoveAlarmCommand; }
public bool IsIdle { get => m_IsIdle; private set => SetProperty(ref m_IsIdle, value); }
public bool ShouldQueryRates { get => m_ShouldQueryRates; private set => SetProperty(ref m_ShouldQueryRates, value); }
public ObservableCollection<ExchangeRate> ExchangeRates { get => m_ExchangeRates; private set => SetProperty(ref m_ExchangeRates, value); }
public ObservableCollection<Alarm> AlarmList { get => m_AlarmList; private set => SetProperty(ref m_AlarmList, value); }

public DateTime LastUpdate { get => m_LastUpdate; private set => SetProperty(ref m_LastUpdate, value); }
public MainViewModel(IWebService webService, IPreferenceService preferenceService)
public MainViewModel(IWebService webService, IPreferenceService preferenceService, IWindowService windowService)
{
m_WebService = webService;
m_PreferenceService = preferenceService;
m_WindowService = windowService;

m_LoadPreferencesCommand = new AsyncRelayCommand(LoadPreferences, LoadPreferencesCanExecute);
m_ShowAlarmWindowCommand = new RelayCommand(ShowAlarmWindow);
m_AddAlarmCommand = new RelayCommand(ShowAddAlarmWindow);
m_RemoveAlarmCommand = new RelayCommand<string>(RemoveAlarm);

m_UpdateTimer = new(QueryExchangeRates, null, Timeout.Infinite, Timeout.Infinite);
}

Expand All @@ -36,7 +45,13 @@ private async Task LoadPreferences()
IsIdle = true;
}

private void QueryExchangeRates(object? state = null) => ShouldQueryRates = true;
private void ShowAddAlarmWindow() => throw new NotImplementedException();

private void RemoveAlarm(string alarmId) => AlarmList.Remove(AlarmList.FirstOrDefault(alarm => alarm.Id == alarmId, new()));

private void ShowAlarmWindow() => m_WindowService.ShowAlarmSettingsWindow();

private void QueryExchangeRates(object? _ = null) => ShouldQueryRates = true;

public void OnExchangeRatesQueried(string sourceHTML)
{
Expand All @@ -47,6 +62,8 @@ public void OnExchangeRatesQueried(string sourceHTML)

public void SourcePageLoaded() => m_UpdateTimer.Change(5000, c_UpdateTimerPeriod);



#region Command States
private bool LoadPreferencesCanExecute() => m_IsIdle;
#endregion
Expand All @@ -58,10 +75,14 @@ public void OnExchangeRatesQueried(string sourceHTML)

private readonly IWebService m_WebService;
private readonly IPreferenceService m_PreferenceService;
private readonly IWindowService m_WindowService;

private ObservableCollection<ExchangeRate> m_ExchangeRates = new();

private readonly IAsyncRelayCommand m_LoadPreferencesCommand;
private readonly IRelayCommand m_ShowAlarmWindowCommand;
private readonly IRelayCommand m_AddAlarmCommand;
private readonly IRelayCommand<string> m_RemoveAlarmCommand;

private readonly Timer m_UpdateTimer;
private const int c_UpdateTimerPeriod = 15000; // 15 seconds
Expand Down
31 changes: 31 additions & 0 deletions views/AlarmSettingsWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<mah:MetroWindow x:Class="DovizKuru.views.AlarmSettingsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:icons="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewmodels="clr-namespace:DovizKuru.viewmodels"
d:DataContext="{d:DesignInstance Type=viewmodels:MainViewModel}"
mc:Ignorable="d" Width="256" SizeToContent="Height"
Title="Alarmlar">
<DockPanel LastChildFill="True">
<TextBlock Text="Alarm Listesi" FontSize="20" FontWeight="Bold" Margin="10" DockPanel.Dock="Left" VerticalAlignment="Center" />
<Button Style="{StaticResource MahApps.Styles.Button.Circle}" Margin="0,5,5,0" Width="22" Height="22" DockPanel.Dock="Right" Command="{Binding AddAlarmCommand}" >
<icons:PackIconMaterial Kind="PlusCircle" Width="20" Height="20" Foreground="DarkGray" />
</Button>
<ItemsControl ItemsSource="{Binding AlarmList}" DockPanel.Dock="Bottom">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Code}" Margin="10" VerticalAlignment="Center" />
<TextBlock Text="{Binding Value}" Margin="10" VerticalAlignment="Center" />
<Button Style="{StaticResource MahApps.Styles.Button.Circle}" Margin="0,5,5,0" Width="22" Height="22" DockPanel.Dock="Right" Command="{Binding DeleteAlarmCommand}" CommandParameter="{Binding Id}">
<icons:PackIconMaterial Kind="DeleteCircle" Width="20" Height="20" Foreground="DarkGray" />
</Button>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DockPanel>
</mah:MetroWindow>
16 changes: 16 additions & 0 deletions views/AlarmSettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using DovizKuru.viewmodels;

namespace DovizKuru.views
{
/// <summary>
/// Interaction logic for AlarmSettingsWindow.xaml
/// </summary>
public partial class AlarmSettingsWindow
{
public AlarmSettingsWindow()
{
this.DataContext = App.Current.ServiceProvider.GetService(typeof(MainViewModel));
InitializeComponent();
}
}
}

0 comments on commit 74d8e5e

Please sign in to comment.