Skip to content

Commit

Permalink
- remove styling/trigger/rowupdates
Browse files Browse the repository at this point in the history
- initialise new observable list with no 'is visible' timers each second -.-
  • Loading branch information
pjmagee committed Nov 23, 2024
1 parent 6c994b1 commit 8a2b809
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 32 deletions.
5 changes: 3 additions & 2 deletions src/Dota2Helper/Dota2Helper/ViewModels/DotaTimerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ public void Update(TimeSpan gameTime)

if (IsEnabled)
{
IsStopped = CalculateIsStopped(gameTime);
IsStarted = CalculateIsStarted(gameTime);
IsStopped = CalculateIsStopped(gameTime);

IsVisible = CalculateIsVisible(gameTime);

TimeRemaining = CalculateTimeRemaining(gameTime);
Expand Down Expand Up @@ -283,7 +284,7 @@ bool CalculateIsStarted(TimeSpan gameTime)

bool CalculateIsVisible(TimeSpan gameTime)
{
return IsEnabled && !IsStopped && IsStarted;
return IsEnabled && IsStarted && !IsStopped;
}

TimeSpan CalculateTimeUntilNextOccurrence(TimeSpan gameTime)
Expand Down
25 changes: 14 additions & 11 deletions src/Dota2Helper/Dota2Helper/ViewModels/TimersViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Dota2Helper.Features.Settings;
using Dota2Helper.Features.TimeProvider;
Expand All @@ -34,7 +35,14 @@ public ObservableCollection<DotaTimerViewModel> Timers
set => SetProperty(ref _timers, value);
}

public ObservableCollection<DotaTimerViewModel> VisibleTimers
{
get => _visibleTimers;
set => SetProperty(ref _visibleTimers, value);
}

readonly SemaphoreSlim _semaphore = new(1, 1);
ObservableCollection<DotaTimerViewModel> _visibleTimers = new();

public TimersViewModel(SettingsViewModel settingsViewModel, ITimeProvider timeProvider)
{
Expand Down Expand Up @@ -68,24 +76,19 @@ static void CloseApplication()

async void UpdateTimers(object? state)
{


if (await _semaphore.WaitAsync(0))
{
try
{
Timers = _settingsViewModel.SelectedProfileViewModel.Timers;

Time = _timeProvider.Time;

Dispatcher.UIThread.Invoke(() =>
{
foreach (var timer in Timers)
{
timer.Update(Time);
}
}
);
foreach (var timer in Timers)
{
timer.Update(Time);
}

VisibleTimers = new(Timers.Where(t => t.IsVisible));
}
finally
{
Expand Down
8 changes: 4 additions & 4 deletions src/Dota2Helper/Dota2Helper/Views/TimersView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
CanUserResizeColumns="False"
CanUserSortColumns="False"
GridLinesVisibility="Horizontal"
ItemsSource="{Binding Timers}">
ItemsSource="{Binding VisibleTimers}">
<DataGrid.Resources>
<!-- Could not find a way to make row not hit test while maintain button hit test -->
<SolidColorBrush x:Key="DataGridRowSelectedBackgroundBrush" Color="Transparent" />
Expand All @@ -70,9 +70,9 @@
<SolidColorBrush x:Key="DataGridRowHoveredBackgroundColor" Color="Transparent" />
</DataGrid.Resources>
<DataGrid.Styles>
<Style Selector="DataGridRow">
<Setter Property="IsVisible" Value="{Binding IsVisible, DataType=vm:DotaTimerViewModel}" />
</Style>
<!-- <Style Selector="DataGridRow"> -->
<!-- <Setter Property="IsVisible" Value="{Binding IsVisible, DataType=vm:DotaTimerViewModel}" /> -->
<!-- </Style> -->
<!-- <Style Selector="DataGridRow.visible"> -->
<!-- <Setter Property="IsVisible" Value="True" /> -->
<!-- </Style> -->
Expand Down
56 changes: 41 additions & 15 deletions src/Dota2Helper/Dota2Helper/Views/TimersView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Avalonia.Controls;
using Avalonia.LogicalTree;
using Avalonia.VisualTree;
Expand All @@ -19,33 +21,57 @@ void OnItemOnPropertyChanged(object? sender, PropertyChangedEventArgs args)
{
try
{
var item = (DotaTimerViewModel) sender!;
var item = (DotaTimerViewModel)sender!;
var dataGrid = this.GetControl<DataGrid>("TimersGrid");

foreach (var descendant in dataGrid.GetVisualDescendants())
foreach (DataGridRow row in dataGrid.GetVisualDescendants().OfType<DataGridRow>())
{
if (descendant is DataGridRow row && row.DataContext is DotaTimerViewModel dataContext)
if (row.DataContext is DotaTimerViewModel dataContext)
{
Debug.WriteLine($"Row {dataContext.Name} IsVisible changed to {item.IsVisible}");
row.IsVisible = item.IsVisible;
if (dataContext == item)
{
row.IsVisible = item.IsVisible;
row.Classes.Remove("visible");
row.Classes.Remove("hidden");

if (item.IsVisible)
{
row.Classes.Add("visible");
}
else
{
row.Classes.Add("hidden");
}
}
}
}
}
finally
catch (Exception e)
{

Debug.WriteLine(e);
}
}

void TimersGrid_OnInitialized(object? sender, EventArgs e)
{
if (DataContext is TimersViewModel viewModel)
{
foreach (var item in viewModel.Timers)
{
item.PropertyChanged -= OnItemOnPropertyChanged;
item.PropertyChanged += OnItemOnPropertyChanged;
}
}
// if (DataContext is TimersViewModel viewModel)
// {
// viewModel.Timers.CollectionChanged += (o, args) => { IsVisibleCalculation(args); };
//
// foreach (var item in viewModel.Timers)
// {
// item.PropertyChanged -= OnItemOnPropertyChanged;
// item.PropertyChanged += OnItemOnPropertyChanged;
// }
// }
}

void IsVisibleCalculation(NotifyCollectionChangedEventArgs args)
{
// foreach (var item in args.NewItems!.OfType<DotaTimerViewModel>())
// {
// item.PropertyChanged -= OnItemOnPropertyChanged;
// item.PropertyChanged += OnItemOnPropertyChanged;
// }
}
}

0 comments on commit 8a2b809

Please sign in to comment.