override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(
this,
viewModelFactory
).get(WeatherForecastDataStreamFlowViewModel::class.java)
// Consume data when fragment is started
lifecycleScope.launchWhenStarted {
// Since collect is a suspend function it needs to be called
// from a coroutine scope
viewModel.weatherForecast.collect {
when (it) {
Result.Loading -> {
Toast.makeText(context, "Loading", Toast.LENGTH_SHORT).show()
}
is Result.Success -> {
tvDegree.text = it.data.toString()
}
Result.Error -> {
Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show()
}
}
}
}
}
@Serializable
data class User(
val name: String,
val email: String,
val age: Int = 13,
val role: Role = Role.Viewer
)
enum class Role { Viewer, Editor, Owner }
class JsonUnitTest {
private val jsonString = """
{
"name" : "John Doe",
"email" : "john.doe@email.com"
}
""".trimIndent()
@Test
fun jsonTest() {
val user = Json.parse(User.serializer(), jsonString)
assertEquals("John Doe", user.name)
assertEquals(Role.Viewer, user.role)
assertEquals(13, user.age)
// User(name=John Doe,
// email=john.doe@email.com,
// age=13,
// role=Viewer)
}
}