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

Changed LoadView to be Async #828

Merged
merged 2 commits into from
Feb 4, 2021
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
@@ -1,22 +1,40 @@
using System;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Internals;

namespace Xamarin.CommunityToolkit.UI.Views
{
[Preserve(Conditional =true)]
[Preserve(Conditional = true)]
public abstract class BaseLazyView : ContentView, IDisposable
{
internal static readonly BindablePropertyKey IsLoadedPropertyKey = BindableProperty.CreateReadOnly(nameof(IsLoaded), typeof(bool), typeof(BaseLazyView), default);

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

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

internal void SetIsLoaded(bool isLoaded) => SetValue(IsLoadedPropertyKey, isLoaded);
/// <summary>
/// This method change the value of the <see cref="IsLoaded"/> property.
/// </summary>
/// <param name="isLoaded"></param>
protected void SetIsLoaded(bool isLoaded) => SetValue(IsLoadedPropertyKey, isLoaded);

public abstract void LoadView();
/// <summary>
/// Use this method to do the initialization of the <see cref="View"/> and change the status IsLoaded value here.
/// </summary>
/// <returns><see cref="ValueTask"/></returns>
public abstract ValueTask LoadViewAsync();

/// <summary>
/// This method dispose the <see cref="Content"/> if it's <see cref="IDisposable"/>.
/// </summary>
public void Dispose()
{
if (Content is IDisposable disposable)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
using Xamarin.Forms;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.UI.Views
{
/// <summary>
/// This a basic implementation of the LazyView based on <see cref="BaseLazyView"/> use this an exemple to create yours
/// </summary>
/// <typeparam name="TView">Any <see cref="View"/></typeparam>
public class LazyView<TView> : BaseLazyView where TView : View, new()
{
public override void LoadView()
/// <summary>
/// This method initialize your <see cref="LazyView{TView}"/>.
/// </summary>
/// <returns><see cref="ValueTask"/></returns>
public override ValueTask LoadViewAsync()
{
View view = new TView { BindingContext = BindingContext };

Content = view;

SetIsLoaded(true);
return new ValueTask(Task.FromResult(true));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,8 @@ void UpdateSelectedIndex(int position, bool hasCurrentItem = false)
}

if (!lazyView?.IsLoaded ?? false)
lazyView?.LoadView();
await lazyView.LoadViewAsync();

var currentTabItem = TabItems[position];
currentTabItem.SizeChanged += OnCurrentTabItemSizeChanged;
UpdateTabIndicatorPosition(currentTabItem);
Expand Down