Created
July 6, 2022 10:17
-
-
Save oianmol/f60db1df89dc87969ecb04dc58b32cfa to your computer and use it in GitHub Desktop.
sdfsdf
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
import Some.MutableLiveData | |
class MyActivity { | |
private val button = Button() | |
val vm = MyViewModel() | |
fun onCreate(){ | |
vm.liveData.observe{value-> | |
/// | |
updateButtonText(text = value) | |
} | |
} | |
fun buttonClick() { | |
vm.doNetowkrCall() | |
} | |
fun updateButtonText(text: String) { | |
button.text = text | |
} | |
} | |
class Button { | |
var text: String? = null | |
} | |
class MyViewModel(private val useCaseNetworkCall: UseCaseNetworkCall) { | |
var liveData = MutableLiveData<String>() | |
private set | |
var liveDataError = MutableLiveData<Throwable>() | |
private set | |
init { | |
doNetowkrCall() | |
} | |
fun doNetowkrCall() { | |
if(hasNetwork){ | |
viwModelScope.launch(CoroutinExceptionHandler{ context,throwable-> | |
liveDataError.value(throwable) | |
}) { | |
val result = useCaseNetworkCall.invoke() | |
liveData.value = result | |
} | |
}else{ | |
liveDataError.value(NoNetworkException()) | |
} | |
} | |
} | |
class UseCaseNetworkCall(private val featureFEtchDataRepo: FeatureFEtchDataRepo) { | |
operator fun invoke() = featureFEtchDataRepo.fetchData() | |
} | |
class UseCaseLocalData(private val featureFEtchDataRepo: FeatureFEtchDataRepo) { | |
operator fun invoke() = featureFEtchDataRepo.fetchLocal() | |
} | |
interface FeatureFEtchDataRepo { | |
fun fetchData(): String | |
fun fetchLocal(): String | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment