Skip to content

Commit

Permalink
Added tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
Jebzou committed Nov 24, 2024
1 parent 41aa569 commit cfb2bf8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 20 deletions.
6 changes: 6 additions & 0 deletions ElectronicObserver/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ private static Tracker JotTracker()
.Property(w => w.ViewModel.DataGridViewModel.ColumnProperties)
.Property(w => w.ViewModel.DataGridViewModel.SortDescriptions);

tracker
.Configure<SenkaLeaderboardManager>()
.Property(w => w.CurrentCutoffData.DataGridViewModel.ColumnProperties)
.Property(w => w.CurrentCutoffData.DataGridViewModel.SortDescriptions)
.Property(w => w.CurrentCutoffData.PagingViewModel.ItemsPerPage);

return tracker;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace ElectronicObserver.Data;

public enum SenkaCutoffKind
public enum SenkaLeaderboardRefreshKind
{
NewDay,
MidDay,
NewMonth,
}
19 changes: 12 additions & 7 deletions ElectronicObserver/Window/Control/Paging/PagingControlViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
Expand All @@ -12,7 +13,7 @@ public partial class PagingControlViewModel : ObservableObject

public List<object> Items { get; set; } = new();

public List<object> DisplayedItems { get; private set; } = new();
public ObservableCollection<object> DisplayedItems { get; private set; } = new();

public int ItemsPerPage { get; set; } = 10;

Expand Down Expand Up @@ -55,15 +56,19 @@ private void UpdateCollection()
{
CurrentPage = Math.Clamp(CurrentPage, 1, LastPage);

DisplayedItems = Items switch
DisplayedItems.Clear();

if (Items.Count > 0)
{
{ Count: > 0 } => Items
IEnumerable<object> items = Items
.Skip(ItemsPerPage * (CurrentPage - 1))
.Take(ItemsPerPage)
.ToList(),
.Take(ItemsPerPage);

_ => new(),
};
foreach (object item in items)
{
DisplayedItems.Add(item);
}
}

OnPropertyChanged(nameof(LastPage));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using ElectronicObserver.Services;
using ElectronicObserver.Utility;
using ElectronicObserver.Utility.Mathematics;
using Jot;

namespace ElectronicObserver.Window.Wpf.SenkaLeaderboard;

Expand All @@ -17,35 +18,39 @@ public partial class SenkaLeaderboardManager : ObservableObject
public SenkaLeaderboardViewModel CurrentCutoffData { get; } = new();

private TimeChangeService TimeChangeService { get; }
private Tracker Tracker { get; }

[ObservableProperty]
private partial SenkaCutoffKind CurrentSenkaCutoffKind { get; set; }
private partial SenkaLeaderboardRefreshKind CurrentSenkaLeaderboardRefreshKind { get; set; }

public SenkaLeaderboardManager(TimeChangeService timeChangeService)
public SenkaLeaderboardManager(TimeChangeService timeChangeService, Tracker tracker)
{
APIObserver.Instance.ApiReqRanking_Mxltvkpyuklh.ResponseReceived += HandleData;

TimeChangeService = timeChangeService;
TimeChangeService.HourChanged += () => CurrentSenkaCutoffKind = GetSankaLeaderboardCutoffKind();
TimeChangeService.HourChanged += () => CurrentSenkaLeaderboardRefreshKind = GetSankaLeaderboardRefreshKind();

CurrentSenkaCutoffKind = GetSankaLeaderboardCutoffKind();
Tracker = tracker;

PropertyChanged += OnSenkaLeaderboardCutoffChanged;
CurrentSenkaLeaderboardRefreshKind = GetSankaLeaderboardRefreshKind();

PropertyChanged += OnSenkaLeaderboardRefreshKindChanged;

CurrentCutoffData.Update();
StartJotTracking();
}

private void OnSenkaLeaderboardCutoffChanged(object? sender, PropertyChangedEventArgs e)
private void OnSenkaLeaderboardRefreshKindChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is not nameof(CurrentSenkaCutoffKind)) return;
if (e.PropertyName is not nameof(CurrentSenkaLeaderboardRefreshKind)) return;

CurrentCutoffData.Reset();
}

private SenkaCutoffKind GetSankaLeaderboardCutoffKind() => DateTimeHelper.GetJapanStandardTimeNow() switch
private SenkaLeaderboardRefreshKind GetSankaLeaderboardRefreshKind() => DateTimeHelper.GetJapanStandardTimeNow() switch
{
{ Hour: >= 15 or < 3 } => SenkaCutoffKind.MidDay,
_ => SenkaCutoffKind.NewDay,
{ Hour: >= 15 or < 3 } => SenkaLeaderboardRefreshKind.MidDay,
_ => SenkaLeaderboardRefreshKind.NewDay,
};

private void HandleData(string apiname, dynamic data)
Expand All @@ -72,4 +77,9 @@ private void HandleData(string apiname, dynamic data)
CurrentCutoffData.Update();
}
}

private void StartJotTracking()
{
Tracker.Track(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
x:Class="ElectronicObserver.Window.Wpf.SenkaLeaderboard.SenkaLeaderboardView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ElectronicObserver.Window.Wpf.SenkaLeaderboard"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:paging="clr-namespace:ElectronicObserver.Window.Control.Paging"
xmlns:persistentColumns="clr-namespace:ElectronicObserver.Behaviors.PersistentColumns"
d:DataContext="{d:DesignInstance Type=local:SenkaLeaderboardViewModel}"
d:DesignHeight="250"
d:DesignWidth="130"
Expand Down Expand Up @@ -34,10 +36,40 @@
<DataGrid
Grid.Row="1"
AutoGenerateColumns="False"
ColumnWidth="90"
HeadersVisibility="Column"
IsReadOnly="True"
ItemsSource="{Binding PagingViewModel.DisplayedItems}"
VerticalScrollBarVisibility="Visible"
>

<b:Interaction.Behaviors>
<persistentColumns:PersistentColumnsBehavior ColumnProperties="{Binding DataGridViewModel.ColumnProperties}" SortDescriptions="{Binding DataGridViewModel.SortDescriptions}" />
</b:Interaction.Behaviors>


<DataGrid.ColumnHeaderStyle>
<Style BasedOn="{StaticResource DefaultDataGridColumnHeaderStyle}" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Foreground" Value="{Binding DataContext.FontBrush, Source={StaticResource Proxy}}" />
<Setter Property="FontFamily" Value="{Binding DataContext.Font, Source={StaticResource Proxy}}" />
<Setter Property="FontSize" Value="{Binding DataContext.FontSize, Source={StaticResource Proxy}}" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem
Command="{Binding DataContext.DataGridViewModel.HideColumnCommand, Source={StaticResource Proxy}}"
CommandParameter="{Binding}"
Header="{Binding DataContext.DataGridViewModel.DataGrid.HideColumn, Source={StaticResource Proxy}}"
/>
<Separator />
<MenuItem Command="{Binding DataContext.DataGridViewModel.OpenColumnSelectorCommand, Source={StaticResource Proxy}}" Header="{Binding DataContext.DataGridViewModel.DataGrid.OpenColumnSelector, Source={StaticResource Proxy}}" />
<MenuItem Command="{Binding DataContext.DataGridViewModel.ClearSortingCommand, Source={StaticResource Proxy}}" Header="{Binding DataContext.DataGridViewModel.DataGrid.ClearSorting, Source={StaticResource Proxy}}" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</DataGrid.ColumnHeaderStyle>

<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Position}" Header="{Binding DataContext.Translation.Rank, Mode=OneWay, Source={StaticResource Proxy}}" />
<DataGridTextColumn Binding="{Binding AdmiralName}" Header="{Binding DataContext.Translation.Admiral, Mode=OneWay, Source={StaticResource Proxy}}" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using ElectronicObserver.Common.Datagrid;
using ElectronicObserver.Data.Bonodere;
using ElectronicObserver.KancolleApi.Types.ApiReqRanking.Models;
using ElectronicObserver.Resource;
using ElectronicObserver.Utility;
using ElectronicObserver.ViewModels;
using ElectronicObserver.Window.Control.Paging;
using ElectronicObserver.Window.Tools.EquipmentUpgradePlanner;
using Jot;

namespace ElectronicObserver.Window.Wpf.SenkaLeaderboard;

Expand All @@ -25,14 +28,16 @@ public partial class SenkaLeaderboardViewModel : AnchorableViewModel

public PagingControlViewModel PagingViewModel { get; }

public DataGridViewModel<SenkaEntryModel> DataGridViewModel { get; }

[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SubmitDataCommand))]
public partial int LoadedEntriesCount { get; set; }

public BonodereSubmissionService BonodereSubmissionService { get; }

public SenkaLeaderboardTranslationViewModel Translation { get; }

public bool IsBonodereReady => !string.IsNullOrEmpty(Configuration.Config.DataSubmission.BonodereToken);

public SenkaLeaderboardViewModel() : base(SenkaLeaderboardResources.Title, "SenkaLeaderboard", IconContent.FormResourceChart)
Expand All @@ -47,6 +52,7 @@ public SenkaLeaderboardViewModel() : base(SenkaLeaderboardResources.Title, "Senk
BonodereSubmissionService = Ioc.Default.GetRequiredService<BonodereSubmissionService>();

PagingViewModel = new();
DataGridViewModel = new(new());
Update();

Configuration.Instance.ConfigurationChanged += () => OnPropertyChanged(nameof(IsBonodereReady));
Expand Down

0 comments on commit cfb2bf8

Please sign in to comment.