Created
November 4, 2018 17:45
-
-
Save myeonginwoo/0e157af539a1ed3811122c353382c890 to your computer and use it in GitHub Desktop.
This file contains 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(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