Skip to content

Instantly share code, notes, and snippets.

@myeonginwoo
Created November 4, 2018 17:45
Show Gist options
  • Save myeonginwoo/0e157af539a1ed3811122c353382c890 to your computer and use it in GitHub Desktop.
Save myeonginwoo/0e157af539a1ed3811122c353382c890 to your computer and use it in GitHub Desktop.
class MainViewModel(val view: MainView, val dataStore: DataStore) {
private var currentItem: Item by Delegates.observable(Item("kotlin", "#FF0000"),
{ _: KProperty<*>, oldItem: Item, newItem: Item ->
view.onUpdatedItem(oldItem, newItem)
})
private var currentState: NetworkState<Item> by Delegates.observable(NetworkState.Init(),
{ _: KProperty<*>, _: NetworkState<Item>, newState: NetworkState<Item> ->
when (newState) {
is NetworkState.Init -> view.hideProgress()
is NetworkState.Loading -> view.showProgress()
is NetworkState.Success<Item> -> currentItem = newState.item
is NetworkState.Error -> view.onError(newState.throwable)
}
})
fun requestItemInfo(itemType: ItemType) {
currentState = NetworkState.Init()
currentState = try {
NetworkState.Success(dataStore.getItemInfo(itemType))
} catch (error: Throwable) {
NetworkState.Error(error)
} finally {
currentState = NetworkState.Init()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment