Android Weekly Notes #473
Android Weekly Issue #473
Debugging Story: The Case of Rogue Reflection
一个和反射相关的问题的追踪过程.
Kotlin Multiplatform Mobile Codelab
KMP的codelab.
Creating a retro-style game with Jetpack Compose
复古风的游戏.
Scalable Jetpack Compose Navigation
如何扩展Jetpack Compose的Navigation.
定义一个navigation的factory:
// An interface is created to allow a feature module to add their Composable to the NavGraph.
// Defined within a library module
interface ComposeNavigationFactory {
fun create(builder: NavGraphBuilder, navController: NavHostController)
}
然后每个feature实现它:
// An implementation of the interface, as well as a Dagger 2 module installed via hilt.
// Defined within a feature module
internal class Feature1ComposeNavigationFactory @Inject constructor() : ComposeNavigationFactory {
override fun create(builder: NavGraphBuilder, navController: NavHostController) {
builder.composable(
route = "feature1",
content = {
Feature1(
navController = navController
)
}
)
}
}
// Defined within the 'feature 1' module
@Module
@InstallIn(SingletonComponent::class)
internal interface ComposeNavigationFactoryModule {
@Singleton
@Binds
@IntoSet
fun bindComposeNavigationFactory(factory: Feature1ComposeNavigationFactory): ComposeNavigationFactory
}
最后注入:
// An example of a set of factories being used to construct a NavHost.
// Potentially defined within the app module
@AndroidEntryPoint
class ExampleActivity: AppCompatActivity {
@Inject
lateinit var composeNavigationFactories: @JvmSuppressWildcards Set<ComposeNavigationFactory>
@Composable
fun JetpackNavigationHiltApp() {
val navController = rememberNavController()
NavHost(navController, startDestination = "feature1") {
composeNavigationFactories.forEach { factory ->
factory.create(this, navController)
}
}
}
}
作者把这个搞了个库:
https://github.com/LachlanMcKee/Hilt-Compose-Navigation-Factory
还有其他方案比如: https://github.com/zsoltk/compose-router
Providing AssistedInject supported ViewModel for Composable using Hilt
AssistedInject
: 处理有一些参数需要运行时提供.
在composable里需要借助EntryPoint这样搞一波:
@Composable
fun noteDetailViewModel(noteId: String): NoteDetailViewModel {
val factory = EntryPointAccessors.fromActivity(
LocalContext.current as Activity,
MainActivity.ViewModelFactoryProvider::class.java
).noteDetailViewModelFactory()
return viewModel(factory = NoteDetailViewModel.provideFactory(factory, noteId))
}
用的时候:
NavHost(navController, startDestination = Screen.Notes.route, route = NOTY_NAV_HOST_ROUTE) {
composable(
Screen.NotesDetail.route,
arguments = listOf(navArgument(Screen.NotesDetail.ARG_NOTE_ID) { type = NavType.StringType })
) {
val noteId = it.arguments?.getString(Screen.NotesDetail.ARG_NOTE_ID)!!
NoteDetailsScreen(navController, noteDetailViewModel(noteId))
}
}
底部是一个全kotlin技术栈的应用:
https://github.com/PatilShreyas/NotyKT
Two-way communication without internet: Nearby Connections (Part 2 of 3)
利用Nearby API, 没网的时候也可以通信.
Using Composition in Kotlin
在kotlin中使用组合.
Advanced Usage of WorkManager in multi-process apps
WorkManager多进程的例子:
https://github.com/android/architecture-components-samples/tree/main/WorkManagerMultiprocessSample
Scope Storage Myths
Android 11之后, 如果app不访问别的app拥有的文件, 则不需要请求READ_EXTERNAL_STORAGE
权限.
还可以这样:
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="29" />
Refactoring Kotlin type-signatures for fun and profit
Compose: Strikethru Animation
一个icon的动画.
Browsing Jetpack Compose samples
看Jetpack Compose Sample的方法.
有一个Android Code Search的网站:
https://cs.android.com/
有一个code search plugin:
https://plugins.jetbrains.com/plugin/12578-codesearch
源码:
https://github.com/guojianhua/code-search
Code
- 笔记应用: https://github.com/PatilShreyas/NotyKT
- 一个要取代ViewModel的小工具: https://github.com/marcellogalhardo/retained
- 选取图片和照相的库: https://github.com/SimformSolutionsPvtLtd/SSImagePicker