Skip to content

Commit

Permalink
reimplement settings page.Because of microsoft/microsoft-ui-xaml#8810
Browse files Browse the repository at this point in the history
…we still need the CommunityToolkit rc version!!!!
  • Loading branch information
RobertK66 committed Jul 29, 2024
1 parent cc8496a commit 5410d40
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 69 deletions.
8 changes: 6 additions & 2 deletions AudioCollectionImpl/JsonMediaRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public async Task LoadAllAsync(object rootPath) {
if (!loading) {
loading = true;
if (rootPath is string dirPath) {
Repositories.Clear();
Categories.Clear();
foreach (var f in Directory.GetFiles(dirPath, "*.json")) {
Log?.LogDebug("Scanning {path} for media content.", f);
if (reLoadPath != null) {
Expand Down Expand Up @@ -76,12 +78,14 @@ private async Task AddRepos(string reposid, string path) {
foreach (var item in cont) {
if (item is IMedia media) {
rep.Add(media);
} else {
Log?.LogInformation($"Entry '{item.Name}' with type '{item.GetType().Name}' can not be added as IMedia element. Add \"type\":\"radio\" or \"type\":\"cd\" to your json objects.");
}
}
}
Log?.LogInformation("Added {count} entries from {name}[{id}]", cont?.Count, cat.Name, reposid);
Log?.LogInformation("Added {count} entries from {name}[{id}]", rep.Count, cat.Name, reposid);
} catch (Exception ex) {
// Silently skip all non media json ...
// Skip all non media json ...
Log?.LogTrace("Exception beim Laden eines Repositories: {repName}, {ex}", path, ex);
}
}
Expand Down
25 changes: 11 additions & 14 deletions WinUiHomeAudio/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ public MainPage() {

_ccRepos = App.Host.Services.GetRequiredService<ChromeCastRepository>();

var settings = App.Host.Services.GetRequiredService<AppSettings>();
IEnumerable<IMediaRepository> mrs = App.Host.Services.GetServices<IMediaRepository>();
foreach (IMediaRepository mr in mrs) {

// IMediaRepository mr = App.Host.Services.GetServices<IMediaRepository>();


mr.GetCategories().CollectionChanged += (s, e) => {
if (e.NewItems != null) {
foreach (var ni in e.NewItems) {
Expand All @@ -77,21 +76,19 @@ public MainPage() {
}
};

//mr.GetRadioCategories().CollectionChanged += (s, e) => {
// if (e.NewItems != null) {
// foreach (var ni in e.NewItems) {
// if (ni is MediaCategory mc) {
// Categories.Add(new Category() { Glyph = Symbol.Account, Name = mc.Name ?? "unknown", Tag = mc.Id });
// }
// }
// }
//};

_ = mr.LoadAllAsync(ApplicationData.Current.LocalFolder.Path);
_ = mr.LoadAllAsync(settings.ReposPath);
}
}


public void ReconfigureMediaFolder(string reposRootPath) {
Categories.Clear();
IEnumerable<IMediaRepository> mrs = App.Host.Services.GetServices<IMediaRepository>();
foreach (IMediaRepository mr in mrs) {
_ = mr.LoadAllAsync(reposRootPath);
}
}

private void NavigationView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args) {
if (args.IsSettingsInvoked) {
//this.ccPlayer.Visibility = Visibility.Collapsed;
Expand Down
16 changes: 16 additions & 0 deletions WinUiHomeAudio/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
Expand All @@ -12,6 +13,7 @@
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using WinUiHomeAudio.model;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
Expand All @@ -26,6 +28,20 @@ public sealed partial class MainWindow : Window {

public MainWindow() {
this.InitializeComponent();

var appSettings = App.Host.Services.GetRequiredService<AppSettings>();

if (appSettings.IsLeftMode) {
MainPage.MainNavPane.PaneDisplayMode = NavigationViewPaneDisplayMode.Auto;
} else {
MainPage.MainNavPane.PaneDisplayMode = NavigationViewPaneDisplayMode.Top;
}

var t = appSettings.GetEnum<ElementTheme>(appSettings.UiTheme);
if (this.Content is FrameworkElement rootElement) {
rootElement.RequestedTheme = t;
}

}


Expand Down
3 changes: 3 additions & 0 deletions WinUiHomeAudio/WinUiHomeAudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<UseWinUI>true</UseWinUI>
<EnableMsixTooling>true</EnableMsixTooling>
<Nullable>enable</Nullable>
<PackageCertificateThumbprint>E5C8D50612BBEEAAE1A270272585C328BABCED24</PackageCertificateThumbprint>
<PackageCertificateKeyFile>WinUiHomeAudio_TemporaryKey.pfx</PackageCertificateKeyFile>
<AppxPackageSigningEnabled>True</AppxPackageSigningEnabled>
</PropertyGroup>
<ItemGroup>
<None Remove="MainPage.xaml" />
Expand Down
55 changes: 52 additions & 3 deletions WinUiHomeAudio/model/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
namespace WinUiHomeAudio.model {
using System;
using System.Reflection;
using Windows.Storage;

namespace WinUiHomeAudio.model {
public class AppSettings {
public string? AutoConnectName { get; internal set; } = "My JBL";//"Wohnzimmer"; //"Büro";
public string AppId { get; internal set; } = "46C1A819";

private const string c_IsLeftMode = "NavView_IsLeftMode";
private const string c_UiTheme = "App_Theme";
private const string c_ReposPath = "Repos_Path";
private const string c_AutoConnect = "Auto_Connect";
private const string c_AppId = "App_Id";

private string _AppId = (ApplicationData.Current.LocalSettings.Values[c_AppId] as string) ?? "46C1A819";
public string AppId {
get { return _AppId; }
set { if (!Object.Equals(_AppId, value)) { _AppId = value; ApplicationData.Current.LocalSettings.Values[c_AppId] = value; } }
}

private string? _AutoConnectName = (ApplicationData.Current.LocalSettings.Values[c_AutoConnect] as string)??"My JBL";
public string? AutoConnectName {
get { return _AutoConnectName; }
set { if (!Object.Equals(_AutoConnectName, value)) { _AutoConnectName = value; ApplicationData.Current.LocalSettings.Values[c_AutoConnect] = value; } }
}

private string _ReposPath = (ApplicationData.Current.LocalSettings.Values[c_ReposPath] as string) ?? ApplicationData.Current.LocalFolder.Path;
public string ReposPath {
get { return _ReposPath; }
set { if (!Object.Equals(_ReposPath, value)) { _ReposPath = value; ApplicationData.Current.LocalSettings.Values[c_ReposPath] = value; } }
}


private string _UiTheme = (ApplicationData.Current.LocalSettings.Values[c_UiTheme] as string) ?? "Default";
public string UiTheme {
get { return _UiTheme; }
set { if (!Object.Equals(_UiTheme, value)) { _UiTheme = value; ApplicationData.Current.LocalSettings.Values[c_UiTheme] = value; } }
}

private bool _IsLeftMode = (ApplicationData.Current.LocalSettings.Values[c_IsLeftMode] as bool?) ?? true;
public bool IsLeftMode {
get { return _IsLeftMode; }
set { if (!Object.Equals(_IsLeftMode, value)) { _IsLeftMode = value; ApplicationData.Current.LocalSettings.Values[c_IsLeftMode] = value; } }
}



public TEnum GetEnum<TEnum>(string text) where TEnum : struct {
if (!typeof(TEnum).GetTypeInfo().IsEnum) {
throw new InvalidOperationException("Generic parameter 'TEnum' must be an enum.");
}
return (TEnum)Enum.Parse(typeof(TEnum), text);
}

}
}
6 changes: 3 additions & 3 deletions WinUiHomeAudio/pages/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@

<TextBlock Style="{StaticResource SettingsSectionHeaderTextBlockStyle}" Text="About" />

<labs:SettingsExpander x:Name="VersionExpander" Description="© 2023 Robert'sw" Header="lkjklöjlö jklöj klökj lk" HeaderIcon="PreviewLink">
<labs:SettingsExpander x:Name="VersionExpander" Description="© 2023 Robert'sw" Header="My Home Audio" HeaderIcon="PreviewLink">
<TextBlock
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
IsTextSelectionEnabled="True"
Text="{x:Bind PackageVersion}" />
<labs:SettingsExpander.Items>
<labs:SettingsCard
<!--<labs:SettingsCard
x:Name="versioncard"
Header="{x:Bind EaName}"
Description="{x:Bind EaDecription}"
Expand All @@ -102,7 +102,7 @@
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
IsTextSelectionEnabled="True"
Text="{x:Bind EaVersion}" />
</labs:SettingsCard>
</labs:SettingsCard>-->
</labs:SettingsExpander.Items>
</labs:SettingsExpander>
</StackPanel>
Expand Down
79 changes: 32 additions & 47 deletions WinUiHomeAudio/pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using CommunityToolkit.WinUI.Controls;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.UI;
Expand Down Expand Up @@ -51,24 +52,6 @@ public sealed partial class SettingsPage : VmPage {
//private static int navigatecount = 0;
private TimeSpan? loadTime = null;

public string EaVersion {
get {
var version = System.Reflection.Assembly.GetEntryAssembly()?.GetName().Version;
return string.Format("{0}.{1}.{2}.{3}", version?.Major, version?.Minor, version?.Build, version?.Revision);
}
}

public string EaName {
get {
return System.Reflection.Assembly.GetEntryAssembly()?.FullName ?? "<null>";
}
}

public string EaDecription {
get {
return System.Reflection.Assembly.GetEntryAssembly()?.Location ?? "unknown";
}
}

public string PackageVersion {
get {
Expand All @@ -84,14 +67,13 @@ public string PackageVersion {

public String RepositoryPath {
get {
return "Settings.ReposPath;";
}
return Settings.ReposPath; }

set {
//if (!Object.Equals(Settings.ReposPath, value)) {
// Settings.ReposPath = value;
// RaisePropertyChanged();
//}
if (!Object.Equals(Settings.ReposPath, value)) {
Settings.ReposPath = value;
RaisePropertyChanged();
}
}
}

Expand Down Expand Up @@ -136,13 +118,13 @@ public ObservableCollection<String> RepositoryFiles {
}


public string WinAppSdkDetails {
get => "App.WinAppSdkDetails;";
}
//public string WinAppSdkDetails {
// get => "App.WinAppSdkDetails;";
//}

public string WinAppSdkRuntimeDetails {
get => "App.WinAppSdkRuntimeDetails;";
}
//public string WinAppSdkRuntimeDetails {
// get => "App.WinAppSdkRuntimeDetails;";
//}

public SettingsPage() {
DateTime startTime = DateTime.Now;
Expand All @@ -153,15 +135,16 @@ public SettingsPage() {

var aa = Assembly.GetEntryAssembly()?.GetReferencedAssemblies();
if (aa != null) {
foreach (var a in aa) {
foreach (var a in aa.OrderBy(y=>y.Name)) {
try {
if (a != null) {
var asm = Assembly.Load(a);
//this.VersionExpander.Items.Add(new SettingsCard() {
// Header = a.FullName,
// Content = string.Format("{0}.{1}.{2}.{3} ", a.Version?.Major, a.Version?.Minor, a.Version?.Build, a.Version?.Revision),
// Description = asm.Location
//});
System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(asm.Location);
this.VersionExpander.Items.Add(new SettingsCard() {
Header = fvi.InternalName??fvi.FileName,
Content = fvi.FileVersion, //string.Format("{0}.{1}.{2}.{3} ", a.Version?.Major, a.Version?.Minor, a.Version?.Build, a.Version?.Revision),
Description = a.FullName
});
}
} catch { }
}
Expand All @@ -179,7 +162,7 @@ private int ListReposFiles() {
try {
RepositoryFiles.Clear();
foreach (var s in Directory.GetFiles(RepositoryPath, "*.json")) {
RepositoryFiles.Add(s);
RepositoryFiles.Add(Path.GetFullPath(s));
i++;
}
} catch (Exception ex) {
Expand All @@ -188,15 +171,18 @@ private int ListReposFiles() {
return i;
}




private void themeMode_SelectionChanged_1(object sender, SelectionChangedEventArgs e) {
var selectedTheme = ((ComboBoxItem)themeMode.SelectedItem)?.Tag?.ToString();
if (selectedTheme != null) {
//var t = App.GetEnum<ElementTheme>(selectedTheme);
var t = Settings.GetEnum<ElementTheme>(selectedTheme);

//if (App.Current.m_window?.Content is FrameworkElement rootElement) {
// rootElement.RequestedTheme = t;
// ApplicationData.Current.LocalSettings.Values[AppSettingKeys.UiTheme] = t.ToString();
//}
if ((App.Current as WinUiHomeAudio.App)?.m_window?.Content is FrameworkElement rootElement) {
rootElement.RequestedTheme = t;
Settings.UiTheme = t.ToString();
}
}

}
Expand All @@ -206,10 +192,10 @@ private void navigationLocation_SelectionChanged_1(object sender, SelectionChang
if (navPane != null) {
if (navigationLocation.SelectedIndex == 0) {
navPane.PaneDisplayMode = NavigationViewPaneDisplayMode.Auto;
//ApplicationData.Current.LocalSettings.Values[AppSettingKeys.IsLeftMode] = true;
Settings.IsLeftMode = true;
} else {
navPane.PaneDisplayMode = NavigationViewPaneDisplayMode.Top;
//ApplicationData.Current.LocalSettings.Values[AppSettingKeys.IsLeftMode] = false;
Settings.IsLeftMode = false;
}
}
}
Expand All @@ -218,8 +204,7 @@ private void Page_Loaded(object sender, RoutedEventArgs e) {
loadcount++;
Log.LogDebug(string.Format("Settings Page constructed: {0}, loaded: {1} ", constcount, loadcount));

bool? isLeft = null; // ApplicationData.Current.LocalSettings.Values[AppSettingKeys.IsLeftMode];
if (isLeft == null || ((bool)isLeft == true)) {
if (Settings.IsLeftMode) {
navigationLocation.SelectedIndex = 0;
} else {
navigationLocation.SelectedIndex = 1;
Expand Down Expand Up @@ -252,7 +237,7 @@ private void reposPath_TextChanged(object sender, TextChangedEventArgs e) {

private void CreateFileList() {
if (ListReposFiles() > 0) {
//App.Current.ReconfigureMainWindow(RepositoryPath);
(App.Current as WinUiHomeAudio.App)?.m_window?.MainPage?.ReconfigureMediaFolder(RepositoryPath);
}
}

Expand Down

0 comments on commit 5410d40

Please sign in to comment.