Skip to content

Commit

Permalink
Updated documentation for RunTestsExpandableListViewAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Nov 22, 2023
1 parent 2d4437d commit 4b51010
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class RunTestsActivity : AbstractActivity() {
return@map testSuite.runTestsGroupItem(preferenceManager)
}

mAdapter = RunTestsExpandableListViewAdapter(this, tsGroups, viewModel)
mAdapter = RunTestsExpandableListViewAdapter(tsGroups, viewModel)

binding.expandableListView.setAdapter(mAdapter)
for (i in 0 until mAdapter.groupCount) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openobservatory.ooniprobe.activity.runtests.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -19,38 +18,77 @@ import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite
import org.openobservatory.ooniprobe.test.test.AbstractTest


/**
* Adapter class for the ExpandableListView in RunTestsActivity.
* @param groupedListData List of GroupItem objects.
* @param viewModel RunTestsViewModel object.
*/
class RunTestsExpandableListViewAdapter(
private val mContext: Context,
private val mGroupListData: List<GroupItem>,
private val mViewModel: RunTestsViewModel
private val groupedListData: List<GroupItem>,
private val viewModel: RunTestsViewModel
) : BaseExpandableListAdapter() {
/**
* @return Number of groups in the list.
*/
override fun getGroupCount(): Int {
return mGroupListData.size
return groupedListData.size
}

override fun getChildrenCount(groupPosition: Int): Int = mGroupListData[groupPosition].nettests.size
/**
* @param groupPosition Position of the group in the list.
* @return Number of children in the group.
*/
override fun getChildrenCount(groupPosition: Int): Int = groupedListData[groupPosition].nettests.size

override fun getGroup(groupPosition: Int): GroupItem = mGroupListData[groupPosition]
/**
* @param groupPosition Position of the group in the list.
* @return GroupItem object.
*/
override fun getGroup(groupPosition: Int): GroupItem = groupedListData[groupPosition]

/**
* @param groupPosition Position of the group in the list.
* @param childPosition Position of the child in the group.
* @return ChildItem object.
*/
override fun getChild(groupPosition: Int, childPosition: Int): ChildItem =
mGroupListData[groupPosition].nettests[childPosition]
groupedListData[groupPosition].nettests[childPosition]

/**
* @param groupPosition Position of the group in the list.
* @return Group position.
*/
override fun getGroupId(groupPosition: Int): Long = groupPosition.toLong()

/**
* @param groupPosition Position of the group in the list.
* @param childPosition Position of the child in the group.
* @return Child position.
*/
override fun getChildId(groupPosition: Int, childPosition: Int): Long = childPosition.toLong()

/**
* @return True if the IDs are stable.
*/
override fun hasStableIds(): Boolean = false

/**
* @param groupPosition Position of the group in the list.
* @param isExpanded True if the group is expanded.
* @param convertView View object.
* @param parent ViewGroup object.
* @return View object.
*/
override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup): View? {
var convertView =
convertView ?: LayoutInflater.from(mContext).inflate(R.layout.run_tests_group_list_item, parent, false)
convertView ?: LayoutInflater.from(parent.context).inflate(R.layout.run_tests_group_list_item, parent, false)
val groupItem = getGroup(groupPosition)
val testSuite = AbstractSuite.getTestSuiteByName(groupItem.name)
convertView.findViewById<TextView>(R.id.group_name).text = parent.context.resources.getText(testSuite.title)
convertView.findViewById<ImageView>(R.id.group_icon).setImageResource(testSuite.icon)
val groupIndicator = convertView.findViewById<ImageView>(R.id.group_indicator)
val groupSelectionIndicator = convertView.findViewById<ImageView>(R.id.group_select_indicator)
val selectedAllBtnStatus = mViewModel.selectedAllBtnStatus.getValue()
val selectedAllBtnStatus = viewModel.selectedAllBtnStatus.getValue()
if (selectedAllBtnStatus == SELECT_ALL) {
groupItem.selected = true
for (childItem in groupItem.nettests) {
Expand All @@ -70,18 +108,18 @@ class RunTestsExpandableListViewAdapter(
// NOTE: This is the only place where ExperimentalSuite.NAME is used.
// This doesn't follow the normal rule where the component tests make up the suite.
if (groupItem.name == ExperimentalSuite.NAME) {
mViewModel.enableTest(ExperimentalSuite.NAME)
viewModel.enableTest(ExperimentalSuite.NAME)
}
} else {
groupSelectionIndicator.setImageResource(R.drawable.check_box_outline_blank)
if (groupItem.name == ExperimentalSuite.NAME) {
mViewModel.disableTest(ExperimentalSuite.NAME)
viewModel.disableTest(ExperimentalSuite.NAME)
}
}
} else {
groupSelectionIndicator.setImageResource(R.drawable.check_box_outline_blank)
if (groupItem.name == ExperimentalSuite.NAME) {
mViewModel.disableTest(ExperimentalSuite.NAME)
viewModel.disableTest(ExperimentalSuite.NAME)
}
}
groupSelectionIndicator.setOnClickListener {
Expand All @@ -90,20 +128,20 @@ class RunTestsExpandableListViewAdapter(
for (childItem in groupItem.nettests) {
childItem.selected = false
}
if (isNotSelectedAnyGroupItem(mGroupListData)) {
mViewModel.setSelectedAllBtnStatus(SELECT_NONE)
if (isNotSelectedAnyGroupItem(groupedListData)) {
viewModel.setSelectedAllBtnStatus(SELECT_NONE)
} else {
mViewModel.setSelectedAllBtnStatus(SELECT_SOME)
viewModel.setSelectedAllBtnStatus(SELECT_SOME)
}
} else {
groupItem.selected = true
for (childItem in groupItem.nettests) {
childItem.selected = true
}
if (isSelectedAllItems(mGroupListData)) {
mViewModel.setSelectedAllBtnStatus(SELECT_ALL)
if (isSelectedAllItems(groupedListData)) {
viewModel.setSelectedAllBtnStatus(SELECT_ALL)
} else {
mViewModel.setSelectedAllBtnStatus(SELECT_SOME)
viewModel.setSelectedAllBtnStatus(SELECT_SOME)
}
}
notifyDataSetChanged()
Expand All @@ -116,6 +154,14 @@ class RunTestsExpandableListViewAdapter(
return convertView
}

/**
* @param groupPosition Position of the group in the list.
* @param childPosition Position of the child in the group.
* @param isLastChild True if the child is the last child in the group.
* @param convertView View object.
* @param parent ViewGroup object.
* @return View object.
*/
override fun getChildView(
groupPosition: Int,
childPosition: Int,
Expand All @@ -124,7 +170,7 @@ class RunTestsExpandableListViewAdapter(
parent: ViewGroup
): View? {
var convertView =
convertView ?: LayoutInflater.from(mContext).inflate(R.layout.run_tests_child_list_item, parent, false)
convertView ?: LayoutInflater.from(parent.context).inflate(R.layout.run_tests_child_list_item, parent, false)
val childItem = getChild(groupPosition, childPosition)
val groupItem = getGroup(groupPosition)
val nettest = AbstractTest.getTestByName(childItem.name)
Expand All @@ -149,23 +195,23 @@ class RunTestsExpandableListViewAdapter(
setOnClickListener {
if (childItem.selected) {
childItem.selected = false
mViewModel.disableTest(childItem.name)
viewModel.disableTest(childItem.name)
if (isNotSelectedAnyChildItems(groupItem.nettests)) {
groupItem.selected = false
}
if (isNotSelectedAnyItems(mGroupListData)) {
mViewModel.setSelectedAllBtnStatus(SELECT_NONE)
if (isNotSelectedAnyItems(groupedListData)) {
viewModel.setSelectedAllBtnStatus(SELECT_NONE)
} else {
mViewModel.setSelectedAllBtnStatus(SELECT_SOME)
viewModel.setSelectedAllBtnStatus(SELECT_SOME)
}
} else {
childItem.selected = true
mViewModel.enableTest(childItem.name)
viewModel.enableTest(childItem.name)
groupItem.selected = true
if (isSelectedAllItems(mGroupListData)) {
mViewModel.setSelectedAllBtnStatus(SELECT_ALL)
if (isSelectedAllItems(groupedListData)) {
viewModel.setSelectedAllBtnStatus(SELECT_ALL)
} else {
mViewModel.setSelectedAllBtnStatus(SELECT_SOME)
viewModel.setSelectedAllBtnStatus(SELECT_SOME)
}
}
notifyDataSetChanged()
Expand All @@ -174,8 +220,17 @@ class RunTestsExpandableListViewAdapter(
return convertView
}

/**
* @param groupPosition Position of the group in the list.
* @param childPosition Position of the child in the group.
* @return True if the child is selectable.
*/
override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = false

/**
* @param groupItemsList List of GroupItem objects.
* @return True if no group item in the list is selected.
*/
private fun isNotSelectedAnyGroupItem(groupItemsList: List<GroupItem>): Boolean {
for (groupItem in groupItemsList) {
if (groupItem.selected) {
Expand All @@ -185,6 +240,10 @@ class RunTestsExpandableListViewAdapter(
return true
}

/**
* @param childItemList List of ChildItem objects.
* @return True if no child item in the list is selected.
*/
private fun isNotSelectedAnyChildItems(childItemList: List<ChildItem>): Boolean {
for (childItem in childItemList) {
if (childItem.selected) {
Expand All @@ -194,6 +253,10 @@ class RunTestsExpandableListViewAdapter(
return true
}

/**
* @param childItemList List of ChildItem objects.
* @return True if all child items in the list are selected.
*/
private fun isSelectAllChildItems(childItemList: List<ChildItem>): Boolean {
for (childItem in childItemList) {
if (!childItem.selected) {
Expand All @@ -203,6 +266,10 @@ class RunTestsExpandableListViewAdapter(
return true
}

/**
* @param groupItemList List of GroupItem objects.
* @return True if all group items in the list are selected.
*/
private fun isSelectedAllItems(groupItemList: List<GroupItem>?): Boolean {
for (groupItem in groupItemList!!) {
if (!groupItem.selected) {
Expand All @@ -215,6 +282,10 @@ class RunTestsExpandableListViewAdapter(
return true
}

/**
* @param groupItemList List of GroupItem objects.
* @return True if no group item in the list is selected.
*/
private fun isNotSelectedAnyItems(groupItemList: List<GroupItem>?): Boolean {
for (groupItem in groupItemList!!) {
if (groupItem.selected) {
Expand Down

0 comments on commit 4b51010

Please sign in to comment.