diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/RunTestsActivity.kt b/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/RunTestsActivity.kt index 463923148..7ad4e06b0 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/RunTestsActivity.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/RunTestsActivity.kt @@ -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) { diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/adapter/RunTestsExpandableListViewAdapter.kt b/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/adapter/RunTestsExpandableListViewAdapter.kt index 7415076d8..58ac76a9d 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/adapter/RunTestsExpandableListViewAdapter.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/adapter/RunTestsExpandableListViewAdapter.kt @@ -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 @@ -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, - private val mViewModel: RunTestsViewModel + private val groupedListData: List, + 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(R.id.group_name).text = parent.context.resources.getText(testSuite.title) convertView.findViewById(R.id.group_icon).setImageResource(testSuite.icon) val groupIndicator = convertView.findViewById(R.id.group_indicator) val groupSelectionIndicator = convertView.findViewById(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) { @@ -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 { @@ -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() @@ -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, @@ -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) @@ -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() @@ -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): Boolean { for (groupItem in groupItemsList) { if (groupItem.selected) { @@ -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): Boolean { for (childItem in childItemList) { if (childItem.selected) { @@ -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): Boolean { for (childItem in childItemList) { if (!childItem.selected) { @@ -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?): Boolean { for (groupItem in groupItemList!!) { if (!groupItem.selected) { @@ -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?): Boolean { for (groupItem in groupItemList!!) { if (groupItem.selected) {