Skip to content

Commit 79833ee

Browse files
committed
ActivityExtensions: Use (?:) expressions instead of defaultValue arguments.
Signed-off-by: Fung <fython@163.com>
1 parent 2a55874 commit 79833ee

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

demo/src/main/kotlin/moe/feng/kotlinyan/ActivityExtDemoFragment.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,20 @@ class ActivityExtDemoFragment : Fragment(), AndroidExtensions, AppCompatExtensio
7474
val intent = Intent()
7575
intent["intExtra"] = 15
7676
intent["stringExtra"] = "Hello"
77-
Log.i(TAG, intent["intExtra"]?.asInt()?.toString())
78-
Log.i(TAG, intent["stringExtra"]?.asString())
77+
Log.i(TAG, "intExtra=" + (intent["intExtra"]?.asInt() ?: -1))
78+
Log.i(TAG, "stringExtra=" + (intent["stringExtra"]?.asString() ?: "defaultValue"))
79+
Log.i(TAG, "nonExistExtra=" + (intent["nonExistExtra"]?.asString() ?: "defaultValue"))
80+
}
81+
view[R.id.btn_test_bundle].onClick {
82+
val bundle = Bundle()
83+
bundle["int"] = 18
84+
bundle["string"] = "Nyanyanyanyan"
85+
bundle["stringArray"] = arrayOf("test", "lalala")
86+
Log.i(TAG, "intExtra=" + (bundle["int"] as? Int ?: -1))
87+
Log.i(TAG, "string=" + (bundle["string"] as? String ?: "defaultValue"))
88+
Log.i(TAG, "stringArray=" +
89+
((bundle["stringArray"] as? Array<String>)?.reduce { a, b -> "$a,$b" } ?: "defaultValue"))
90+
Log.i(TAG, "nonExist=" + (bundle["nonExist"] as? String ?: "defaultValue"))
7991
}
8092
}
8193

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,25 @@ interface AndroidExtensions : ActivityExtensions, ViewExtensions, ResourcesExten
5555

5656
fun asCharSequence(): CharSequence = intent.getCharSequenceExtra(key)
5757

58-
fun asInt(defValue: Int): Int = intent.getIntExtra(key, defValue)
58+
fun asInt(): Int = intent.getIntExtra(key, 0)
5959

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)
60+
fun asBoolean(): Boolean = intent.getBooleanExtra(key, false)
6661

67-
fun asChar(defValue: Char): Char = intent.getCharExtra(key, defValue)
62+
fun asChar(): Char = intent.getCharExtra(key, ' ')
6863

69-
fun asByte(defValue: Byte = 0): Byte = intent.getByteExtra(key, defValue)
64+
fun asByte(): Byte = intent.getByteExtra(key, 0)
7065

7166
fun asByteArray(): ByteArray = intent.getByteArrayExtra(key)
7267

7368
fun asBundle(): Bundle = intent.getBundleExtra(key)
7469

75-
fun asDouble(defValue: Double = .0): Double = intent.getDoubleExtra(key, defValue)
70+
fun asDouble(): Double = intent.getDoubleExtra(key, .0)
7671

77-
fun asFloat(defValue: Float = 0F): Float = intent.getFloatExtra(key, defValue)
72+
fun asFloat(): Float = intent.getFloatExtra(key, 0F)
7873

79-
fun asLong(defValue: Long = 0): Long = intent.getLongExtra(key, defValue)
74+
fun asLong(): Long = intent.getLongExtra(key, 0)
8075

81-
fun asShort(defValue: Short = 0): Short = intent.getShortExtra(key, defValue)
76+
fun asShort(): Short = intent.getShortExtra(key, 0)
8277

8378
fun asBooleanArray(): BooleanArray = intent.getBooleanArrayExtra(key)
8479

0 commit comments

Comments
 (0)