Created
May 8, 2019 14:08
-
-
Save kvandake/0a524601253f86949582d535c6177e32 to your computer and use it in GitHub Desktop.
BaseContentLoadedViewModel BL
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 System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
public class ContentLoadedProperties : EmptyDataSetProperties | |
{ | |
private ContentLoadedType type; | |
public ContentLoadedType Type | |
{ | |
get { return this.type; } | |
set { this.SetProperty(ref this.type, value); } | |
} | |
public enum ContentLoadedType | |
{ | |
Loading, | |
Loaded, | |
Empty, | |
} | |
} | |
public interface IContentLoadedViewModel | |
{ | |
ContentLoadedProperties ContentLoadedProperties { get; } | |
} | |
public abstract class BaseContentLoadedViewModel<TModel> : BaseViewModel, IContentLoadedViewModel where TModel : class | |
{ | |
private ContentLoadedProperties contentLoadedProperties; | |
private TModel model; | |
private readonly CancellationTokenSource cancellationTokenSource; | |
public BaseContentLoadedViewModel() | |
{ | |
this.cancellationTokenSource = new CancellationTokenSource(); | |
} | |
public ContentLoadedProperties ContentLoadedProperties | |
{ | |
get => this.contentLoadedProperties ?? (this.contentLoadedProperties = new ContentLoadedProperties()); | |
set => this.SetProperty(ref this.contentLoadedProperties, value); | |
} | |
public TModel Model | |
{ | |
get => this.model; | |
set => this.SetProperty(ref this.model, value); | |
} | |
protected abstract string EmptyModelTitle { get; } | |
protected virtual bool LoadModelOnStart => true; | |
// protected virtual bool ShowLoadingWhenLoadModel => true; | |
public override void Start() | |
{ | |
base.Start(); | |
if (this.LoadModelOnStart) | |
{ | |
this.LoadModelInBackground(); | |
} | |
} | |
protected abstract Task<TModel> LoadModel(CancellationToken cancellationToken); | |
protected virtual async Task ReloadModel() | |
{ | |
try | |
{ | |
this.ContentLoadedProperties.Type = ContentLoadedProperties.ContentLoadedType.Loading; | |
var newModel = await PollyWrapper.Execute(this.LoadModel, this.LoadModelFromCache, this.cancellationTokenSource.Token); | |
this.SetModel(newModel); | |
if (newModel == null) | |
{ | |
this.ContentLoadedProperties.Title = this.EmptyModelTitle; | |
this.ContentLoadedProperties.Type = ContentLoadedProperties.ContentLoadedType.Empty; | |
} | |
else | |
{ | |
this.ContentLoadedProperties.Type = ContentLoadedProperties.ContentLoadedType.Loaded; | |
} | |
} | |
catch (Exception ex) | |
{ | |
this.ContentLoadedProperties.Title = ex.Message; | |
this.ContentLoadedProperties.Type = ContentLoadedProperties.ContentLoadedType.Empty; | |
} | |
} | |
protected virtual TModel LoadModelFromCache(Exception ex) | |
{ | |
return default(TModel); | |
} | |
protected virtual async void LoadModelInBackground() | |
{ | |
await this.ReloadModel(); | |
} | |
protected virtual void SetModel(TModel model) | |
{ | |
this.Model = model; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment