Skip to content

Commit 45379e9

Browse files
committed
refactor: provide default keys for extras or arguments
1 parent 508e40e commit 45379e9

File tree

1 file changed

+15
-54
lines changed

1 file changed

+15
-54
lines changed

extension/src/main/kotlin/co/anitrend/arch/extension/ext/ControllerExtensions.kt

+15-54
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ import timber.log.Timber
2525
/**
2626
* Lazy intent parameters for saved state handle
2727
*
28-
* @param key lookup key for the embedded item in the [SavedStateHandle.get]
2928
* @param default default value to use when key does not exist
29+
* @param key lookup key for the embedded item in the [SavedStateHandle.get]
3030
*
3131
* @return [Lazy] of the target type
3232
*/
3333
inline fun <reified T> SavedStateHandle.extra(
34-
key: String,
3534
default: T,
35+
key: String = T::class.java.simpleName,
3636
): Lazy<T> =
3737
lazy(PUBLICATION) {
3838
try {
@@ -53,30 +53,21 @@ inline fun <reified T> SavedStateHandle.extra(
5353
*
5454
* @return [Lazy] of the target type
5555
*/
56-
inline fun <reified T> SavedStateHandle.extra(key: String): Lazy<T?> =
57-
lazy(PUBLICATION) {
58-
try {
59-
if (contains(key)) {
60-
get(key) as? T
61-
} else {
62-
null
63-
}
64-
} catch (e: Exception) {
65-
error(e)
66-
}
67-
}
56+
inline fun <reified T> SavedStateHandle.extra(
57+
key: String = T::class.java.simpleName
58+
): Lazy<T?> = extra(key = key, default = null)
6859

6960
/**
7061
* Lazy intent parameters for fragment activities
7162
*
72-
* @param key lookup key for the embedded item in the [FragmentActivity.getIntent]
7363
* @param default default value to use when key does not exist
64+
* @param key lookup key for the embedded item in the [FragmentActivity.getIntent]
7465
*
7566
* @return [Lazy] of the target type
7667
*/
7768
inline fun <reified T> FragmentActivity.extra(
78-
key: String,
7969
default: T,
70+
key: String = T::class.java.simpleName,
8071
): Lazy<T> =
8172
lazy(PUBLICATION) {
8273
try {
@@ -103,36 +94,21 @@ inline fun <reified T> FragmentActivity.extra(
10394
*
10495
* @return [Lazy] of the target type
10596
*/
106-
inline fun <reified T> FragmentActivity.extra(key: String): Lazy<T?> =
107-
lazy(PUBLICATION) {
108-
try {
109-
if (intent?.extras?.containsKey(key) == true) {
110-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
111-
intent?.extras?.getParcelable(key, T::class.java) as T
112-
} else {
113-
@Suppress("DEPRECATION")
114-
intent?.extras?.get(key) as T
115-
}
116-
} else {
117-
Timber.w("$this does not have an argument with key: $key")
118-
null
119-
}
120-
} catch (e: Exception) {
121-
error(e)
122-
}
123-
}
97+
inline fun <reified T> FragmentActivity.extra(
98+
key: String = T::class.java.simpleName
99+
): Lazy<T?> = extra(key = key, default = null)
124100

125101
/**
126102
* Lazy intent parameters for fragments
127103
*
128-
* @param key lookup key for the embedded item in the [Fragment.getArguments]
129104
* @param default default value to use when key does not exist
105+
* @param key lookup key for the embedded item in the [Fragment.getArguments]
130106
*
131107
* @return [Lazy] of the target type
132108
*/
133109
inline fun <reified T> Fragment.argument(
134-
key: String,
135110
default: T,
111+
key: String = T::class.java.simpleName,
136112
): Lazy<T> =
137113
lazy(PUBLICATION) {
138114
try {
@@ -159,21 +135,6 @@ inline fun <reified T> Fragment.argument(
159135
*
160136
* @return [Lazy] of the target type
161137
*/
162-
inline fun <reified T> Fragment.argument(key: String): Lazy<T?> =
163-
lazy(PUBLICATION) {
164-
try {
165-
if (arguments?.containsKey(key) == true) {
166-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
167-
arguments?.getParcelable(key, T::class.java) as T
168-
} else {
169-
@Suppress("DEPRECATION")
170-
arguments?.get(key) as T
171-
}
172-
} else {
173-
Timber.w("$this does not have an argument with key: $key")
174-
null
175-
}
176-
} catch (e: Exception) {
177-
error(e)
178-
}
179-
}
138+
inline fun <reified T> Fragment.argument(
139+
key: String = T::class.java.simpleName
140+
): Lazy<T?> = argument(key = key, default = null)

0 commit comments

Comments
 (0)