Skip to content

Instantly share code, notes, and snippets.

@marenovakovic
Created March 30, 2021 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marenovakovic/51694221c1e4c3478db3a16386fe744f to your computer and use it in GitHub Desktop.
Save marenovakovic/51694221c1e4c3478db3a16386fe744f to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity() {
private val viewModel: NetworkStatusViewModel by lazy {
ViewModelProvider(
this,
object : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
val networkStatusTracker = NetworkStatusTracker(this@MainActivity)
return NetworkStatusViewModel(networkStatusTracker) as T
}
},
).get(NetworkStatusViewModel::class.java)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel.state.observe(this) { state ->
findViewById<TextView>(R.id.textView).text = when (state) {
MyState.Fetched -> "Fetched"
MyState.Error -> "Error"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment