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 current recyclerview implementation with BindableAdapter usage in Help Activity #956

Merged
merged 3 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
52 changes: 0 additions & 52 deletions app/src/main/java/org/oppia/app/help/HelpCategoryAdapter.kt

This file was deleted.

48 changes: 27 additions & 21 deletions app/src/main/java/org/oppia/app/help/HelpFragmentPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,48 @@ import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import org.oppia.app.R
import org.oppia.app.databinding.HelpFragmentBinding
import org.oppia.app.databinding.HelpItemBinding
import org.oppia.app.fragment.FragmentScope
import org.oppia.app.recyclerview.BindableAdapter
import org.oppia.app.viewmodel.ViewModelProvider
import javax.inject.Inject

/** The presenter for [HelpFragment]. */
@FragmentScope
class HelpFragmentPresenter @Inject constructor(
private val activity: AppCompatActivity,
private val fragment: Fragment
private val fragment: Fragment,
private val viewModelProvider: ViewModelProvider<HelpListViewModel>
) {
private val arrayList = ArrayList<HelpViewModel>()
private lateinit var binding: HelpFragmentBinding

fun handleCreateView(inflater: LayoutInflater, container: ViewGroup?): View? {
val binding: HelpFragmentBinding =
HelpFragmentBinding.inflate(
inflater,
container,
/* attachToRoot= */ false
)
binding.lifecycleOwner = fragment
val viewModel = getHelpListViewModel()

binding = HelpFragmentBinding.inflate(inflater, container, /* attachToRoot = */ false)
binding.helpFragmentRecyclerView.apply {
adapter = HelpCategoryAdapter(activity, getRecyclerViewItemList())
layoutManager = LinearLayoutManager(activity)
layoutManager = LinearLayoutManager(activity.applicationContext)
adapter = createRecyclerViewAdapter()
}

binding.let {
it.lifecycleOwner = fragment
it.viewModel = viewModel
}
return binding.root
}

private fun getRecyclerViewItemList(): ArrayList<HelpViewModel> {
for (item in HelpItems.values()) {
if (item == HelpItems.FAQ) {
val category1 = fragment.getString(R.string.frequently_asked_questions_FAQ)
val helpViewModel = HelpViewModel(category1)
arrayList.add(helpViewModel)
}
}
return arrayList
private fun createRecyclerViewAdapter(): BindableAdapter<HelpItemViewModel> {
return BindableAdapter.SingleTypeBuilder
.newBuilder<HelpItemViewModel>()
.registerViewDataBinderWithSameModelType(
inflateDataBinding = HelpItemBinding::inflate,
setViewModel = HelpItemBinding::setViewModel
).build()
}

private fun getHelpListViewModel(): HelpListViewModel {
return viewModelProvider.getForFragment(fragment, HelpListViewModel::class.java)
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/org/oppia/app/help/HelpItemViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.oppia.app.help

import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import org.oppia.app.R

/** [ViewModel] for the recycler view of HelpActivity. */
class HelpItemViewModel(
val activity: AppCompatActivity,
val title: String
) : ViewModel() {
fun onClick(title: String) {
if (title == activity.getString(R.string.frequently_asked_questions_FAQ)) {
val routeToFAQListener = activity as RouteToFAQListListener
routeToFAQListener.onRouteToFAQList()
}
}
}
28 changes: 28 additions & 0 deletions app/src/main/java/org/oppia/app/help/HelpListViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.oppia.app.help

import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import org.oppia.app.R
import javax.inject.Inject

/** View model in [HelpFragment]. */
class HelpListViewModel @Inject constructor(
val activity: AppCompatActivity
) : ViewModel() {
private val arrayList = ArrayList<HelpItemViewModel>()

val helpItemItemList: List<HelpItemViewModel> by lazy {
getRecyclerViewItemList()
}

private fun getRecyclerViewItemList(): ArrayList<HelpItemViewModel> {
for (item in HelpItems.values()) {
if (item == HelpItems.FAQ) {
val category1 = activity.getString(R.string.frequently_asked_questions_FAQ)
val helpViewModel = HelpItemViewModel(activity,category1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit spacing issue:
val helpViewModel = HelpItemViewModel(activity, category1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

arrayList.add(helpViewModel)
}
}
return arrayList
}
}
6 changes: 0 additions & 6 deletions app/src/main/java/org/oppia/app/help/HelpViewModel.kt

This file was deleted.

3 changes: 2 additions & 1 deletion app/src/main/res/layout-land/help_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<variable
name="viewModel"
type="org.oppia.app.help.HelpViewModel" />
type="org.oppia.app.help.HelpItemViewModel" />
</data>

<TextView
Expand All @@ -19,6 +19,7 @@
android:paddingTop="20dp"
android:paddingEnd="16dp"
android:paddingBottom="20dp"
android:onClick="@{() -> viewModel.onClick(viewModel.title)}"
android:text="@{viewModel.title}"
android:textColor="@color/oppiaPrimaryText"
android:textSize="16sp"
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/res/layout/help_fragment.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

<variable
name="viewModel"
type="org.oppia.app.help.HelpListViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
Expand All @@ -8,6 +16,7 @@
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/help_fragment_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
app:list="@{viewModel.helpItemItemList}" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
3 changes: 2 additions & 1 deletion app/src/main/res/layout/help_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<variable
name="viewModel"
type="org.oppia.app.help.HelpViewModel" />
type="org.oppia.app.help.HelpItemViewModel" />
</data>

<TextView
Expand All @@ -19,6 +19,7 @@
android:paddingTop="20dp"
android:paddingEnd="16dp"
android:paddingBottom="20dp"
android:onClick="@{() -> viewModel.onClick(viewModel.title)}"
android:text="@{viewModel.title}"
android:textColor="@color/oppiaPrimaryText"
android:textSize="16sp"
Expand Down