mirror of
https://github.com/denglihong2007/CRSim
synced 2026-08-12 04:35:39 +08:00
decouple: 项目解耦合
This commit is contained in:
128
CRSim.ScreenSimulator/ViewModels/Windows/ScreenViewModel.cs
Normal file
128
CRSim.ScreenSimulator/ViewModels/Windows/ScreenViewModel.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CRSim.Core.Services;
|
||||
using CRSim.Core.Models;
|
||||
using CRSim.ScreenSimulator.Models;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace CRSim.ScreenSimulator.ViewModels
|
||||
{
|
||||
public partial class ScreenViewModel : ObservableObject
|
||||
{
|
||||
private readonly ITimeService _timeService;
|
||||
public readonly Settings _settings;
|
||||
private readonly TaskCompletionSource<bool> _dataLoaded = new();
|
||||
[ObservableProperty]
|
||||
private DateTime _currentTime = new();
|
||||
[ObservableProperty]
|
||||
private string _text = "";
|
||||
[ObservableProperty]
|
||||
public int _location;
|
||||
[ObservableProperty]
|
||||
private Station _thisStation;
|
||||
[ObservableProperty]
|
||||
private string _thisPlatform;
|
||||
[ObservableProperty]
|
||||
private string _thisTicketCheck;
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
public List<TrainInfo> TrainInfo { get; set; } = [];
|
||||
|
||||
public StationType StationType = StationType.Both;
|
||||
|
||||
protected ScreenViewModel(ITimeService timeService, ISettingsService settingsService)
|
||||
{
|
||||
|
||||
_timeService = timeService;
|
||||
_settings = settingsService.GetSettings();
|
||||
|
||||
_timeService.OneSecondElapsed += OnTimeElapsed;
|
||||
_timeService.RefreshSecondsElapsed += RefreshData;
|
||||
}
|
||||
public int CurrentPageIndex = 0;
|
||||
public int ItemsPerPage;
|
||||
public int PageCount;
|
||||
|
||||
public virtual void RefreshData(object? sender, EventArgs e)
|
||||
{
|
||||
if (CurrentPageIndex==0)
|
||||
{
|
||||
List<TrainInfo> itemsToRemove = [];
|
||||
foreach(TrainInfo trainInfo in TrainInfo)
|
||||
{
|
||||
if (trainInfo.DepartureTime == null)
|
||||
{
|
||||
if (trainInfo.ArrivalTime.Value.Add(_settings.StopDisplayFromArrivalDuration) < _timeService.GetDateTimeNow())
|
||||
{
|
||||
itemsToRemove.Add(trainInfo);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (trainInfo.DepartureTime.Value.Subtract(_settings.StopDisplayUntilDepartureDuration) < _timeService.GetDateTimeNow())
|
||||
{
|
||||
itemsToRemove.Add(trainInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (var item in itemsToRemove)
|
||||
{
|
||||
TrainInfo.Remove(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTimeElapsed(object? sender, EventArgs e)
|
||||
{
|
||||
CurrentTime = _timeService.GetDateTimeNow();
|
||||
}
|
||||
|
||||
public Task WaitForDataLoadAsync() => _dataLoaded.Task;
|
||||
|
||||
public void LoadData(Station station, string ticketCheck, string platform)
|
||||
{
|
||||
ThisStation = station;
|
||||
ThisPlatform = platform;
|
||||
ThisTicketCheck = ticketCheck;
|
||||
var trains = station.TrainStops;
|
||||
foreach (var trainNumber in trains)
|
||||
{
|
||||
|
||||
if (trainNumber != null &&
|
||||
(StationType == StationType.Both ||
|
||||
trainNumber.StationType == StationType.Both ||
|
||||
StationType == trainNumber.StationType) &&
|
||||
(ticketCheck == string.Empty || trainNumber.TicketChecks.Contains(ticketCheck)) &&
|
||||
(platform == string.Empty || trainNumber.Platform == platform))
|
||||
{
|
||||
var now = _timeService.GetDateTimeNow();
|
||||
var today = now.Date;
|
||||
|
||||
DateTime? AdjustTime(TimeSpan? time) =>
|
||||
time.HasValue ? (today.Add(time.Value) > now ? today.Add(time.Value) : today.Add(time.Value).AddDays(1)) : null;
|
||||
|
||||
TrainInfo.Add(new TrainInfo
|
||||
{
|
||||
TrainNumber = trainNumber.Number,
|
||||
Terminal = trainNumber.Terminal,
|
||||
Origin = trainNumber.Origin,
|
||||
ArrivalTime = AdjustTime(trainNumber.ArrivalTime),
|
||||
DepartureTime = AdjustTime(trainNumber.DepartureTime),
|
||||
TicketChecks = trainNumber.TicketChecks,
|
||||
WaitingArea = trainNumber.StationType == StationType.Arrival
|
||||
? string.Empty
|
||||
: station.WaitingAreas.FirstOrDefault(x => x.TicketChecks.Intersect(trainNumber.TicketChecks).Any())?.Name ?? string.Empty,
|
||||
Platform = trainNumber.Platform,
|
||||
Length = trainNumber.Length,
|
||||
Landmark = trainNumber.Landmark,
|
||||
State = TimeSpan.Zero
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
TrainInfo = [.. TrainInfo.OrderBy(x => x.DepartureTime??x.ArrivalTime)];
|
||||
RefreshData(null, null);
|
||||
_dataLoaded.SetResult(true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user