Skip to content

Commit

Permalink
Update to latest version of Berry.Maui
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry08 committed Jun 10, 2024
1 parent 31c6dcd commit 708a308
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 86 deletions.
45 changes: 44 additions & 1 deletion Yosu/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Linq;
using Berry.Maui.Behaviors;
using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Behaviors;
using CommunityToolkit.Maui.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
Expand All @@ -9,7 +12,7 @@
using Microsoft.Maui.Networking;
using Yosu.Services;
using Yosu.Services.AlertDialog;
using Yosu.Utils;
using NavigationBarStyle = Berry.Maui.Behaviors.NavigationBarStyle;

namespace Yosu;

Expand Down Expand Up @@ -94,6 +97,46 @@ public static void ApplyTheme()
Current.UserAppTheme = preferenceService.AppTheme;
}

public static void RefreshCurrentPageBehaviors()
{
if (Current is null)
return;

var page = Current.MainPage;
if (page is null)
return;

var gray900Color = Current.Resources["Gray900"];

foreach (var behavior in page.Behaviors.OfType<StatusBarBehavior>())
{
behavior.SetAppTheme(
StatusBarBehavior.StatusBarColorProperty,
Colors.White,
Colors.Black
);
behavior.SetAppTheme(
StatusBarBehavior.StatusBarStyleProperty,
StatusBarStyle.DarkContent,
StatusBarStyle.LightContent
);
}

foreach (var behavior in page.Behaviors.OfType<NavigationBarBehavior>())
{
behavior.SetAppTheme(
NavigationBarBehavior.NavigationBarColorProperty,
Colors.White,
gray900Color
);
behavior.SetAppTheme(
NavigationBarBehavior.NavigationBarStyleProperty,
NavigationBarStyle.DarkContent,
NavigationBarStyle.LightContent
);
}
}

#if ANDROID
public static void StartForeground()
{
Expand Down
7 changes: 7 additions & 0 deletions Yosu/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
x:Class="Yosu.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:berry="https://schemas.jerry08/dotnet/2023/maui"
xmlns:local="clr-namespace:Yosu"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:views="clr-namespace:Yosu.Views"
Shell.FlyoutBehavior="Disabled"
Shell.NavBarIsVisible="False">

<Shell.Behaviors>
<toolkit:StatusBarBehavior StatusBarColor="{AppThemeBinding Light={StaticResource White}, Dark={StaticResource Black}}" StatusBarStyle="{AppThemeBinding Light=DarkContent, Dark=LightContent}" />
<berry:NavigationBarBehavior NavigationBarColor="{AppThemeBinding Light={StaticResource Gray100}, Dark={StaticResource Gray900}}" NavigationBarStyle="{AppThemeBinding Light=DarkContent, Dark=LightContent}" />
</Shell.Behaviors>

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate views:MainPage}"
Expand Down
75 changes: 40 additions & 35 deletions Yosu/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,44 +71,49 @@
VerticalOptions="Center"
WidthRequest="50" />

<cm:ContextMenuContainer
x:Name="ActionsInlineMenu"
<Border
Grid.Column="1"
Margin="0,0,24,0"
HeightRequest="40"
HeightRequest="45"
HorizontalOptions="EndAndExpand"
VerticalOptions="Center"
WidthRequest="40">
<cm:ContextMenuContainer.MenuItems>
<cm:ContextMenuItem Command="{Binding GoToHistoryCommand}" Text="History" />
<cm:ContextMenuItem Command="{Binding ShowSearchOptionsCommand}" Text="Search Options" />
<cm:ContextMenuItem Command="{Binding GoToSettingsCommand}" Text="Settings" />
<cm:ContextMenuItem Command="{Binding DonateCommand}" Text="Donate" />
</cm:ContextMenuContainer.MenuItems>
<cm:ContextMenuContainer.Content>
<ImageButton
Padding="2"
HeightRequest="30"
HorizontalOptions="EndAndExpand"
VerticalOptions="Center"
WidthRequest="30">
<ImageButton.Behaviors>
<berry:TouchBehavior
Command="{Binding BindingContext.ShowContextMenuCommand, Source={x:Reference this}}"
CommandParameter="{Binding ., Source={x:Reference ActionsInlineMenu}}"
NativeAnimation="True" />
</ImageButton.Behaviors>
<ImageButton.Source>
<FontImageSource
FontFamily="Material"
Glyph="{x:Static materialDesign:MaterialDesignIcons.Settings}"
Size="30"
Color="{AppThemeBinding Light={StaticResource Gray400},
Dark={StaticResource White}}" />
</ImageButton.Source>
</ImageButton>
</cm:ContextMenuContainer.Content>
</cm:ContextMenuContainer>
StrokeThickness="0"
WidthRequest="45">
<Border.StrokeShape>
<RoundRectangle CornerRadius="20" />
</Border.StrokeShape>
<cm:ContextMenuContainer
HorizontalOptions="Center"
ShowOnClick="True"
VerticalOptions="Center">
<cm:ContextMenuContainer.MenuItems>
<cm:ContextMenuItem Command="{Binding GoToHistoryCommand}" Text="History" />
<cm:ContextMenuItem Command="{Binding ShowSearchOptionsCommand}" Text="Search Options" />
<cm:ContextMenuItem Command="{Binding GoToSettingsCommand}" Text="Settings" />
<cm:ContextMenuItem Command="{Binding DonateCommand}" Text="Donate" />
</cm:ContextMenuContainer.MenuItems>
<cm:ContextMenuContainer.Content>
<Grid>
<Grid.Behaviors>
<berry:TouchBehavior NativeAnimation="True" />
</Grid.Behaviors>
<ImageButton
Margin="4"
HorizontalOptions="Center"
VerticalOptions="Center">
<ImageButton.Source>
<FontImageSource
FontFamily="Material"
Glyph="{x:Static materialDesign:MaterialDesignIcons.Settings}"
Size="40"
Color="{AppThemeBinding Light={StaticResource Primary},
Dark={StaticResource White}}" />
</ImageButton.Source>
</ImageButton>
</Grid>

</cm:ContextMenuContainer.Content>
</cm:ContextMenuContainer>
</Border>
</Grid>

<!-- SEARCH -->
Expand Down
8 changes: 7 additions & 1 deletion Yosu/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Views;
using Microsoft.Maui.Controls;
using Yosu.ViewModels;

Expand Down Expand Up @@ -38,6 +37,13 @@ public MainPage(MainCollectionViewModel viewModel, IPopupService popupService)
//Loaded += MainPage_Loaded;
}

protected override void OnAppearing()
{
base.OnAppearing();

App.RefreshCurrentPageBehaviors();
}

private async void MainPage_Loaded(object? sender, System.EventArgs e)
{
//var loadingPopupViewModel = new LoadingPopupViewModel();
Expand Down
17 changes: 1 addition & 16 deletions Yosu/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@
using CommunityToolkit.Maui;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Compatibility.Hosting;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.LifecycleEvents;
using Plugin.ContextMenuContainer;
using SkiaSharp.Views.Maui.Controls.Hosting;
using Woka;
using Yosu.Handlers;
using Yosu.Services;
using Yosu.Services.AlertDialog;
using Yosu.ViewModels;
using Yosu.ViewModels.Settings;
using Yosu.Views;
using Yosu.Views.Settings;
using IStatusBarStyleManager = Yosu.Utils.IStatusBarStyleManager;
using StatusBarStyleManager = Yosu.Utils.StatusBarStyleManager;

namespace Yosu;

Expand Down Expand Up @@ -47,18 +43,7 @@ public static MauiApp CreateMauiApp()
.UseSkiaSharp()
.UseBerry()
.UseMauiCompatibility()
.ConfigureMauiHandlers(handlers =>
{
#if ANDROID
handlers.AddHandler<CheckBox, MaterialCheckBoxHandler>();
handlers.AddHandler<Switch, MaterialSwitchHandler>();
#endif

handlers.AddHandler(
typeof(ContextMenuContainer),
typeof(ContextMenuContainerRenderer)
);
})
.ConfigureContextMenuContainer()
.ConfigureWorkarounds()
.ConfigureLifecycleEvents(events =>
{
Expand Down
2 changes: 1 addition & 1 deletion Yosu/ViewModels/Framework/CollectionViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public partial class CollectionViewModelBase : BaseViewModel
[ObservableProperty]
private SelectionMode _selectionMode;

public ObservableRangeCollection<object> SelectedEntities { get; } = new();
public ObservableRangeCollection<object> SelectedEntities { get; } = [];

[ObservableProperty]
private bool _isSelectAllChecked;
Expand Down
34 changes: 22 additions & 12 deletions Yosu/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace Yosu.ViewModels.Settings;

public partial class SettingsViewModel : BaseViewModel
{
private readonly IStatusBarStyleManager _statusBarStyleManager;

[ObservableProperty]
private PreferenceService _preference = default!;

Expand All @@ -23,14 +21,8 @@ public partial class SettingsViewModel : BaseViewModel
[ObservableProperty]
private string _defaultDownloadDirectory = default!;

public SettingsViewModel(
PreferenceService preferenceService,
SettingsService settingsService,
IStatusBarStyleManager statusBarStyleManager
)
public SettingsViewModel(PreferenceService preferenceService, SettingsService settingsService)
{
_statusBarStyleManager = statusBarStyleManager;

Preference = preferenceService;
Preference.Load();

Expand All @@ -39,6 +31,22 @@ IStatusBarStyleManager statusBarStyleManager

Settings.PropertyChanged += (_, _) => Settings.Save();

Preference.PropertyChanged += (s, e) =>
{
Preference.Save();

if (e.PropertyName == nameof(Preference.AppTheme))
{
App.ApplyTheme();

#if ANDROID
Platform.CurrentActivity?.Recreate();
#endif

App.RefreshCurrentPageBehaviors();
}
};

#if ANDROID
DefaultDownloadDirectory = Path.Combine(
Android
Expand All @@ -60,13 +68,15 @@ private void ThemeSelected(int index)
Preference.Save();
App.ApplyTheme();

_statusBarStyleManager.SetDefault();
#if ANDROID
Platform.CurrentActivity?.Recreate();
#endif

Application.Current.MainPage = new AppShell();
App.RefreshCurrentPageBehaviors();
}

[RelayCommand]
private async Task PickDownloadLocation()
private async Task PickDownloadLocationAsync()
{
#if ANDROID
var result = await Shell.Current.DisplayActionSheet(
Expand Down
6 changes: 1 addition & 5 deletions Yosu/Views/ContextMenus/DownloadFailedContextMenu.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
Padding="0">
<Grid>
<cm:ContextMenuContainer
x:Name="ActionsInlineMenu"
Margin="0"
Padding="0"
BackgroundColor="Transparent"
Expand Down Expand Up @@ -59,10 +58,7 @@
</Border.StrokeShape>
<Grid>
<Grid.Behaviors>
<berry:TouchBehavior
Command="{Binding Tag.ShowContextMenuCommand, Source={x:Reference this}}"
CommandParameter="{Binding ., Source={x:Reference ActionsInlineMenu}}"
NativeAnimation="True" />
<berry:TouchBehavior NativeAnimation="True" />
</Grid.Behaviors>
<Image HeightRequest="25">
<Image.Triggers>
Expand Down
6 changes: 1 addition & 5 deletions Yosu/Views/ContextMenus/DownloadingContextMenu.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
Padding="0">
<Grid>
<cm:ContextMenuContainer
x:Name="ActionsInlineMenu"
Margin="0"
Padding="0"
BackgroundColor="Transparent"
Expand Down Expand Up @@ -60,10 +59,7 @@
</Border.StrokeShape>
<Grid>
<Grid.Behaviors>
<berry:TouchBehavior
Command="{Binding Tag.ShowContextMenuCommand, Source={x:Reference this}}"
CommandParameter="{Binding ., Source={x:Reference ActionsInlineMenu}}"
NativeAnimation="True" />
<berry:TouchBehavior NativeAnimation="True" />
</Grid.Behaviors>
<Image HeightRequest="25">
<Image.Triggers>
Expand Down
6 changes: 1 addition & 5 deletions Yosu/Views/ContextMenus/InitialContextMenu.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
(Tested on Android)
-->
<cm:ContextMenuContainer
x:Name="ActionsInlineMenu"
Margin="0"
Padding="0"
BackgroundColor="Transparent"
Expand Down Expand Up @@ -66,10 +65,7 @@
</Border.StrokeShape>
<Grid>
<Grid.Behaviors>
<berry:TouchBehavior
Command="{Binding Tag.ShowContextMenuCommand, Source={x:Reference this}}"
CommandParameter="{Binding ., Source={x:Reference ActionsInlineMenu}}"
NativeAnimation="True" />
<berry:TouchBehavior NativeAnimation="True" />
</Grid.Behaviors>
<Image HeightRequest="25">
<Image.Triggers>
Expand Down
6 changes: 1 addition & 5 deletions Yosu/Views/HistoryCollectionView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
VerticalOptions="Center" />

<cm:ContextMenuContainer
x:Name="ActionsInlineMenu"
Grid.Column="2"
Margin="0,0,24,0"
HeightRequest="30"
Expand All @@ -78,10 +77,7 @@
<cm:ContextMenuContainer.Content>
<Grid>
<Grid.Behaviors>
<berry:TouchBehavior
Command="{Binding BindingContext.ShowContextMenuCommand, Source={x:Reference this}}"
CommandParameter="{Binding ., Source={x:Reference ActionsInlineMenu}}"
NativeAnimation="True" />
<berry:TouchBehavior NativeAnimation="True" />
</Grid.Behaviors>
<ImageButton
HeightRequest="30"
Expand Down

0 comments on commit 708a308

Please sign in to comment.