From f4f7d5934d626cc9ad997497b738d97dd407583b Mon Sep 17 00:00:00 2001 From: Andruxxa7 Date: Mon, 28 Aug 2023 23:29:43 +0300 Subject: [PATCH] Fix issue with SetUserDefaultBio on start if all on pause --- TelegramMusicStatus/Program.cs | 8 ++-- TelegramMusicStatus/Services/TasksService.cs | 38 +++++++------------ .../Services/TelegramStatusService.cs | 2 +- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/TelegramMusicStatus/Program.cs b/TelegramMusicStatus/Program.cs index 91fdc04..6c2ca5d 100644 --- a/TelegramMusicStatus/Program.cs +++ b/TelegramMusicStatus/Program.cs @@ -13,7 +13,7 @@ internal static class Program private static ITelegramStatusService? _telegramService; private static ISpotifyMusicService? _spotifyService; private static IAIMPMusicService? _aimpService; - private static ITasksService _musicService; + private static ITasksService? _musicService; private static void Main() { @@ -39,7 +39,7 @@ private static async Task Run() _spotifyService = serviceProvider.GetService(); _aimpService = serviceProvider.GetService(); _musicService = serviceProvider.GetService(); - _timer = new Timer(_config.Entries.Settings?.Interval is >= 10 and <= 300 + _timer = new Timer(_config.Entries.Settings.Interval is >= 10 and <= 300 ? _config.Entries.Settings.Interval * 1000 : 30000); _timer.Elapsed += TimerElapsed; @@ -61,11 +61,13 @@ private static async void TimerElapsed(object? sender, ElapsedEventArgs? e) if (_spotifyService is not null && await _musicService.SpotifyTask()) return; if (_aimpService is not null && await _musicService.AIMPTask()) return; - if (_config?.Entries.Settings is null || !_config.Entries.Settings.IsDeployed) { await PausePrompt(); } + + if (_config?.Entries.Settings is not null && _config.Entries.Settings.IsDefaultBioOnPause) + await _telegramService.SetUserDefaultBio(); } private static Task PausePrompt() diff --git a/TelegramMusicStatus/Services/TasksService.cs b/TelegramMusicStatus/Services/TasksService.cs index 7dccf95..8ce606a 100644 --- a/TelegramMusicStatus/Services/TasksService.cs +++ b/TelegramMusicStatus/Services/TasksService.cs @@ -1,6 +1,4 @@ -using TelegramMusicStatus.Config; - -namespace TelegramMusicStatus.Services; +namespace TelegramMusicStatus.Services; public interface ITasksService { @@ -11,23 +9,21 @@ public interface ITasksService public class TasksService : ITasksService { private ITelegramStatusService _telegramService; - private IConfig _config; private ISpotifyMusicService _spotifyService; private IAIMPMusicService _aimpMusicService; - public TasksService(ITelegramStatusService telegramService, IConfig config, - IAIMPMusicService aimpMusicService = null, ISpotifyMusicService spotifyService = null) + public TasksService(ITelegramStatusService telegramService, IAIMPMusicService aimpMusicService = null, + ISpotifyMusicService spotifyService = null) { this._telegramService = telegramService; this._spotifyService = spotifyService; - this._config = config; this._aimpMusicService = aimpMusicService; } public async Task SpotifyTask() { - if (_spotifyService is null) return false; - var status = await _spotifyService.GetCurrentlyPlayingStatus(); + if (this._spotifyService is null) return false; + var status = await this._spotifyService.GetCurrentlyPlayingStatus(); if (status.Bio is null) { Utils.WriteLine("Spotify web player paused."); @@ -37,14 +33,10 @@ public async Task SpotifyTask() Utils.WriteLine( $"(Spotify) Current state is {(status.IsPlaying ? "playing" : "paused")}, now playing: {status.Bio}"); - switch (status.IsPlaying) + if (status.IsPlaying) { - case true: - await _telegramService.ChangeUserBio(Utils.FormatTrackInfo(status.Bio)); - return true; - case false when _config.Entries.Settings is { IsDefaultBioOnPause: true }: - await _telegramService.SetUserDefaultBio(); - break; + await this._telegramService.ChangeUserBio(Utils.FormatTrackInfo(status.Bio)); + return true; } return false; @@ -52,8 +44,8 @@ public async Task SpotifyTask() public async Task AIMPTask() { - if (_aimpMusicService is null) return false; - var status = await _aimpMusicService.GetCurrentlyPlayingStatus(); + if (this._aimpMusicService is null) return false; + var status = await this._aimpMusicService.GetCurrentlyPlayingStatus(); if (status.Bio is null) { Utils.WriteLine("AIMP player paused."); @@ -63,14 +55,10 @@ public async Task AIMPTask() Utils.WriteLine( $"(AIMP) Current state is {(status.IsPlaying ? "playing" : "paused")}, now playing: {status.Bio}"); - switch (status.IsPlaying) + if (status.IsPlaying) { - case true: - await _telegramService.ChangeUserBio(Utils.FormatTrackInfo(status.Bio)); - return true; - case false when _config.Entries.Settings is { IsDefaultBioOnPause: true }: - await _telegramService.SetUserDefaultBio(); - break; + await this._telegramService.ChangeUserBio(Utils.FormatTrackInfo(status.Bio)); + return true; } return false; diff --git a/TelegramMusicStatus/Services/TelegramStatusService.cs b/TelegramMusicStatus/Services/TelegramStatusService.cs index da9a696..bd5a428 100644 --- a/TelegramMusicStatus/Services/TelegramStatusService.cs +++ b/TelegramMusicStatus/Services/TelegramStatusService.cs @@ -43,7 +43,7 @@ public async Task SaveCurrentBioToConfig() this._currentBio = status; if (Utils.IsValidTrackInfoFormat(status) || this._config.Entries.UserBio == status) return; this._userDefaultBio = status; - Config.SaveConfig(this._config.Entries with { UserBio = this._userDefaultBio }); + await Config.SaveConfig(this._config.Entries with { UserBio = this._userDefaultBio }); } private async Task Init()