Skip to content

Commit

Permalink
Improve deprecation cycle for resultBack APIs with no nav type parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
raamcosta committed Jul 20, 2024
1 parent ec4254e commit d70d005
Show file tree
Hide file tree
Showing 18 changed files with 112 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,23 @@ val CORE_DIRECTION_NAV_GRAPH_SPEC = Importable("DirectionNavGraphSpec", "$CORE_P
val CORE_TYPED_NAV_GRAPH_SPEC = Importable("TypedNavGraphSpec", "$CORE_PACKAGE_NAME.spec.TypedNavGraphSpec")
val CORE_ACTIVITY_DESTINATION_SPEC = Importable("ActivityDestinationSpec", "$CORE_PACKAGE_NAME.spec.ActivityDestinationSpec")
val CORE_DIRECTION_ACTIVITY_DESTINATION_SPEC = Importable("DirectionActivityDestinationSpec", "$CORE_PACKAGE_NAME.spec.DirectionActivityDestinationSpec")
val CORE_STRING_NAV_TYPE = Importable("DestinationsStringNavType", "$CORE_PACKAGE_NAME.navargs.primitives.DestinationsStringNavType")
val CORE_INT_NAV_TYPE = Importable("DestinationsIntNavType", "$CORE_PACKAGE_NAME.navargs.primitives.DestinationsIntNavType")
val CORE_BOOLEAN_NAV_TYPE = Importable("DestinationsBooleanNavType", "$CORE_PACKAGE_NAME.navargs.primitives.DestinationsBooleanNavType")
val CORE_FLOAT_NAV_TYPE = Importable("DestinationsFloatNavType", "$CORE_PACKAGE_NAME.navargs.primitives.DestinationsFloatNavType")
val CORE_LONG_NAV_TYPE = Importable("DestinationsLongNavType", "$CORE_PACKAGE_NAME.navargs.primitives.DestinationsLongNavType")
val CORE_LONG_ARRAY_NAV_TYPE = Importable("DestinationsLongArrayNavType", "$CORE_PACKAGE_NAME.navargs.primitives.array.DestinationsLongArrayNavType")
val CORE_BOOLEAN_ARRAY_NAV_TYPE = Importable("DestinationsBooleanArrayNavType", "$CORE_PACKAGE_NAME.navargs.primitives.array.DestinationsBooleanArrayNavType")
val CORE_INT_ARRAY_NAV_TYPE = Importable("DestinationsIntArrayNavType", "$CORE_PACKAGE_NAME.navargs.primitives.array.DestinationsIntArrayNavType")
val CORE_FLOAT_ARRAY_NAV_TYPE = Importable("DestinationsFloatArrayNavType", "$CORE_PACKAGE_NAME.navargs.primitives.array.DestinationsFloatArrayNavType")
val CORE_STRING_ARRAY_NAV_TYPE = Importable("DestinationsStringArrayNavType", "$CORE_PACKAGE_NAME.navargs.primitives.array.DestinationsStringArrayNavType")
val CORE_BOOLEAN_ARRAY_LIST_NAV_TYPE = Importable("DestinationsBooleanArrayListNavType", "$CORE_PACKAGE_NAME.navargs.primitives.arraylist.DestinationsBooleanArrayListNavType")
val CORE_FLOAT_ARRAY_LIST_NAV_TYPE = Importable("DestinationsFloatArrayListNavType", "$CORE_PACKAGE_NAME.navargs.primitives.arraylist.DestinationsFloatArrayListNavType")
val CORE_INT_ARRAY_LIST_NAV_TYPE = Importable("DestinationsIntArrayListNavType", "$CORE_PACKAGE_NAME.navargs.primitives.arraylist.DestinationsIntArrayListNavType")
val CORE_LONG_ARRAY_LIST_NAV_TYPE = Importable("DestinationsLongArrayListNavType", "$CORE_PACKAGE_NAME.navargs.primitives.arraylist.DestinationsLongArrayListNavType")
val CORE_STRING_ARRAY_LIST_NAV_TYPE = Importable("DestinationsStringArrayListNavType", "$CORE_PACKAGE_NAME.navargs.primitives.arraylist.DestinationsStringArrayListNavType")

val CORE_STRING_NAV_TYPE = Importable("stringNavType", "$CORE_PACKAGE_NAME.navargs.primitives.stringNavType")
val CORE_INT_NAV_TYPE = Importable("intNavType", "$CORE_PACKAGE_NAME.navargs.primitives.intNavType")
val CORE_BOOLEAN_NAV_TYPE = Importable("booleanNavType", "$CORE_PACKAGE_NAME.navargs.primitives.booleanNavType")
val CORE_FLOAT_NAV_TYPE = Importable("floatNavType", "$CORE_PACKAGE_NAME.navargs.primitives.floatNavType")
val CORE_LONG_NAV_TYPE = Importable("longNavType", "$CORE_PACKAGE_NAME.navargs.primitives.longNavType")
val CORE_LONG_ARRAY_NAV_TYPE = Importable("longArrayNavType", "$CORE_PACKAGE_NAME.navargs.primitives.array.longArrayNavType")
val CORE_BOOLEAN_ARRAY_NAV_TYPE = Importable("booleanArrayNavType", "$CORE_PACKAGE_NAME.navargs.primitives.array.booleanArrayNavType")
val CORE_INT_ARRAY_NAV_TYPE = Importable("intArrayNavType", "$CORE_PACKAGE_NAME.navargs.primitives.array.intArrayNavType")
val CORE_FLOAT_ARRAY_NAV_TYPE = Importable("floatArrayNavType", "$CORE_PACKAGE_NAME.navargs.primitives.array.floatArrayNavType")
val CORE_STRING_ARRAY_NAV_TYPE = Importable("stringArrayNavType", "$CORE_PACKAGE_NAME.navargs.primitives.array.stringArrayNavType")
val CORE_BOOLEAN_ARRAY_LIST_NAV_TYPE = Importable("booleanArrayListNavType", "$CORE_PACKAGE_NAME.navargs.primitives.arraylist.booleanArrayListNavType")
val CORE_FLOAT_ARRAY_LIST_NAV_TYPE = Importable("floatArrayListNavType", "$CORE_PACKAGE_NAME.navargs.primitives.arraylist.floatArrayListNavType")
val CORE_INT_ARRAY_LIST_NAV_TYPE = Importable("intArrayListNavType", "$CORE_PACKAGE_NAME.navargs.primitives.arraylist.intArrayListNavType")
val CORE_LONG_ARRAY_LIST_NAV_TYPE = Importable("longArrayListNavType", "$CORE_PACKAGE_NAME.navargs.primitives.arraylist.longArrayListNavType")
val CORE_STRING_ARRAY_LIST_NAV_TYPE = Importable("stringArrayListNavType", "$CORE_PACKAGE_NAME.navargs.primitives.arraylist.stringArrayListNavType")

const val CORE_DIRECTION = "Direction"
const val CORE_DESTINATION_ANIMATION_STYLE = "DestinationStyle.Animated"
const val CORE_BOTTOM_SHEET_DESTINATION_STYLE = "DestinationStyleBottomSheet"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import android.os.Bundle
import androidx.lifecycle.SavedStateHandle
import com.ramcosta.composedestinations.navargs.DestinationsNavType

object DestinationsBooleanNavType : DestinationsNavType<Boolean?>() {
val booleanNavType: DestinationsNavType<Boolean?> = DestinationsBooleanNavType

internal object DestinationsBooleanNavType : DestinationsNavType<Boolean?>() {

override fun put(bundle: Bundle, key: String, value: Boolean?) {
when (val bundleValue = booleanToBundleValue(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import android.os.Bundle
import androidx.lifecycle.SavedStateHandle
import com.ramcosta.composedestinations.navargs.DestinationsNavType

object DestinationsFloatNavType : DestinationsNavType<Float?>() {
val floatNavType: DestinationsNavType<Float?> = DestinationsFloatNavType

internal object DestinationsFloatNavType : DestinationsNavType<Float?>() {

override fun put(bundle: Bundle, key: String, value: Float?) {
when (val bundleValue = floatToBundleValue(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import android.os.Bundle
import androidx.lifecycle.SavedStateHandle
import com.ramcosta.composedestinations.navargs.DestinationsNavType

object DestinationsIntNavType : DestinationsNavType<Int?>() {
val intNavType: DestinationsNavType<Int?> = DestinationsIntNavType

internal object DestinationsIntNavType : DestinationsNavType<Int?>() {

override fun put(bundle: Bundle, key: String, value: Int?) {
when (val bundleValue = intToBundleValue(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import android.os.Bundle
import androidx.lifecycle.SavedStateHandle
import com.ramcosta.composedestinations.navargs.DestinationsNavType

object DestinationsLongNavType : DestinationsNavType<Long?>() {
val longNavType: DestinationsNavType<Long?> = DestinationsLongNavType

internal object DestinationsLongNavType : DestinationsNavType<Long?>() {

override fun put(bundle: Bundle, key: String, value: Long?) {
when (val bundleValue = longToBundleValue(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.lifecycle.SavedStateHandle
import com.ramcosta.composedestinations.navargs.DestinationsNavType
import com.ramcosta.composedestinations.navargs.utils.encodeForRoute

val stringNavType: DestinationsStringNavType = DestinationsStringNavType

object DestinationsStringNavType : DestinationsNavType<String?>() {

internal const val ENCODED_EMPTY_STRING = "%02%03"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import com.ramcosta.composedestinations.navargs.primitives.DECODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.ENCODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.encodedComma

object DestinationsBooleanArrayNavType : DestinationsNavType<BooleanArray?>() {
val booleanArrayNavType: DestinationsNavType<BooleanArray?> = DestinationsBooleanArrayNavType

internal object DestinationsBooleanArrayNavType : DestinationsNavType<BooleanArray?>() {

override fun put(bundle: Bundle, key: String, value: BooleanArray?) {
bundle.putBooleanArray(key, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import com.ramcosta.composedestinations.navargs.primitives.DECODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.ENCODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.encodedComma

object DestinationsFloatArrayNavType : DestinationsNavType<FloatArray?>() {
val floatArrayNavType: DestinationsNavType<FloatArray?> = DestinationsFloatArrayNavType

internal object DestinationsFloatArrayNavType : DestinationsNavType<FloatArray?>() {

override fun put(bundle: Bundle, key: String, value: FloatArray?) {
bundle.putFloatArray(key, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import com.ramcosta.composedestinations.navargs.primitives.DECODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.ENCODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.encodedComma

object DestinationsIntArrayNavType : DestinationsNavType<IntArray?>() {
val intArrayNavType: DestinationsNavType<IntArray?> = DestinationsIntArrayNavType

internal object DestinationsIntArrayNavType : DestinationsNavType<IntArray?>() {

override fun put(bundle: Bundle, key: String, value: IntArray?) {
bundle.putIntArray(key, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import com.ramcosta.composedestinations.navargs.primitives.DECODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.ENCODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.encodedComma

object DestinationsLongArrayNavType : DestinationsNavType<LongArray?>() {
val longArrayNavType: DestinationsNavType<LongArray?> = DestinationsLongArrayNavType

internal object DestinationsLongArrayNavType : DestinationsNavType<LongArray?>() {

override fun put(bundle: Bundle, key: String, value: LongArray?) {
bundle.putLongArray(key, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import com.ramcosta.composedestinations.navargs.primitives.ENCODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.encodedComma
import com.ramcosta.composedestinations.navargs.utils.encodeForRoute

object DestinationsStringArrayNavType : DestinationsNavType<Array<String>?>() {
val stringArrayNavType: DestinationsNavType<Array<String>?> = DestinationsStringArrayNavType

internal object DestinationsStringArrayNavType : DestinationsNavType<Array<String>?>() {

override fun put(bundle: Bundle, key: String, value: Array<String>?) {
bundle.putStringArray(key, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import com.ramcosta.composedestinations.navargs.primitives.DECODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.ENCODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.encodedComma

object DestinationsBooleanArrayListNavType : DestinationsNavType<ArrayList<Boolean>?>() {
val booleanArrayListNavType: DestinationsNavType<ArrayList<Boolean>?> = DestinationsBooleanArrayListNavType

internal object DestinationsBooleanArrayListNavType : DestinationsNavType<ArrayList<Boolean>?>() {

override fun put(bundle: Bundle, key: String, value: ArrayList<Boolean>?) {
bundle.putBooleanArray(key, value.toArray())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import com.ramcosta.composedestinations.navargs.primitives.DECODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.ENCODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.encodedComma

object DestinationsFloatArrayListNavType : DestinationsNavType<ArrayList<Float>?>() {
val floatArrayListNavType: DestinationsNavType<ArrayList<Float>?> = DestinationsFloatArrayListNavType

internal object DestinationsFloatArrayListNavType : DestinationsNavType<ArrayList<Float>?>() {

override fun put(bundle: Bundle, key: String, value: ArrayList<Float>?) {
bundle.putFloatArray(key, value.toArray())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import com.ramcosta.composedestinations.navargs.primitives.DECODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.ENCODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.encodedComma

object DestinationsIntArrayListNavType : DestinationsNavType<ArrayList<Int>?>() {
val intArrayListNavType: DestinationsNavType<ArrayList<Int>?> = DestinationsIntArrayListNavType

internal object DestinationsIntArrayListNavType : DestinationsNavType<ArrayList<Int>?>() {

override fun put(bundle: Bundle, key: String, value: ArrayList<Int>?) {
bundle.putIntegerArrayList(key, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import com.ramcosta.composedestinations.navargs.primitives.DECODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.ENCODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.encodedComma

object DestinationsLongArrayListNavType : DestinationsNavType<ArrayList<Long>?>() {
val longArrayListNavType: DestinationsNavType<ArrayList<Long>?> = DestinationsLongArrayListNavType

internal object DestinationsLongArrayListNavType : DestinationsNavType<ArrayList<Long>?>() {

override fun put(bundle: Bundle, key: String, value: ArrayList<Long>?) {
bundle.putLongArray(key, value.toArray())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import com.ramcosta.composedestinations.navargs.primitives.ENCODED_NULL
import com.ramcosta.composedestinations.navargs.primitives.encodedComma
import com.ramcosta.composedestinations.navargs.utils.encodeForRoute

object DestinationsStringArrayListNavType : DestinationsNavType<ArrayList<String>?>() {
val stringArrayListNavType: DestinationsNavType<ArrayList<String>?> = DestinationsStringArrayListNavType

internal object DestinationsStringArrayListNavType : DestinationsNavType<ArrayList<String>?>() {

override fun put(bundle: Bundle, key: String, value: ArrayList<String>?) {
bundle.putStringArrayList(key, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import androidx.compose.runtime.Immutable
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import com.ramcosta.composedestinations.navargs.DestinationsNavType
import com.ramcosta.composedestinations.navargs.primitives.DestinationsBooleanNavType
import com.ramcosta.composedestinations.navargs.primitives.DestinationsFloatNavType
import com.ramcosta.composedestinations.navargs.primitives.DestinationsIntNavType
import com.ramcosta.composedestinations.navargs.primitives.DestinationsLongNavType
import com.ramcosta.composedestinations.navargs.primitives.DestinationsStringNavType
import com.ramcosta.composedestinations.navigation.DestinationDependenciesContainer
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.result.ResultBackNavigator
Expand All @@ -15,6 +20,7 @@ import com.ramcosta.composedestinations.result.resultBackNavigator
import com.ramcosta.composedestinations.result.resultRecipient
import com.ramcosta.composedestinations.spec.DestinationSpec
import com.ramcosta.composedestinations.spec.TypedDestinationSpec
import kotlin.reflect.KClass

/**
* Scope where a destination screen will be called in.
Expand Down Expand Up @@ -108,3 +114,44 @@ interface AnimatedDestinationScope<T> : DestinationScope<T>, AnimatedVisibilityS
*/
@Immutable
interface BottomSheetDestinationScope<T> : DestinationScope<T>, ColumnScope

//region deprecated
@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated(
level = DeprecationLevel.ERROR,
message = "\n" +
"Use the `resultRecipient` version that takes in a `DestinationsNavType` parameter!\n" +
"Example: for Boolean results, it's `booleanNavType`, for custom types it's `customTypeClassNameNavType`."
)
@Composable
inline fun <reified D : DestinationSpec, R, reified T : R & Any>
DestinationScopeWithNoDependencies<*>.resultRecipient(): ResultRecipient<D, R> =
resultRecipient(tryGettingNavType(T::class))

@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated(
level = DeprecationLevel.ERROR,
message = "\n" +
"Use the `resultBackNavigator` version that takes in a `DestinationsNavType` parameter!\n" +
"Example: for Boolean results, it's `booleanNavType`, for custom types it's `customTypeClassNameNavType`."
)
@Composable
inline fun <R, reified T : R & Any> DestinationScopeWithNoDependencies<*>.resultBackNavigator(): ResultBackNavigator<R> =
resultBackNavigator(tryGettingNavType(T::class))

@PublishedApi
@Suppress("UNCHECKED_CAST")
internal fun <R, T : R & Any> tryGettingNavType(kClass: KClass<T>): DestinationsNavType<in R> {
return when (kClass) {
String::class -> DestinationsStringNavType
Boolean::class -> DestinationsBooleanNavType
Float::class -> DestinationsFloatNavType
Int::class -> DestinationsIntNavType
Long::class -> DestinationsLongNavType
else -> error(
"Use the `resultBackNavigator`/`resultRecipient` version that takes in a `DestinationsNavType` parameter!\n" +
"Example: for Boolean results, it's `booleanNavType`, for custom types it's `customTypeClassNameNavType`."
)
} as DestinationsNavType<in R>
}
//
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import com.ramcosta.composedestinations.bottomsheet.utils.bottomSheetComposable
import com.ramcosta.composedestinations.generated.featurey.navtype.internalBackResultNavType
import com.ramcosta.composedestinations.manualcomposablecalls.ManualComposableCallsBuilder
import com.ramcosta.composedestinations.manualcomposablecalls.composable
import com.ramcosta.composedestinations.navargs.primitives.DestinationsBooleanNavType
import com.ramcosta.composedestinations.navargs.primitives.booleanNavType
import com.ramcosta.composedestinations.navigation.dependency
import com.ramcosta.composedestinations.navigation.destination
import com.ramcosta.composedestinations.navigation.getBackStackEntry
Expand Down Expand Up @@ -111,7 +111,7 @@ private fun ManualComposableCallsBuilder.greetingScreen(
uiEvents = vm as GreetingUiEvents,
uiState = vm as GreetingUiState,
test = "testing param from NavHost",
resultRecipient = resultRecipient(DestinationsBooleanNavType),
resultRecipient = resultRecipient(booleanNavType),
featYResult = resultRecipient(internalBackResultNavType),
)
}
Expand Down Expand Up @@ -143,7 +143,7 @@ fun SampleAppAnimatedNavHostExample(
drawerController = drawerController,
uiEvents = vm as GreetingUiEvents,
uiState = vm as GreetingUiState,
resultRecipient = resultRecipient(DestinationsBooleanNavType),
resultRecipient = resultRecipient(booleanNavType),
featYResult = resultRecipient(internalBackResultNavType),
testProfileDeepLink = testProfileDeepLink,
test = "testing param from NavHost",
Expand All @@ -156,7 +156,7 @@ fun SampleAppAnimatedNavHostExample(

dialogComposable(GoToProfileConfirmationDestination) {
GoToProfileConfirmation(
resultNavigator = resultBackNavigator(navController, DestinationsBooleanNavType)
resultNavigator = resultBackNavigator(navController, booleanNavType)
)
}

Expand Down

0 comments on commit d70d005

Please sign in to comment.