Skip to content

Commit

Permalink
Add auto save feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jooapa committed Feb 10, 2024
1 parent 208a544 commit d78b487
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ public static void CheckKeyboard()
Console.ReadKey(true);
}
break;
case ConsoleKey.D4: // autosave or not
Preferences.isAutoSave = !Preferences.isAutoSave;
TUI.RehreshCurrentView();
break;
case ConsoleKey.Tab:
TUI.Help();

Expand Down
11 changes: 11 additions & 0 deletions src/Message.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using jammer;
using Spectre.Console;


namespace jammer
{
public class Message
{

}
}
3 changes: 2 additions & 1 deletion src/Play.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static void PlaySong(string[] songs, int Currentindex)
Start.lastSeconds = -1;
Utils.currentSong = path;
Utils.currentSongIndex = Currentindex;

Playlists.AutoSave();
// Init audio
try
{
Expand Down Expand Up @@ -346,6 +346,7 @@ public static void DeleteSong(int index)
}
Start.state = MainStates.playing;

Playlists.AutoSave();
// if no songs left, add "" to Utils.songs
PlaySong(Utils.songs, Utils.currentSongIndex);
}
Expand Down
34 changes: 26 additions & 8 deletions src/Playlists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ static public void Create(string playlist)
// y/n prompt
if (Console.ReadLine() == "y")
{
Utils.currentPlaylist = playlistName;
File.Create(playlistPath);
}
}
else
{
Utils.currentPlaylist = playlistName;
File.Create(playlistPath);
}
}
Expand Down Expand Up @@ -185,24 +187,40 @@ static public void Show(string playlist)
}
}

static public void Save(string playlistName)
static public void Save(string playlistName, bool force = false)
{
string playlistPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/jammer/playlists/" + playlistName + ".jammer";
// if playlist exists, overwrite it with y/n
if (File.Exists(playlistPath))
{
Console.WriteLine("Playlist already exists in " + playlistPath + ". Overwrite? (y/n)");
// y/n prompt
if (Console.ReadLine() == "y")
{
File.Delete(playlistPath);
File.WriteAllLines(playlistPath, Utils.songs);
if (!force) {
Console.WriteLine("Playlist already exists in " + playlistPath + ". Overwrite? (y/n)");
string? input = Console.ReadLine();
// y/n prompt
if (input != "y")
{
return;
}
}
File.Delete(playlistPath);
File.WriteAllLines(playlistPath, Utils.songs);
Utils.currentPlaylist = playlistName;
}
else
{
File.WriteAllLines(playlistPath, Utils.songs);
Utils.currentPlaylist = playlistName;
}
}

static public void AutoSave() {
if (!Preferences.isAutoSave) {
return;
}
if (Utils.currentPlaylist == "") {
return;
}
Save(Utils.currentPlaylist, true);
}

static public void List()
Expand All @@ -216,7 +234,7 @@ static public void ListOnly() {
string[] playlists = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/jammer/playlists/");
foreach (string playlist in playlists)
{
AnsiConsole.WriteLine(Path.GetFileName(playlist));
AnsiConsole.WriteLine(Path.GetFileNameWithoutExtension(playlist));
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/Preferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Preferences
public static bool isLoop = GetIsLoop();
public static bool isMuted = GetIsMuted();
public static bool isShuffle = GetIsShuffle();
public static bool isAutoSave = GetIsAutoSave();

static public void CheckJammerFolderExists()
{
Expand Down Expand Up @@ -48,6 +49,8 @@ static public void SaveSettings()
settings.rewindSeconds = rewindSeconds;
settings.changeVolumeBy = changeVolumeBy;
settings.isShuffle = isShuffle;
settings.isAutoSave = isAutoSave;

string jsonString = JsonSerializer.Serialize(settings);
// delete file if exists
if (File.Exists(jammerPath))
Expand Down Expand Up @@ -177,6 +180,21 @@ static public bool GetIsShuffle()
}
}

static public bool GetIsAutoSave()
{
string jammerPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/jammer/settings.json";
if (File.Exists(jammerPath))
{
string jsonString = File.ReadAllText(jammerPath);
Settings? settings = JsonSerializer.Deserialize<Settings>(jsonString);
return settings?.isAutoSave ?? false;
}
else
{
return false;
}
}

static public void OpenJammerFolder()
{
string jammerPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\jammer";
Expand Down Expand Up @@ -205,6 +223,7 @@ public class Settings
public int rewindSeconds { get; set; }
public float changeVolumeBy { get; set; }
public bool isShuffle { get; set; }
public bool isAutoSave { get; set; }
}
}
}
9 changes: 7 additions & 2 deletions src/TUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ public static void PlaylistInput() {
}
songToAdd = Absolute.Correctify(new string[] { songToAdd })[0];
Play.AddSong(songToAdd);
Playlists.AutoSave();
break;
case "2": // delete current song from playlist
Play.DeleteSong(Utils.currentSongIndex);
Playlists.AutoSave();
break;
case "3": // show songs in playlist
AnsiConsole.Markup("\nEnter playlist name: ");
Expand Down Expand Up @@ -210,6 +212,7 @@ public static void PlaylistInput() {
Utils.currentSongIndex = Array.IndexOf(Utils.songs, currentSong);
// set newsong from suffle to the current song
Utils.currentSong = Utils.songs[Utils.currentSongIndex];
Playlists.AutoSave();
break;
case "9": // play single song
AnsiConsole.Markup("\nSeperate songs with space\n");
Expand All @@ -232,6 +235,7 @@ public static void PlaylistInput() {

Utils.songs = songsToPlay;
Utils.currentSongIndex = 0;
Utils.currentPlaylist = "";
Play.StopSong();
Play.PlaySong(Utils.songs, Utils.currentSongIndex);
break;
Expand Down Expand Up @@ -267,7 +271,7 @@ static public void UIComponent_Songs(Table table) {
if (Utils.currentPlaylist == "") {
table.AddRow(GetAllSongs());
} else {
table.AddRow("'playlist " + Utils.currentPlaylist + ".jammer'\n" + GetAllSongs());
table.AddRow("playlist [cyan]" + Utils.currentPlaylist + "[/]\n" + GetAllSongs());
}
}

Expand All @@ -276,7 +280,7 @@ static public void UIComponent_Normal(Table table) {
if (Utils.currentPlaylist == "") {
table.AddRow(GetPrevCurrentNextSong());
} else {
table.AddRow("'playlist " + Utils.currentPlaylist + ".jammer'\n" + GetPrevCurrentNextSong());
table.AddRow("playlist [cyan]" + Utils.currentPlaylist + "[/]\n" + GetPrevCurrentNextSong());
}
}

Expand Down Expand Up @@ -325,6 +329,7 @@ static public void DrawSettings() {
table.AddRow("Forward seconds", Preferences.forwardSeconds + " sec", "[green]1[/] to change");
table.AddRow("Rewind seconds", Preferences.rewindSeconds + " sec", "[green]2[/] to change");
table.AddRow("Change Volume by", Preferences.changeVolumeBy * 100 + " %", "[green]3[/] to change");
table.AddRow("Auto Save", Preferences.isAutoSave + "", "[green]4[/] to toggle");

AnsiConsole.Clear();
AnsiConsole.Write(table);
Expand Down

0 comments on commit d78b487

Please sign in to comment.