Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Screenbox.Core/Common/ServiceHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Screenbox.Core.Contexts;
using Screenbox.Core.Controllers;
using Screenbox.Core.Factories;
using Screenbox.Core.Helpers;
using Screenbox.Core.Services;
using Screenbox.Core.ViewModels;

Expand Down Expand Up @@ -45,9 +44,6 @@ public static void PopulateCoreServices(ServiceCollection services)
services.AddSingleton<VolumeViewModel>(); // Avoid thread lock
services.AddSingleton<MediaListViewModel>(); // Global playlist

// Misc
services.AddTransient<LastPositionTracker>();

// Factories
services.AddSingleton<MediaViewModelFactory>();
services.AddSingleton<StorageItemViewModelFactory>();
Expand All @@ -62,6 +58,7 @@ public static void PopulateCoreServices(ServiceCollection services)

// Controllers
services.AddSingleton<LibraryController>();
services.AddSingleton<LastPositionTracker>();

// Services
services.AddSingleton<IPlayerService, PlayerService>();
Expand Down
136 changes: 136 additions & 0 deletions Screenbox.Core/Controllers/LastPositionTracker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using Screenbox.Core.Messages;
using Screenbox.Core.Models;
using Screenbox.Core.Services;
using Windows.Storage;

namespace Screenbox.Core.Controllers;

public sealed class LastPositionTracker : ObservableRecipient,
IRecipient<SuspendingMessage>
{
private const int Capacity = 64;
private const string SaveFileName = "last_positions.bin";

public bool IsLoaded => LastUpdated != default;

public DateTimeOffset LastUpdated { get; private set; }

private readonly IFilesService _filesService;
private List<MediaLastPosition> _lastPositions = new(Capacity + 1);
private MediaLastPosition? _updateCache;
private string? _removeCache;

public LastPositionTracker(IFilesService filesService)
{
_filesService = filesService;

IsActive = true;
}

public void Receive(SuspendingMessage message)
{
message.Reply(SaveToDiskAsync());
}

public void UpdateLastPosition(string location, TimeSpan position)
{
LastUpdated = DateTimeOffset.Now;
_removeCache = null;
MediaLastPosition? item = _updateCache;
if (item?.Location == location)
{
item.Position = position;
if (_lastPositions.FirstOrDefault() != item)
{
int index = _lastPositions.IndexOf(item);
if (index >= 0)
{
_lastPositions.RemoveAt(index);
}

_lastPositions.Insert(0, item);
}
}
else
{
item = _lastPositions.Find(x => x.Location == location);
if (item == null)
{
item = new MediaLastPosition(location, position);
_lastPositions.Insert(0, item);
if (_lastPositions.Count > Capacity)
{
_lastPositions.RemoveAt(Capacity);
}
}
else
{
item.Position = position;
}
}

_updateCache = item;
}

public TimeSpan GetPosition(string location)
{
return _lastPositions.Find(x => x.Location == location)?.Position ?? TimeSpan.Zero;
}

public void RemovePosition(string location)
{
LastUpdated = DateTimeOffset.Now;
if (_removeCache == location) return;
_lastPositions.RemoveAll(x => x.Location == location);
_removeCache = location;
}

public void ClearAll()
{
LastUpdated = DateTimeOffset.Now;
_lastPositions.Clear();
_updateCache = null;
_removeCache = null;
}

public async Task SaveToDiskAsync()
{
try
{
await _filesService.SaveToDiskAsync(ApplicationData.Current.TemporaryFolder, SaveFileName, _lastPositions);
}
catch (FileLoadException)
{
// File in use. Skipped
}
}

public async Task LoadFromDiskAsync()
{
try
{
List<MediaLastPosition> lastPositions =
await _filesService.LoadFromDiskAsync<List<MediaLastPosition>>(ApplicationData.Current.TemporaryFolder, SaveFileName);
lastPositions.Capacity = Capacity;
_lastPositions = lastPositions;
LastUpdated = DateTimeOffset.UtcNow;
}
catch (FileNotFoundException)
{
// pass
}
catch (Exception)
{
// pass
}
}
}
129 changes: 0 additions & 129 deletions Screenbox.Core/Helpers/LastPositionTracker.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Screenbox.Core/Screenbox.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<Compile Include="Helpers\EnumExtensions.cs" />
<Compile Include="Helpers\FilesHelpers.cs" />
<Compile Include="Helpers\LanguageHelper.cs" />
<Compile Include="Helpers\LastPositionTracker.cs" />
<Compile Include="Controllers\LastPositionTracker.cs" />
<Compile Include="Helpers\ListGrouping.cs" />
<Compile Include="Helpers\LivelyWallpaperUtil.cs" />
<Compile Include="Helpers\MediaGroupingHelpers.cs" />
Expand Down
Loading