private void HandleNewForgotPasswordRequestStatus(ForgotPasswordRequestStatus status) { switch (status) { case ForgotPasswordRequestStatus.Idle: PasswordResetLiveData.PostValue(RxWrapper <bool> .NoData()); break; case ForgotPasswordRequestStatus.Pending: PasswordResetLiveData.PostValue(RxWrapper <bool> .Pending()); break; case ForgotPasswordRequestStatus.Success: LongToast(AppRes["ForgotPasswordSuccess"]); PasswordResetLiveData.PostValue(RxWrapper <bool> .Ok(true)); break; case ForgotPasswordRequestStatus.BadData: ShortToast(AppRes["ForgotPasswordBadData"]); PasswordResetLiveData.PostValue(RxWrapper <bool> .Ok(false)); break; case ForgotPasswordRequestStatus.NoNetwork: ShortToast(AppRes["NoNetworkToast"]); PasswordResetLiveData.PostValue(RxWrapper <bool> .Error(new Exception(AppRes["NoNetworkToast"]))); break; case ForgotPasswordRequestStatus.Error: default: ShortToast(AppRes["ForgotPasswordError"]); Log.Error("ForgotPasswordPageViewModel", "ForgotPasswordRequest returned unknown error"); PasswordResetLiveData.PostValue(RxWrapper <bool> .Error(new Exception(AppRes["ForgotPasswordError"]))); break; } }
/// <summary> /// Simulated network fetch /// Error every third call /// </summary> public void GetFakeNetworking() { //Check if request is not already pending if (FakeNetworkingLiveData.Value?.Status != RxStatus.Pending) { //Notify UI that request is pending FakeNetworkingLiveData.PostValue(RxWrapper <SampleResponse> .Pending()); //Fake request latency 0.5-2s Task.Delay(_randomGen.Next(500, 2000)).ContinueWith(task => { switch (_fakeNetworkSequence) { case 0: var model1 = new SampleResponse { Id = 1, Name = "Ok response 1" }; //Post fake fetch data to UI FakeNetworkingLiveData.PostValue(RxWrapper <SampleResponse> .Ok(model1)); break; case 1: var model2 = new SampleResponse { Id = 2, Name = "Ok response 2" }; //Post fake fetch data to UI FakeNetworkingLiveData.PostValue(RxWrapper <SampleResponse> .Ok(model2)); break; case 2: //Fake error has occured during network call FakeNetworkingLiveData.PostValue(RxWrapper <SampleResponse> .Error(new Exception("No network"))); break; } ++_fakeNetworkSequence; if (_fakeNetworkSequence > 2) { _fakeNetworkSequence = 0; } }); } }
public void RxWrapperInitTest() { var testData = TestUtils.RandomString(32); var testStatus = (RxStatus) new Random().Next(0, 2); var testException = new Exception(TestUtils.RandomString(32)); _rxWrapper = new RxWrapper <string>(testStatus, testData, testException); Assert.AreEqual(testData, _rxWrapper.Data); Assert.AreEqual(testStatus, _rxWrapper.Status); Assert.AreEqual(testException, _rxWrapper.Exception); _rxWrapper = new RxWrapper <string>(testData); Assert.AreEqual(testData, _rxWrapper.Data); Assert.AreEqual(RxStatus.Ok, _rxWrapper.Status); Assert.IsNull(_rxWrapper.Exception); _rxWrapper = new RxWrapper <string>(testStatus); Assert.IsNull(_rxWrapper.Data); Assert.AreEqual(testStatus, _rxWrapper.Status); Assert.IsNull(_rxWrapper.Exception); _rxWrapper = new RxWrapper <string>(testException); Assert.IsNull(_rxWrapper.Data); Assert.AreEqual(RxStatus.Error, _rxWrapper.Status); Assert.AreEqual(testException, _rxWrapper.Exception); _rxWrapper = RxWrapper <string> .Ok(testData); Assert.AreEqual(testData, _rxWrapper.Data); Assert.AreEqual(RxStatus.Ok, _rxWrapper.Status); Assert.IsNull(_rxWrapper.Exception); _rxWrapper = RxWrapper <string> .Pending(); Assert.IsNull(_rxWrapper.Data); Assert.AreEqual(RxStatus.Pending, _rxWrapper.Status); Assert.IsNull(_rxWrapper.Exception); _rxWrapper = RxWrapper <string> .Error(testException); Assert.IsNull(_rxWrapper.Data); Assert.AreEqual(RxStatus.Error, _rxWrapper.Status); Assert.AreEqual(testException, _rxWrapper.Exception); }