Created
November 4, 2018 17:32
-
-
Save myeonginwoo/8d58ce27dfa0d0b594d23eed540ed8ca 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 by Delegates.observable(NetworkState.Init(), | |
{ _: KProperty<*>, _: NetworkState, newState: NetworkState -> | |
when (newState) { | |
is NetworkState.Init -> view.hideProgress() | |
is NetworkState.Loading -> view.showProgress() | |
is NetworkState.Success -> currentItem = newState.item | |
is NetworkState.Error -> view.onError(newState.throwable) | |
} | |
}) | |
fun requestItemInfo(itemType: ItemType) { | |
currentState = NetworkState.Loading() | |
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