Last active
June 27, 2019 14:55
-
-
Save ilyaqznetsow/c848fff720706a78ff27f10cf9ca4057 to your computer and use it in GitHub Desktop.
LazyView with IsLoading and Fade animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LazyPage : ContentPage { | |
LazyView<EntryView> lazyView; | |
public LazyPage() { | |
Content = new LazyView<EntryView>{IsLoading = true}.Assign(out lazyView); | |
} | |
protected override async void OnAppearing() { | |
base.OnAppearing(); | |
await Task.Delay(50); | |
lazyView.IsLoading = false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ORIGINAL FROM https://github.com/roubachof/Sharpnado.Presentation.Forms/blob/master/Sharpnado.Presentation.Forms/CustomViews/LazyView.cs | |
public interface ILazyView { | |
View Content { get; set; } | |
Color AccentColor { get; } | |
bool IsLoaded { get; } | |
void LoadView(); | |
} | |
public class BindingItem { | |
public BindableProperty TargetProperty { get; set; } | |
public string SourcePropertyName { get; set; } | |
public BindingMode Mode { get; set; } | |
public IValueConverter Converter { get; set; } | |
public object ConverterParameter { get; set; } | |
public string StringFormat { get; set; } | |
public object Source { get; set; } | |
public BindingItem(BindableProperty targetProperty, | |
string sourcePropertyName = null, BindingMode mode = BindingMode.Default, | |
IValueConverter converter = null, object converterParameter = null, string stringFormat = null, | |
object source = null) { | |
TargetProperty = targetProperty; | |
SourcePropertyName = sourcePropertyName; | |
Mode = mode; | |
Converter = converter; | |
ConverterParameter = converterParameter; | |
StringFormat = stringFormat; | |
Source = source; | |
} | |
} | |
public abstract class ALazyView : ContentView, ILazyView, IDisposable { | |
public static readonly BindableProperty AccentColorProperty = BindableProperty.Create( | |
nameof(AccentColor), | |
typeof(Color), | |
typeof(ILazyView), | |
Color.Accent, | |
propertyChanged: AccentColorChanged); | |
public static readonly BindableProperty IsLoadingProperty = BindableProperty.Create( | |
nameof(IsLoading), | |
typeof(bool), | |
typeof(ILazyView), | |
propertyChanged: IsLoadingChanged); | |
public Color AccentColor { | |
get => (Color)GetValue(AccentColorProperty); | |
set => SetValue(AccentColorProperty, value); | |
} | |
public bool IsLoading { | |
get => (bool)GetValue(IsLoadingProperty); | |
set => SetValue(IsLoadingProperty, value); | |
} | |
public static readonly BindableProperty BindingsProperty = BindableProperty.Create( | |
nameof(Bindings), | |
typeof(IList<BindingItem>), | |
typeof(ILazyView) | |
); | |
public IList<BindingItem> Bindings { | |
get => (IList<BindingItem>)GetValue(BindingsProperty); | |
set => SetValue(BindingsProperty, value); | |
} | |
public bool IsLoaded { get; protected set; } | |
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; | |
} | |
} | |
private static void AccentColorChanged(BindableObject bindable, object oldvalue, object newvalue) { | |
var lazyView = (ILazyView)bindable; | |
if (lazyView.Content is ActivityIndicator activityIndicator) { | |
activityIndicator.Color = (Color)newvalue; | |
} | |
} | |
private static async void IsLoadingChanged(BindableObject bindable, object oldvalue, object newvalue) { | |
var lazyView = (ILazyView)bindable; | |
bool isLoading = (bool)newvalue; | |
if (isLoading) { | |
if (lazyView.Content != null) | |
await lazyView.Content.FadeTo(0); | |
lazyView.Content = new ActivityIndicator { | |
Color = lazyView.AccentColor, | |
HorizontalOptions = LayoutOptions.Center, | |
VerticalOptions = LayoutOptions.Center, | |
IsRunning = true, | |
}; | |
} else { | |
lazyView.LoadView(); | |
} | |
} | |
} | |
public class LazyView<TView> : ALazyView | |
where TView : View, new() { | |
public override async void LoadView() { | |
IsLoaded = true; | |
View view = new TView { BindingContext = BindingContext }; | |
view.Opacity = 0; | |
if (Bindings?.Count > 0) | |
foreach (var b in Bindings) | |
view.SetBinding( | |
b.TargetProperty, new Binding( | |
b.SourcePropertyName, | |
b.Mode, | |
b.Converter, | |
b.ConverterParameter, | |
b.StringFormat, | |
b.Source | |
)); | |
await Task.Delay(150); | |
Content = view; | |
await Content.FadeTo(1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SamplePage : ContentPage { | |
public SamplePage() { | |
//custom view is any of type View | |
Content = new LazyView<CustomView>() { | |
Bindings = new List<BindingItem> { new BindingItem(CustomView.SomeProperty, "SampleCommandPropertyName", source:BindingContext)} | |
//Bind extension from https://github.com/VincentH-Net/CSharpForMarkup | |
.Bind(LazyView<CustomView>.IsLoadingProperty, IsLoadingProperty.PropertyName, source: this) | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment