Skip to content

Commit

Permalink
Version 0.7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dsafa committed Nov 22, 2018
2 parents cfe39fd + d7c559c commit 80893e9
Show file tree
Hide file tree
Showing 16 changed files with 295 additions and 227 deletions.
5 changes: 4 additions & 1 deletion src/AudioBand/AudioBand.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<Reference Include="CSDeskBand.Win, Version=2.0.0.0, Culture=neutral, PublicKeyToken=66d19cfc65a1cc19, processorArchitecture=MSIL">
<HintPath>..\packages\CSDeskBand.Win.2.1.0\lib\net461\CSDeskBand.Win.dll</HintPath>
</Reference>
<Reference Include="FastMember, Version=1.4.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\FastMember.1.4.1\lib\net461\FastMember.dll</HintPath>
</Reference>
<Reference Include="MahApps.Metro, Version=1.6.5.1, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MahApps.Metro.1.6.5\lib\net46\MahApps.Metro.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -230,7 +233,7 @@
<Compile Include="Views\Winforms\FormattedTextLabel.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="AudioSource\AudioSourceManager.cs" />
<Compile Include="AudioSource\AudioSourceLoader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down
65 changes: 65 additions & 0 deletions src/AudioBand/AudioSource/AudioSourceLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Linq;
using Nett;
using NLog;

namespace AudioBand.AudioSource
{
internal class AudioSourceLoader
{
[ImportMany(AllowRecomposition = true)]
public IEnumerable <IAudioSource> AudioSources { get; private set; } = new List<IAudioSource>();

public event EventHandler AudioSourcesChanged;

private const string PluginFolderName = "AudioSources";
private const string ManifestFileName = "AudioSource.manifest";
private static readonly string PluginFolderPath = Path.Combine(DirectoryHelper.BaseDirectory, PluginFolderName);
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();

public void LoadAudioSources()
{
Logger.Debug($"Searching for audio sources in path `{PluginFolderPath}`");

var catalog = new AggregateCatalog();
var directories = Directory.EnumerateDirectories(PluginFolderPath, "*", SearchOption.TopDirectoryOnly);

foreach (var directory in directories)
{
Logger.Debug($"Searching in {directory} for {ManifestFileName}");

var manifest = Directory.GetFiles(directory, ManifestFileName, SearchOption.TopDirectoryOnly).FirstOrDefault();
if (manifest == null)
{
continue;
}

var manifestData = Toml.ReadFile<Manifest>(manifest);
Logger.Debug($"Reading {manifest}. Audio sources: [{string.Join("," , manifestData.AudioSources)}]");

foreach (var audioSourceFileName in manifestData.AudioSources)
{
var audioSourcePath = Path.Combine(directory, audioSourceFileName);
catalog.Catalogs.Add(new AssemblyCatalog(audioSourcePath));
}
}

var container = new CompositionContainer(catalog);
container.ComposeParts(this);

foreach (var audioSource in AudioSources)
{
audioSource.Logger = new AudioSourceLogger(audioSource.Name);
}
}

private class Manifest
{
public List<string> AudioSources { get; set; }
}
}
}
82 changes: 0 additions & 82 deletions src/AudioBand/AudioSource/AudioSourceManager.cs

This file was deleted.

28 changes: 27 additions & 1 deletion src/AudioBand/MainControl.Bindings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Linq;
using System;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Threading;
using AudioBand.ViewModels;
using AudioBand.Views.Winforms;

Expand All @@ -22,6 +24,18 @@ private void InitializeBindingSources(AlbumArtPopupVM albumArtPopupVm, AlbumArtV
}

void ICustomLabelHost.AddCustomTextLabel(CustomLabelVM customLabel)
{
if (InvokeRequired)
{
BeginInvoke((Action) (() => AddCustomLabelText(customLabel)));
}
else
{
AddCustomLabelText(customLabel);
}
}

private void AddCustomLabelText(CustomLabelVM customLabel)
{
var label = new FormattedTextLabel(customLabel.FormatString, customLabel.Color, customLabel.FontSize, customLabel.FontFamily, customLabel.TextAlignment);
var labelBindingSource = new BindingSource(components);
Expand Down Expand Up @@ -51,6 +65,18 @@ void ICustomLabelHost.AddCustomTextLabel(CustomLabelVM customLabel)
}

void ICustomLabelHost.RemoveCustomTextLabel(CustomLabelVM customLabel)
{
if (InvokeRequired)
{
BeginInvoke((Action) (() => RemoveCustomTextLabel(customLabel)));
}
else
{
RemoveCustomTextLabel(customLabel);
}
}

void RemoveCustomTextLabel(CustomLabelVM customLabel)
{
var control = Controls.Find(CustomLabelControlsKey, true)
.Cast<FormattedTextLabel>()
Expand Down
4 changes: 2 additions & 2 deletions src/AudioBand/MainControl.EventHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ private async void AudioSourceMenuItemOnClicked(object sender, EventArgs eventAr
await UnsubscribeToAudioSource(_currentAudioSource);

item.Checked = true;
_currentAudioSource = _audioSourceManager.AudioSources.First(c => c.Name == item.Text);
_currentAudioSource = _audioSourceLoader.AudioSources.First(c => c.Name == item.Text);
await SubscribeToAudioSource(_currentAudioSource);
}
catch (Exception e)
{
Logger.Debug("Error activating audio source");
Logger.Debug(e, "Error activating audio source");
}
}

Expand Down
47 changes: 29 additions & 18 deletions src/AudioBand/MainControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ namespace AudioBand
public partial class MainControl : CSDeskBandWin
{
private static readonly ILogger Logger = LogManager.GetLogger("Audio Band");
private AudioSourceManager _audioSourceManager;
private AppSettings _appSettings;
private readonly AudioSourceLoader _audioSourceLoader = new AudioSourceLoader();
private readonly AppSettings _appSettings = new AppSettings();
private readonly Dispatcher _uiDispatcher;
private SettingsWindow _settingsWindow;
private IAudioSource _currentAudioSource;
private DeskBandMenu _pluginSubMenu;
private CancellationTokenSource _audioSourceTokenSource = new CancellationTokenSource();
private SettingsWindowVM _settingsWindowVm;

#region Models

Expand All @@ -56,7 +58,9 @@ static MainControl()
MaxArchiveFiles = 3,
ArchiveOldFileOnStartup = true,
FileName = "${environment:variable=TEMP}/AudioBand.log",
ConcurrentWrites = true
KeepFileOpen = true,
OpenFileCacheTimeout = 30,
Layout = NLog.Layouts.Layout.FromString("${longdate}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}")
};

var nullTarget = new NullTarget();
Expand All @@ -82,25 +86,31 @@ public MainControl()
#if DEBUG
System.Diagnostics.Debugger.Launch();
#endif
_uiDispatcher = Dispatcher.CurrentDispatcher;
InitializeAsync();
}

private async void InitializeAsync()
private async Task InitializeAsync()
{
try
{
_audioSourceManager = new AudioSourceManager();
_appSettings = new AppSettings();
await Task.Run(() =>
{
_audioSourceLoader.LoadAudioSources();
Options.ContextMenuItems = BuildContextMenu();
InitializeModels();
}).ConfigureAwait(false);

Options.ContextMenuItems = BuildContextMenu();
_settingsWindowVm = await SetupViewModels().ConfigureAwait(false);

await Dispatcher.CurrentDispatcher.InvokeAsync(() =>
await _uiDispatcher.InvokeAsync(() =>
{
InitializeModels();
SetupViewModelsAndWindow();
_settingsWindow = new SettingsWindow(_settingsWindowVm);
_settingsWindow.Saved += Saved;
ElementHost.EnableModelessKeyboardInterop(_settingsWindow);
});

await SelectAudioSourceFromSettings();
await SelectAudioSourceFromSettings().ConfigureAwait(false);
Logger.Debug("Initialization complete");
}
catch (Exception e)
Expand All @@ -123,7 +133,7 @@ private void InitializeModels()
_trackModel = new Track();
}

private void SetupViewModelsAndWindow()
private async Task<SettingsWindowVM> SetupViewModels()
{
var albumArt = new AlbumArtVM(_albumArtModel, _trackModel);
var albumArtPopup = new AlbumArtPopupVM(_albumArtPopupModel, _trackModel);
Expand All @@ -135,7 +145,7 @@ private void SetupViewModelsAndWindow()
var progressBar = new ProgressBarVM(_progressBarModel, _trackModel);
var allAudioSourceSettings = new List<AudioSourceSettingsVM>();

foreach (var audioSource in _audioSourceManager.AudioSources)
foreach (var audioSource in _audioSourceLoader.AudioSources)
{
var matchingSetting = _audioSourceSettingsModel.FirstOrDefault(s => s.AudioSourceName == audioSource.Name);
if (matchingSetting != null)
Expand All @@ -150,7 +160,7 @@ private void SetupViewModelsAndWindow()
}
}

InitializeBindingSources(albumArtPopup, albumArt, audioBand, nextButton, playPauseButton, prevButton, progressBar);
await _uiDispatcher.InvokeAsync(() => InitializeBindingSources(albumArtPopup, albumArt, audioBand, nextButton, playPauseButton, prevButton, progressBar));

var vm = new SettingsWindowVM
{
Expand All @@ -165,9 +175,10 @@ private void SetupViewModelsAndWindow()
CustomLabelsVM = customLabels,
AudioSourceSettingsVM = allAudioSourceSettings
};
_settingsWindow = new SettingsWindow(vm);
_settingsWindow.Saved += Saved;
ElementHost.EnableModelessKeyboardInterop(_settingsWindow);

vm.BeginEdit();

return vm;
}

private void Saved(object o, EventArgs eventArgs)
Expand All @@ -177,7 +188,7 @@ private void Saved(object o, EventArgs eventArgs)

private List<DeskBandMenuItem> BuildContextMenu()
{
var pluginList = _audioSourceManager.AudioSources.Select(audioSource =>
var pluginList = _audioSourceLoader.AudioSources.Select(audioSource =>
{
var item = new DeskBandMenuAction(audioSource.Name);
item.Clicked += AudioSourceMenuItemOnClicked;
Expand Down
4 changes: 3 additions & 1 deletion src/AudioBand/ViewModels/AudioSourceSettingVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public AudioSourceSettingVM(AudioSourceSetting model, AudioSourceSettingInfo set
{
Remember = false;
}

ApplyChanges();
}

/// <summary>
Expand All @@ -91,7 +93,7 @@ protected override void OnEndEdit()
ApplyChanges();
}

public void UpdateValue()
public void ValueChanged()
{
Model.Value = _settingInfo.GetValue();
}
Expand Down
9 changes: 2 additions & 7 deletions src/AudioBand/ViewModels/AudioSourceSettingsVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,20 @@ internal class AudioSourceSettingsVM : ViewModelBase<AudioSourceSettings>
public AudioSourceSettingsVM(AudioSourceSettings settings, IAudioSource audioSource) : base(settings)
{
Settings = CreateSettingViewModels(Model, audioSource);
foreach (var audioSourceSettingVm in Settings)
{
audioSourceSettingVm.ApplyChanges();
}

audioSource.PropertyChanged += AudioSourceOnPropertyChanged;
}

private void AudioSourceOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
Settings.FirstOrDefault(s => s.PropertyName == propertyChangedEventArgs.PropertyName)?.UpdateValue();
Settings.FirstOrDefault(s => s.PropertyName == propertyChangedEventArgs.PropertyName)?.ValueChanged();
}

private List<AudioSourceSettingVM> CreateSettingViewModels(AudioSourceSettings existingSettings, IAudioSource source)
{
var viewmodels = new List<AudioSourceSettingVM>();
var audioSourceSettingInfos = source.GetSettings();

foreach (var audioSourceSettingInfo in audioSourceSettingInfos)
foreach (var audioSourceSettingInfo in audioSourceSettingInfos.OrderByDescending(s => s.Attribute.Priority))
{
var matchingSetting = existingSettings.Settings.FirstOrDefault(s => s.Name == audioSourceSettingInfo.Attribute.Name);
if (matchingSetting != null)
Expand Down
Loading

0 comments on commit 80893e9

Please sign in to comment.