From fbbdddef8c4b0e7cb5f6ac1343e818defc96d4a7 Mon Sep 17 00:00:00 2001 From: Tangem Date: Sun, 28 Oct 2018 21:16:10 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../tangem/data/network/exception/Failure.kt | 31 +++++++++++++++++ .../presentation/viewmodel/BaseViewModel.kt | 34 +++++++++++++++++++ .../viewmodel/LoadedWalletViewModel.kt | 33 ++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 app/src/main/java/com/tangem/data/network/exception/Failure.kt create mode 100644 app/src/main/java/com/tangem/presentation/viewmodel/BaseViewModel.kt create mode 100644 app/src/main/java/com/tangem/presentation/viewmodel/LoadedWalletViewModel.kt diff --git a/app/src/main/java/com/tangem/data/network/exception/Failure.kt b/app/src/main/java/com/tangem/data/network/exception/Failure.kt new file mode 100644 index 0000000000..f6b9deaa4c --- /dev/null +++ b/app/src/main/java/com/tangem/data/network/exception/Failure.kt @@ -0,0 +1,31 @@ +/** + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.dmmatrix.epro.core.exception + +/** + * Base Class for handling errors/failures/exceptions. + * Every feature specific failure should extend [FeatureFailure] class. + */ +sealed class Failure { + class ApplicationError(val message: String) : Failure() + class NotFound : FeatureFailure() + class NetworkConnection : Failure() + class ServerError : Failure() + class Unauthorized : Failure() // 401 + + /** * Extend this class for feature specific failures.*/ + abstract class FeatureFailure : Failure() +} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/presentation/viewmodel/BaseViewModel.kt b/app/src/main/java/com/tangem/presentation/viewmodel/BaseViewModel.kt new file mode 100644 index 0000000000..959bc6d6d3 --- /dev/null +++ b/app/src/main/java/com/tangem/presentation/viewmodel/BaseViewModel.kt @@ -0,0 +1,34 @@ +/** + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.tangem.presentation.viewmodel + +import android.arch.lifecycle.MutableLiveData +import android.arch.lifecycle.ViewModel +import com.dmmatrix.epro.core.exception.Failure + +/** + * Base ViewModel class with default Failure handling. + * @see ViewModel + * @see Failure + */ +abstract class BaseViewModel : ViewModel() { + + var failure: MutableLiveData = MutableLiveData() + + protected fun handleFailure(failure: Failure) { + this.failure.value = failure + } +} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/presentation/viewmodel/LoadedWalletViewModel.kt b/app/src/main/java/com/tangem/presentation/viewmodel/LoadedWalletViewModel.kt new file mode 100644 index 0000000000..46b4defd2c --- /dev/null +++ b/app/src/main/java/com/tangem/presentation/viewmodel/LoadedWalletViewModel.kt @@ -0,0 +1,33 @@ +package com.tangem.presentation.viewmodel + +import android.arch.lifecycle.MutableLiveData +import com.dmmatrix.epro.core.exception.Failure + +class LoadedWalletViewModel : BaseViewModel() { + + private val state: MutableLiveData = MutableLiveData() + + fun getState() = state + + enum class State { + ServerError, + Failed, + Success + } + + fun connectToken(data: String, data2: String) { + + } + + private fun handleTokensSaveFailure(failure: Failure) { + state.value = State.Success + handleFailure(failure) + } + + private fun handleLoginFailure(failure: Failure) { + when (failure) { + is Failure.ServerError -> state.value = State.ServerError + } + handleFailure(failure) + } +} \ No newline at end of file