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

Implemented LazyView #796

Merged
merged 3 commits into from
Feb 3, 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
57 changes: 57 additions & 0 deletions samples/XCT.Sample/Pages/Views/TabView/LazyTabPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8" ?>
<pages:BasePage
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.TabView.LazyTabPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.TabView"
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
xmlns:vm="clr-namespace:Xamarin.CommunityToolkit.Sample.ViewModels.Views.Tabs"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit">
<pages:BasePage.Content>
<Grid>
<xct:TabView
TabContentBackgroundColor="Yellow"
TabIndicatorColor="Yellow"
TabStripBackgroundColor="Blue"
TabStripHeight="60"
TabStripPlacement="Bottom">
<xct:TabViewItem
FontSize="12"
Icon="triangle.png"
Text="Tab Normal 1"
TextColor="White"
TextColorSelected="Yellow">
<local:NormalTestView />
</xct:TabViewItem>
<xct:TabViewItem
FontSize="12"
Icon="circle.png"
Text="Tab Lazy 2"
TextColor="White"
TextColorSelected="Yellow">
<xct:LazyView
x:TypeArguments="local:LazyTestView"
BindingContext="{x:Static vm:LazyTestViewModel.Current}"
IsLoaded="{Binding Loaded}" />
</xct:TabViewItem>
<xct:TabViewItem
FontSize="12"
Icon="triangle.png"
Text="Tab Normal 3"
TextColor="White"
TextColorSelected="Yellow">
<local:NormalTestView />
</xct:TabViewItem>

<xct:TabViewItem
FontSize="12"
Icon="triangle.png"
Text="Tab Lazy 4"
TextColor="White"
TextColorSelected="Yellow">
<xct:LazyView x:TypeArguments="local:LazyTestView" BindingContext="{x:Static vm:LazyTestViewModel.Current}" />
</xct:TabViewItem>
</xct:TabView>
</Grid>
</pages:BasePage.Content>
</pages:BasePage>
7 changes: 7 additions & 0 deletions samples/XCT.Sample/Pages/Views/TabView/LazyTabPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Xamarin.CommunityToolkit.Sample.Pages.Views.TabView
{
public partial class LazyTabPage
{
public LazyTabPage() => InitializeComponent();
}
}
17 changes: 17 additions & 0 deletions samples/XCT.Sample/Pages/Views/TabView/LazyTestView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.TabView.LazyTestView"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit">
<ContentView.Content>
<Grid RowDefinitions="auto, *">
<Label
FontSize="Title"
HorizontalTextAlignment="Center"
Text="{Binding Title}"
TextColor="Black" />
<xct:UniformGrid x:Name="uniformGrid" Grid.Row="1" />
</Grid>
</ContentView.Content>
</ContentView>
29 changes: 29 additions & 0 deletions samples/XCT.Sample/Pages/Views/TabView/LazyTestView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Xamarin.CommunityToolkit.Sample.ViewModels.Views.Tabs;
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.Pages.Views.TabView
{
public partial class LazyTestView : ContentView
{
public LazyTestView()
{
InitializeComponent();

Build();
NormalTestViewModel.Current.LoadedViews += "LazyView Loaded \n";
}

void Build()
{
for (var i = 0; i < 117; i++)
{
var box = new BoxView
{
BackgroundColor = i % 2 == 0 ? Color.Blue : Color.Fuchsia
};

uniformGrid.Children.Add(box);
}
}
}
}
11 changes: 11 additions & 0 deletions samples/XCT.Sample/Pages/Views/TabView/NormalTestView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.TabView.NormalTestView"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<ContentView.Content>
<StackLayout>
<Label Text="{Binding LoadedViews}" />
</StackLayout>
</ContentView.Content>
</ContentView>
16 changes: 16 additions & 0 deletions samples/XCT.Sample/Pages/Views/TabView/NormalTestView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Xamarin.CommunityToolkit.Sample.ViewModels.Views.Tabs;
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.Pages.Views.TabView
{
public partial class NormalTestView : ContentView
{
public NormalTestView()
{
InitializeComponent();
BindingContext = NormalTestViewModel.Current;

NormalTestViewModel.Current.LoadedViews += "NormalTestLoaded \n";
}
}
}
5 changes: 4 additions & 1 deletion samples/XCT.Sample/ViewModels/Views/TabViewViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public class TabViewViewModel : BaseGalleryViewModel
"Customize the tabs width"),

new SectionModel(typeof(NoContentPage), "Tab without Content",
"Only the TabStrip is visible")
"Only the TabStrip is visible"),

new SectionModel(typeof(LazyTabPage), "LazyLoadingTab",
"See how you can implement LazyViews that are loaded just when you navigate to them"),
};
}
}
27 changes: 27 additions & 0 deletions samples/XCT.Sample/ViewModels/Views/Tabs/LazyTestViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Xamarin.CommunityToolkit.ObjectModel;

namespace Xamarin.CommunityToolkit.Sample.ViewModels.Views.Tabs
{
sealed class LazyTestViewModel : ObservableObject
{
public static LazyTestViewModel Current { get; } = new LazyTestViewModel();

string title;

public string Title
{
get => title;
set => SetProperty(ref title, value);
}

bool loaded;

public bool Loaded
{
get => loaded;
set => SetProperty(ref loaded, value);
}

public LazyTestViewModel() => Title = "Lazy Tab Sample";
}
}
17 changes: 17 additions & 0 deletions samples/XCT.Sample/ViewModels/Views/Tabs/NormalTestViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Xamarin.CommunityToolkit.ObjectModel;

namespace Xamarin.CommunityToolkit.Sample.ViewModels.Views.Tabs
{
sealed class NormalTestViewModel : ObservableObject
{
public static NormalTestViewModel Current { get; } = new NormalTestViewModel();

string loadedViews;

public string LoadedViews
{
get => loadedViews;
set => SetProperty(ref loadedViews, value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using Xamarin.Forms;
using Xamarin.Forms.Internals;

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

public static readonly BindableProperty IsLoadedProperty = IsLoadedPropertyKey.BindableProperty;

public bool IsLoaded => (bool)GetValue(IsLoadedProperty);

internal void SetIsLoaded(bool isLoaded) => SetValue(IsLoadedPropertyKey, isLoaded);

public abstract void LoadView();

public void Dispose()
{
if (Content is IDisposable disposable)
disposable.Dispose();
}

protected override void OnBindingContextChanged()
{
if (Content != null && !(Content is ActivityIndicator))
Content.BindingContext = BindingContext;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.UI.Views
{
public class LazyView<TView> : BaseLazyView where TView : View, new()
{
public override void LoadView()
{
View view = new TView { BindingContext = BindingContext };

Content = view;

SetIsLoaded(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ public void Dispose()

if (TabItems != null)
TabItems.CollectionChanged -= OnTabItemsCollectionChanged;

var lazyView = (((TabViewItem)contentContainer.CurrentItem).Content as BaseLazyView) ?? (TabItems[SelectedIndex].Content as BaseLazyView);
lazyView?.Dispose();
}

public ObservableCollection<TabViewItem> TabItems { get; set; }
Expand Down Expand Up @@ -775,6 +778,8 @@ void UpdateSelectedIndex(int position, bool hasCurrentItem = false)

var tabViewItem = TabItems[SelectedIndex];

var lazyView = (currentItem?.Content as BaseLazyView) ?? (tabViewItem?.Content as BaseLazyView);

contentIndex = contentTabItems.IndexOf(currentItem ?? tabViewItem);
tabStripIndex = TabItems.IndexOf(currentItem ?? tabViewItem);

Expand All @@ -788,6 +793,8 @@ void UpdateSelectedIndex(int position, bool hasCurrentItem = false)
TabItems[index].IsSelected = false;
}

if (!lazyView?.IsLoaded ?? false)
lazyView?.LoadView();
var currentTabItem = TabItems[position];
currentTabItem.SizeChanged += OnCurrentTabItemSizeChanged;
UpdateTabIndicatorPosition(currentTabItem);
Expand Down