Skip to content

Commit

Permalink
Fix crash in AudioViewModel on rotation by ensuring state initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
TanishMoral11 committed Oct 29, 2024
1 parent 0dc284f commit 30ca796
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.oppia.android.app.player.audio

import android.util.Log
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import androidx.lifecycle.LiveData
Expand Down Expand Up @@ -67,12 +68,15 @@ class AudioViewModel @Inject constructor(
}

fun setStateAndExplorationId(newState: State?, id: String?) {
if (newState != null && id != null) {
state = newState
explorationId = id
if (newState == null || id == null) {
Log.e("AudioViewModel", "Failed to set state or id - parameters are null")
return
}
state = newState
explorationId = id
}


fun loadMainContentAudio(allowAutoPlay: Boolean, reloadingContent: Boolean) {
hasFeedback = false
loadAudio(contentId = null, allowAutoPlay, reloadingContent)
Expand All @@ -90,8 +94,11 @@ class AudioViewModel @Inject constructor(
* @param allowAutoPlay If false, audio is guaranteed not to be autoPlayed.
*/
private fun loadAudio(contentId: String?, allowAutoPlay: Boolean, reloadingMainContent: Boolean) {
// Check if 'state' is initialized before proceeding.
if (!::state.isInitialized) return
if (!::state.isInitialized || !::explorationId.isInitialized) {
Log.w("AudioViewModel", "Cannot load audio: state or explorationId is not initialized.")
return
}

val targetContentId = contentId ?: state.content.contentId
val voiceoverMapping =
state.recordedVoiceoversMap[targetContentId] ?: VoiceoverMapping.getDefaultInstance()
Expand Down

0 comments on commit 30ca796

Please sign in to comment.