Minor cleanup

This commit is contained in:
Syer10
2022-11-01 18:18:59 -04:00
parent 6f307a7a47
commit fa7742a44a
3 changed files with 15 additions and 8 deletions

View File

@@ -35,7 +35,7 @@ interface ChapterRepository {
/* TODO add once ktorfit supports nullable paremters
@FormUrlEncoded
@PATCH("/api/v1/manga/{mangaId}/chapter/{chapterIndex}")
@PATCH("api/v1/manga/{mangaId}/chapter/{chapterIndex}")
fun updateChapter(
@Path("mangaId") mangaId: Long,
@Path("chapterIndex") chapterIndex: Int,

View File

@@ -29,7 +29,9 @@ actual inline fun <reified VM : ViewModel> Screen.stateViewModel(
val viewModelFactory = LocalViewModels.current
val lifecycle = LocalLifecycleOwner.current as AndroidScreenLifecycleOwner
val handle = remember {
lifecycle.defaultViewModelCreationExtras.addScreenModelKey<VM>(this, tag).createSavedStateHandle()
lifecycle.defaultViewModelCreationExtras
.addScreenModelKey<VM>(this, tag)
.createSavedStateHandle()
}
return rememberScreenModel(tag) { viewModelFactory.factory(handle) }
}

View File

@@ -25,11 +25,10 @@ import java.lang.reflect.Method
private const val SAVED_STATE_KEY = "androidx.lifecycle.internal.SavedStateHandlesProvider"
val SavedStateHandleSupportClass: Class<*> by lazy {
Class.forName("androidx.lifecycle.SavedStateHandleSupport")
}
val getSavedStateHandlesVM: Method by lazy {
SavedStateHandleSupportClass.methods.first { it.name == "getSavedStateHandlesVM" }
Class.forName("androidx.lifecycle.SavedStateHandleSupport")
.methods
.first { it.name == "getSavedStateHandlesVM" }
}
private fun createSavedStateHandle(
@@ -43,7 +42,12 @@ private fun createSavedStateHandle(
// for a given key stored in our ViewModel, use that. Otherwise, create
// a new SavedStateHandle, providing it any restored state we might have saved
val vm = getSavedStateHandlesVM.invoke(null, viewModelStoreOwner)!!
val handles = vm::class.java.methods.first { it.name == "getHandles" }.invoke(vm) as MutableMap<String, SavedStateHandle>
@Suppress("UNCHECKED_CAST")
val handles = vm::class.java
.methods
.first { it.name == "getHandles" }
.invoke(vm) as MutableMap<String, SavedStateHandle>
return handles[key] ?: SavedStateHandle.createHandle(
provider.consumeRestoredStateForKey(key),
defaultArgs
@@ -103,7 +107,8 @@ internal class SavedStateHandlesProvider(
* Restore the state associated with a particular SavedStateHandle, identified by its [key]
*/
fun consumeRestoredStateForKey(key: String): Bundle? {
return savedStateRegistry::class.java.methods
return savedStateRegistry::class.java
.methods
.find { it.name == "consumeRestoredStateForKey" }
?.invoke(savedStateRegistry, key) as? Bundle
}