Skip to content

Commit

Permalink
Fixes part of #123, #116, and #130: Cellular Data Controller (#194)
Browse files Browse the repository at this point in the history
* Created CellularDialogController

* Started making tests for controller

* Removed import

* Fixed test cases and renamed to hideDialog

* Fixed comments

* Added use cellular data value and added test cases

* Started adding test cases

* Setup initial testing environment for state fragment

* Add more test cases

* Changed cellular data controller methods to always and never use cellular data
  • Loading branch information
jamesxu0 authored Oct 6, 2019
1 parent 8efee4b commit cf4b52c
Show file tree
Hide file tree
Showing 11 changed files with 608 additions and 42 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
android:theme="@style/OppiaTheme">
<activity android:name=".player.exploration.ExplorationActivity"/>
<activity android:name=".topic.TopicActivity"/>
<activity android:name="org.oppia.app.player.state.testing.StateFragmentTestActivity"/>
<activity android:name="org.oppia.app.home.HomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/org/oppia/app/activity/ActivityComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dagger.Subcomponent
import org.oppia.app.fragment.FragmentComponent
import org.oppia.app.home.HomeActivity
import org.oppia.app.player.exploration.ExplorationActivity
import org.oppia.app.player.state.testing.StateFragmentTestActivity
import org.oppia.app.topic.TopicActivity
import javax.inject.Provider

Expand All @@ -24,4 +25,5 @@ interface ActivityComponent {
fun inject(explorationActivity: ExplorationActivity)
fun inject(homeActivity: HomeActivity)
fun inject(topicActivity: TopicActivity)
fun inject(stateFragmentTestActivity: StateFragmentTestActivity)
}
47 changes: 5 additions & 42 deletions app/src/main/java/org/oppia/app/player/state/StateFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,21 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import org.oppia.app.fragment.InjectableFragment
import org.oppia.app.player.audio.CellularDataDialogFragment
import org.oppia.app.player.audio.CellularDataInterface
import javax.inject.Inject

private const val TAG_CELLULAR_DATA_DIALOG = "CELLULAR_DATA_DIALOG"

/** Fragment that represents the current state of an exploration. */
class StateFragment : InjectableFragment(), CellularDataInterface {
@Inject
lateinit var stateFragmentPresenter: StateFragmentPresenter
// Control this boolean value from controllers in domain module.
private var showCellularDataDialog = true

init {
// TODO(#116): Code to control the value of showCellularDataDialog using AudioController.
}
@Inject lateinit var stateFragmentPresenter: StateFragmentPresenter

override fun onAttach(context: Context?) {
super.onAttach(context)
fragmentComponent.inject(this)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return stateFragmentPresenter.handleCreateView(inflater, container)
}

fun dummyButtonClicked() {
if (showCellularDataDialog) {
stateFragmentPresenter.setAudioFragmentVisible(false)
showCellularDataDialogFragment()
} else {
stateFragmentPresenter.setAudioFragmentVisible(true)
}
}

private fun showCellularDataDialogFragment() {
val previousFragment = childFragmentManager.findFragmentByTag(TAG_CELLULAR_DATA_DIALOG)
if (previousFragment != null) {
childFragmentManager.beginTransaction().remove(previousFragment).commitNow()
}
val dialogFragment = CellularDataDialogFragment.newInstance()
dialogFragment.showNow(childFragmentManager, TAG_CELLULAR_DATA_DIALOG)
}

override fun enableAudioWhileOnCellular(saveUserChoice: Boolean) {
stateFragmentPresenter.setAudioFragmentVisible(true)
// saveUserChoice -> true -> save this preference
// saveUserChoice -> false -> do not save this preference
}

override fun disableAudioWhileOnCellular(saveUserChoice: Boolean) {
// saveUserChoice -> true -> save this preference
// saveUserChoice -> false -> do not save this preference
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = stateFragmentPresenter.handleCreateView(inflater, container)
override fun enableAudioWhileOnCellular(saveUserChoice: Boolean) = stateFragmentPresenter.handleEnableAudio(saveUserChoice)
override fun disableAudioWhileOnCellular(saveUserChoice: Boolean) = stateFragmentPresenter.handleDisableAudio(saveUserChoice)
fun dummyButtonClicked() = stateFragmentPresenter.handleAudioClick()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,39 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import org.oppia.app.databinding.StateFragmentBinding
import org.oppia.app.fragment.FragmentScope
import org.oppia.app.model.CellularDataPreference
import org.oppia.app.player.audio.CellularDataDialogFragment
import org.oppia.app.viewmodel.ViewModelProvider
import org.oppia.domain.audio.CellularDialogController
import org.oppia.util.data.AsyncResult
import javax.inject.Inject

private const val TAG_CELLULAR_DATA_DIALOG = "CELLULAR_DATA_DIALOG"

/** The presenter for [StateFragment]. */
@FragmentScope
class StateFragmentPresenter @Inject constructor(
private val fragment: Fragment,
private val cellularDialogController: CellularDialogController,
private val viewModelProvider: ViewModelProvider<StateViewModel>
) {

private var showCellularDataDialog = true
private var useCellularData = false

fun handleCreateView(inflater: LayoutInflater, container: ViewGroup?): View? {
cellularDialogController.getCellularDataPreference()
.observe(fragment, Observer<AsyncResult<CellularDataPreference>>{
if (it.isSuccess()) {
val prefs = it.getOrDefault(CellularDataPreference.getDefaultInstance())
showCellularDataDialog = !(prefs.hideDialog)
useCellularData = prefs.useCellularData
}
})

val binding = StateFragmentBinding.inflate(inflater, container, /* attachToRoot= */ false)
binding.let {
it.stateFragment = fragment as StateFragment
Expand All @@ -24,6 +45,35 @@ class StateFragmentPresenter @Inject constructor(
return binding.root
}

fun handleAudioClick() {
if (showCellularDataDialog) {
setAudioFragmentVisible(false)
showCellularDataDialogFragment()
} else {
setAudioFragmentVisible(useCellularData)
}
}

fun handleEnableAudio(saveUserChoice: Boolean) {
setAudioFragmentVisible(true)
if (saveUserChoice)
cellularDialogController.setAlwaysUseCellularDataPreference()
}

fun handleDisableAudio(saveUserChoice: Boolean) {
if (saveUserChoice)
cellularDialogController.setNeverUseCellularDataPreference()
}

private fun showCellularDataDialogFragment() {
val previousFragment = fragment.childFragmentManager.findFragmentByTag(TAG_CELLULAR_DATA_DIALOG)
if (previousFragment != null) {
fragment.childFragmentManager.beginTransaction().remove(previousFragment).commitNow()
}
val dialogFragment = CellularDataDialogFragment.newInstance()
dialogFragment.showNow(fragment.childFragmentManager, TAG_CELLULAR_DATA_DIALOG)
}

private fun getStateViewModel(): StateViewModel {
return viewModelProvider.getForFragment(fragment, StateViewModel::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.oppia.app.player.state.testing

import android.os.Bundle
import org.oppia.app.activity.InjectableAppCompatActivity
import javax.inject.Inject

/** Test Activity used for testing StateFragment */
class StateFragmentTestActivity : InjectableAppCompatActivity() {

@Inject lateinit var stateFragmentTestActivityController: StateFragmentTestActivityPresenter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityComponent.inject(this)
stateFragmentTestActivityController.handleOnCreate()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.oppia.app.player.state.testing

import androidx.appcompat.app.AppCompatActivity
import org.oppia.app.R
import org.oppia.app.activity.ActivityScope
import org.oppia.app.player.state.StateFragment
import javax.inject.Inject

/** The presenter for [StateFragmentTestActivity] */
@ActivityScope
class StateFragmentTestActivityPresenter @Inject constructor(
private val activity: AppCompatActivity
) {
fun handleOnCreate() {
activity.setContentView(R.layout.state_fragment_test_activity)
if (getStateFragment() == null) {
activity.supportFragmentManager.beginTransaction().add(
R.id.state_fragment_placeholder,
StateFragment()
).commitNow()
}
}

private fun getStateFragment(): StateFragment? {
return activity.supportFragmentManager.findFragmentById(R.id.state_fragment_placeholder) as StateFragment?
}
}
8 changes: 8 additions & 0 deletions app/src/main/res/layout/state_fragment_test_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/state_fragment_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".player.state.testing.StateFragmentTestActivity" />
Loading

0 comments on commit cf4b52c

Please sign in to comment.