From 2620f1a048ed4f9d68bfd007305509266edc45e1 Mon Sep 17 00:00:00 2001 From: Rd Date: Thu, 21 Nov 2024 22:19:37 +0530 Subject: [PATCH] Utilized MediatorLiveData to set a initial Pending value to avoid NPE --- .../state/DragDropSortInteractionView.kt | 6 +- .../DragAndDropSortInteractionViewModel.kt | 111 ++++++++++++++++-- 2 files changed, 104 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/oppia/android/app/player/state/DragDropSortInteractionView.kt b/app/src/main/java/org/oppia/android/app/player/state/DragDropSortInteractionView.kt index d8323b53491..365ad640831 100644 --- a/app/src/main/java/org/oppia/android/app/player/state/DragDropSortInteractionView.kt +++ b/app/src/main/java/org/oppia/android/app/player/state/DragDropSortInteractionView.kt @@ -76,11 +76,7 @@ class DragDropSortInteractionView @JvmOverloads constructor( * Note that this needs to be used instead of the generic RecyclerView 'data' binding adapter * since this one takes into account initialization order with other binding properties. */ - fun setDraggableData(dataList: List?) { - if (dataList == null) { - Log.e("failingtocallobserver", "Received null dataList") - return - } + fun setDraggableData(dataList: List) { this.dataList = dataList maybeInitializeAdapter() } diff --git a/app/src/main/java/org/oppia/android/app/player/state/itemviewmodel/DragAndDropSortInteractionViewModel.kt b/app/src/main/java/org/oppia/android/app/player/state/itemviewmodel/DragAndDropSortInteractionViewModel.kt index 79c215f14c9..fc941c2f4cf 100644 --- a/app/src/main/java/org/oppia/android/app/player/state/itemviewmodel/DragAndDropSortInteractionViewModel.kt +++ b/app/src/main/java/org/oppia/android/app/player/state/itemviewmodel/DragAndDropSortInteractionViewModel.kt @@ -6,6 +6,7 @@ import androidx.databinding.Observable import androidx.databinding.ObservableField import androidx.fragment.app.Fragment import androidx.lifecycle.LiveData +import androidx.lifecycle.MediatorLiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.Transformations import androidx.recyclerview.widget.RecyclerView @@ -89,7 +90,7 @@ class DragAndDropSortInteractionViewModel private constructor( computeOriginalChoiceItems(contentIdHtmlMap, choiceSubtitledHtmls, this, resourceHandler) private var _choiceItems: MutableList = mutableListOf() - private var _choiceItemsLiveData = MutableLiveData>() + private var _choiceItemsLiveData = MutableLiveData>(_originalChoiceItems) var choiceItems: LiveData> = _choiceItemsLiveData private var pendingAnswerError: String? = null @@ -351,10 +352,14 @@ class DragAndDropSortInteractionViewModel private constructor( dragAndDropSortInteractionViewModel: DragAndDropSortInteractionViewModel, resourceHandler: AppLanguageResourceHandler ): MutableList { - val ephemeralStateLiveData: LiveData> by lazy { + /*val ephemeralStateLiveData: LiveData> by lazy { explorationProgressController.getCurrentState().toLiveData() - } + }*/ + + val ephemeralStateLiveData: LiveData> = explorationProgressController.getCurrentState().toLiveData() + Log.d("checkingchoiceitems", "computeSelectedChoices ephemeral state live data - $ephemeralStateLiveData") + Log.d("checkingchoiceitems", "computeSelectedChoices ephemeral state live data value - ${ephemeralStateLiveData.value}") /*ephemeralStateLiveData.observe(fragment) { result -> _choiceItems = processEphemeralStateResult( @@ -369,12 +374,25 @@ class DragAndDropSortInteractionViewModel private constructor( Log.d("checkingchoiceitems", "computeSelectedChoices choiceItems before switch map- ${choiceItems.value}") - choiceItems = Transformations.switchMap(ephemeralStateLiveData) { result -> + + /*val filteredEphemeralStateLiveData = MediatorLiveData>().apply { + addSource(ephemeralStateLiveData) { result -> + if (result != null) { + value = result + } + } + } + + +// choiceItems = Transformations.switchMap(ephemeralStateLiveData) { result -> +// choiceItems = Transformations.switchMap(ephemeralStateLiveData) { result -> + choiceItems = Transformations.switchMap(filteredEphemeralStateLiveData) { result -> Log.d("checkingchoiceitems", "computeSelectedChoices in transformations switch map") MutableLiveData>().apply { value = when (result) { is AsyncResult.Failure, is AsyncResult.Pending -> _originalChoiceItems is AsyncResult.Success -> { + Log.d("checkingchoiceitems", "computeSelectedChoices in transformations switch map ephemeral live data - ${ephemeralStateLiveData.value}") Log.d("checkingchoiceitems", "computeSelectedChoices in transformations switch map success case") // try choice items here _choiceItems = processEphemeralStateResult( @@ -384,17 +402,94 @@ class DragAndDropSortInteractionViewModel private constructor( resourceHandler ) _choiceItemsLiveData.value = _choiceItems + Log.d("checkingchoiceitems", "computeSelectedChoices end of transformatins ephemeral _choiceItemsLiveData - ${_choiceItemsLiveData.value}") _originalChoiceItems = _choiceItems.toMutableList() _choiceItems } } - Log.d("checkingchoiceitems", "computeSelectedChoices end of transformatins switch map") - Log.d("checkingchoiceitems", "computeSelectedChoices end of transformatins switch map _choiceItems - $_choiceItems") - Log.d("checkingchoiceitems", "computeSelectedChoices end of transformatins switch map _choiceItemsLiveData - ${_choiceItemsLiveData.value}") - Log.d("checkingchoiceitems", "computeSelectedChoices end of transformatins switch map _originalChoiceItems - $_originalChoiceItems") + Log.d("checkingchoiceitems", "computeSelectedChoices end of transformatins switch map") + Log.d("checkingchoiceitems", "computeSelectedChoices end of transformatins switch map _choiceItems - $_choiceItems") + Log.d("checkingchoiceitems", "computeSelectedChoices end of transformatins switch map _choiceItemsLiveData - ${_choiceItemsLiveData.value}") + Log.d("checkingchoiceitems", "computeSelectedChoices end of transformatins switch map _originalChoiceItems - $_originalChoiceItems") + } + } as MutableLiveData>*/ + + + + + + + + + // Create a MediatorLiveData that only updates when the value is non-null + val filteredEphemeralStateLiveData = MediatorLiveData>().apply { + value = AsyncResult.Pending() + addSource(ephemeralStateLiveData) { result -> +// if (result != null) { + value = result +// } } } + // this works +// Now switchMap with the filtered LiveData +// choiceItems = Transformations.switchMap(filteredEphemeralStateLiveData) { result -> + /*choiceItems = Transformations.switchMap(filteredEphemeralStateLiveData) { result -> + Log.d("checkingchoiceitems", "computeSelectedChoices in transformations switch map") + MutableLiveData>().apply { + value = when (result) { + is AsyncResult.Failure, is AsyncResult.Pending -> { + _originalChoiceItems + } + is AsyncResult.Success -> { + Log.d("checkingchoiceitems", "computeSelectedChoices in transformations switch map success case") + _choiceItems = processEphemeralStateResult( + result.value, + contentIdHtmlMap, + dragAndDropSortInteractionViewModel, + resourceHandler + ) + _choiceItemsLiveData.value = _choiceItems + _originalChoiceItems = _choiceItems.toMutableList() + _choiceItems // Return the processed choice items + } + else -> _originalChoiceItems + } + Log.d("checkingchoiceitems", "computeSelectedChoices end of transformations switch map") + } + }*/ + + + + choiceItems = Transformations.map(filteredEphemeralStateLiveData) { result -> + Log.d("checkingchoiceitems", "computeSelectedChoices in transformations switch map") + when (result) { + is AsyncResult.Failure, is AsyncResult.Pending -> { + Log.d("checkingchoiceitems", "computeSelectedChoices in transformations switch map pending case") + _originalChoiceItems + } + is AsyncResult.Success -> { + Log.d("checkingchoiceitems", "computeSelectedChoices in transformations switch map success case") + _choiceItems = processEphemeralStateResult( + result.value, + contentIdHtmlMap, + dragAndDropSortInteractionViewModel, + resourceHandler + ) + _choiceItemsLiveData.value = _choiceItems + _originalChoiceItems = _choiceItems.toMutableList() + _choiceItems + } + else -> _originalChoiceItems + } + } + + + + + + + Log.d("checkingchoiceitems", "computeSelectedChoices choiceItems after switch map- ${choiceItems.value}") Log.d("checkingchoiceitems", "computeSelectedChoices _choiceItems after switch map- $_choiceItems") Log.d("checkingchoiceitems", "computeSelectedChoices _originalChoiceItems after switch map - $_originalChoiceItems")