Skip to content

Commit

Permalink
seletion state retain support in question player
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishwajith-Shettigar committed Jul 9, 2024
1 parent c64e91f commit d5b205e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class StateFragment :
/** Arguments key for StateFragment. */
const val STATE_FRAGMENT_ARGUMENTS_KEY = "StateFragment.arguments"

/** Arguments key for StateFragment. */
const val STATE_FRAGMENT_USER_ANSWER_STATE_KEY = "StateFragment.user_answer_state"
/** Arguments key for StateFragment saved state. */
const val STATE_FRAGMENT_STATE_KEY = "StateFragment.state"

/**
* Creates a new instance of a StateFragment.
Expand Down Expand Up @@ -92,7 +92,7 @@ class StateFragment :
arguments?.getProto(STATE_FRAGMENT_ARGUMENTS_KEY, StateFragmentArguments.getDefaultInstance())

val userAnswerState = savedInstanceState?.getProto(
STATE_FRAGMENT_USER_ANSWER_STATE_KEY,
STATE_FRAGMENT_STATE_KEY,
UserAnswerState.getDefaultInstance()
) ?: UserAnswerState.getDefaultInstance()

Expand Down Expand Up @@ -161,7 +161,7 @@ class StateFragment :
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putProto(
STATE_FRAGMENT_USER_ANSWER_STATE_KEY,
STATE_FRAGMENT_STATE_KEY,
stateFragmentPresenter.getUserAnswerState()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import org.oppia.android.app.player.state.listener.SubmitNavigationButtonListene
import org.oppia.android.util.extensions.getProto
import org.oppia.android.util.extensions.putProto
import javax.inject.Inject
import org.oppia.android.app.model.UserAnswerState
import org.oppia.android.app.player.state.StateFragment

/** Fragment that contains all questions in Question Player. */
class QuestionPlayerFragment :
Expand Down Expand Up @@ -52,8 +54,12 @@ class QuestionPlayerFragment :
val args = checkNotNull(arguments) {
"Expected arguments to be passed to QuestionPlayerFragment"
}
val userAnswerState = savedInstanceState?.getProto(
QUESTION_PLAYER_FRAGMENT_STATE_KEY,
UserAnswerState.getDefaultInstance()
) ?: UserAnswerState.getDefaultInstance()
val profileId = args.getProto(PROFILE_ID_ARGUMENT_KEY, ProfileId.getDefaultInstance())
return questionPlayerFragmentPresenter.handleCreateView(inflater, container, profileId)
return questionPlayerFragmentPresenter.handleCreateView(inflater, container, profileId,userAnswerState)
}

override fun onAnswerReadyForSubmission(answer: UserAnswer) {
Expand Down Expand Up @@ -98,6 +104,9 @@ class QuestionPlayerFragment :
companion object {
private const val PROFILE_ID_ARGUMENT_KEY = "QuestionPlayerFragment.profile_id"

/** Arguments key for QuestionPlayerFragment saved state. */
const val QUESTION_PLAYER_FRAGMENT_STATE_KEY = "QuestionPlayerFragment.state"

/**
* Creates a new fragment to play a question session.
*
Expand All @@ -112,4 +121,12 @@ class QuestionPlayerFragment :
}
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putProto(
QUESTION_PLAYER_FRAGMENT_STATE_KEY,
questionPlayerFragmentPresenter.getUserAnswerState()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.oppia.android.util.data.DataProvider
import org.oppia.android.util.data.DataProviders.Companion.toLiveData
import org.oppia.android.util.gcsresource.QuestionResourceBucketName
import javax.inject.Inject
import org.oppia.android.app.model.UserAnswerState

/** The presenter for [QuestionPlayerFragment]. */
@FragmentScope
Expand Down Expand Up @@ -69,7 +70,8 @@ class QuestionPlayerFragmentPresenter @Inject constructor(
fun handleCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
profileId: ProfileId
profileId: ProfileId,
userAnswerState: UserAnswerState
): View? {
binding = QuestionPlayerFragmentBinding.inflate(
inflater,
Expand All @@ -79,7 +81,7 @@ class QuestionPlayerFragmentPresenter @Inject constructor(
this.profileId = profileId

recyclerViewAssembler = createRecyclerViewAssembler(
assemblerBuilderFactory.create(resourceBucketName, "skill", profileId),
assemblerBuilderFactory.create(resourceBucketName, "skill", profileId,userAnswerState),
binding.congratulationsTextView,
binding.congratulationsTextConfettiView
)
Expand Down Expand Up @@ -154,6 +156,11 @@ class QuestionPlayerFragmentPresenter @Inject constructor(
recyclerViewAssembler.adapter.notifyDataSetChanged()
}

/** Returns the [UserAnswerState] representing the user's current pending answer. */
fun getUserAnswerState(): UserAnswerState {
return questionViewModel.getUserAnswerState(recyclerViewAssembler::getPendingAnswerHandler)
}

/**
* Updates whether the submit button should be active based on whether the pending answer is in an
* error state.
Expand Down Expand Up @@ -257,6 +264,9 @@ class QuestionPlayerFragmentPresenter @Inject constructor(
private fun subscribeToAnswerOutcome(
answerOutcomeResultLiveData: LiveData<AsyncResult<AnsweredQuestionOutcome>>
) {
if (questionViewModel.getCanSubmitAnswer().get() == true) {
recyclerViewAssembler.resetUserAnswerState()
}
val answerOutcomeLiveData =
Transformations.map(answerOutcomeResultLiveData, ::processAnsweredQuestionOutcome)
answerOutcomeLiveData.observe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.oppia.android.app.translation.AppLanguageResourceHandler
import org.oppia.android.app.viewmodel.ObservableArrayList
import org.oppia.android.app.viewmodel.ObservableViewModel
import javax.inject.Inject
import org.oppia.android.app.model.UserAnswerState

/** [ObservableViewModel] for the question player. */
class QuestionPlayerViewModel @Inject constructor(
Expand Down Expand Up @@ -92,6 +93,13 @@ class QuestionPlayerViewModel @Inject constructor(
}
}

fun getUserAnswerState(
retrieveAnswerHandler: (List<StateItemViewModel>) -> InteractionAnswerHandler?
): UserAnswerState {
return retrieveAnswerHandler(getAnswerItemList())?.getUserAnswerState()
?: UserAnswerState.getDefaultInstance()
}

private fun getPendingAnswerWithoutError(
answerHandler: InteractionAnswerHandler?
): UserAnswer? {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/item_selection_interaction_items.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
android:id="@+id/item_selection_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@{viewModel.isEnabled ? @color/component_color_shared_item_selection_interaction_enabled_color : @color/component_color_shared_item_selection_interaction_disabled_color}"
android:clickable="false"
android:enabled="@{viewModel.isEnabled}"
android:focusable="false"
android:labelFor="@id/item_selection_contents_text_view"
app:buttonTint="@{viewModel.isEnabled ? @color/component_color_shared_item_selection_interaction_enabled_color : @color/component_color_shared_item_selection_interaction_disabled_color}"
android:checked="@{viewModel.answerSelected}"
/>

<TextView
android:id="@+id/item_selection_contents_text_view"
android:layout_width="wrap_content"
Expand Down

0 comments on commit d5b205e

Please sign in to comment.