|
import android.os.Build |
|
import android.os.Bundle |
|
import android.os.Parcelable |
|
import androidx.navigation.NavType |
|
import kotlinx.parcelize.Parcelize |
|
import kotlinx.serialization.Serializable |
|
import kotlinx.serialization.encodeToString |
|
import kotlinx.serialization.decodeFromString |
|
import kotlinx.serialization.json.Json |
|
|
|
/** |
|
* A simple data class representing a person, which can be passed between navigation destinations. |
|
* |
|
* @property name The name of the person. |
|
* @property age The age of the person. |
|
*/ |
|
@Serializable // Enables JSON serialization/deserialization using kotlinx.serialization |
|
@Parcelize // Implements Parcelable without boilerplate |
|
data class Person(val name: String, val age: Int) : Parcelable |
|
|
|
/** |
|
* Creates a custom [NavType] that handles navigation arguments of type [T], where [T] must be [Parcelable]. |
|
* This allows passing complex objects like data classes through Jetpack Navigation arguments. |
|
* |
|
* The value is serialized into a JSON string and deserialized back into an object using kotlinx.serialization. |
|
* |
|
* @return A custom [NavType] that knows how to store and retrieve [T] from a [Bundle] and serialize it. |
|
*/ |
|
inline fun <reified T : Parcelable> NavType.Companion.mapper(): NavType<T> { |
|
return object : NavType<T>(isNullableAllowed = false) { |
|
|
|
/** |
|
* Inserts the [value] into the [bundle] under the given [key]. |
|
*/ |
|
override fun put(bundle: Bundle, key: String, value: T) { |
|
bundle.putParcelable(key, value) |
|
} |
|
|
|
/** |
|
* Retrieves a value of type [T] from the [bundle] using the given [key]. |
|
* Supports backward compatibility for older Android versions. |
|
*/ |
|
override fun get(bundle: Bundle, key: String): T? { |
|
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { |
|
@Suppress("DEPRECATION") |
|
bundle.getParcelable(key) |
|
} else { |
|
bundle.getParcelable(key, T::class.java) |
|
} |
|
} |
|
|
|
/** |
|
* Parses a JSON [value] string and returns the deserialized object of type [T]. |
|
*/ |
|
override fun parseValue(value: String): T { |
|
return Json.decodeFromString<T>(value) |
|
} |
|
|
|
/** |
|
* Serializes the [value] into a JSON string. |
|
*/ |
|
override fun serializeAsValue(value: T): String { |
|
return Json.encodeToString(value) |
|
} |
|
|
|
/** |
|
* Returns the class name of [T] as the name for the navigation argument type. |
|
*/ |
|
override val name = T::class.java.name |
|
} |
|
} |
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import androidx.navigation.NavType
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
/**
Parcelable
است برای ذخیره در Bundle.Serializable
برای تبدیل به JSON هنگام انتقال در route.*/
@serializable
@parcelize
data class Person(
val name: String,
val age: Int
) : Parcelable
/**
Parcelable
هستند.inline
وreified
به ما اجازه میدهد که نوع راNavType.mapper<MyType>()
نوع مورد نظر را بسازیم.NavType<T>
که توانایی تبدیل داده به/از Bundle و JSON را دارد.*/
inline fun NavType.Companion.mapper(): NavType {
return object : NavType(
isNullableAllowed = false
) {
/**
* ذخیرهی آبجکت در
Bundle
با استفاده ازputParcelable
.*
* @param bundle بستهای که داده باید در آن ذخیره شود.
* @param key کلیدی که داده با آن در Bundle ذخیره میشود.
* @param value شیء مورد نظر برای ذخیره.
*/
}