From 0dc284f35fa0712f64147fd264a55980b66abcd5 Mon Sep 17 00:00:00 2001 From: TanishMoral11 Date: Mon, 28 Oct 2024 12:49:36 +0530 Subject: [PATCH 1/2] Fix crash in AudioViewModel by initializing state variables --- .../oppia/android/app/player/audio/AudioViewModel.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 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 07037abd873..954be314639 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 @@ -66,9 +66,11 @@ class AudioViewModel @Inject constructor( processPlayStatusLiveData() } - fun setStateAndExplorationId(newState: State, id: String) { - state = newState - explorationId = id + fun setStateAndExplorationId(newState: State?, id: String?) { + if (newState != null && id != null) { + state = newState + explorationId = id + } } fun loadMainContentAudio(allowAutoPlay: Boolean, reloadingContent: Boolean) { @@ -88,6 +90,8 @@ 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 val targetContentId = contentId ?: state.content.contentId val voiceoverMapping = state.recordedVoiceoversMap[targetContentId] ?: VoiceoverMapping.getDefaultInstance() From 30ca79653dba6cd474fe71f6daa3154c5547d7f1 Mon Sep 17 00:00:00 2001 From: TanishMoral11 Date: Wed, 30 Oct 2024 02:35:14 +0530 Subject: [PATCH 2/2] 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()