Skip to content

Commit

Permalink
More updates as part of refactoring for issue oppia#2658.
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinGitonga committed Jul 18, 2022
1 parent f9105ac commit 20c1ff3
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import javax.inject.Inject
@FragmentScope
class AdministratorControlsFragmentPresenter @Inject constructor(
private val activity: AppCompatActivity,
private val fragment: Fragment
private val fragment: Fragment,
private val multiTypeBuilder: BindableAdapter.MultiTypeBuilder.Factory
) {
private lateinit var binding: AdministratorControlsFragmentBinding
private lateinit var linearLayoutManager: LinearLayoutManager
Expand Down Expand Up @@ -77,8 +78,8 @@ class AdministratorControlsFragmentPresenter @Inject constructor(
/** Returns the recycler view adapter for the controls panel in administrator controls fragment. */
private fun createRecyclerViewAdapter(isMultipane: Boolean):
BindableAdapter<AdministratorControlsItemViewModel> {
return BindableAdapter.MultiTypeBuilder
.Factory(fragment).create<AdministratorControlsItemViewModel, ViewType> { viewModel ->
return multiTypeBuilder
.create<AdministratorControlsItemViewModel, ViewType> { viewModel ->
viewModel.isMultipane.set(isMultipane)
when (viewModel) {
is AdministratorControlsGeneralViewModel -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import javax.inject.Inject
class ProfileAndDeviceIdFragmentPresenter @Inject constructor(
private val fragment: Fragment,
private val profileListViewModelFactory: ProfileListViewModel.Factory,
private val multiTypeBuilderFactory: BindableAdapter.MultiTypeBuilder.Factory
private val adapterFactory: BindableAdapter.MultiTypeBuilder.Factory
) {

private lateinit var binding: ProfileAndDeviceIdFragmentBinding
Expand All @@ -40,15 +40,14 @@ class ProfileAndDeviceIdFragmentPresenter @Inject constructor(
}

private fun createRecyclerViewAdapter(): BindableAdapter<ProfileListItemViewModel> {
return multiTypeBuilderFactory.create<ProfileListItemViewModel,
ProfileListItemViewType> { viewModel ->
return adapterFactory.create<ProfileListItemViewModel, ProfileListItemViewType> { viewModel ->
when (viewModel) {
is DeviceIdItemViewModel -> ProfileListItemViewType.DEVICE_ID
is ProfileLearnerIdItemViewModel -> ProfileListItemViewType.LEARNER_ID
is SyncStatusItemViewModel -> ProfileListItemViewType.SYNC_STATUS
else -> error("Encountered unexpected view model: $viewModel")
}
}.setLifecycleOwner(fragment)
}
.registerViewDataBinder(
viewType = ProfileListItemViewType.DEVICE_ID,
inflateDataBinding = ProfileListDeviceIdItemBinding::inflate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MarkChaptersCompletedFragmentPresenter @Inject constructor(
private val fragment: Fragment,
private val viewModelProvider: ViewModelProvider<MarkChaptersCompletedViewModel>,
private val modifyLessonProgressController: ModifyLessonProgressController,
private val multiTypeBuilderFactory: BindableAdapter.MultiTypeBuilder.Factory
private val adapterFactory: BindableAdapter.MultiTypeBuilder.Factory
) : ChapterSelector {
private lateinit var binding: MarkChaptersCompletedFragmentBinding
private lateinit var linearLayoutManager: LinearLayoutManager
Expand Down Expand Up @@ -95,8 +95,7 @@ class MarkChaptersCompletedFragmentPresenter @Inject constructor(
}

private fun createRecyclerViewAdapter(): BindableAdapter<MarkChaptersCompletedItemViewModel> {
return multiTypeBuilderFactory.create<MarkChaptersCompletedItemViewModel,
ViewType> { viewModel ->
return adapterFactory.create<MarkChaptersCompletedItemViewModel, ViewType> { viewModel ->
when (viewModel) {
is StorySummaryViewModel -> ViewType.VIEW_TYPE_STORY
is ChapterSummaryViewModel -> ViewType.VIEW_TYPE_CHAPTER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ class ComingSoonTopicsListView @JvmOverloads constructor(
@Inject
lateinit var oppiaLogger: OppiaLogger

@Inject
lateinit var fragment: Fragment

@Inject
lateinit var singleTypeAdapterFactory: BindableAdapter.SingleTypeBuilder.Factory

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ class PromotedStoryListView @JvmOverloads constructor(
@Inject
lateinit var singleTypeBuilderFactory: BindableAdapter.SingleTypeBuilder.Factory

lateinit var promotedDataList: List<PromotedStoryViewModel>

override fun onAttachedToWindow() {
super.onAttachedToWindow()

val viewComponentFactory = FragmentManager.findFragment<Fragment>(this) as ViewComponentFactory
val viewComponentFactory =
FragmentManager.findFragment<Fragment>(this) as ViewComponentFactory
val viewComponent = viewComponentFactory.createViewComponent(this) as ViewComponentImpl
viewComponent.inject(this)

Expand All @@ -53,12 +55,31 @@ class PromotedStoryListView @JvmOverloads constructor(
snapHelper.attachToRecyclerView(this)
}

private fun checkIfComponentsInitialized() {
if (::bindingInterface.isInitialized &&
::bindingInterface.isInitialized &&
::oppiaLogger.isInitialized &&
::fragment.isInitialized &&
::singleTypeBuilderFactory.isInitialized &&
::promotedDataList.isInitialized
) {
bindDataToAdapter()
}
}

/**
* Sets the list of promoted stories that this view shows to the learner.
*
* @param newDataList the new list of stories to present
*/
fun setPromotedStoryList(newDataList: List<PromotedStoryViewModel>?) {
if (newDataList != null) {
promotedDataList = newDataList
checkIfComponentsInitialized()
}
}

private fun bindDataToAdapter() {
// To reliably bind data only after the adapter is created, we manually set the data so we can first
// check for the adapter; when using an existing [RecyclerViewBindingAdapter] there is no reliable
// way to check that the adapter is created.
Expand All @@ -67,10 +88,10 @@ class PromotedStoryListView @JvmOverloads constructor(
if (adapter == null) {
adapter = createAdapter()
}
if (newDataList == null) {
oppiaLogger.w(PROMOTED_STORY_LIST_VIEW_TAG, "Failed to resolve new story list data")
if (::promotedDataList.isInitialized) {
(adapter as BindableAdapter<*>).setDataUnchecked(promotedDataList)
} else {
(adapter as BindableAdapter<*>).setDataUnchecked(newDataList)
oppiaLogger.w(PROMOTED_STORY_LIST_VIEW_TAG, "Failed to resolve new story list data")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class AppLanguageFragmentPresenter @Inject constructor(

private fun createRecyclerViewAdapter(): BindableAdapter<LanguageItemViewModel> {
return singleTypeBuilderFactory.create<LanguageItemViewModel>()
.setLifecycleOwner(fragment)
.registerViewDataBinderWithSameModelType(
inflateDataBinding = LanguageItemsBinding::inflate,
setViewModel = LanguageItemsBinding::setViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class AudioLanguageFragmentPresenter @Inject constructor(

private fun createRecyclerViewAdapter(): BindableAdapter<LanguageItemViewModel> {
return singleTypeBuilderFactory.create<LanguageItemViewModel>()
.setLifecycleOwner(fragment)
.registerViewDataBinderWithSameModelType(
inflateDataBinding = LanguageItemsBinding::inflate,
setViewModel = LanguageItemsBinding::setViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ReadingTextSizeFragmentPresenter @Inject constructor(
.registerViewDataBinderWithSameModelType(
inflateDataBinding = TextSizeItemsBinding::inflate,
setViewModel = TextSizeItemsBinding::setViewModel
).setLifecycleOwner(fragment)
)
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ class DragDropSortInteractionView @JvmOverloads constructor(
@Inject
lateinit var viewBindingShim: ViewBindingShim

@Inject
lateinit var fragment: Fragment

@Inject
lateinit var singleTypeBuilderFactory: BindableAdapter.SingleTypeBuilder.Factory

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ class SelectionInteractionView @JvmOverloads constructor(
@Inject
lateinit var bindingInterface: ViewBindingShim

@Inject
lateinit var fragment: Fragment

@Inject
lateinit var singleTypeBuilderFactory: BindableAdapter.SingleTypeBuilder.Factory

Expand Down Expand Up @@ -112,8 +109,7 @@ class SelectionInteractionView @JvmOverloads constructor(
)
.build()
SelectionItemInputType.RADIO_BUTTONS ->
BindableAdapter.SingleTypeBuilder
.Factory(fragment).create<SelectionInteractionContentViewModel>()
singleTypeBuilderFactory.create<SelectionInteractionContentViewModel>()
.registerViewBinder(
inflateView = { parent ->
bindingInterface.provideMultipleChoiceInteractionItemsInflatedView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import org.oppia.android.app.player.state.ConfettiConfig.MEDIUM_CONFETTI_BURST
import org.oppia.android.app.player.state.ConfettiConfig.MINI_CONFETTI_BURST
import org.oppia.android.app.player.state.listener.RouteToHintsAndSolutionListener
import org.oppia.android.app.player.stopplaying.StopStatePlayingSessionWithSavedProgressListener
import org.oppia.android.app.recyclerview.BindableAdapter
import org.oppia.android.app.topic.conceptcard.ConceptCardFragment.Companion.CONCEPT_CARD_DIALOG_FRAGMENT_TAG
import org.oppia.android.app.utility.SplitScreenManager
import org.oppia.android.app.viewmodel.ViewModelProvider
Expand Down Expand Up @@ -69,8 +68,7 @@ class StateFragmentPresenter @Inject constructor(
@DefaultResourceBucketName private val resourceBucketName: String,
private val assemblerBuilderFactory: StatePlayerRecyclerViewAssembler.Builder.Factory,
private val splitScreenManager: SplitScreenManager,
private val oppiaClock: OppiaClock,
private val multiTypeAdapterBuilderFactory: BindableAdapter.MultiTypeBuilder.Factory
private val oppiaClock: OppiaClock
) {

private val routeToHintsAndSolutionListener = activity as RouteToHintsAndSolutionListener
Expand Down Expand Up @@ -118,8 +116,7 @@ class StateFragmentPresenter @Inject constructor(
assemblerBuilderFactory.create(resourceBucketName, entityType, profileId),
binding.congratulationsTextView,
binding.congratulationsTextConfettiView,
binding.fullScreenConfettiView,
multiTypeAdapterBuilderFactory
binding.fullScreenConfettiView
)

val stateRecyclerViewAdapter = recyclerViewAssembler.adapter
Expand Down Expand Up @@ -220,12 +217,10 @@ class StateFragmentPresenter @Inject constructor(
builder: StatePlayerRecyclerViewAssembler.Builder,
congratulationsTextView: TextView,
congratulationsTextConfettiView: KonfettiView,
fullScreenConfettiView: KonfettiView,
multiTypeBuilderFactory: BindableAdapter.MultiTypeBuilder.Factory
fullScreenConfettiView: KonfettiView
): StatePlayerRecyclerViewAssembler {
val isTablet = context.resources.getBoolean(R.bool.isTablet)
return builder
.addAdapterBuilderFactory(multiTypeBuilderFactory)
.hasConversationView(hasConversationView)
.addContentSupport()
.addFeedbackSupport()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -887,11 +887,13 @@ class StatePlayerRecyclerViewAssembler private constructor(
private val interactionViewModelFactoryMap: Map<String, InteractionItemFactory>,
private val backgroundCoroutineDispatcher: CoroutineDispatcher,
private val resourceHandler: AppLanguageResourceHandler,
private val translationController: TranslationController
private val translationController: TranslationController,
private val adapterBuilderFactory: BindableAdapter.MultiTypeBuilder.Factory,
private val singleAdapterFactory: BindableAdapter.SingleTypeBuilder.Factory
) {

private lateinit var adapterBuilder:
BindableAdapter.MultiTypeBuilder<StateItemViewModel, StateItemViewModel.ViewType>
private var adapterBuilder: BindableAdapter.MultiTypeBuilder<StateItemViewModel,
StateItemViewModel.ViewType> = adapterBuilderFactory.create { it.viewType }

/**
* Tracks features individually enabled for the assembler. No features are enabled by default.
Expand Down Expand Up @@ -1116,8 +1118,7 @@ class StatePlayerRecyclerViewAssembler private constructor(
gcsEntityId: String,
supportsConceptCards: Boolean
): BindableAdapter<StringList> {
return BindableAdapter.SingleTypeBuilder
.Factory(fragment).create<StringList>()
return singleAdapterFactory.create<StringList>()
.registerViewBinder(
inflateView = { parent ->
SubmittedAnswerListItemBinding.inflate(
Expand All @@ -1138,8 +1139,7 @@ class StatePlayerRecyclerViewAssembler private constructor(
gcsEntityId: String,
supportsConceptCards: Boolean
): BindableAdapter<String> {
return BindableAdapter.SingleTypeBuilder
.Factory(fragment).create<String>()
return singleAdapterFactory.create<String>()
.registerViewBinder(
inflateView = { parent ->
SubmittedHtmlAnswerItemBinding.inflate(
Expand Down Expand Up @@ -1183,17 +1183,6 @@ class StatePlayerRecyclerViewAssembler private constructor(
}
}

/**
* Add AdapterBuilderFactory passed through injection from [StateFragmentPresenter]
*/
fun addAdapterBuilderFactory
(multiTypeBuilderFactory: BindableAdapter.MultiTypeBuilder.Factory): Builder {
adapterBuilder = multiTypeBuilderFactory.create {
it.viewType
}
return this
}

/**
* Adds support for automatically collapsing past wrong answers. This feature is not enabled
* without [addPastAnswersSupport] also being enabled.
Expand Down Expand Up @@ -1400,7 +1389,9 @@ class StatePlayerRecyclerViewAssembler private constructor(
String, @JvmSuppressWildcards InteractionItemFactory>,
@BackgroundDispatcher private val backgroundCoroutineDispatcher: CoroutineDispatcher,
private val resourceHandler: AppLanguageResourceHandler,
private val translationController: TranslationController
private val translationController: TranslationController,
private val multiAdapterBuilderFactory: BindableAdapter.MultiTypeBuilder.Factory,
private val singleAdapterFactory: BindableAdapter.SingleTypeBuilder.Factory
) {
/**
* Returns a new [Builder] for the specified GCS resource bucket information for loading
Expand All @@ -1418,7 +1409,9 @@ class StatePlayerRecyclerViewAssembler private constructor(
interactionViewModelFactoryMap,
backgroundCoroutineDispatcher,
resourceHandler,
translationController
translationController,
multiAdapterBuilderFactory,
singleAdapterFactory
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ProfileChooserFragmentPresenter @Inject constructor(
private val viewModelProvider: ViewModelProvider<ProfileChooserViewModel>,
private val profileManagementController: ProfileManagementController,
private val oppiaLogger: OppiaLogger,
private val multiTypeAdapterBuilder: BindableAdapter.MultiTypeBuilder.Factory
private val adapterBuilder: BindableAdapter.MultiTypeBuilder.Factory
) {
private lateinit var binding: ProfileChooserFragmentBinding
val hasProfileEverBeenAddedValue = ObservableField<Boolean>(true)
Expand Down Expand Up @@ -149,8 +149,7 @@ class ProfileChooserFragmentPresenter @Inject constructor(
}

private fun createRecyclerViewAdapter(): BindableAdapter<ProfileChooserUiModel> {
return multiTypeAdapterBuilder.create<ProfileChooserUiModel,
ProfileChooserUiModel.ModelTypeCase>(
return adapterBuilder.create<ProfileChooserUiModel, ProfileChooserUiModel.ModelTypeCase>(
ProfileChooserUiModel::getModelTypeCase
)
.registerViewDataBinderWithSameModelType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ private const val TAG_PROFILE_PICTURE_EDIT_DIALOG = "PROFILE_PICTURE_EDIT_DIALOG
@FragmentScope
class ProfileProgressFragmentPresenter @Inject constructor(
private val activity: AppCompatActivity,
private val fragment: Fragment
private val fragment: Fragment,
private val viewModel: ProfileProgressViewModel,
private val multiTypeAdapterBuilder: BindableAdapter.MultiTypeBuilder.Factory
) {

@Inject
lateinit var viewModel: ProfileProgressViewModel

@Inject
lateinit var multiTypeAdapterBuilder: BindableAdapter.MultiTypeBuilder.Factory

fun handleCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down
Loading

0 comments on commit 20c1ff3

Please sign in to comment.