Skip to content

Commit

Permalink
Save volume into db
Browse files Browse the repository at this point in the history
  • Loading branch information
xBaank committed Aug 13, 2024
1 parent 326779c commit 0a1d3b4
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 11 deletions.
41 changes: 33 additions & 8 deletions Console/Audio/PlayerController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Console.Audio.Containers.Matroska;
using Console.Audio.DownloadHandlers;
using Console.Repositories;
using Nito.AsyncEx;
using Nito.Disposables.Internals;
using YoutubeExplode;
Expand All @@ -8,17 +9,20 @@

namespace Console.Audio;

public class PlayerController : IAsyncDisposable
internal class PlayerController(YoutubeClient youtubeClient, SettingsRepository settingsRepository)
: IAsyncDisposable
{
private readonly AsyncLock _lock = new();
private readonly YoutubeClient _youtubeClient;
private float _volume = 0.5f;
private readonly YoutubeClient _youtubeClient = youtubeClient;
private readonly SettingsRepository _settingsRepository = settingsRepository;
private float _volume = settingsRepository.GetSettings().Volume / 100f;
private PlayState _state = PlayState.Stopped;
private List<IVideo> _queue = [];
private int _currentSongIndex = 0;
private Matroska? _matroskaPlayerBuffer = null;
private AudioSender? _audioSender = null;
private CancellationTokenSource _currentSongTokenSource = new();
private CancellationTokenSource _currentSettingsTokenSource = new();
private bool _disposed = false;

public event Action? StateChanged;
Expand All @@ -44,22 +48,41 @@ public int Volume
return;

_volume = value / 100f;
SaveVolume(value);

if (_audioSender is not null)
_audioSender.Volume = _volume;
}
}

private void SaveVolume(int value)
{
_currentSettingsTokenSource.Cancel();
_currentSettingsTokenSource = new();

Task.Run(
async () =>
{
await Task.Delay(1000, _currentSettingsTokenSource.Token);
var settings = await _settingsRepository.GetSettingsAsync(
_currentSettingsTokenSource.Token
);
settings.Volume = value;
await _settingsRepository.SaveSettingsAsync(
settings,
_currentSettingsTokenSource.Token
);
},
_currentSettingsTokenSource.Token
);
}

public TimeSpan? Time => _audioSender?.CurrentTime;
public TimeSpan? TotalTime => _matroskaPlayerBuffer?.TotalTime ?? Song?.Duration;
public IVideo? Song => _queue.ElementAtOrDefault(_currentSongIndex);
public IReadOnlyCollection<IVideo> Songs => _queue;
public LoopState LoopState { get; set; }

public PlayerController(YoutubeClient youtubeClient)
{
_youtubeClient = youtubeClient;
}

public async ValueTask DisposeAsync()
{
if (_disposed)
Expand All @@ -81,6 +104,8 @@ public async ValueTask DisposeAsync()
await _audioSender.DisposeAsync().ConfigureAwait(false);
}

await _settingsRepository.DisposeAsync();

// Suppress finalization
GC.SuppressFinalize(this);
}
Expand Down
8 changes: 6 additions & 2 deletions Console/Commands/MainCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ public async ValueTask ExecuteAsync(IConsole console)
var serviceProvider = new ServiceCollection()
.AddScoped<MyDbContext>()
.AddScoped<LocalPlaylistsRepository>()
.AddScoped<SettingsRepository>()
.AddSingleton<SharedCancellationTokenSource>()
.AddSingleton<HttpClient>()
.AddSingleton(provider =>
{
var youtubeClient = provider.GetRequiredService<YoutubeClient>();
return new PlayerController(youtubeClient);
var settingsRepository = provider.GetRequiredService<SettingsRepository>();
return new PlayerController(youtubeClient, settingsRepository);
})
.AddSingleton(provider =>
{
Expand Down Expand Up @@ -189,8 +191,10 @@ public async ValueTask ExecuteAsync(IConsole console)
})
.BuildServiceProvider();

var dbContext = serviceProvider.GetRequiredService<MyDbContext>();
using var dbContext = serviceProvider.GetRequiredService<MyDbContext>();
using var settingsRepository = serviceProvider.GetRequiredService<SettingsRepository>();
await dbContext.Database.MigrateAsync();
await settingsRepository.InitializeAsync();

await using var playerController = serviceProvider.GetRequiredService<PlayerController>();
var playerView = serviceProvider.GetRequiredService<PlayerView>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ internal class MyDbContext : DbContext
public DbSet<LocalSong> Songs { get; set; }
public DbSet<LocalPlaylist> Playlists { get; set; }
public DbSet<LocalPlaylistSong> PlaylistSongs { get; set; }
public DbSet<Setting> Settings { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=my_playlists.db");
optionsBuilder.UseSqlite("Data Source=data.db");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down
10 changes: 10 additions & 0 deletions Console/Database/Setting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.EntityFrameworkCore;

namespace Console.Database;

[PrimaryKey("Id")]
internal class Setting
{
public int Id { get; set; }
public int Volume { get; set; }
}
130 changes: 130 additions & 0 deletions Console/Migrations/20240813103834_Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Console/Migrations/20240813103834_Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Console.Migrations
{
/// <inheritdoc />
public partial class Settings : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Settings",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Volume = table.Column<float>(type: "REAL", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Settings", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Settings");
}
}
}
Loading

0 comments on commit 0a1d3b4

Please sign in to comment.