With the release of Kotlin 1.3, coroutines are no longer experimental. You are correct on the migration from the experimental version to the release version.
To your second point about network connectivity, it is somewhat beyond the scope of this book to solve the problem. But borrowing from BNR Android Programming, a solution to do so is below. This builds off my solution posted in Challenge: Live Data
The following helper function checks the network state
private fun isNetworkAvailableAndConnected(): Boolean {
val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
return cm.activeNetworkInfo?.isConnected ?: false
}
Then inside of onCreate()
, we can choose how to handle network connectivity
characterData = savedInstanceState?.characterData ?: let {
if( isNetworkAvailableAndConnected() ) {
fetchCharacterFromAPI()
CharacterGenerator.placeHolderCharacter()
} else {
Toast.makeText(baseContext, "Turn on network connectivity for characters of lore", Toast.LENGTH_SHORT).show()
CharacterGenerator.generate()
}
}
Finally, I chose to override the onResume()
callback to turn the generate button on/off
override fun onResume() {
super.onResume()
generateButton.isEnabled = isNetworkAvailableAndConnected()
}