Android Weekly Notes #464
2021-06-20 本文已影响0人
圣骑士wind
Android Weekly Issue #464
WebRTC Sample in Kotlin
WebRTC是一个传递视频, 音频和通用数据的平台.
要在两个设备之间传输, 需要一个signaling server.
本文的作者使用了Cloud Firestore作为Signaling Server. 实现了一个简易的视频通话.
Flow/LiveData….What Are They?
这个作者用了一个登录的例子, 代码有点过于详细了.
LiveData的优点:
- 根据生命周期自动解绑, 没有内存泄漏风险.
- UI和数据保持一致.
- 屏幕旋转处理.
- 共享资源.
LiveData的缺点:
- 执行上下文缺少控制.
- 线程问题.
- 和协程没啥配套关系.
- 使用
Transformations
的时候很多样板代码.
Flow的缺点:
- 不支持一次性操作(比如SnackBar) -> Fix: Use Channels.
- 不支持根据生命周期自动解绑. 两个不好的解决办法: 1: 手动停止collect. 2: asLiveData. 好的解决办法: 使用
lifecycle-runtime-ktx
里的高级方法.
Navigation: Conditional Navigation
作者的例子挺好的, Repository还用的DataStore.
这个例子的app有三种模式:
- 只卖甜甜圈; 将会移除
BottomNavigationView
. - 甜甜圈和咖啡;
- 没有指定. 将会呈现选择界面.
然后介绍了如何测试: (androidTest)
@Test
fun testFirstRun() {
// Create a mock NavController
val mockNavController = TestNavHostController(
ApplicationProvider.getApplicationContext()
)
mockNavController.setGraph(R.navigation.nav_graph)
//...
}
How to test a time-dependent coroutine
如何测试一个依赖时间的协程.
例子还挺好玩的:
测试两分钟做60个俯卧撑:
@Test
fun `makes 60 push ups during 2 minutes exercise`() = runBlockingTest {
val expectedPushUps = 60
// Set coroutine clock to exercise duration (2 minutes)
advanceTimeBy(PushUpTraining.EXERCISE_DURATION)
// Collect all training states (BodyUp & BodyDown) to a list
val results = pushUpTraining.start()
.toList()
// Dividing by two because two states represent a push up (BodyUp & BodyDown)
val actualPushUps = results.size / 2
assertEquals(
expected = expectedPushUps,
actual = actualPushUps,
)
}
测试一分钟做30个:
@Test
fun `makes 30 push ups in 1 minute of the exercise`() = runBlockingTest {
val exerciseDurationInMillis = 60000L
val expectedPushUps = 30
// Collect all training states (BodyUp & BodyDown) to a list
val results = mutableListOf<PushUpState>()
val job = launch {
pushUpTraining.start()
.collect { results.add(it) }
}
// Set coroutine clock to 1 minute
advanceTimeBy(exerciseDurationInMillis)
// Cancel coroutine to check result at specific time
job.cancel()
// Dividing by two because two states represent a push up (BodyUp & BodyDown)
val actualPushUps = results.size / 2
assertEquals(
expected = expectedPushUps,
actual = actualPushUps,
)
}
Bye XML, it was nice knowing you (pt. 1)
xml的缺点和Compose的优点.
Modular Navigation with Jetpack Compose
Compose的跨module跳转.