Created
January 31, 2021 18:52
-
-
Save Hackmodford/c9440d4d07235452cea7dcdc23c70044 to your computer and use it in GitHub Desktop.
A MvxViewModel that works with ReactiveUI
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
using System; | |
using System.Threading.Tasks; | |
using MvvmCross.ViewModels; | |
using ReactiveUI; | |
namespace ReactiveUI.Mvx | |
{ | |
public abstract class ReactiveMvxViewModel : ReactiveObject, IMvxViewModel | |
{ | |
public virtual void ViewCreated() | |
{ | |
} | |
public virtual void ViewAppearing() | |
{ | |
} | |
public virtual void ViewAppeared() | |
{ | |
} | |
public virtual void ViewDisappearing() | |
{ | |
} | |
public virtual void ViewDisappeared() | |
{ | |
} | |
public virtual void ViewDestroy(bool viewFinishing = true) | |
{ | |
} | |
public void Init(IMvxBundle parameters) | |
{ | |
InitFromBundle(parameters); | |
} | |
public void ReloadState(IMvxBundle state) | |
{ | |
ReloadFromBundle(state); | |
} | |
public virtual void Start() | |
{ | |
} | |
public void SaveState(IMvxBundle state) | |
{ | |
SaveStateToBundle(state); | |
} | |
protected virtual void InitFromBundle(IMvxBundle parameters) | |
{ | |
} | |
protected virtual void ReloadFromBundle(IMvxBundle state) | |
{ | |
} | |
protected virtual void SaveStateToBundle(IMvxBundle bundle) | |
{ | |
} | |
public virtual void Prepare() | |
{ | |
} | |
public virtual Task Initialize() | |
{ | |
return Task.FromResult(true); | |
} | |
private MvxNotifyTask _initializeTask; | |
public MvxNotifyTask InitializeTask | |
{ | |
get => _initializeTask; | |
set => this.RaiseAndSetIfChanged(ref _initializeTask, value); | |
} | |
} | |
public abstract class ReactiveMvxViewModel<TParameter> : ReactiveMvxViewModel, IMvxViewModel<TParameter> | |
where TParameter : notnull | |
{ | |
public abstract void Prepare(TParameter parameter); | |
} | |
//TODO: Not possible to name MvxViewModel, name is MvxViewModelResult for now | |
public abstract class ReactiveMvxViewModelResult<TResult> : ReactiveMvxViewModel, IMvxViewModelResult<TResult> | |
where TResult : notnull | |
{ | |
public TaskCompletionSource<object> CloseCompletionSource { get; set; } | |
public override void ViewDestroy(bool viewFinishing = true) | |
{ | |
if (viewFinishing && CloseCompletionSource != null && !CloseCompletionSource.Task.IsCompleted && !CloseCompletionSource.Task.IsFaulted) | |
CloseCompletionSource?.TrySetCanceled(); | |
base.ViewDestroy(viewFinishing); | |
} | |
} | |
public abstract class ReactiveMvxViewModel<TParameter, TResult> : ReactiveMvxViewModelResult<TResult>, IMvxViewModel<TParameter, TResult> | |
where TParameter : notnull | |
where TResult : notnull | |
{ | |
public abstract void Prepare(TParameter parameter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment