Skip to content

Commit d6cf1c9

Browse files
yonghanJubngsh
andcommitted
✨ serializable, parcelable 구현 객체 직렬화 확장함수 생성
Co-authored-by: bngsh <[email protected]>
1 parent 5948ea1 commit d6cf1c9

File tree

1 file changed

+29
-2
lines changed
  • presentation/src/main/java/com/whyranoid/presentation/util

1 file changed

+29
-2
lines changed

presentation/src/main/java/com/whyranoid/presentation/util/Extensions.kt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.whyranoid.presentation.util
22

3+
import android.content.Intent
4+
import android.os.Build
5+
import android.os.Bundle
6+
import android.os.Parcelable
37
import androidx.lifecycle.Lifecycle
48
import androidx.lifecycle.LifecycleOwner
59
import androidx.lifecycle.lifecycleScope
610
import androidx.lifecycle.repeatOnLifecycle
711
import kotlinx.coroutines.launch
12+
import java.io.Serializable
813
import java.text.SimpleDateFormat
9-
import java.util.Date
10-
import java.util.Locale
14+
import java.util.*
1115

1216
fun LifecycleOwner.repeatWhenUiStarted(block: suspend () -> Unit) {
1317
lifecycleScope.launch {
@@ -21,3 +25,26 @@ fun Date.dateToString(format: String): String {
2125
val formatter = SimpleDateFormat(format, Locale.getDefault())
2226
return formatter.format(this)
2327
}
28+
29+
inline fun <reified T : Serializable> Intent.getSerializableData(key: String): T? = when {
30+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getSerializableExtra(
31+
key,
32+
T::class.java
33+
)
34+
else -> @Suppress("DEPRECATION") getSerializableExtra(key) as? T
35+
}
36+
37+
inline fun <reified T : Serializable> Bundle.getSerializableData(key: String): T? = when {
38+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getSerializable(key, T::class.java)
39+
else -> @Suppress("DEPRECATION") getSerializable(key) as? T
40+
}
41+
42+
inline fun <reified T : Parcelable> Intent.getParcelableData(key: String): T? = when {
43+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getParcelableExtra(key, T::class.java)
44+
else -> @Suppress("DEPRECATION") getSerializableExtra(key) as? T
45+
}
46+
47+
inline fun <reified T : Parcelable> Bundle.getParcelableData(key: String): T? = when {
48+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getParcelable(key, T::class.java)
49+
else -> @Suppress("DEPRECATION") getSerializable(key) as? T
50+
}

0 commit comments

Comments
 (0)