Last active
October 25, 2019 20:21
-
-
Save CostaFot/c2b656729f611f9ee4699822f4bda5e0 to your computer and use it in GitHub Desktop.
CatModel!
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
class MainViewModel : ViewModel() { | |
private val compositeDisposableOnDestroy = CompositeDisposable() | |
private var latestCatCall: Disposable? = null | |
// the list that will be observed by the activity | |
val bunchOfCats = MutableLiveData<List<NetCat>>() | |
// the error message observed | |
val errorMessage = MutableLiveData<String>() | |
// the API call | |
fun getSomeCats() { | |
// initialising the repository class with the necessary information | |
val catsRepository = CatsRepository(serverUrl, BuildConfig.DEBUG, apiKey) | |
// stopping the last call if it's already running (optional) | |
latestCatCall?.dispose() | |
// perform the API call | |
// asking for 10 cats. Don't care in what category so just passing null | |
latestCatCall = | |
catsRepository.getNumberOfRandomCats(10, null).subscribeOn(Schedulers.io()) | |
.doOnSubscribe { | |
compositeDisposableOnDestroy.add(it) | |
} | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe { result -> | |
when { | |
result.hasError() -> result.errorMessage?.let { | |
// anyone who observes this will be notified of the change automatically | |
errorMessage.postValue("Error getting cats $it") | |
} | |
?: run { | |
// anyone who observes this will be notified of the change automatically | |
errorMessage.postValue("Null error :(") | |
} | |
result.hasCats() -> result.netCats?.let { | |
// anyone who observes this will be notified of the change automatically | |
bunchOfCats.postValue(it) | |
// clearing the error if it existed (hacky and optional) | |
errorMessage.postValue("") | |
} | |
?: run { | |
// anyone who observes this will be notified of the change automatically | |
errorMessage.postValue("Null list of cats :(") | |
} | |
else -> { | |
// anyone who observes this will be notified of the change automatically | |
errorMessage.postValue("No cats available :(") | |
} | |
} | |
} | |
} | |
// clearing the collection of disposables = no memory leaks no matter what | |
override fun onCleared() { | |
compositeDisposableOnDestroy.clear() | |
Log.d("TAG", "Clearing ViewModel") | |
super.onCleared() | |
} | |
} |
Hey, thanks for the kind words :)
You can find the CatsRepository
class here.
In the constructor it has these 3 fields declared (lines 5,6 and 7).
The latestCatCall: Disposable
variable is a var (on line 4 of this file). So it can be changed at any time.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! Thanks a lot for the awesome article on Medium, sir! Could you please tell how are you initialize CatsRepository(serverUrl, BuildConfig.DEBUG, apiKey) when the class has no such fields. And how could you reinitialize variable latestCatsCall, earlier declared val (final)?