Created
May 8, 2019 14:12
-
-
Save kvandake/abf1bd0917276b5defc4e0d0b61a1cae to your computer and use it in GitHub Desktop.
BaseContentLoadedView for Xamarin iOS
This file contains hidden or 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
namespace <Namespace> | |
{ | |
using CoreAnimation; | |
using Foundation; | |
using MvvmCross.Binding.BindingContext; | |
using MvvmCross.Platform; | |
using UIKit; | |
public abstract class BaseContentLoadedView<TViewModel> : BaseView<TViewModel> | |
where TViewModel : class, IBaseViewModel, IContentLoadedViewModel | |
{ | |
private ContentLoadedProperties contentLoadedProperties; | |
private UIActivityIndicatorView indicatorView; | |
private UILabel titleLabel; | |
private UILabel descriptionLabel; | |
private UIView emptyContainerView; | |
private ContentLoadedProperties.ContentLoadedType contentLoadedType; | |
private string emptyTitle; | |
private string emptyButtonTitle; | |
private string emptyDescription; | |
private UIButton emptyButton; | |
protected BaseContentLoadedView(string nibName, NSBundle bundle) | |
: base(nibName, bundle) | |
{ | |
} | |
protected BaseContentLoadedView() | |
{ | |
} | |
protected abstract UIView ContentProcessingView { get; } | |
protected abstract UIView ContentView { get; } | |
public ContentLoadedProperties ContentLoadedProperties | |
{ | |
get => this.contentLoadedProperties; | |
set => this.SetContentLoadedProperties(value); | |
} | |
public ContentLoadedProperties.ContentLoadedType ContentLoadedType | |
{ | |
get => this.contentLoadedType; | |
set => this.SetContentLoadedType(value); | |
} | |
public string EmptyTitle | |
{ | |
get => this.emptyTitle; | |
set => this.SetEmptyTitle(value); | |
} | |
public string EmptyButtonTitle | |
{ | |
get => this.emptyButtonTitle; | |
set => this.SetEmptyButtonTitle(value); | |
} | |
public string EmptyDescription | |
{ | |
get => this.emptyDescription; | |
set => this.SetEmptyDescription(value); | |
} | |
protected override void Binding() | |
{ | |
base.Binding(); | |
if (this.ContentProcessingView == null || this.ContentView == null) | |
{ | |
return; | |
} | |
this.ConfigureContentProcessingView(this.ContentProcessingView); | |
var set = this.CreateBindingSet<BaseContentLoadedView<TViewModel>, TViewModel>(); | |
set.Bind(this).For(v => v.ContentLoadedProperties).To(vm => vm.ContentLoadedProperties); | |
set.Bind(this).For(v => v.ContentLoadedType).To(vm => vm.ContentLoadedProperties.Type); | |
set.Bind(this).For(v => v.EmptyTitle).To(vm => vm.ContentLoadedProperties.Title); | |
set.Bind(this).For(v => v.EmptyDescription).To(vm => vm.ContentLoadedProperties.Description); | |
set.Bind(this.emptyButton).To(vm => vm.ContentLoadedProperties.RetryButtonCommand); | |
set.Bind(this).For(v => v.EmptyButtonTitle).To(vm => vm.ContentLoadedProperties.RetryButtonTitle); | |
set.Apply(); | |
} | |
protected void SetContentLoadedProperties(ContentLoadedProperties value) | |
{ | |
this.contentLoadedProperties = value; | |
if (value == null) | |
{ | |
return; | |
} | |
} | |
protected void SetContentLoadedType(ContentLoadedProperties.ContentLoadedType value) | |
{ | |
this.contentLoadedType = value; | |
var fromView = this.ContentView; | |
var toView = this.ContentProcessingView; | |
switch (value) | |
{ | |
case ContentLoadedProperties.ContentLoadedType.Loading: | |
this.indicatorView.StartAnimating(); | |
this.emptyContainerView.Hidden = true; | |
break; | |
case ContentLoadedProperties.ContentLoadedType.Loaded: | |
this.indicatorView.StopAnimating(); | |
fromView = this.ContentProcessingView; | |
toView = this.ContentView; | |
break; | |
case ContentLoadedProperties.ContentLoadedType.Empty: | |
this.indicatorView.StopAnimating(); | |
this.emptyContainerView.Hidden = false; | |
break; | |
default: | |
break; | |
} | |
if (value == ContentLoadedProperties.ContentLoadedType.Loaded) | |
{ | |
fromView.Alpha = 1; | |
toView.Alpha = 0; | |
UIView.AnimateNotify(CATransaction.AnimationDuration, () => | |
{ | |
fromView.Alpha = 0; | |
toView.Alpha = 1; | |
}, finished => | |
{ | |
fromView.Hidden = true; | |
toView.Hidden = false; | |
}); | |
} | |
else | |
{ | |
fromView.Hidden = true; | |
toView.Hidden = false; | |
} | |
} | |
protected void SetEmptyTitle(string value) | |
{ | |
this.emptyTitle = value; | |
if (Mvx.TryResolve(out IEmptyDataSetSourceDelegate dataSetSourceDelegate)) | |
{ | |
if (!string.IsNullOrEmpty(value)) | |
{ | |
this.titleLabel.AttributedText = dataSetSourceDelegate.GetTitle(value); | |
return; | |
} | |
} | |
this.titleLabel.Text = value; | |
} | |
protected void SetEmptyButtonTitle(string value) | |
{ | |
this.emptyButtonTitle = value; | |
var isEmpty = string.IsNullOrEmpty(value); | |
this.emptyButton.Hidden = isEmpty; | |
this.emptyButton.SetTitle(value, UIControlState.Normal); | |
if (!isEmpty && Mvx.TryResolve(out IEmptyDataSetSourceDelegate dataSetSourceDelegate)) | |
{ | |
this.emptyButton.SetAttributedTitle(dataSetSourceDelegate.GetButtonAttributedString(value), UIControlState.Normal); | |
return; | |
} | |
this.emptyButton.SetTitle(value, UIControlState.Normal); | |
} | |
protected void SetEmptyDescription(string value) | |
{ | |
this.emptyDescription = value; | |
var isEmpty = string.IsNullOrEmpty(value); | |
this.descriptionLabel.Hidden = isEmpty; | |
if (!isEmpty && Mvx.TryResolve(out IEmptyDataSetSourceDelegate dataSetSourceDelegate)) | |
{ | |
this.descriptionLabel.AttributedText = dataSetSourceDelegate.GetDescription(value); | |
return; | |
} | |
this.descriptionLabel.Text = value; | |
} | |
private void ConfigureContentProcessingView(UIView contentView) | |
{ | |
this.indicatorView = new UIActivityIndicatorView(Mvx.TryResolve(out IEmptyDataSetSourceDelegate dataSetSourceDelegate) | |
? dataSetSourceDelegate.RefreshableActivityIndicatorViewStyle | |
: UIActivityIndicatorViewStyle.Gray) | |
{ | |
TranslatesAutoresizingMaskIntoConstraints = false, | |
HidesWhenStopped = true | |
}; | |
this.titleLabel = new UILabel | |
{ | |
TranslatesAutoresizingMaskIntoConstraints = false, | |
Lines = 0 | |
}; | |
this.descriptionLabel = new UILabel | |
{ | |
TranslatesAutoresizingMaskIntoConstraints = false, | |
Lines = 0 | |
}; | |
var textContainerView = new UIView | |
{ | |
TranslatesAutoresizingMaskIntoConstraints = false | |
}; | |
this.emptyButton = new UIButton(UIButtonType.System) | |
{ | |
TranslatesAutoresizingMaskIntoConstraints = false, | |
TintColor = this.ThemeManager().Colors.TintColor | |
}; | |
this.emptyContainerView = new UIView | |
{ | |
TranslatesAutoresizingMaskIntoConstraints = false | |
}; | |
// textContainerView | |
textContainerView.AddSubviews(this.titleLabel, this.descriptionLabel); | |
this.titleLabel.SetFillXContraintTo(textContainerView); | |
this.descriptionLabel.SetFillXContraintTo(textContainerView); | |
textContainerView.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|[titleLabel]-2-[descriptionLabel]|", 0, "titleLabel", this.titleLabel, "descriptionLabel", this.descriptionLabel)); | |
// emptyContainerView | |
this.emptyContainerView.AddSubviews(textContainerView, this.emptyButton); | |
this.emptyContainerView.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|[textContainerView]-2-[emptyButton]|", 0, "textContainerView", textContainerView, "emptyButton", this.emptyButton)); | |
this.emptyButton.SetCenterXContraintTo(this.emptyContainerView); | |
textContainerView.SetFillXContraintTo(this.emptyContainerView); | |
// contentView | |
contentView.AddSubviews(this.indicatorView, this.emptyContainerView); | |
this.indicatorView.SetCenterContraintTo(contentView); | |
this.emptyContainerView.SetFillXContraintTo(contentView, 8); | |
this.emptyContainerView.SetCenterYContraintTo(contentView); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment