-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: simplify passing arguments to scoped ViewModels (#2023)
Co-authored-by: Jakub Żerko <iot.zerko@gmail.com>
- Loading branch information
Showing
12 changed files
with
235 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/src/main/kotlin/com/wire/android/di/ViewModelScoped.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android.di | ||
|
||
import android.os.Bundle | ||
import androidx.compose.runtime.Composable | ||
import androidx.core.os.bundleOf | ||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import com.sebaslogen.resaca.hilt.hiltViewModelScoped | ||
import dev.ahmedmourad.bundlizer.Bundlizer | ||
import kotlinx.serialization.InternalSerializationApi | ||
import kotlinx.serialization.serializer | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* Returns a proper scoped arguments instance from the given [SavedStateHandle] for the scoped [ViewModel]. | ||
*/ | ||
inline fun <reified R : ScopedArgs> SavedStateHandle.scopedArgs(): R = | ||
scopedArgs(R::class, this) | ||
|
||
/** | ||
* Returns a proper scoped arguments instance from the given [SavedStateHandle]. | ||
* | ||
* @param argsClass the class of the arguments, must implement [ScopedArgs] and be serializable | ||
* @param argsContainer the [SavedStateHandle] to get the arguments from | ||
*/ | ||
@OptIn(InternalSerializationApi::class) | ||
fun <R : ScopedArgs> scopedArgs(argsClass: KClass<R>, argsContainer: SavedStateHandle): R = | ||
Bundlizer.unbundle(argsClass.serializer(), argsContainer.toBundle()) | ||
|
||
/** | ||
* Custom implementation of [hiltViewModelScoped] that takes proper scoped serializable arguments that implement [ScopedArgs] | ||
* and provides them into scoped [ViewModel] converting it automatically to [Bundle] using [Bundlizer]. | ||
* Proper key will be taken from the [ScopedArgs.key] property. | ||
* | ||
* @param arguments The arguments that will be provided to the [ViewModel], must implement [ScopedArgs] and be serializable | ||
*/ | ||
@OptIn(InternalSerializationApi::class) | ||
@Composable | ||
inline fun <reified T : ViewModel, reified R : ScopedArgs> hiltViewModelScoped(arguments: R): T = | ||
hiltViewModelScoped(key = arguments.key, defaultArguments = Bundlizer.bundle(R::class.serializer(), arguments)) | ||
|
||
/** | ||
* Creates a [Bundle] with all key-values from the given [SavedStateHandle]. | ||
*/ | ||
@Suppress("SpreadOperator") | ||
fun SavedStateHandle.toBundle(): Bundle = bundleOf(*(keys().map { it to get<Any>(it) }.toTypedArray())) | ||
|
||
/** | ||
* Interface for arguments for scoped ViewModels. | ||
* It is used to provide a unique key for the scoped ViewModel. | ||
*/ | ||
interface ScopedArgs { | ||
val key: Any? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
app/src/main/kotlin/com/wire/android/ui/connection/ConnectionActionButtonArgs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android.ui.connection | ||
|
||
import com.wire.android.di.ScopedArgs | ||
import com.wire.kalium.logic.data.user.UserId | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class ConnectionActionButtonArgs( | ||
val userId: UserId, | ||
val userName: String | ||
) : ScopedArgs { | ||
override val key = "$ARGS_KEY:$userId" | ||
|
||
companion object { | ||
const val ARGS_KEY = "ConnectionActionButtonArgsKey" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
app/src/main/kotlin/com/wire/android/ui/home/conversations/model/CompositeMessageArgs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android.ui.home.conversations.model | ||
|
||
import com.wire.android.di.ScopedArgs | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class CompositeMessageArgs( | ||
val messageId: String | ||
) : ScopedArgs { | ||
override val key = "$ARGS_KEY:$messageId" | ||
|
||
companion object { | ||
const val ARGS_KEY = "CompositeMessageArgsKey" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.