From 30ca79653dba6cd474fe71f6daa3154c5547d7f1 Mon Sep 17 00:00:00 2001 From: TanishMoral11 Date: Wed, 30 Oct 2024 02:35:14 +0530 Subject: [PATCH] Fix crash in AudioViewModel on rotation by ensuring state initialization --- .../android/app/player/audio/AudioViewModel.kt | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/oppia/android/app/player/audio/AudioViewModel.kt b/app/src/main/java/org/oppia/android/app/player/audio/AudioViewModel.kt index 954be314639..53e1f47b2d8 100644 --- a/app/src/main/java/org/oppia/android/app/player/audio/AudioViewModel.kt +++ b/app/src/main/java/org/oppia/android/app/player/audio/AudioViewModel.kt @@ -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 @@ -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) @@ -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()