From 36d52f3c28d6e42e10dd4fab5b7c00306a200365 Mon Sep 17 00:00:00 2001 From: Veena Date: Wed, 16 Oct 2019 12:14:28 +0530 Subject: [PATCH] Fix #235: Change fragment implementation from directly xml code to better kotlin code. (#238) * fetching exploration id from exploration fragment * added framelayout for audiofragment * Update StateFragmentPresenter.kt * Update StateFragmentPresenter.kt * Update ExplorationActivity.kt * Update ExplorationActivity.kt * Update StateFragmentPresenter.kt * Update StateFragmentPresenter.kt * corrected audio fragment implementation * Update StateFragmentTest.kt * Update StateFragmentTest.kt * Update StateFragmentTest.kt * Update StateFragmentTest.kt * Update ExplorationActivityPresenter.kt * Update ExplorationFragmentPresenter.kt * Update state_fragment.xml --- .../player/exploration/ExplorationActivity.kt | 4 +- .../ExplorationActivityPresenter.kt | 12 ++++-- .../ExplorationFragmentPresenter.kt | 23 ++++++++++- .../player/state/StateFragmentPresenter.kt | 41 +++++++++++++++---- .../oppia/app/player/state/StateViewModel.kt | 8 +--- .../main/res/layout/exploration_fragment.xml | 7 +--- app/src/main/res/layout/state_fragment.xml | 15 +++---- .../app/player/state/StateFragmentTest.kt | 1 + 8 files changed, 75 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/org/oppia/app/player/exploration/ExplorationActivity.kt b/app/src/main/java/org/oppia/app/player/exploration/ExplorationActivity.kt index caf6374f3ed..6e5508f7392 100755 --- a/app/src/main/java/org/oppia/app/player/exploration/ExplorationActivity.kt +++ b/app/src/main/java/org/oppia/app/player/exploration/ExplorationActivity.kt @@ -12,11 +12,13 @@ const val EXPLORATION_ACTIVITY_TOPIC_ID_ARGUMENT_KEY = "ExplorationActivity.expl class ExplorationActivity : InjectableAppCompatActivity() { @Inject lateinit var explorationActivityPresenter: ExplorationActivityPresenter + private lateinit var explorationId : String override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) activityComponent.inject(this) - explorationActivityPresenter.handleOnCreate() + explorationId = intent.getStringExtra(EXPLORATION_ACTIVITY_TOPIC_ID_ARGUMENT_KEY) + explorationActivityPresenter.handleOnCreate(explorationId) } companion object { diff --git a/app/src/main/java/org/oppia/app/player/exploration/ExplorationActivityPresenter.kt b/app/src/main/java/org/oppia/app/player/exploration/ExplorationActivityPresenter.kt index 0541e00e35b..94faaf4371c 100755 --- a/app/src/main/java/org/oppia/app/player/exploration/ExplorationActivityPresenter.kt +++ b/app/src/main/java/org/oppia/app/player/exploration/ExplorationActivityPresenter.kt @@ -1,20 +1,24 @@ package org.oppia.app.player.exploration +import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import org.oppia.app.R import org.oppia.app.activity.ActivityScope -import org.oppia.app.home.HomeFragment import javax.inject.Inject -/** The controller for [ExplorationActivity]. */ +/** The Presenter for [ExplorationActivity]. */ @ActivityScope class ExplorationActivityPresenter @Inject constructor(private val activity: AppCompatActivity) { - fun handleOnCreate() { + fun handleOnCreate(explorationId: String) { activity.setContentView(R.layout.exploration_activity) if (getExplorationFragment() == null) { + val explorationFragment = ExplorationFragment() + val args = Bundle() + args.putString(EXPLORATION_ACTIVITY_TOPIC_ID_ARGUMENT_KEY, explorationId) + explorationFragment.arguments = args activity.supportFragmentManager.beginTransaction().add( R.id.exploration_fragment_placeholder, - ExplorationFragment() + explorationFragment ).commitNow() } } diff --git a/app/src/main/java/org/oppia/app/player/exploration/ExplorationFragmentPresenter.kt b/app/src/main/java/org/oppia/app/player/exploration/ExplorationFragmentPresenter.kt index 8fc0223f7fa..7e13a8dcad5 100755 --- a/app/src/main/java/org/oppia/app/player/exploration/ExplorationFragmentPresenter.kt +++ b/app/src/main/java/org/oppia/app/player/exploration/ExplorationFragmentPresenter.kt @@ -1,19 +1,38 @@ package org.oppia.app.player.exploration +import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment +import org.oppia.app.R import org.oppia.app.databinding.ExplorationFragmentBinding import org.oppia.app.fragment.FragmentScope +import org.oppia.app.player.state.StateFragment import javax.inject.Inject -/** The controller for [ExplorationFragment]. */ +/** The presenter for [ExplorationFragment]. */ @FragmentScope class ExplorationFragmentPresenter @Inject constructor( private val fragment: Fragment ) { fun handleCreateView(inflater: LayoutInflater, container: ViewGroup?): View? { - return ExplorationFragmentBinding.inflate(inflater, container, /* attachToRoot= */ false).root + val binding = ExplorationFragmentBinding.inflate(inflater, container, /* attachToRoot= */ false).root + + if (getStateFragment() == null) { + val stateFragment = StateFragment() + val args = Bundle() + args.putString(EXPLORATION_ACTIVITY_TOPIC_ID_ARGUMENT_KEY, fragment.arguments!!.getString(EXPLORATION_ACTIVITY_TOPIC_ID_ARGUMENT_KEY)) + stateFragment.arguments = args + fragment.childFragmentManager.beginTransaction().add( + R.id.state_fragment_placeholder, + stateFragment + ).commitNow() + } + return binding + } + + private fun getStateFragment(): StateFragment? { + return fragment.childFragmentManager.findFragmentById(R.id.state_fragment_placeholder) as StateFragment? } } diff --git a/app/src/main/java/org/oppia/app/player/state/StateFragmentPresenter.kt b/app/src/main/java/org/oppia/app/player/state/StateFragmentPresenter.kt index 45a30923e1a..a1c0ea3501e 100755 --- a/app/src/main/java/org/oppia/app/player/state/StateFragmentPresenter.kt +++ b/app/src/main/java/org/oppia/app/player/state/StateFragmentPresenter.kt @@ -7,11 +7,14 @@ import androidx.fragment.app.Fragment import androidx.lifecycle.LiveData import androidx.lifecycle.Observer import androidx.lifecycle.Transformations +import org.oppia.app.R import org.oppia.app.databinding.StateFragmentBinding import org.oppia.app.fragment.FragmentScope import org.oppia.app.model.CellularDataPreference import org.oppia.app.model.EphemeralState +import org.oppia.app.player.audio.AudioFragment import org.oppia.app.player.audio.CellularDataDialogFragment +import org.oppia.app.player.exploration.EXPLORATION_ACTIVITY_TOPIC_ID_ARGUMENT_KEY import org.oppia.app.viewmodel.ViewModelProvider import org.oppia.domain.audio.CellularDialogController import org.oppia.domain.exploration.ExplorationProgressController @@ -20,6 +23,7 @@ import org.oppia.util.logging.Logger import javax.inject.Inject private const val TAG_CELLULAR_DATA_DIALOG = "CELLULAR_DATA_DIALOG" +private const val TAG_AUDIO_FRAGMENT = "AUDIO_FRAGMENT" /** The presenter for [StateFragment]. */ @FragmentScope @@ -33,6 +37,7 @@ class StateFragmentPresenter @Inject constructor( private var showCellularDataDialog = true private var useCellularData = false + private var explorationId: String? = null fun handleCreateView(inflater: LayoutInflater, container: ViewGroup?): View? { cellularDialogController.getCellularDataPreference() @@ -49,6 +54,7 @@ class StateFragmentPresenter @Inject constructor( it.stateFragment = fragment as StateFragment it.viewModel = getStateViewModel() } + explorationId = fragment.arguments!!.getString(EXPLORATION_ACTIVITY_TOPIC_ID_ARGUMENT_KEY) subscribeToCurrentState() @@ -57,22 +63,28 @@ class StateFragmentPresenter @Inject constructor( fun handleAudioClick() { if (showCellularDataDialog) { - setAudioFragmentVisible(false) + showHideAudioFragment(false) showCellularDataDialogFragment() } else { - setAudioFragmentVisible(useCellularData) + if (useCellularData) { + showHideAudioFragment(getAudioFragment() == null) + } else { + showHideAudioFragment(false) + } } } fun handleEnableAudio(saveUserChoice: Boolean) { - setAudioFragmentVisible(true) - if (saveUserChoice) + showHideAudioFragment(true) + if (saveUserChoice) { cellularDialogController.setAlwaysUseCellularDataPreference() + } } fun handleDisableAudio(saveUserChoice: Boolean) { - if (saveUserChoice) + if (saveUserChoice) { cellularDialogController.setNeverUseCellularDataPreference() + } } private fun showCellularDataDialogFragment() { @@ -88,8 +100,23 @@ class StateFragmentPresenter @Inject constructor( return viewModelProvider.getForFragment(fragment, StateViewModel::class.java) } - fun setAudioFragmentVisible(isVisible: Boolean) { - getStateViewModel().setAudioFragmentVisible(isVisible) + private fun getAudioFragment(): Fragment? { + return fragment.childFragmentManager.findFragmentByTag(TAG_AUDIO_FRAGMENT) + } + + private fun showHideAudioFragment(isVisible: Boolean) { + if (isVisible) { + if (getAudioFragment() == null) { + fragment.childFragmentManager.beginTransaction().add( + R.id.audio_fragment_placeholder, AudioFragment(), + TAG_AUDIO_FRAGMENT + ).commitNow() + } + } else { + if (getAudioFragment() != null) { + fragment.childFragmentManager.beginTransaction().remove(getAudioFragment()!!).commitNow() + } + } } private fun subscribeToCurrentState() { diff --git a/app/src/main/java/org/oppia/app/player/state/StateViewModel.kt b/app/src/main/java/org/oppia/app/player/state/StateViewModel.kt index 5af8d2ac519..9d0f34d87d6 100644 --- a/app/src/main/java/org/oppia/app/player/state/StateViewModel.kt +++ b/app/src/main/java/org/oppia/app/player/state/StateViewModel.kt @@ -7,10 +7,4 @@ import javax.inject.Inject /** [ViewModel] for state-fragment. */ @FragmentScope -class StateViewModel @Inject constructor() : ViewModel() { - var isAudioFragmentVisible = ObservableField(false) - - fun setAudioFragmentVisible(isVisible: Boolean) { - isAudioFragmentVisible.set(isVisible) - } -} +class StateViewModel @Inject constructor() : ViewModel() {} diff --git a/app/src/main/res/layout/exploration_fragment.xml b/app/src/main/res/layout/exploration_fragment.xml index 231f8162b38..618f7f3da10 100755 --- a/app/src/main/res/layout/exploration_fragment.xml +++ b/app/src/main/res/layout/exploration_fragment.xml @@ -5,16 +5,13 @@ - - - diff --git a/app/src/main/res/layout/state_fragment.xml b/app/src/main/res/layout/state_fragment.xml index d1cff627ed7..4c30178d521 100755 --- a/app/src/main/res/layout/state_fragment.xml +++ b/app/src/main/res/layout/state_fragment.xml @@ -14,21 +14,15 @@ - - - + app:layout_constraintTop_toTopOf="parent"/>