NeoDay Android SDK - Survey

Handling Survey view with SurveyListener

The default behavior of the SDK is to show a Survey screen whenever one of the SDK's screens is currently visible. However you can implement the SurveyListener in order to control when a Survey screen is displayed. If you are using the Custom Event Trigger see the section When using Custom Events to trigger Survey for how to deal with Survey being displayed.

class ExampleActivity : AppCompatActivity() {
    private val viewModel: ExampleActivityViewModel by viewModels { ExampleActivityViewModelFactory() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Set the Survey listener
        exampleApp.neoDaySDK.surveyListener = object : SurveyListener {
            override fun receivedSurvey(controller: Result<NeoDayViewController>) {
                try {
                    viewModel.surveyViewController = result.getOrThrow()
                    viewModel.showSurvey.value = true
                } catch (ex: Exception) {
                    ex.printStackTrace()
                }
            }
        }

        setContent {
            MaterialTheme {
                val navHostController = rememberNavController()

                if(viewModel.showSurvey.value) {
                    viewModel.showSurvey.value = false
                    navHostController.navigate("SURVEY")
                }

                Navigation(
                    navHostController = navHostController,
                    viewModel = viewModel,
                    modifier = Modifier.fillMaxSize()
                )
            }
        }
    }
}

@Composable
fun Navigation(
    navHostController: NavHostController,
    viewModel: MainActivityViewModel,
    modifier: Modifier = Modifier
) {
    NavHost(
        navController = navHostController,
        startDestination = "<start destination>",
        modifier = modifier
    ) {
        // ...
        composable("SURVEY") {
            viewModel.surveyViewController?.let {
                it.backButtonListener = { navHostController.navigateUp() }
                Column(modifier = Modifier.fillMaxSize()) {
                    it.getView().invoke()
                }
            }
            
            SubscribeToLifecycle(key = Unit) { event ->
                if (event == Lifecycle.Event.ON_DESTROY) {
                    mainActivityViewModel.surveyViewController = null
                }
            }
        }
        // ...
    }
}

@Composable
fun SubscribeToLifecycle(
    key: Any?,
    lifecycle: Lifecycle = LocalLifecycleOwner.current.lifecycle,
    onNewEvent: (Lifecycle.Event) -> Unit
) {
    DisposableEffect(key) {
        val observer = LifecycleEventObserver { _, event ->
            onNewEvent.invoke(event)
        }
        lifecycle.addObserver(observer)
        onDispose {
            lifecycle.removeObserver(observer)
        }
    }
}

When using Custom Events to trigger Survey

Caching Survey

Whenever Custom Event Triggers are used to trigger a Survey we advise you to handle showing the Survey yourselve by caching it first. And then to show it when its appropriate to do so within your app.

class ExampleActivity : AppCompatActivity() {
    private val viewModel: ExampleActivityViewModel by viewModels { ExampleActivityViewModelFactory() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Set the Survey listener
        exampleApp.neoDaySDK.surveyListener = object : SurveyListener {
            override fun receivedSurvey(controller: Result<NeoDayViewController>) {
                try {
                    if(<some condition that requires caching>) {
                        // Cache the controller and present it at later time inside the app.
                        viewModel.pendingSurveyViewController = result.getOrThrow()
                    } else {
                        // Regular Survey handling
                    }
                } catch (ex: Exception) {
                    ex.printStackTrace()
                }
            }
        }
        ...
    }
}

Clearing the Survey cache

Whenever you remove the stored token from the NeoDay SDK when logging out of your app, the Survey cache will also need to be cleared. In case your user logs back in with another account while the app is still open, you don't want to accidentally show a Survey that was meant for the previous logged in user.

viewModel.pendingSurveyViewController?.let {
    viewModel.surveyViewController = it
    // Clear the Survey cache
    viewModel.pendingSurveyViewController = null
    viewModel.showSurvey.value = true
}