Skip to content

Commit 26a346b

Browse files
committed
AndroidExtensions: Intent extras getter
Signed-off-by: Fung <fython@163.com>
1 parent 6aa67b6 commit 26a346b

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

kotlinyan-common/src/main/kotlin/moe/feng/kotlinyan/common/AndroidExtensions.kt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,72 @@ interface AndroidExtensions : ActivityExtensions, ViewExtensions, ResourcesExten
4646
}
4747
}
4848

49+
operator fun Intent.get(key: String) : IntentExtraGetter?
50+
= if (hasExtra(key)) IntentExtraGetter(this, key) else null
51+
52+
class IntentExtraGetter internal constructor(private val intent: Intent, val key: String) {
53+
54+
fun asString(): String = intent.getStringExtra(key)
55+
56+
fun asCharSequence(): CharSequence = intent.getCharSequenceExtra(key)
57+
58+
fun asInt(defValue: Int): Int = intent.getIntExtra(key, defValue)
59+
60+
fun asInt(): Int? {
61+
val value = intent.getIntExtra(key, Int.MIN_VALUE)
62+
return if (value != Int.MIN_VALUE) value else null
63+
}
64+
65+
fun asBoolean(defValue: Boolean = false): Boolean = intent.getBooleanExtra(key, defValue)
66+
67+
fun asChar(defValue: Char): Char = intent.getCharExtra(key, defValue)
68+
69+
fun asByte(defValue: Byte = 0): Byte = intent.getByteExtra(key, defValue)
70+
71+
fun asByteArray(): ByteArray = intent.getByteArrayExtra(key)
72+
73+
fun asBundle(): Bundle = intent.getBundleExtra(key)
74+
75+
fun asDouble(defValue: Double = .0): Double = intent.getDoubleExtra(key, defValue)
76+
77+
fun asFloat(defValue: Float = 0F): Float = intent.getFloatExtra(key, defValue)
78+
79+
fun asLong(defValue: Long = 0): Long = intent.getLongExtra(key, defValue)
80+
81+
fun asShort(defValue: Short = 0): Short = intent.getShortExtra(key, defValue)
82+
83+
fun asBooleanArray(): BooleanArray = intent.getBooleanArrayExtra(key)
84+
85+
fun asCharArray(): CharArray = intent.getCharArrayExtra(key)
86+
87+
fun asDoubleArray(): DoubleArray = intent.getDoubleArrayExtra(key)
88+
89+
fun asIntArray(): IntArray = intent.getIntArrayExtra(key)
90+
91+
fun asFloatArray(): FloatArray = intent.getFloatArrayExtra(key)
92+
93+
fun asLongArray(): LongArray = intent.getLongArrayExtra(key)
94+
95+
fun asShortArray(): ShortArray = intent.getShortArrayExtra(key)
96+
97+
fun asCharSequenceArray(): Array<out CharSequence> = intent.getCharSequenceArrayExtra(key)
98+
99+
fun <T : Parcelable> asParcelable(): T = intent.getParcelableExtra<T>(key)
100+
101+
fun asSerializable(): Serializable = intent.getSerializableExtra(key)
102+
103+
fun asParacelableArray(): Array<out Parcelable> = intent.getParcelableArrayExtra(key)
104+
105+
fun <T : Parcelable> asParacelableList(): java.util.ArrayList<T> = intent.getParcelableArrayListExtra<T>(key)
106+
107+
fun asCharSequenceList(): java.util.ArrayList<CharSequence> = intent.getCharSequenceArrayListExtra(key)
108+
109+
fun asIntList(): java.util.ArrayList<Int> = intent.getIntegerArrayListExtra(key)
110+
111+
fun asStringList(): java.util.ArrayList<String> = intent.getStringArrayListExtra(key)
112+
113+
}
114+
49115
@SuppressLint("NewApi")
50116
operator fun Bundle.set(key: String, value: Any) {
51117
when (value) {

0 commit comments

Comments
 (0)