Skip to content

Commit

Permalink
Fix issue with SetUserDefaultBio on start if all on pause
Browse files Browse the repository at this point in the history
  • Loading branch information
wh0o7 committed Aug 28, 2023
1 parent e3c1d53 commit f4f7d59
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
8 changes: 5 additions & 3 deletions TelegramMusicStatus/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -39,7 +39,7 @@ private static async Task Run()
_spotifyService = serviceProvider.GetService<ISpotifyMusicService>();
_aimpService = serviceProvider.GetService<IAIMPMusicService>();
_musicService = serviceProvider.GetService<ITasksService>();
_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;
Expand All @@ -61,11 +61,13 @@ private static async void TimerElapsed(object? sender, ElapsedEventArgs? e)
if (_spotifyService is not null && await _musicService.SpotifyTask()) return;

Check warning on line 61 in TelegramMusicStatus/Program.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

Dereference of a possibly null reference.

Check warning on line 61 in TelegramMusicStatus/Program.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

Dereference of a possibly null reference.
if (_aimpService is not null && await _musicService.AIMPTask()) return;

Check warning on line 62 in TelegramMusicStatus/Program.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

Dereference of a possibly null reference.

Check warning on line 62 in TelegramMusicStatus/Program.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

Dereference of a possibly null reference.


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();

Check warning on line 70 in TelegramMusicStatus/Program.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

Dereference of a possibly null reference.

Check warning on line 70 in TelegramMusicStatus/Program.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

Dereference of a possibly null reference.
}

private static Task PausePrompt()
Expand Down
38 changes: 13 additions & 25 deletions TelegramMusicStatus/Services/TasksService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using TelegramMusicStatus.Config;

namespace TelegramMusicStatus.Services;
namespace TelegramMusicStatus.Services;

public interface ITasksService
{
Expand All @@ -11,23 +9,21 @@ public interface ITasksService
public class TasksService : ITasksService
{
private ITelegramStatusService _telegramService;
private IConfig<MainConfig> _config;
private ISpotifyMusicService _spotifyService;
private IAIMPMusicService _aimpMusicService;

public TasksService(ITelegramStatusService telegramService, IConfig<MainConfig> config,
IAIMPMusicService aimpMusicService = null, ISpotifyMusicService spotifyService = null)
public TasksService(ITelegramStatusService telegramService, IAIMPMusicService aimpMusicService = null,

Check warning on line 15 in TelegramMusicStatus/Services/TasksService.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

Cannot convert null literal to non-nullable reference type.

Check warning on line 15 in TelegramMusicStatus/Services/TasksService.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

Cannot convert null literal to non-nullable reference type.
ISpotifyMusicService spotifyService = null)

Check warning on line 16 in TelegramMusicStatus/Services/TasksService.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

Cannot convert null literal to non-nullable reference type.

Check warning on line 16 in TelegramMusicStatus/Services/TasksService.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

Cannot convert null literal to non-nullable reference type.
{
this._telegramService = telegramService;
this._spotifyService = spotifyService;
this._config = config;
this._aimpMusicService = aimpMusicService;
}

public async Task<bool> 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.");
Expand All @@ -37,23 +33,19 @@ public async Task<bool> 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;
}

public async Task<bool> 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.");
Expand All @@ -63,14 +55,10 @@ public async Task<bool> 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;
Expand Down
2 changes: 1 addition & 1 deletion TelegramMusicStatus/Services/TelegramStatusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MainConfig>.SaveConfig(this._config.Entries with { UserBio = this._userDefaultBio });
await Config<MainConfig>.SaveConfig(this._config.Entries with { UserBio = this._userDefaultBio });
}

private async Task Init()
Expand Down

0 comments on commit f4f7d59

Please sign in to comment.