Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix part of #632: Replace LanguageSelectionAdapter with BindableAdapter #2392

Merged
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5cf09d0
issue 2098
FareesHussain Nov 11, 2020
d3696f2
Merge branch 'develop' of https://github.com/oppia/oppia-android into…
FareesHussain Dec 3, 2020
f3483f8
test commit
FareesHussain Dec 31, 2020
e970871
Merge branch 'develop' of https://github.com/FareesHussain/oppia-andr…
FareesHussain Dec 31, 2020
2110cdf
Update gradle.xml
FareesHussain Dec 31, 2020
e09a602
Update misc.xml
FareesHussain Dec 31, 2020
38fb39f
Fix gradle.xml
FareesHussain Jan 1, 2021
4c4d22e
Merge branch 'develop' of https://github.com/FareesHussain/oppia-andr…
FareesHussain Jan 1, 2021
1abdd4c
Merge branch 'develop' of https://github.com/FareesHussain/oppia-andr…
FareesHussain Jan 1, 2021
7c8bdae
initial inmplementation
FareesHussain Jan 4, 2021
cc86c6a
Ready for Review
FareesHussain Jan 5, 2021
7fbe08a
Merge branch 'develop' of https://github.com/oppia/oppia-android into…
FareesHussain Jan 5, 2021
409ccb3
Updated BAZEL with new viewmodels
FareesHussain Jan 5, 2021
e73d04d
Reformatting code and making changes in other configuration
FareesHussain Jan 5, 2021
e9a7532
Fixed the Tablet issue
FareesHussain Jan 6, 2021
8da06e9
Adding Requested changes
FareesHussain Jan 8, 2021
f658450
Resolving requested changes
FareesHussain Jan 11, 2021
2d0da65
NIT fix
FareesHussain Jan 11, 2021
6199e2d
Resolving requested changes
FareesHussain Jan 12, 2021
7b532cd
NIT fix
FareesHussain Jan 12, 2021
678c846
NIT fix
FareesHussain Jan 12, 2021
48ff579
Merge branch 'develop' of https://github.com/oppia/oppia-android into…
FareesHussain Jan 12, 2021
93ad8da
Merge branch 'develop' of https://github.com/oppia/oppia-android into…
FareesHussain Jan 20, 2021
173255a
update fun name
FareesHussain Jan 20, 2021
60e22ea
using databinding for audio language selection
FareesHussain Jan 20, 2021
d65bb2f
using databinding for app language selection
FareesHussain Jan 20, 2021
d4bf82e
ktlint fix
FareesHussain Jan 20, 2021
a2ab17c
Fix bazel check
FareesHussain Jan 20, 2021
ea70b99
Resolving Requested changes
FareesHussain Jan 21, 2021
c7481dd
Nit fix
FareesHussain Jan 21, 2021
8a6ac34
Radio Button fix
FareesHussain Jan 21, 2021
d6d0185
NIT fix
FareesHussain Jan 21, 2021
08f57c1
Merge branch 'develop' of https://github.com/oppia/oppia-android into…
FareesHussain Feb 2, 2021
29e9fe1
Using lifecycle owner to update the radio button
FareesHussain Feb 2, 2021
09809a2
Nit fix
FareesHussain Feb 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ LISTENERS = [
"src/main/java/org/oppia/android/app/options/LoadAppLanguageListListener.kt",
"src/main/java/org/oppia/android/app/options/LoadAudioLanguageListListener.kt",
"src/main/java/org/oppia/android/app/options/LoadReadingTextSizeListener.kt",
"src/main/java/org/oppia/android/app/options/LanguageRadioButtonListener.kt",
"src/main/java/org/oppia/android/app/options/RouteToAppLanguageListListener.kt",
"src/main/java/org/oppia/android/app/options/RouteToAudioLanguageListListener.kt",
"src/main/java/org/oppia/android/app/options/RouteToReadingTextSizeListener.kt",
Expand Down Expand Up @@ -178,6 +179,8 @@ VIEW_MODELS = [
"src/main/java/org/oppia/android/app/onboarding/ViewPagerSlide.kt",
"src/main/java/org/oppia/android/app/ongoingtopiclist/OngoingTopicItemViewModel.kt",
"src/main/java/org/oppia/android/app/ongoingtopiclist/OngoingTopicListViewModel.kt",
"src/main/java/org/oppia/android/app/options/LanguageItemViewModel.kt",
"src/main/java/org/oppia/android/app/options/LanguageSelectionViewModel.kt",
"src/main/java/org/oppia/android/app/options/OptionControlsViewModel.kt",
"src/main/java/org/oppia/android/app/options/OptionsAppLanguageViewModel.kt",
"src/main/java/org/oppia/android/app/options/OptionsAudioLanguageViewModel.kt",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ private const val KEY_APP_LANGUAGE_PREFERENCE_SUMMARY_VALUE =
private const val SELECTED_LANGUAGE_SAVED_KEY = "AppLanguageFragment.selected_language"

/** The fragment to change the language of the app. */
class AppLanguageFragment : InjectableFragment() {
class AppLanguageFragment :
InjectableFragment(),
LanguageRadioButtonListener {

@Inject
lateinit var appLanguageFragmentPresenter: AppLanguageFragmentPresenter
Expand Down Expand Up @@ -63,4 +65,8 @@ class AppLanguageFragment : InjectableFragment() {
appLanguageFragmentPresenter.getLanguageSelected()
)
}

override fun onLanguageSelected(selectedLanguage: String) {
appLanguageFragmentPresenter.onLanguageSelected(selectedLanguage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import org.oppia.android.app.recyclerview.BindableAdapter
import org.oppia.android.app.viewmodel.ViewModelProvider
import org.oppia.android.databinding.AppLanguageFragmentBinding
import org.oppia.android.databinding.LanguageItemsBinding
import javax.inject.Inject

/** The presenter for [AppLanguageFragment]. */
class AppLanguageFragmentPresenter @Inject constructor(private val fragment: Fragment) {
private lateinit var languageSelectionAdapter: LanguageSelectionAdapter
class AppLanguageFragmentPresenter @Inject constructor(
private val fragment: Fragment,
private val viewModelProvider: ViewModelProvider<LanguageSelectionViewModel>
) {
private lateinit var prefSummaryValue: String

private lateinit var languageSelectionViewModel: LanguageSelectionViewModel
fun handleOnCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -25,25 +30,37 @@ class AppLanguageFragmentPresenter @Inject constructor(private val fragment: Fra
/* attachToRoot= */ false
)
this.prefSummaryValue = prefSummaryValue
languageSelectionAdapter = LanguageSelectionAdapter(prefKey) {
updateAppLanguage(it)
}
languageSelectionViewModel = getLanguageSelectionViewModel()
binding.viewModel = languageSelectionViewModel
languageSelectionViewModel.selectedLanguage.value = prefSummaryValue
binding.languageRecyclerView.apply {
adapter = languageSelectionAdapter
adapter = createRecyclerViewAdapter()
}

binding.appLanguageToolbar?.setNavigationOnClickListener {
val message = languageSelectionAdapter.getSelectedLanguage()
val message = languageSelectionViewModel.selectedLanguage.value
val intent = Intent()
intent.putExtra(MESSAGE_APP_LANGUAGE_ARGUMENT_KEY, message)
(fragment.activity as AppLanguageActivity).setResult(REQUEST_CODE_APP_LANGUAGE, intent)
(fragment.activity as AppLanguageActivity).finish()
}

createAdapter()
return binding.root
}

fun getLanguageSelected(): String? {
return languageSelectionViewModel.selectedLanguage.value
}

private fun createRecyclerViewAdapter(): BindableAdapter<LanguageItemViewModel> {
return BindableAdapter.SingleTypeBuilder
.newBuilder<LanguageItemViewModel>()
.registerViewDataBinderWithSameModelType(
inflateDataBinding = LanguageItemsBinding::inflate,
setViewModel = LanguageItemsBinding::setViewModel
).build()
}

private fun updateAppLanguage(appLanguage: String) {
// The first branch of (when) will be used in the case of multipane
when (val parentActivity = fragment.activity) {
Expand All @@ -54,18 +71,11 @@ class AppLanguageFragmentPresenter @Inject constructor(private val fragment: Fra
}
}

fun getLanguageSelected(): String {
return languageSelectionAdapter.getSelectedLanguage()
fun onLanguageSelected(selectedLanguage: String) {
languageSelectionViewModel.selectedLanguage.value = selectedLanguage
}

private fun createAdapter() {
// TODO(#669): Replace dummy list with actual language list from backend.
val languageList = ArrayList<String>()
languageList.add("English")
languageList.add("French")
languageList.add("Hindi")
languageList.add("Chinese")
languageSelectionAdapter.setLanguageList(languageList)
languageSelectionAdapter.setDefaultLanguageSelected(prefSummaryValue = prefSummaryValue)
private fun getLanguageSelectionViewModel(): LanguageSelectionViewModel {
return viewModelProvider.getForFragment(fragment, LanguageSelectionViewModel::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ private const val SELECTED_AUDIO_LANGUAGE_SAVED_KEY =
"AudioLanguageFragment.selected_audio_language"

/** The fragment to change the default audio language of the app. */
class AudioLanguageFragment : InjectableFragment() {
class AudioLanguageFragment :
InjectableFragment(),
LanguageRadioButtonListener {

@Inject
lateinit var audioLanguageFragmentPresenter: AudioLanguageFragmentPresenter
Expand Down Expand Up @@ -68,4 +70,8 @@ class AudioLanguageFragment : InjectableFragment() {
audioLanguageFragmentPresenter.getLanguageSelected()
)
}

override fun onLanguageSelected(selectedLanguage: String) {
audioLanguageFragmentPresenter.onLanguageSelected(selectedLanguage)
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package org.oppia.android.app.options

import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import org.oppia.android.app.recyclerview.BindableAdapter
import org.oppia.android.app.viewmodel.ViewModelProvider
import org.oppia.android.databinding.AudioLanguageFragmentBinding
import org.oppia.android.databinding.LanguageItemsBinding
import javax.inject.Inject

/** The presenter for [AudioLanguageFragment]. */
class AudioLanguageFragmentPresenter @Inject constructor(private val fragment: Fragment) {
class AudioLanguageFragmentPresenter @Inject constructor(
private val fragment: Fragment,
private val viewModelProvider: ViewModelProvider<LanguageSelectionViewModel>
BenHenning marked this conversation as resolved.
Show resolved Hide resolved
) {

private lateinit var prefSummaryValue: String
private lateinit var languageSelectionAdapter: LanguageSelectionAdapter

private lateinit var languageSelectionViewModel: LanguageSelectionViewModel
fun handleOnCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -25,27 +32,43 @@ class AudioLanguageFragmentPresenter @Inject constructor(private val fragment: F
container,
/* attachToRoot= */ false
)
languageSelectionViewModel = getLanguageSelectionViewModel()
binding.viewModel = languageSelectionViewModel
prefSummaryValue = prefValue
languageSelectionAdapter = LanguageSelectionAdapter(prefKey) {
updateAudioLanguage(it)
}
languageSelectionViewModel.selectedLanguage.value = prefSummaryValue
binding.audioLanguageRecyclerView.apply {
adapter = languageSelectionAdapter
adapter = createRecyclerViewAdapter()
}

binding.audioLanguageToolbar?.setNavigationOnClickListener {
val message = languageSelectionAdapter.getSelectedLanguage()
val message = languageSelectionViewModel.selectedLanguage.value
val intent = Intent()
intent.putExtra(MESSAGE_AUDIO_LANGUAGE_ARGUMENT_KEY, message)
(fragment.activity as AudioLanguageActivity).setResult(REQUEST_CODE_AUDIO_LANGUAGE, intent)
(fragment.activity as AudioLanguageActivity).finish()
}
createAdapter()

languageSelectionViewModel.selectedLanguage.observe(
fragment,
Observer {
Log.d("selectedLanguage", "selected language -> $it")
}
)
BenHenning marked this conversation as resolved.
Show resolved Hide resolved

return binding.root
}

fun getLanguageSelected(): String {
return languageSelectionAdapter.getSelectedLanguage()
fun getLanguageSelected(): String? {
return languageSelectionViewModel.selectedLanguage.value
}
rt4914 marked this conversation as resolved.
Show resolved Hide resolved

private fun createRecyclerViewAdapter(): BindableAdapter<LanguageItemViewModel> {
return BindableAdapter.SingleTypeBuilder
.newBuilder<LanguageItemViewModel>()
.registerViewDataBinderWithSameModelType(
inflateDataBinding = LanguageItemsBinding::inflate,
setViewModel = LanguageItemsBinding::setViewModel
).build()
}

private fun updateAudioLanguage(audioLanguage: String) {
Expand All @@ -58,15 +81,12 @@ class AudioLanguageFragmentPresenter @Inject constructor(private val fragment: F
}
}

private fun createAdapter() {
// TODO(#669): Replace dummy list with actual language list from backend.
val languageList = ArrayList<String>()
languageList.add("No Audio")
languageList.add("English")
languageList.add("French")
languageList.add("Hindi")
languageList.add("Chinese")
languageSelectionAdapter.setLanguageList(languageList)
languageSelectionAdapter.setDefaultLanguageSelected(prefSummaryValue = prefSummaryValue)
fun onLanguageSelected(selectedLanguage: String) {
languageSelectionViewModel.selectedLanguage.value = selectedLanguage
updateAudioLanguage(selectedLanguage)
}

private fun getLanguageSelectionViewModel(): LanguageSelectionViewModel {
return viewModelProvider.getForFragment(fragment, LanguageSelectionViewModel::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.oppia.android.app.options

import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import org.oppia.android.app.viewmodel.ObservableViewModel

/**
* Language item view model for the recycler view in [AppLanguageFragment] and
* [AudioLanguageFragment].
*/
class LanguageItemViewModel(
val language: String,
val selectedLanguage: LiveData<String>,
BenHenning marked this conversation as resolved.
Show resolved Hide resolved
val languageRadioButtonListener: LanguageRadioButtonListener
) : ObservableViewModel() {
val isLanguageSelected: LiveData<Boolean> by lazy {
Transformations.map(selectedLanguage) { it == language }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.oppia.android.app.options

interface LanguageRadioButtonListener {
BenHenning marked this conversation as resolved.
Show resolved Hide resolved
fun onLanguageSelected(selectedLanguage: String)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.oppia.android.app.options

import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import org.oppia.android.app.fragment.FragmentScope
import org.oppia.android.app.viewmodel.ObservableViewModel
import javax.inject.Inject

/**
* Language list view model for the recycler view in [AppLanguageFragment] and
* [AudioLanguageFragment].
*/
@FragmentScope
class LanguageSelectionViewModel @Inject constructor(
rt4914 marked this conversation as resolved.
Show resolved Hide resolved
BenHenning marked this conversation as resolved.
Show resolved Hide resolved
val activity: AppCompatActivity,
val fragment: Fragment
) : ObservableViewModel() {

val selectedLanguage = MutableLiveData<String>()
val languageRadioButtonListener = fragment as LanguageRadioButtonListener

private val appLanguagesList = listOf<LanguageItemViewModel>(
LanguageItemViewModel("English", selectedLanguage, languageRadioButtonListener),
LanguageItemViewModel("French", selectedLanguage, languageRadioButtonListener),
LanguageItemViewModel("Hindi", selectedLanguage, languageRadioButtonListener),
LanguageItemViewModel("Chinese", selectedLanguage, languageRadioButtonListener)
)
private val audioLanguagesList = listOf<LanguageItemViewModel>(
LanguageItemViewModel("No Audio", selectedLanguage, languageRadioButtonListener),
LanguageItemViewModel("English", selectedLanguage, languageRadioButtonListener),
LanguageItemViewModel("French", selectedLanguage, languageRadioButtonListener),
LanguageItemViewModel("Hindi", selectedLanguage, languageRadioButtonListener),
LanguageItemViewModel("Chinese", selectedLanguage, languageRadioButtonListener)
)

val recyclerViewAudioLanguageList: List<LanguageItemViewModel> by lazy {
audioLanguagesList
}

val recyclerViewAppLanguageList: List<LanguageItemViewModel> by lazy {
appLanguagesList
}
}
Loading