diff --git a/MusicX/Models/ConfigModel.cs b/MusicX/Models/ConfigModel.cs index ed3b180d..5b83a412 100644 --- a/MusicX/Models/ConfigModel.cs +++ b/MusicX/Models/ConfigModel.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using MusicX.Services.Player; namespace MusicX.Models { @@ -53,5 +54,9 @@ public class ConfigModel public string? DeviceId { get; set; } public string? ExchangeToken { get; set; } + + public bool? SavePlayerState { get; set; } + + public PlayerState? LastPlayerState { get; set; } } } diff --git a/MusicX/RootWindow.xaml.cs b/MusicX/RootWindow.xaml.cs index 224aeed8..c5f9ca6f 100644 --- a/MusicX/RootWindow.xaml.cs +++ b/MusicX/RootWindow.xaml.cs @@ -64,8 +64,6 @@ public RootWindow(NavigationService navigationService, VkService vkService, Logg playerSerivce.TrackChangedEvent += PlayerSerivce_TrackChangedEvent; - Closing += RootWindow_Closing; - SingleAppService.Instance.RunWitchArgs += Instance_RunWitchArgs; togetherService.ConnectedToSession += TogetherServiceOnConnectedToSession; @@ -125,29 +123,6 @@ await Task.Factory.StartNew(async() => } - private async void RootWindow_Closing(object? sender, CancelEventArgs e) - { - try - { - var listenTogetherService = StaticService.Container.GetRequiredService(); - - if (listenTogetherService.IsConnectedToServer && listenTogetherService.PlayerMode != PlayerMode.None) - { - if (listenTogetherService.PlayerMode == PlayerMode.Owner) - { - await listenTogetherService.StopPlaySessionAsync(); - } - else - { - await listenTogetherService.LeavePlaySessionAsync(); - } - } - }catch(Exception ex) - { - //nothing - } - - } private void PlayerSerivce_TrackChangedEvent(object? sender, EventArgs e) { if (PlayerShowed) return; @@ -316,6 +291,13 @@ private async void Window_Loaded(object sender, RoutedEventArgs e) if (config.NotifyMessages is null) config.NotifyMessages = new() { ShowListenTogetherModal = true, LastShowedTelegramBlock = null }; + if (config.LastPlayerState is not null) + { + await playerControl.PlayerService.RestoreFromStateAsync(config.LastPlayerState); + + config.LastPlayerState = null; + } + await configService.SetConfig(config); if(config.NotifyMessages.ShowListenTogetherModal) @@ -575,11 +557,35 @@ private void NotifyIcon_LeftClick(NotifyIcon sender, RoutedEventArgs e) }*/ - private void RootWindow_OnClosing(object? sender, CancelEventArgs e) + private async void RootWindow_OnClosing(object? sender, CancelEventArgs e) { configService.Config.Width = Width; configService.Config.Height = Height; - configService.SetConfig(configService.Config).SafeFireAndForget(continueOnCapturedContext: true); + + if (configService.Config.SavePlayerState is true) + configService.Config.LastPlayerState = PlayerState.CreateOrNull(playerControl.PlayerService); + + await configService.SetConfig(configService.Config); + + try + { + var listenTogetherService = StaticService.Container.GetRequiredService(); + + if (listenTogetherService.IsConnectedToServer && listenTogetherService.PlayerMode != PlayerMode.None) + { + if (listenTogetherService.PlayerMode == PlayerMode.Owner) + { + await listenTogetherService.StopPlaySessionAsync(); + } + else + { + await listenTogetherService.LeavePlaySessionAsync(); + } + } + }catch(Exception ex) + { + //nothing + } } private void RootFrame_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e) diff --git a/MusicX/Services/Player/PlayerService.cs b/MusicX/Services/Player/PlayerService.cs index ce52de0a..b2450410 100644 --- a/MusicX/Services/Player/PlayerService.cs +++ b/MusicX/Services/Player/PlayerService.cs @@ -89,8 +89,7 @@ public PlayerService(Logger logger, ISnackbarService snackbarService, public async Task RestoreFromStateAsync(PlayerState state) { - await PlayAsync(state.Playlist, state.Track); - Seek(state.Position); + await PlayAsync(state.Playlist, state.Track, state.Position); } public async void Play() @@ -118,12 +117,13 @@ await Task.WhenAll( _statsListeners.Select(b => b.TrackChangedAsync(previousTrack, CurrentTrack!, ChangeReason.TrackChange))); } - private async Task PlayTrackAsync(PlaylistTrack track) + private async Task PlayTrackAsync(PlaylistTrack track, TimeSpan? position = null) { try { if (CurrentTrack == track) { + if (position is not null) Seek(position.Value); player.Play(); return; } @@ -172,6 +172,8 @@ private async Task PlayTrackAsync(PlaylistTrack track) await NextTrack(); return; } + + if (position is not null) Seek(position.Value); player.Play(); UpdateWindowsData().SafeFireAndForget(); @@ -195,12 +197,12 @@ private async Task PlayTrackAsync(PlaylistTrack track) } - public async Task PlayAsync(IPlaylist playlist, PlaylistTrack? firstTrack = null) + public async Task PlayAsync(IPlaylist playlist, PlaylistTrack? firstTrack = null, TimeSpan? startPosition = null) { if(_listenTogetherService.PlayerMode == PlayerMode.Listener) { await _listenTogetherService.LeavePlaySessionAsync(); - await Application.Current.Dispatcher.InvokeAsync(() => PlayAsync(playlist).SafeFireAndForget()); + await Application.Current.Dispatcher.InvokeAsync(() => PlayAsync(playlist, firstTrack, startPosition).SafeFireAndForget()); return; } @@ -237,7 +239,7 @@ await Application.Current.Dispatcher.InvokeAsync(() => }); } - firstTrackTask = PlayTrackAsync(firstTrack); + firstTrackTask = PlayTrackAsync(firstTrack, startPosition); await Task.WhenAll( _statsListeners.Select(b => b.TrackChangedAsync(CurrentTrack, firstTrack, ChangeReason.PlaylistChange))); @@ -260,7 +262,7 @@ await Task.WhenAll( if (firstTrack is null) { var previousTrack = CurrentTrack; - await PlayTrackAsync(Tracks[0]); + await PlayTrackAsync(Tracks[0], startPosition); await Task.WhenAll( _statsListeners.Select(b => b.TrackChangedAsync(previousTrack, CurrentTrack!, ChangeReason.PlaylistChange))); diff --git a/MusicX/ViewModels/Modals/AvailableNewUpdateModalViewModel.cs b/MusicX/ViewModels/Modals/AvailableNewUpdateModalViewModel.cs index 3740d49c..e695f595 100644 --- a/MusicX/ViewModels/Modals/AvailableNewUpdateModalViewModel.cs +++ b/MusicX/ViewModels/Modals/AvailableNewUpdateModalViewModel.cs @@ -48,12 +48,11 @@ private async Task Execute() await _updateManager.DownloadUpdatesAsync(UpdateInfo, ProgressHandler); var playerState = PlayerState.CreateOrNull(StaticService.Container.GetRequiredService()); + var configService = StaticService.Container.GetRequiredService(); - _updateManager.WaitExitThenApplyUpdates(UpdateInfo, restartArgs: new [] - { - "--play", - playerState is null ? "null" : JsonSerializer.Serialize(playerState) - }); + configService.Config.LastPlayerState = playerState; + + _updateManager.WaitExitThenApplyUpdates(UpdateInfo); Application.Current.Shutdown(); } diff --git a/MusicX/Views/SettingsView.xaml b/MusicX/Views/SettingsView.xaml index 29115330..bea7c027 100644 --- a/MusicX/Views/SettingsView.xaml +++ b/MusicX/Views/SettingsView.xaml @@ -261,6 +261,19 @@ FontSize="18" Unchecked="MinimizeToTray_Unchecked" /> + + + + + diff --git a/MusicX/Views/SettingsView.xaml.cs b/MusicX/Views/SettingsView.xaml.cs index f6fd6574..39ebc129 100644 --- a/MusicX/Views/SettingsView.xaml.cs +++ b/MusicX/Views/SettingsView.xaml.cs @@ -96,6 +96,7 @@ private async void SettingsView_Loaded(object sender, RoutedEventArgs e) WinterTheme.IsChecked = config.WinterTheme.Value; MinimizeToTray.IsChecked = config.MinimizeToTray.Value; GetBetaUpdates.IsChecked = config.GetBetaUpdates.Value; + SavePlayerState.IsChecked = config.SavePlayerState.GetValueOrDefault(); UserName.Text = config.UserName; @@ -586,5 +587,12 @@ private void CatalogsCard_Click(object sender, RoutedEventArgs e) { StaticService.Container.GetRequiredService().OpenSection("profiles"); } + + private async void SavePlayerState_OnCheckChanged(object sender, RoutedEventArgs e) + { + config.SavePlayerState = SavePlayerState.IsChecked; + + await configService.SetConfig(config); + } } } diff --git a/MusicX/Views/StartingWindow.xaml.cs b/MusicX/Views/StartingWindow.xaml.cs index 1e022d8e..a0c5deaa 100644 --- a/MusicX/Views/StartingWindow.xaml.cs +++ b/MusicX/Views/StartingWindow.xaml.cs @@ -186,13 +186,6 @@ await container.GetRequiredService() { await rootWindow.StartListenTogether(arg[1]); } - - var playArgIndex = Array.BinarySearch(_args, "--play", - StringComparer.OrdinalIgnoreCase); - if (playArgIndex >= 0 && playArgIndex + 1 < _args.Length && - JsonSerializer.Deserialize(string.Join(string.Empty, _args[(playArgIndex + 1)..])) is { } state) - await container.GetRequiredService() - .RestoreFromStateAsync(state); } this.Close();