Skip to content
Draft
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
23 changes: 20 additions & 3 deletions Screenbox.Core/Common/ServiceHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
using Microsoft.Extensions.DependencyInjection;
using Screenbox.Core.Contexts;
using Screenbox.Core.Factories;
using Screenbox.Core.Helpers;
using Screenbox.Core.Services;
using Screenbox.Core.ViewModels;

namespace Screenbox.Core;

public static class ServiceHelpers
{
public static void PopulateCoreServices(ServiceCollection services)
{
// Contexts
services.AddSingleton<NavigationContext>();
services.AddSingleton<VolumeContext>();
services.AddSingleton<MediaListContext>();
services.AddSingleton<MediaViewModelFactoryContext>();
services.AddSingleton<AlbumFactoryContext>();
services.AddSingleton<ArtistFactoryContext>();
services.AddSingleton<LibVlcContext>();
services.AddSingleton<TransportControlsContext>();
services.AddSingleton<NotificationContext>();
services.AddSingleton<CastContext>();
services.AddSingleton<WindowContext>();
services.AddSingleton<LibraryContext>();
services.AddSingleton<LastPositionContext>();

// View models
services.AddTransient<PlayerElementViewModel>();
services.AddTransient<PropertyViewModel>();
Expand Down Expand Up @@ -38,9 +55,9 @@ public static void PopulateCoreServices(ServiceCollection services)
services.AddTransient<LivelyWallpaperPlayerViewModel>();
services.AddTransient<LivelyWallpaperSelectorViewModel>();
services.AddTransient<HomePageViewModel>();
services.AddSingleton<CommonViewModel>(); // Shared between many pages
services.AddSingleton<VolumeViewModel>(); // Avoid thread lock
services.AddSingleton<MediaListViewModel>(); // Global playlist
services.AddTransient<CommonViewModel>();
services.AddTransient<VolumeViewModel>();
services.AddTransient<MediaListViewModel>();

// Misc
services.AddTransient<LastPositionTracker>();
Expand Down
12 changes: 12 additions & 0 deletions Screenbox.Core/Contexts/AlbumFactoryContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#nullable enable

using System.Collections.Generic;
using Screenbox.Core.ViewModels;

namespace Screenbox.Core.Contexts;

public sealed class AlbumFactoryContext
{
public Dictionary<string, AlbumViewModel> Albums { get; } = new();
public AlbumViewModel? UnknownAlbum { get; set; }
}
12 changes: 12 additions & 0 deletions Screenbox.Core/Contexts/ArtistFactoryContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#nullable enable

using System.Collections.Generic;
using Screenbox.Core.ViewModels;

namespace Screenbox.Core.Contexts;

public sealed class ArtistFactoryContext
{
public Dictionary<string, ArtistViewModel> Artists { get; } = new();
public ArtistViewModel? UnknownArtist { get; set; }
}
13 changes: 13 additions & 0 deletions Screenbox.Core/Contexts/CastContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#nullable enable

using System.Collections.Generic;
using LibVLCSharp.Shared;
using Screenbox.Core.Models;

namespace Screenbox.Core.Contexts;

public sealed class CastContext
{
public List<Renderer> Renderers { get; } = new();
public RendererDiscoverer? Discoverer { get; set; }
}
15 changes: 15 additions & 0 deletions Screenbox.Core/Contexts/LastPositionContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#nullable enable

using System;
using System.Collections.Generic;
using Screenbox.Core.Models;

namespace Screenbox.Core.Contexts;

public sealed class LastPositionContext
{
public DateTimeOffset LastUpdated { get; set; }
public List<MediaLastPosition> LastPositions { get; set; } = new(65);
public MediaLastPosition? UpdateCache { get; set; }
public string? RemoveCache { get; set; }
}
13 changes: 13 additions & 0 deletions Screenbox.Core/Contexts/LibVlcContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#nullable enable

using LibVLCSharp.Shared;
using Screenbox.Core.Playback;

namespace Screenbox.Core.Contexts;

public sealed class LibVlcContext
{
public VlcMediaPlayer? MediaPlayer { get; set; }
public LibVLC? LibVlc { get; set; }
public bool UseFutureAccessList { get; set; } = true;
}
31 changes: 31 additions & 0 deletions Screenbox.Core/Contexts/LibraryContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#nullable enable

using System.Collections.Generic;
using System.Threading;
using Screenbox.Core.ViewModels;
using Windows.Devices.Enumeration;
using Windows.Storage;
using Windows.Storage.Search;
using Windows.System;

namespace Screenbox.Core.Contexts;

public sealed class LibraryContext
{
public StorageLibrary? MusicLibrary { get; set; }
public StorageLibrary? VideosLibrary { get; set; }
public bool IsLoadingVideos { get; set; }
public bool IsLoadingMusic { get; set; }
public StorageFileQueryResult? MusicLibraryQueryResult { get; set; }
public StorageFileQueryResult? VideosLibraryQueryResult { get; set; }
public List<MediaViewModel> Songs { get; set; } = new();
public List<MediaViewModel> Videos { get; set; } = new();
public CancellationTokenSource? MusicFetchCancellation { get; set; }
public CancellationTokenSource? VideosFetchCancellation { get; set; }
public bool MusicChangeTrackerAvailable { get; set; }
public bool VideosChangeTrackerAvailable { get; set; }
public DispatcherQueueTimer? MusicRefreshTimer { get; set; }
public DispatcherQueueTimer? VideosRefreshTimer { get; set; }
public DispatcherQueueTimer? StorageDeviceRefreshTimer { get; set; }
public DeviceWatcher? PortableStorageDeviceWatcher { get; set; }
}
26 changes: 26 additions & 0 deletions Screenbox.Core/Contexts/MediaListContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#nullable enable

using System.Collections.Generic;
using System.Threading;
using Screenbox.Core.Models;
using Screenbox.Core.Playback;
using Screenbox.Core.ViewModels;
using Windows.Media;
using Windows.Storage.Search;

namespace Screenbox.Core.Contexts;

public sealed class MediaListContext
{
public Playlist Playlist { get; set; } = new();
public List<MediaViewModel> MediaBuffer { get; set; } = new();
public IMediaPlayer? MediaPlayer { get; set; }
public object? DelayPlay { get; set; }
public bool DeferCollectionChanged { get; set; }
public StorageFileQueryResult? NeighboringFilesQuery { get; set; }
public CancellationTokenSource? PlayFilesCancellation { get; set; }
public MediaPlaybackAutoRepeatMode RepeatMode { get; set; }
public bool ShuffleMode { get; set; }
public MediaViewModel? CurrentItem { get; set; }
public int CurrentIndex { get; set; } = -1;
}
13 changes: 13 additions & 0 deletions Screenbox.Core/Contexts/MediaViewModelFactoryContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#nullable enable

using System;
using System.Collections.Generic;
using Screenbox.Core.ViewModels;

namespace Screenbox.Core.Contexts;

public sealed class MediaViewModelFactoryContext
{
public Dictionary<string, WeakReference<MediaViewModel>> References { get; } = new();
public int ReferencesCleanUpThreshold { get; set; } = 1000;
}
18 changes: 18 additions & 0 deletions Screenbox.Core/Contexts/NavigationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#nullable enable

using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Screenbox.Core.Contexts;

public sealed class NavigationContext
{
public Dictionary<Type, string> NavigationStates { get; } = new();
public Dictionary<string, object> PageStates { get; } = new();
public NavigationViewDisplayMode NavigationViewDisplayMode { get; set; }
public Thickness ScrollBarMargin { get; set; }
public Thickness FooterBottomPaddingMargin { get; set; }
public double FooterBottomPaddingHeight { get; set; }
}
8 changes: 8 additions & 0 deletions Screenbox.Core/Contexts/NotificationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#nullable enable

namespace Screenbox.Core.Contexts;

public sealed class NotificationContext
{
public string? ProgressTitle { get; set; }
}
10 changes: 10 additions & 0 deletions Screenbox.Core/Contexts/TransportControlsContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#nullable enable

using System;

namespace Screenbox.Core.Contexts;

public sealed class TransportControlsContext
{
public DateTime LastUpdated { get; set; } = DateTime.MinValue;
}
14 changes: 14 additions & 0 deletions Screenbox.Core/Contexts/VolumeContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#nullable enable

using Screenbox.Core.Playback;

namespace Screenbox.Core.Contexts;

public sealed class VolumeContext
{
public int MaxVolume { get; set; }
public int Volume { get; set; }
public bool IsMute { get; set; }
public IMediaPlayer? MediaPlayer { get; set; }
public bool IsInitialized { get; set; }
}
12 changes: 12 additions & 0 deletions Screenbox.Core/Contexts/WindowContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#nullable enable

using Screenbox.Core.Enums;
using Windows.UI.Core;

namespace Screenbox.Core.Contexts;

public sealed class WindowContext
{
public CoreCursor? Cursor { get; set; }
public WindowViewMode ViewMode { get; set; }
}
31 changes: 15 additions & 16 deletions Screenbox.Core/Factories/AlbumViewModelFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable
#nullable enable

using Screenbox.Core.Contexts;
using Screenbox.Core.Enums;
using Screenbox.Core.Services;
using Screenbox.Core.ViewModels;
Expand All @@ -12,19 +13,17 @@ namespace Screenbox.Core.Factories
{
public sealed class AlbumViewModelFactory
{
public AlbumViewModel UnknownAlbum { get; }
public AlbumViewModel UnknownAlbum => State.UnknownAlbum!;

public IReadOnlyCollection<AlbumViewModel> AllAlbums { get; }

private readonly Dictionary<string, AlbumViewModel> _allAlbums;
public IReadOnlyCollection<AlbumViewModel> AllAlbums => State.Albums.Values;
private readonly IResourceService _resourceService;
private readonly AlbumFactoryContext State;

public AlbumViewModelFactory(IResourceService resourceService)
public AlbumViewModelFactory(IResourceService resourceService, AlbumFactoryContext state)
{
_resourceService = resourceService;
UnknownAlbum = new AlbumViewModel(resourceService.GetString(ResourceName.UnknownAlbum), resourceService.GetString(ResourceName.UnknownArtist));
_allAlbums = new Dictionary<string, AlbumViewModel>();
AllAlbums = _allAlbums.Values;
State = state;
State.UnknownAlbum ??= new AlbumViewModel(resourceService.GetString(ResourceName.UnknownAlbum), resourceService.GetString(ResourceName.UnknownArtist));
}

public AlbumViewModel GetAlbumFromName(string albumName, string artistName)
Expand All @@ -37,7 +36,7 @@ public AlbumViewModel GetAlbumFromName(string albumName, string artistName)
string albumKey = albumName.Trim().ToLower(CultureInfo.CurrentUICulture);
string artistKey = artistName.Trim().ToLower(CultureInfo.CurrentUICulture);
string key = GetAlbumKey(albumKey, artistKey);
return _allAlbums.GetValueOrDefault(key, UnknownAlbum);
return State.Albums.GetValueOrDefault(key, UnknownAlbum);
}

public AlbumViewModel AddSongToAlbum(MediaViewModel song, string albumName, string artistName, uint year)
Expand Down Expand Up @@ -68,7 +67,7 @@ public AlbumViewModel AddSongToAlbum(MediaViewModel song, string albumName, stri

album.RelatedSongs.Add(song);
UpdateAlbumDateAdded(album, song);
return _allAlbums[key] = album;
return State.Albums[key] = album;
}

public void Remove(MediaViewModel song)
Expand All @@ -81,18 +80,18 @@ public void Remove(MediaViewModel song)
{
string albumKey = album.Name.Trim().ToLower(CultureInfo.CurrentUICulture);
string artistKey = album.ArtistName.Trim().ToLower(CultureInfo.CurrentUICulture);
_allAlbums.Remove(GetAlbumKey(albumKey, artistKey));
State.Albums.Remove(GetAlbumKey(albumKey, artistKey));
}
}

public void Compact()
{
List<string> albumKeysToRemove =
_allAlbums.Where(p => p.Value.RelatedSongs.Count == 0).Select(p => p.Key).ToList();
State.Albums.Where(p => p.Value.RelatedSongs.Count == 0).Select(p => p.Key).ToList();

foreach (string albumKey in albumKeysToRemove)
{
_allAlbums.Remove(albumKey);
State.Albums.Remove(albumKey);
}
}

Expand All @@ -106,15 +105,15 @@ public void Clear()
UnknownAlbum.RelatedSongs.Clear();
UnknownAlbum.DateAdded = default;

foreach ((string _, AlbumViewModel album) in _allAlbums)
foreach ((string _, AlbumViewModel album) in State.Albums)
{
foreach (MediaViewModel media in album.RelatedSongs)
{
media.Album = null;
}
}

_allAlbums.Clear();
State.Albums.Clear();
}

private static void UpdateAlbumDateAdded(AlbumViewModel album, MediaViewModel song)
Expand Down
Loading