Skip to content

Commit

Permalink
Update whatIfHasSerializableExtra using whatIfNotNullAs
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Nov 8, 2020
1 parent 8a03221 commit e3bafdc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.os.Bundle
import android.os.Parcelable
import com.skydoves.whatif.WhatIfInlineOnly
import com.skydoves.whatif.whatIfNotNull
import com.skydoves.whatif.whatIfNotNullAs
import java.io.Serializable

/**
Expand Down Expand Up @@ -180,11 +181,11 @@ inline fun Activity.whatIfHasCharSequenceExtra(
*/
@JvmSynthetic
@WhatIfInlineOnly
inline fun Activity.whatIfHasSerializableExtra(
inline fun <reified T : Serializable> Activity.whatIfHasSerializableExtra(
name: String,
whatIf: (Serializable) -> Unit
whatIf: (T) -> Unit
) {
this.intent.getSerializableExtra(name).whatIfNotNull(
this.intent.getSerializableExtra(name).whatIfNotNullAs<T>(
whatIf = { whatIf(it) },
whatIfNot = { }
)
Expand All @@ -198,12 +199,12 @@ inline fun Activity.whatIfHasSerializableExtra(
*/
@JvmSynthetic
@WhatIfInlineOnly
inline fun Activity.whatIfHasSerializableExtra(
inline fun <reified T : Serializable> Activity.whatIfHasSerializableExtra(
name: String,
whatIf: (Serializable) -> Unit,
whatIf: (T) -> Unit,
whatIfNot: () -> Unit
) {
this.intent.getSerializableExtra(name).whatIfNotNull(
this.intent.getSerializableExtra(name).whatIfNotNullAs<T>(
whatIf = { whatIf(it) },
whatIfNot = { whatIfNot() }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,23 @@ class WhatIfActivityTest {

assertNull(poster)
}

@Test
fun whatIfHasSerializableExtraTest() {
var poster: PosterSerializable? = null

this.mainTestActivity.whatIfHasSerializableExtra<PosterSerializable>("serializable") {
poster = it
}

assertThat(poster, `is`(PosterSerializable.create()))

this.mainTestActivity.whatIfHasSerializableExtra<PosterSerializable>(
name = "null",
whatIf = { poster = it },
whatIfNot = { poster = null }
)

assertNull(poster)
}
}

0 comments on commit e3bafdc

Please sign in to comment.