diff --git a/FRESHMusicPlayer/FRESHMusicPlayer-Avalonia/ViewModels/MainWindowViewModel.cs b/FRESHMusicPlayer/FRESHMusicPlayer-Avalonia/ViewModels/MainWindowViewModel.cs index 91f1664..f730be5 100644 --- a/FRESHMusicPlayer/FRESHMusicPlayer-Avalonia/ViewModels/MainWindowViewModel.cs +++ b/FRESHMusicPlayer/FRESHMusicPlayer-Avalonia/ViewModels/MainWindowViewModel.cs @@ -358,6 +358,7 @@ namespace FRESHMusicPlayer.ViewModels Integrations.Add(new PlaytimeLoggingIntegration(Player)); if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && Config.IntegrateMPRIS) Integrations.Add(new MPRISIntegration(this, Window)); + await PerformAutoImport(); } public async void CloseThings() @@ -380,6 +381,32 @@ namespace FRESHMusicPlayer.ViewModels LoggingHandler.Log("Goodbye!"); } + public async Task PerformAutoImport() + { + if (Config.AutoImportPaths.Count <= 0) return; // not really needed but prevents going through unneeded + // effort (and showing the notification) + var filesToImport = new List(); + var library = Library.Read(); + await Task.Run(() => + { + foreach (var folder in Config.AutoImportPaths) + { + var files = Directory.EnumerateFiles(folder, "*", SearchOption.AllDirectories) + .Where(name => name.EndsWith(".mp3") + || name.EndsWith(".wav") || name.EndsWith(".m4a") || name.EndsWith(".ogg") + || name.EndsWith(".flac") || name.EndsWith(".aiff") + || name.EndsWith(".wma") + || name.EndsWith(".aac")).ToArray(); + foreach (var file in files) + { + if (!library.Select(x => x.Path).Contains(file)) + filesToImport.Add(file); + } + } + Library.Import(filesToImport); + }); + } + private int selectedTab; public int SelectedTab { diff --git a/FRESHMusicPlayer/FRESHMusicPlayer-Avalonia/ViewModels/SettingsViewModel.cs b/FRESHMusicPlayer/FRESHMusicPlayer-Avalonia/ViewModels/SettingsViewModel.cs index b7d435d..a9a84e3 100644 --- a/FRESHMusicPlayer/FRESHMusicPlayer-Avalonia/ViewModels/SettingsViewModel.cs +++ b/FRESHMusicPlayer/FRESHMusicPlayer-Avalonia/ViewModels/SettingsViewModel.cs @@ -8,6 +8,13 @@ using System.Text; using System.Threading.Tasks; using ReactiveUI; using System.Collections.ObjectModel; +using Avalonia; +using Avalonia.Controls.ApplicationLifetimes; +using System.Reflection; +using System.Diagnostics; +using System.IO; +using FRESHMusicPlayer.Handlers; +using Avalonia.Controls; namespace FRESHMusicPlayer.ViewModels { @@ -36,7 +43,33 @@ namespace FRESHMusicPlayer.ViewModels return AvailableLanguages.First(x => x.Code == Config?.Language); else return AvailableLanguages[0]; } - set => Config.Language = value.Code; + set + { + Config.Language = value.Code; + CheckRestartNeeded(); + } + } + + public string AutoImportText + { + get + { + var stringBuilder = new StringBuilder(); + stringBuilder.AppendLine(Properties.Resources.Settings_AutoImport_Info); + if (Config.AutoImportPaths is not null) + { + foreach (var path in Config.AutoImportPaths) + stringBuilder.AppendLine(path); + } + return stringBuilder.ToString(); + } + } + + private bool isRestartNeeded = false; + public bool IsRestartNeeded + { + get => isRestartNeeded; + set => this.RaiseAndSetIfChanged(ref isRestartNeeded, value); } public bool PlaytimeLogging @@ -58,6 +91,7 @@ namespace FRESHMusicPlayer.ViewModels set { Config.IntegrateDiscordRPC = value; + CheckRestartNeeded(); } } public bool IntegrateMPRIS @@ -66,6 +100,7 @@ namespace FRESHMusicPlayer.ViewModels set { Config.IntegrateMPRIS = value; + CheckRestartNeeded(); } } public bool MPRISShowCoverArt @@ -85,14 +120,28 @@ namespace FRESHMusicPlayer.ViewModels } } + private ConfigurationFile workingConfig; + + private Window Window + { + get + { + if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) + return desktop.MainWindow; + else return null; + } + } + public SettingsViewModel() { - + } public void StartThings() { + workingConfig = new ConfigurationFile { Language = Config.Language, IntegrateDiscordRPC = Config.IntegrateDiscordRPC, IntegrateMPRIS = Config.IntegrateMPRIS }; // Copy of config that's compared to the actual config file to set AppRestartNeeded. this.RaisePropertyChanged(nameof(Language)); + this.RaisePropertyChanged(nameof(AutoImportText)); this.RaisePropertyChanged(nameof(PlaytimeLogging)); this.RaisePropertyChanged(nameof(ShowTimeInWindow)); this.RaisePropertyChanged(nameof(IntegrateDiscordRPC)); @@ -105,6 +154,35 @@ namespace FRESHMusicPlayer.ViewModels public void ViewSourceCodeCommand() => InterfaceUtils.OpenURL(@"https://github.com/Royce551/FRESHMusicPlayer"); public void ViewLicenseCommand() => InterfaceUtils.OpenURL(@"https://choosealicense.com/licenses/gpl-3.0/"); public void ViewWebsiteCommand() => InterfaceUtils.OpenURL(@"https://royce551.github.io/FRESHMusicPlayer"); + + public void RestartCommand() + { + if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifeTime) + { + lifeTime.Shutdown(); + Process.Start(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "FRESHMusicPlayer")); + } + } + + public async void AddFolderCommand() + { + var dialog = new OpenFolderDialog(); + var directory = await dialog.ShowAsync(Window); + if (directory is not null) + Config.AutoImportPaths.Add(directory); + this.RaisePropertyChanged(nameof(AutoImportText)); + } + public void ClearAllCommand() + { + Config.AutoImportPaths.Clear(); + this.RaisePropertyChanged(nameof(AutoImportText)); + } + + private void CheckRestartNeeded() + { + if (workingConfig.Language == Config.Language && workingConfig.IntegrateDiscordRPC == Config.IntegrateDiscordRPC && workingConfig.IntegrateMPRIS == Config.IntegrateMPRIS) IsRestartNeeded = false; + else IsRestartNeeded = true; + } } public class DisplayLanguage diff --git a/FRESHMusicPlayer/FRESHMusicPlayer-Avalonia/Views/Settings.axaml b/FRESHMusicPlayer/FRESHMusicPlayer-Avalonia/Views/Settings.axaml index 571df22..884567a 100644 --- a/FRESHMusicPlayer/FRESHMusicPlayer-Avalonia/Views/Settings.axaml +++ b/FRESHMusicPlayer/FRESHMusicPlayer-Avalonia/Views/Settings.axaml @@ -32,9 +32,9 @@ - -