Пример #1
0
        /// <summary>
        /// Setup our suspension driver for a class derived off ISuspensionHost interface.
        /// This will make your suspension host respond to suspend and resume requests.
        /// </summary>
        /// <param name="item">The suspension host.</param>
        /// <param name="driver">The suspension driver.</param>
        /// <returns>A disposable which will stop responding to Suspend and Resume requests.</returns>
        public static IDisposable SetupDefaultSuspendResume(this ISuspensionHost item, ISuspensionDriver?driver = null)
        {
            if (item is null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            var ret = new CompositeDisposable();

            driver ??= Locator.Current.GetService <ISuspensionDriver>();

            ret.Add(item.ShouldInvalidateState
                    .SelectMany(_ => driver.InvalidateState())
                    .LoggedCatch(item, Observables.Unit, "Tried to invalidate app state")
                    .Subscribe(_ => item.Log().Info("Invalidated app state")));

            ret.Add(item.ShouldPersistState
                    .SelectMany(x => driver.SaveState(item.AppState !).Finally(x.Dispose))
                    .LoggedCatch(item, Observables.Unit, "Tried to persist app state")
                    .Subscribe(_ => item.Log().Info("Persisted application state")));

            ret.Add(item.IsResuming.Merge(item.IsLaunchingNew)
                    .SelectMany(_ => driver.LoadState())
                    .LoggedCatch(
                        item,
                        Observable.Defer(() => Observable.Return(item.CreateNewAppState?.Invoke())),
                        "Failed to restore app state from storage, creating from scratch")
                    .Subscribe(x => item.AppState = x ?? item.CreateNewAppState?.Invoke()));

            return(ret);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="This"></param>
        /// <param name="driver"></param>
        /// <returns></returns>
        public static IDisposable SetupDefaultSuspendResume(this ISuspensionHost This, ISuspensionDriver driver = null)
        {
            var ret = new CompositeDisposable();

            driver = driver ?? Locator.Current.GetService <ISuspensionDriver>();

            ret.Add(This.ShouldInvalidateState
                    .SelectMany(_ => driver.InvalidateState())
                    .LoggedCatch(This, Observables.Unit, "Tried to invalidate app state")
                    .Subscribe(_ => This.Log().Info("Invalidated app state")));

            ret.Add(This.ShouldPersistState
                    .SelectMany(x => driver.SaveState(This.AppState).Finally(x.Dispose))
                    .LoggedCatch(This, Observables.Unit, "Tried to persist app state")
                    .Subscribe(_ => This.Log().Info("Persisted application state")));

            ret.Add(Observable.Merge(This.IsResuming, This.IsLaunchingNew)
                    .SelectMany(x => driver.LoadState())
                    .LoggedCatch(This,
                                 Observable.Defer(() => Observable.Return(This.CreateNewAppState())),
                                 "Failed to restore app state from storage, creating from scratch")
                    .Subscribe(x => This.AppState = x ?? This.CreateNewAppState()));

            return(ret);
        }