Skip to content

Commit

Permalink
Try fix #57
Browse files Browse the repository at this point in the history
  • Loading branch information
Miaoyww committed Sep 1, 2024
1 parent 4f580f7 commit 558279e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
9 changes: 6 additions & 3 deletions NonsPlayer.Core/Services/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public void Load()
}
else
{
Directory.CreateDirectory(Path.GetDirectoryName(Settings.ConfigFilePath));
File.Create(Settings.ConfigFilePath).Close();
Create();
}
}
catch(Exception e)
Expand All @@ -43,7 +42,11 @@ public void Load()
}

}

public void Create()
{
Directory.CreateDirectory(Path.GetDirectoryName(Settings.ConfigFilePath));
File.Create(Settings.ConfigFilePath).Close();
}
public void Save()
{
if (!File.Exists(Settings.ConfigFilePath))
Expand Down
8 changes: 7 additions & 1 deletion NonsPlayer/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ public App()
#region Local

Log.Information("Start init local service");
GetService<LocalService>().LocalLoadFailed += OnLocalLoadFailed;
GetService<LocalService>().LoadFromFile();

#endregion
#region Counter

Expand All @@ -199,6 +200,11 @@ public App()

}

private void OnLocalLoadFailed(string param)
{
ExceptionService.Instance.Throw(param);
}

private void OnConfigLoadFailed(string param)
{
Log.Error("Config load failed: {param}", param);
Expand Down
43 changes: 26 additions & 17 deletions NonsPlayer/Services/LocalService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.Json;
using Exception = System.Exception;

namespace NonsPlayer.Services;

public class LocalService
{
#region 事件注册

public delegate void LocalFolderModelEventHandler();
public delegate void LocalFolderModelEventHandler(string param);

public event LocalFolderModelEventHandler? LocalFolderChanged;
public event LocalFolderModelEventHandler? LocalLoadFailed;

#endregion

Expand Down Expand Up @@ -45,7 +47,7 @@ public bool TryAddSong(LocalMusic song)
if (songItem.FilePath.Equals(song.FilePath)) return false;
}

LocalFolderChanged?.Invoke();
LocalFolderChanged?.Invoke(string.Empty);
var result = Songs.Add(song);
return result;
}
Expand All @@ -57,7 +59,7 @@ public void AddSongs(IEnumerable<LocalMusic> songs)
TryAddSong(inputSongItem);
}

LocalFolderChanged?.Invoke();
LocalFolderChanged?.Invoke(string.Empty);
}

public bool HasDirectory(string path)
Expand Down Expand Up @@ -86,29 +88,36 @@ public bool TryDelDirection(string path)
if (!TryGetModel(path, out var result)) return false;
Directories.Remove(result);
Save();
LocalFolderChanged?.Invoke();
LocalFolderChanged?.Invoke(string.Empty);
return true;
}

public void LoadFromFile()
{
var data = FileService.ReadData(_dataKey);
if (!string.IsNullOrEmpty(data))
try
{
var value = JArray.Parse(data);
Directories.Clear();
var index = 0;
foreach (var item in value)
var data = FileService.ReadData(_dataKey);
if (!string.IsNullOrEmpty(data))
{
index++;
Directories.Add(new LocalFolderModel(
(item)["path"].ToString(),
index.ToString("D2")
));
var value = JArray.Parse(data);
Directories.Clear();
var index = 0;
foreach (var item in value)
{
index++;
Directories.Add(new LocalFolderModel(
(item)["path"].ToString(),
index.ToString("D2")
));
}

LocalFolderChanged?.Invoke(string.Empty);
}

LocalFolderChanged?.Invoke();
}catch(Exception e)
{
LocalLoadFailed?.Invoke(e.Message);
}

}

public void Save()
Expand Down
2 changes: 1 addition & 1 deletion NonsPlayer/ViewModels/Pages/MainPages/LocalViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public LocalViewModel()
LocalService.LocalFolderChanged += LocalServiceOnLocalFolderChanged;
}

private void LocalServiceOnLocalFolderChanged()
private void LocalServiceOnLocalFolderChanged(string e)
{
RefreshInfo();
}
Expand Down

0 comments on commit 558279e

Please sign in to comment.