Skip to content

Commit dbe7ee9

Browse files
tasomaniacelihart
authored andcommitted
Fix a uniqueId related problem in unit tests (airbnb#258)
* Fix a uniqueId related problem in unit tests In unit tests, `onCreate` is never called. That means that the lateinit will never be instantiated. Instead, make `persistedViewId` really the persisted one (not with a default). That means it is nullable. Then when `mvrxId` is accessed for the first time, we generate the id. * Introduced `MvRxViewId` to encapsulate id save/restore logic and provide re-usability * Simplify lazy value logic in MvRxViewId
1 parent fc96461 commit dbe7ee9

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

mvrx/src/main/kotlin/com/airbnb/mvrx/BaseMvRxFragment.kt

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.airbnb.mvrx
33
import android.os.Bundle
44
import androidx.fragment.app.Fragment
55
import androidx.lifecycle.LifecycleOwner
6-
import java.util.UUID
76

87
/**
98
* Make your base Fragment class extend this to get MvRx functionality.
@@ -14,13 +13,12 @@ abstract class BaseMvRxFragment : Fragment(), MvRxView {
1413

1514
override val mvrxViewModelStore by lazy { MvRxViewModelStore(viewModelStore) }
1615

17-
final override val mvrxViewId: String by lazy { mvrxPersistedViewId }
18-
19-
private lateinit var mvrxPersistedViewId: String
16+
private val mvrxViewIdProperty = MvRxViewId()
17+
final override val mvrxViewId: String by mvrxViewIdProperty
2018

2119
override fun onCreate(savedInstanceState: Bundle?) {
2220
mvrxViewModelStore.restoreViewModels(this, savedInstanceState)
23-
mvrxPersistedViewId = savedInstanceState?.getString(PERSISTED_VIEW_ID_KEY) ?: this::class.java.simpleName + "_" + UUID.randomUUID().toString()
21+
mvrxViewIdProperty.restoreFrom(savedInstanceState)
2422
super.onCreate(savedInstanceState)
2523
}
2624

@@ -34,7 +32,7 @@ abstract class BaseMvRxFragment : Fragment(), MvRxView {
3432
override fun onSaveInstanceState(outState: Bundle) {
3533
super.onSaveInstanceState(outState)
3634
mvrxViewModelStore.saveViewModels(outState)
37-
outState.putString(PERSISTED_VIEW_ID_KEY, mvrxViewId)
35+
mvrxViewIdProperty.saveTo(outState)
3836
}
3937

4038
override fun onStart() {
@@ -44,5 +42,3 @@ abstract class BaseMvRxFragment : Fragment(), MvRxView {
4442
postInvalidate()
4543
}
4644
}
47-
48-
private const val PERSISTED_VIEW_ID_KEY = "mvrx:persisted_view_id"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.airbnb.mvrx
2+
3+
import android.os.Bundle
4+
import java.util.*
5+
import kotlin.properties.ReadOnlyProperty
6+
import kotlin.reflect.KProperty
7+
8+
class MvRxViewId : ReadOnlyProperty<MvRxView, String> {
9+
10+
private var value: String? = null
11+
12+
override fun getValue(thisRef: MvRxView, property: KProperty<*>): String {
13+
return value ?: generateUniqueId(thisRef).also { value = it }
14+
}
15+
16+
private fun generateUniqueId(thisRef: MvRxView) = thisRef::class.java.simpleName + "_" + UUID.randomUUID().toString()
17+
18+
fun saveTo(bundle: Bundle) {
19+
bundle.putString(PERSISTED_VIEW_ID_KEY, value)
20+
}
21+
22+
fun restoreFrom(bundle: Bundle?) {
23+
if (value == null) {
24+
value = bundle?.getString(PERSISTED_VIEW_ID_KEY)
25+
}
26+
}
27+
28+
companion object {
29+
private const val PERSISTED_VIEW_ID_KEY = "mvrx:persisted_view_id"
30+
}
31+
}

0 commit comments

Comments
 (0)