Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPS Survey Gating #5356

Merged
merged 19 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.appcompat.widget.Toolbar
import androidx.core.view.doOnPreDraw
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.Transformations
import org.oppia.android.R
import org.oppia.android.app.activity.ActivityScope
Expand Down Expand Up @@ -297,11 +298,7 @@ class ExplorationActivityPresenter @Inject constructor(
}
is AsyncResult.Success -> {
oppiaLogger.d("ExplorationActivity", "Successfully stopped exploration")
if (isCompletion) {
maybeShowSurveyDialog(profileId, topicId)
} else {
backPressActivitySelector()
}
maybeShowSurveyDialog(profileId, topicId)
}
}
}
Expand Down Expand Up @@ -528,42 +525,48 @@ class ExplorationActivityPresenter @Inject constructor(
}

private fun maybeShowSurveyDialog(profileId: ProfileId, topicId: String) {
surveyGatingController.maybeShowSurvey(profileId, topicId).toLiveData()
.observe(
activity
) { gatingResult ->
when (gatingResult) {
is AsyncResult.Pending -> {
oppiaLogger.d("ExplorationActivity", "A gating decision is pending")
}
is AsyncResult.Failure -> {
oppiaLogger.e(
"ExplorationActivity",
"Failed to retrieve gating decision",
gatingResult.error
)
backPressActivitySelector()
}
is AsyncResult.Success -> {
if (gatingResult.value) {
val dialogFragment =
SurveyWelcomeDialogFragment.newInstance(
profileId,
topicId,
explorationId,
SURVEY_QUESTIONS
)
val transaction = activity.supportFragmentManager.beginTransaction()
transaction
.add(dialogFragment, TAG_SURVEY_WELCOME_DIALOG)
.addToBackStack(null)
.commit()
} else {
val liveData = surveyGatingController.maybeShowSurvey(profileId, topicId).toLiveData()
BenHenning marked this conversation as resolved.
Show resolved Hide resolved
liveData.observe(
activity,
object : Observer<AsyncResult<Boolean>> {
override fun onChanged(gatingResult: AsyncResult<Boolean>?) {
when (gatingResult) {
is AsyncResult.Pending -> {
oppiaLogger.d("ExplorationActivity", "A gating decision is pending")
}
is AsyncResult.Failure -> {
oppiaLogger.e(
"ExplorationActivity",
"Failed to retrieve gating decision",
gatingResult.error
)
backPressActivitySelector()
}
is AsyncResult.Success -> {
if (gatingResult.value) {
val dialogFragment =
SurveyWelcomeDialogFragment.newInstance(
profileId,
topicId,
explorationId,
SURVEY_QUESTIONS
)
val transaction = activity.supportFragmentManager.beginTransaction()
transaction
.add(dialogFragment, TAG_SURVEY_WELCOME_DIALOG)
.addToBackStack(null)
.commit()

// Changes to underlying DataProviders will update the gating result.
liveData.removeObserver(this)
} else {
backPressActivitySelector()
}
}
}
}
}
)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ class StateFragmentPresenter @Inject constructor(
fun onReturnToTopicButtonClicked() {
hideKeyboard()
markExplorationCompleted()
maybeShowSurveyDialog(profileId, topicId)
}

private fun showOrHideAudioByState(state: State) {
Expand Down Expand Up @@ -455,13 +454,17 @@ class StateFragmentPresenter @Inject constructor(
fun getExplorationCheckpointState() = explorationCheckpointState

private fun markExplorationCompleted() {
storyProgressController.recordCompletedChapter(
val markStoryCompletedLivedata = storyProgressController.recordCompletedChapter(
profileId,
topicId,
storyId,
explorationId,
oppiaClock.getCurrentTimeMs()
)
).toLiveData()

// Only check gating result when the previous operation has completed because gating depends on
// result of saving the time spent in the exploration, at the end of the exploration.
markStoryCompletedLivedata.observe(fragment, { maybeShowSurveyDialog(profileId, topicId) })
}

private fun showHintsAndSolutions(helpIndex: HelpIndex, isCurrentStatePendingState: Boolean) {
Expand Down Expand Up @@ -535,10 +538,11 @@ class StateFragmentPresenter @Inject constructor(
}

private fun maybeShowSurveyDialog(profileId: ProfileId, topicId: String) {
surveyGatingController.maybeShowSurvey(profileId, topicId).toLiveData()
.observe(
activity,
{ gatingResult ->
val liveData = surveyGatingController.maybeShowSurvey(profileId, topicId).toLiveData()
liveData.observe(
activity,
object : Observer<AsyncResult<Boolean>> {
override fun onChanged(gatingResult: AsyncResult<Boolean>?) {
when (gatingResult) {
is AsyncResult.Pending -> {
oppiaLogger.d("StateFragment", "A gating decision is pending")
Expand All @@ -565,14 +569,18 @@ class StateFragmentPresenter @Inject constructor(
transaction
.add(dialogFragment, TAG_SURVEY_WELCOME_DIALOG)
.commitNow()

// Changes to underlying DataProviders will update the gating result.
// liveData.removeObserver(this)
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
} else {
(activity as StopStatePlayingSessionWithSavedProgressListener)
.deleteCurrentProgressAndStopSession(isCompletion = true)
}
}
}
}
)
}
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.oppia.android.app.player.state.testing
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import org.oppia.android.R
import org.oppia.android.app.activity.ActivityScope
import org.oppia.android.app.model.ProfileId
Expand Down Expand Up @@ -98,7 +97,7 @@ class StateFragmentTestActivityPresenter @Inject constructor(
}
startPlayingProvider.toLiveData().observe(
activity,
Observer<AsyncResult<Any?>> { result ->
{ result ->
when (result) {
is AsyncResult.Pending -> oppiaLogger.d(TEST_ACTIVITY_TAG, "Loading exploration")
is AsyncResult.Failure ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class SurveyWelcomeDialogFragmentPresenter @Inject constructor(
}

profileManagementController.updateSurveyLastShownTimestamp(profileId)

logSurveyPopUpShownEvent(explorationId, topicId, profileId)

return binding.root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import org.oppia.android.domain.oppialogger.analytics.CpuPerformanceSnapshotterM
import org.oppia.android.domain.oppialogger.logscheduler.MetricLogSchedulerModule
import org.oppia.android.domain.oppialogger.loguploader.LogReportWorkerModule
import org.oppia.android.domain.platformparameter.PlatformParameterSingletonModule
import org.oppia.android.domain.profile.ProfileManagementController
import org.oppia.android.domain.question.QuestionModule
import org.oppia.android.domain.spotlight.SpotlightStateController
import org.oppia.android.domain.topic.FRACTIONS_EXPLORATION_ID_0
Expand Down Expand Up @@ -222,20 +223,25 @@ class ExplorationActivityTest {
@Inject
lateinit var fakeAccessibilityService: FakeAccessibilityService

@Inject
lateinit var profileManagementController: ProfileManagementController

private val internalProfileId: Int = 0

private val afternoonUtcTimestampMillis = 1556101812000

@Before
fun setUp() {
Intents.init()
setUpTestApplicationComponent()
testCoroutineDispatchers.registerIdlingResource()
profileTestHelper.initializeProfiles()
fakeOppiaClock.setFakeTimeMode(FakeOppiaClock.FakeTimeMode.MODE_UPTIME_MILLIS)
testCoroutineDispatchers.unregisterIdlingResource()
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
}

@After
fun tearDown() {
testCoroutineDispatchers.unregisterIdlingResource()
Intents.release()
}

Expand Down
Loading
Loading