diff --git a/App.xaml.cs b/App.xaml.cs index 3b0530f..dd0a23f 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -20,7 +20,7 @@ public App() private static IServiceProvider ConfigureServices() { var services = new ServiceCollection(); - services.AddTransient(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/MainWindow.xaml b/MainWindow.xaml index d2020b0..a385b87 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -14,6 +14,13 @@ d:DataContext="{d:DesignInstance Type=viewmodels:MainViewModel}" mc:Ignorable="d" SizeToContent="WidthAndHeight" Title="{Binding LastUpdate, StringFormat=SON GÜNCELLEME:{0}}"> + + + + + diff --git a/models/Alarm.cs b/models/Alarm.cs index 7160dc5..12cc229 100644 --- a/models/Alarm.cs +++ b/models/Alarm.cs @@ -2,6 +2,7 @@ { 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; } @@ -9,6 +10,7 @@ internal record struct Alarm public Alarm(string code, double value, bool isHigh) { + Id = System.Guid.NewGuid().ToString(); Code = code; Value = value; IsHigh = isHigh; diff --git a/services/IWindowService.cs b/services/IWindowService.cs index 5e058c8..b812dde 100644 --- a/services/IWindowService.cs +++ b/services/IWindowService.cs @@ -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); } } diff --git a/services/implementations/WindowService.cs b/services/implementations/WindowService.cs index 695891d..dbd223b 100644 --- a/services/implementations/WindowService.cs +++ b/services/implementations/WindowService.cs @@ -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(); @@ -18,5 +36,9 @@ public void ShowPreferenceWindow() { throw new NotImplementedException(); } + + #region Fields + private AlarmSettingsWindow? m_AlarmSettingsWindow; + #endregion } } diff --git a/viewmodels/MainViewModel.cs b/viewmodels/MainViewModel.cs index 34b7f36..15d07d8 100644 --- a/viewmodels/MainViewModel.cs +++ b/viewmodels/MainViewModel.cs @@ -4,6 +4,7 @@ using DovizKuru.services; using System; using System.Collections.ObjectModel; +using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -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 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 ExchangeRates { get => m_ExchangeRates; private set => SetProperty(ref m_ExchangeRates, value); } public ObservableCollection 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(RemoveAlarm); + m_UpdateTimer = new(QueryExchangeRates, null, Timeout.Infinite, Timeout.Infinite); } @@ -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) { @@ -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 @@ -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 m_ExchangeRates = new(); private readonly IAsyncRelayCommand m_LoadPreferencesCommand; + private readonly IRelayCommand m_ShowAlarmWindowCommand; + private readonly IRelayCommand m_AddAlarmCommand; + private readonly IRelayCommand m_RemoveAlarmCommand; private readonly Timer m_UpdateTimer; private const int c_UpdateTimerPeriod = 15000; // 15 seconds diff --git a/views/AlarmSettingsWindow.xaml b/views/AlarmSettingsWindow.xaml new file mode 100644 index 0000000..770c218 --- /dev/null +++ b/views/AlarmSettingsWindow.xaml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + diff --git a/views/AlarmSettingsWindow.xaml.cs b/views/AlarmSettingsWindow.xaml.cs new file mode 100644 index 0000000..1bd52d0 --- /dev/null +++ b/views/AlarmSettingsWindow.xaml.cs @@ -0,0 +1,16 @@ +using DovizKuru.viewmodels; + +namespace DovizKuru.views +{ + /// + /// Interaction logic for AlarmSettingsWindow.xaml + /// + public partial class AlarmSettingsWindow + { + public AlarmSettingsWindow() + { + this.DataContext = App.Current.ServiceProvider.GetService(typeof(MainViewModel)); + InitializeComponent(); + } + } +}