Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/dark light mode switch #22

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,14 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/

# System Files
.DS_Store
Thumbs.db

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
59 changes: 57 additions & 2 deletions src/Skolplattformen.ElevApp/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
using Skolplattformen.ElevApp.Resources.Themes;


using Skolplattformen.ElevApp.Resources.Themes;
#if __IOS__
using ObjCRuntime;
using UIKit;
using Foundation;
#endif
#if __ANDROID__
using Android.Content.Res;
#endif

namespace Skolplattformen.ElevApp;

Expand All @@ -20,23 +29,69 @@ public App()

}

protected override void OnResume()
{
SwitchTheme();
}

private void SwitchTheme()
{
ICollection<ResourceDictionary> mergedDictionaries = Application.Current.Resources.MergedDictionaries;
if (mergedDictionaries != null)
{
mergedDictionaries.Clear();

switch (Application.Current.RequestedTheme)
switch (MyWorkingRequestedTheme)
{
case AppTheme.Dark:
mergedDictionaries.Add(new DarkTheme());
Application.Current.UserAppTheme = AppTheme.Dark;
break;
// case AppTheme.Light:
default:
mergedDictionaries.Add(new LightTheme());
Application.Current.UserAppTheme = AppTheme.Light;

break;
}

}
}

// Below are fixes to make MAUI switch theme
// https://github.com/dotnet/maui/issues/10729

#if __IOS__
private AppTheme MyWorkingRequestedTheme
{
get
{
if ((OperatingSystem.IsIOS() && !OperatingSystem.IsIOSVersionAtLeast(13, 0)) || (OperatingSystem.IsTvOS() && !OperatingSystem.IsTvOSVersionAtLeast(13, 0)))
return AppTheme.Unspecified;

var traits =
MainThread.InvokeOnMainThreadAsync(() => WindowStateManager.Default.GetCurrentUIViewController()?.TraitCollection)
.GetAwaiter().GetResult() ??
UITraitCollection.CurrentTraitCollection;

var uiStyle = traits.UserInterfaceStyle;

return uiStyle switch
{
UIUserInterfaceStyle.Light => AppTheme.Light,
UIUserInterfaceStyle.Dark => AppTheme.Dark,
_ => AppTheme.Unspecified
};
}
}
#elif __ANDROID__
public AppTheme MyWorkingRequestedTheme => (Android.App.Application.Context.Resources.Configuration.UiMode & UiMode.NightMask) switch
{
UiMode.NightYes => AppTheme.Dark,
UiMode.NightNo => AppTheme.Light,
_ => AppTheme.Unspecified
};
#else
public AppTheme MyWorkingRequestedTheme => Application.Current.RequestedTheme;
#endif
}