Skip to content

Instantly share code, notes, and snippets.

@oianmol
Created July 6, 2022 10:17
Show Gist options
  • Save oianmol/f60db1df89dc87969ecb04dc58b32cfa to your computer and use it in GitHub Desktop.
Save oianmol/f60db1df89dc87969ecb04dc58b32cfa to your computer and use it in GitHub Desktop.
sdfsdf
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