airbnb / mavericks

Mavericks: Android on Autopilot
https://airbnb.io/mavericks/
Apache License 2.0
5.85k stars 499 forks source link

MavericksLauncherMockActivity crashes if there's no App.Theme or if fragments are Hilt fragments #671

Open kenyee opened 1 year ago

kenyee commented 1 year ago

From testing w/ a small Compose test app where there's no App theme, this activity can crash with this error:

UnsupportedOperationException: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f030003 a=-1}, theme={InheritanceMap=[id=0x103013fandroid:style/Theme.DeviceDefault.Light.DarkActionBar, 

Fix is to add an app theme but probably should be documented.

Also, when launching a Hilt Fragment MockableMavericksView from MavericksLauncherMockActivity, this happens:

java.lang.IllegalStateException: Hilt Fragments must be attached to an @AndroidEntryPoint Activity. Found: class com.airbnb.mvrx.launcher.MavericksLauncherMockActivity

Wrapping the MavericksLauncherMockActivity like this had the same error:

@AndroidEntryPoint
class HiltMavericksLauncherMockActivity : MavericksLauncherActivity() {

    companion object {
        private inline fun <reified T> Context.buildIntent(initializer: Intent.() -> Unit = {}): Intent {
            return Intent(this, T::class.java).apply(initializer)
        }

        fun show(context: Context) {
            context.startActivity(context.buildIntent<HiltMavericksLauncherMockActivity>())
        }
    }
}

even when launching with "HiltMavericksLauncherMockActivity.show(this)"

kenyee commented 1 year ago

This looks like just a docs issue...doing this works fine and probably should be part of the mavericks-hilt module?

@AndroidEntryPoint
class HiltMavericksLauncherMockActivity : MavericksBaseLauncherActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (savedInstanceState == null) {
            MavericksLauncherMockActivity.showNextMockFromActivity(
                activity = this,
                showView = { mavericksView ->
                    // Use whatever custom code you want to show your Mavericks View
                    setFragment(mavericksView as Fragment, commitNow = true)
                },
            )
        }
    }
}