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

Implement RefreshViewHandler on Windows #3299

Merged
merged 17 commits into from
Feb 24, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,18 @@
<viewmodels:RefreshViewModel />
</views:BasePage.BindingContext>
<views:BasePage.Content>
<!--
https://github.com/dotnet/maui/issues/2022
<VerticalStackLayout
Margin="12">
<Label
Text="Pull the items down to refresh the ScrollView."
Style="{StaticResource Headline}" />
<Label
Text="{Binding Items.Count, StringFormat='Number of items: {0}'}" />-->

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<VerticalStackLayout Grid.Row="0">
<VerticalStackLayout Margin="12">
<Label
Text="Pull the items down to refresh the ScrollView."
Style="{StaticResource Headline}" />
<Label Text="{Binding Items.Count, StringFormat='Number of items: {0}'}" />
</VerticalStackLayout>
<HorizontalStackLayout>
<Button FontSize="10" Text="Toggle Refresh Color" Clicked="OnToggleRefreshColorClicked"></Button>
<Button FontSize="10" Text="Toggle Background Color" Clicked="OnToggleRefreshBackgroundColorClicked"></Button>
Expand All @@ -52,36 +48,28 @@
<Button FontSize="10" Text="Toggle Refresh" Clicked="OnTriggerRefreshClicked"></Button>
<Button FontSize="10" Text="Toggle Is Enabled" Clicked="OnToggleEnabledClicked"></Button>
</HorizontalStackLayout>
<HorizontalStackLayout>
<VerticalStackLayout>
<Label FontSize="10" Text="{Binding RefreshText}"></Label>
<Label FontSize="10" Text="{Binding EnabledText}"></Label>
</HorizontalStackLayout>
</VerticalStackLayout>
</VerticalStackLayout>
<RefreshView Grid.Row="1"
x:Name="refreshView"
IsRefreshing="{Binding IsRefreshing, Mode=TwoWay}"
IsEnabled="{Binding IsEnabled, Mode=TwoWay}"
RefreshColor="Teal"
Command="{Binding RefreshCommand}">
<RefreshView
Grid.Row="1"
x:Name="refreshView"
IsRefreshing="{Binding IsRefreshing, Mode=TwoWay}"
IsEnabled="{Binding IsEnabled, Mode=TwoWay}"
RefreshColor="Teal"
Command="{Binding RefreshCommand}">
<ScrollView>
<StackLayout
BindableLayout.ItemsSource="{Binding Items}"
BindableLayout.ItemTemplate="{StaticResource ColorItemTemplate}">
</StackLayout>
</ScrollView>
<!--
https://github.com/dotnet/maui/issues/2023
<FlexLayout
<FlexLayout
Direction="Row"
Wrap="Wrap"
AlignItems="Center"
AlignContent="Center"
BindableLayout.ItemsSource="{Binding Items}"
BindableLayout.ItemTemplate="{StaticResource ColorItemTemplate}" />-->

BindableLayout.ItemTemplate="{StaticResource ColorItemTemplate}" />
</ScrollView>
</RefreshView>
</Grid>

<!--</VerticalStackLayout>-->
</views:BasePage.Content>
</views:BasePage>
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void AddItems()
{
Items.Add(new RefreshItem
{
Color = Color.FromRgb(_random.Next(0, 255), _random.Next(0, 255), _random.Next(0, 255)),
Color = Color.FromRgb(_random.NextDouble(), _random.NextDouble(), _random.NextDouble()),
Name = $"Item {_itemNumber++}"
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static IMauiHandlersCollection AddMauiControlsHandlers(this IMauiHandlers
handlersCollection.AddHandler<ImageButton, ImageButtonHandler>();
handlersCollection.AddHandler<IndicatorView, IndicatorViewHandler>();
handlersCollection.AddHandler<RadioButton, RadioButtonHandler>();
handlersCollection.AddHandler<RefreshView, RefreshViewHandler>();
handlersCollection.AddHandler<SwipeItem, SwipeItemMenuItemHandler>();
handlersCollection.AddHandler<SwipeView, SwipeViewHandler>();

Expand All @@ -60,7 +61,6 @@ public static IMauiHandlersCollection AddMauiControlsHandlers(this IMauiHandlers
handlersCollection.AddHandler<MenuBarItem, MenuBarItemHandler>();

#if ANDROID || IOS
handlersCollection.AddHandler<RefreshView, RefreshViewHandler>();
handlersCollection.AddHandler<SwipeItemView, SwipeItemViewHandler>();
#endif
#if WINDOWS || ANDROID
Expand Down
114 changes: 103 additions & 11 deletions src/Core/src/Handlers/RefreshView/RefreshViewHandler.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,127 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.Graphics;
using Microsoft.UI.Xaml.Controls;
using Windows.Foundation;
using WBrush = Microsoft.UI.Xaml.Media.Brush;

namespace Microsoft.Maui.Handlers
{
public partial class RefreshViewHandler : ViewHandler<IRefreshView, RefreshContainer>
{
bool _isLoaded;
Deferral? _refreshCompletionDeferral;

protected override RefreshContainer CreatePlatformView()
{
return new RefreshContainer();
return new RefreshContainer
{
PullDirection = RefreshPullDirection.TopToBottom
};
}

[MissingMapper]
public static void MapIsRefreshing(IRefreshViewHandler handler, IRefreshView refreshView)
protected override void ConnectHandler(RefreshContainer nativeView)
{
nativeView.Loaded += OnLoaded;
nativeView.RefreshRequested += OnRefresh;

base.ConnectHandler(nativeView);
}

[MissingMapper]
public static void MapContent(IRefreshViewHandler handler, IRefreshView refreshView)
protected override void DisconnectHandler(RefreshContainer nativeView)
{
nativeView.Loaded -= OnLoaded;
nativeView.RefreshRequested -= OnRefresh;

CompleteRefresh();

base.DisconnectHandler(nativeView);
}

[MissingMapper]
public static void MapIsRefreshing(IRefreshViewHandler handler, IRefreshView refreshView)
=> (handler as RefreshViewHandler)?.UpdateIsRefreshing();

public static void MapContent(IRefreshViewHandler handler, IRefreshView refreshView)
=> UpdateContent(handler);

public static void MapRefreshColor(IRefreshViewHandler handler, IRefreshView refreshView)
=> UpdateRefreshColor(handler);

public static void MapRefreshViewBackground(IRefreshViewHandler handler, IView view)
=> UpdateBackground(handler);

void UpdateIsRefreshing()
{
if (!_isLoaded)
return;

if (!VirtualView?.IsRefreshing ?? false)
CompleteRefresh();
else if (_refreshCompletionDeferral == null)
PlatformView?.RequestRefresh();
}

[MissingMapper]
public static void MapRefreshViewBackground(IRefreshViewHandler handler, IView view)
static void UpdateContent(IRefreshViewHandler handler)
{
handler.PlatformView.Content =
handler.VirtualView.Content.ToPlatform(handler.MauiContext!);
}

static void UpdateRefreshColor(IRefreshViewHandler handler)
{
if (handler.VirtualView == null || handler.PlatformView?.Visualizer == null)
return;

handler.PlatformView.Visualizer.Foreground = handler.VirtualView.RefreshColor != null
? handler.VirtualView.RefreshColor.ToPlatform()
: (WBrush)UI.Xaml.Application.Current.Resources["DefaultTextForegroundThemeBrush"];
}

static void UpdateBackground(IRefreshViewHandler handler)
{
if (handler.PlatformView?.Visualizer == null)
return;

if (handler.VirtualView.Background != null)
handler.PlatformView.Visualizer.Background = handler.VirtualView.Background.ToPlatform();
}

// Telling the refresh to start before the control has been sized
// causes no refresh circle to show up
void OnLoaded(object sender, object args)
{
var refreshControl = sender as RefreshContainer;

if (refreshControl == null || MauiContext == null)
return;

refreshControl.Loaded -= OnLoaded;
MauiContext.Services
.GetRequiredService<IDispatcher>()
.Dispatch(() =>
{
_isLoaded = true;
UpdateIsRefreshing();
});
}

void OnRefresh(object sender, RefreshRequestedEventArgs args)
{
CompleteRefresh();
_refreshCompletionDeferral = args.GetDeferral();

if (VirtualView != null)
VirtualView.IsRefreshing = true;
}

void CompleteRefresh()
{
if (_refreshCompletionDeferral != null)
{
_refreshCompletionDeferral.Complete();
_refreshCompletionDeferral.Dispose();
_refreshCompletionDeferral = null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ protected override MauiRefreshView CreatePlatformView()
protected override void ConnectHandler(MauiRefreshView platformView)
{
platformView.RefreshControl.ValueChanged += OnRefresh;

base.ConnectHandler(platformView);
}

protected override void DisconnectHandler(MauiRefreshView platformView)
{
platformView.RefreshControl.ValueChanged -= OnRefresh;

base.DisconnectHandler(platformView);
}

Expand Down Expand Up @@ -61,6 +63,5 @@ static void UpdateRefreshColor(IRefreshViewHandler handler)
if (color != null)
handler.PlatformView.RefreshControl.TintColor = color;
}

}
}