Implement Auto Import

This commit is contained in:
Royce551 2021-06-27 21:15:17 -05:00
parent 0a68970545
commit 8588bda722
3 changed files with 113 additions and 5 deletions

View file

@ -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<string>();
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
{

View file

@ -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

View file

@ -32,9 +32,9 @@
<TextBlock FontWeight="Bold" FontSize="18" Text="{x:Static resx:Resources.Settings_AutoImport}"/>
<TextBlock Text="{x:Static resx:Resources.Settings_AutoImport_Info}"/>
<Button Content="{x:Static resx:Resources.Settings_AddFolder}" IsEnabled="False"/>
<Button Content="{x:Static resx:Resources.Settings_ClearAll}" IsEnabled="False"/>
<TextBlock Text="{Binding AutoImportText}"/>
<Button Content="{x:Static resx:Resources.Settings_AddFolder}" Command="{Binding AddFolderCommand}"/>
<Button Content="{x:Static resx:Resources.Settings_ClearAll}" Command="{Binding ClearAllCommand}"/>
<TextBlock FontWeight="Bold" FontSize="18" Text="{x:Static resx:Resources.Settings_Integrations}"/>
@ -46,6 +46,9 @@
<TextBlock FontWeight="Bold" FontSize="18" Text="{x:Static resx:Resources.Settings_Updates}" IsVisible="{Binding IsRunningOnMac}"/>
<CheckBox Content="{x:Static resx:Resources.Settings_CheckForUpdates}" IsChecked="{Binding CheckForUpdates}" IsVisible="{Binding IsRunningOnMac}"/>
<TextBlock Text="FMP needs to restart to apply your changes." Foreground="{StaticResource Green}" IsVisible="{Binding IsRestartNeeded}"/>
<Button Content="Restart now" Foreground="{StaticResource Green}" Command="{Binding RestartCommand}" IsVisible="{Binding IsRestartNeeded}"/>
</StackPanel>
</ScrollViewer>
</TabItem>