Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 33e24e5

Browse files
authored
Changed LoadView to be Async (#828)
* Changed LoadView to ValueTask LoadViewAsync and added summaries * Update src/CommunityToolkit/Xamarin.CommunityToolkit/Views/LazyView/LazyView.shared.cs
1 parent 32f872d commit 33e24e5

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/LazyView/BaseLazyView.shared.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
using System;
2+
using System.Threading.Tasks;
23
using Xamarin.Forms;
34
using Xamarin.Forms.Internals;
45

56
namespace Xamarin.CommunityToolkit.UI.Views
67
{
7-
[Preserve(Conditional =true)]
8+
[Preserve(Conditional = true)]
89
public abstract class BaseLazyView : ContentView, IDisposable
910
{
1011
internal static readonly BindablePropertyKey IsLoadedPropertyKey = BindableProperty.CreateReadOnly(nameof(IsLoaded), typeof(bool), typeof(BaseLazyView), default);
1112

13+
/// <summary>
14+
/// This is a read-only <see cref="BindableProperty"/> that indicates when the view is loaded.
15+
/// </summary>
1216
public static readonly BindableProperty IsLoadedProperty = IsLoadedPropertyKey.BindableProperty;
1317

18+
/// <summary>
19+
/// This is a read-only property that indicates when the view is loaded.
20+
/// </summary>
1421
public bool IsLoaded => (bool)GetValue(IsLoadedProperty);
1522

16-
internal void SetIsLoaded(bool isLoaded) => SetValue(IsLoadedPropertyKey, isLoaded);
23+
/// <summary>
24+
/// This method change the value of the <see cref="IsLoaded"/> property.
25+
/// </summary>
26+
/// <param name="isLoaded"></param>
27+
protected void SetIsLoaded(bool isLoaded) => SetValue(IsLoadedPropertyKey, isLoaded);
1728

18-
public abstract void LoadView();
29+
/// <summary>
30+
/// Use this method to do the initialization of the <see cref="View"/> and change the status IsLoaded value here.
31+
/// </summary>
32+
/// <returns><see cref="ValueTask"/></returns>
33+
public abstract ValueTask LoadViewAsync();
1934

35+
/// <summary>
36+
/// This method dispose the <see cref="Content"/> if it's <see cref="IDisposable"/>.
37+
/// </summary>
2038
public void Dispose()
2139
{
2240
if (Content is IDisposable disposable)
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1-
using Xamarin.Forms;
1+
using System.Threading.Tasks;
2+
using Xamarin.Forms;
23

34
namespace Xamarin.CommunityToolkit.UI.Views
45
{
6+
/// <summary>
7+
/// This a basic implementation of the LazyView based on <see cref="BaseLazyView"/> use this an exemple to create yours
8+
/// </summary>
9+
/// <typeparam name="TView">Any <see cref="View"/></typeparam>
510
public class LazyView<TView> : BaseLazyView where TView : View, new()
611
{
7-
public override void LoadView()
12+
/// <summary>
13+
/// This method initialize your <see cref="LazyView{TView}"/>.
14+
/// </summary>
15+
/// <returns><see cref="ValueTask"/></returns>
16+
public override ValueTask LoadViewAsync()
817
{
918
View view = new TView { BindingContext = BindingContext };
1019

1120
Content = view;
1221

1322
SetIsLoaded(true);
23+
return new ValueTask(Task.FromResult(true));
1424
}
1525
}
16-
}
26+
}

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/TabView/TabView.shared.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,8 @@ void UpdateSelectedIndex(int position, bool hasCurrentItem = false)
794794
}
795795

796796
if (!lazyView?.IsLoaded ?? false)
797-
lazyView?.LoadView();
797+
await lazyView.LoadViewAsync();
798+
798799
var currentTabItem = TabItems[position];
799800
currentTabItem.SizeChanged += OnCurrentTabItemSizeChanged;
800801
UpdateTabIndicatorPosition(currentTabItem);

0 commit comments

Comments
 (0)