Skip to content

Commit

Permalink
Updated button status
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Nov 2, 2023
1 parent 9309eff commit 51f350e
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View.GONE
import android.view.View.VISIBLE
import org.openobservatory.ooniprobe.R
import org.openobservatory.ooniprobe.activity.AbstractActivity
import org.openobservatory.ooniprobe.activity.runtests.RunTestsViewModel.Companion.NOT_SELECT_ANY
import org.openobservatory.ooniprobe.activity.runtests.RunTestsViewModel.Companion.SELECT_ALL
import org.openobservatory.ooniprobe.activity.runtests.RunTestsViewModel.Companion.SELECT_SOME
import org.openobservatory.ooniprobe.activity.runtests.adapter.RunTestsExpandableListViewAdapter
import org.openobservatory.ooniprobe.activity.runtests.models.ChildItem
import org.openobservatory.ooniprobe.activity.runtests.models.GroupItem
Expand All @@ -18,6 +22,7 @@ import javax.inject.Inject

class RunTestsActivity : AbstractActivity() {
lateinit var binding: ActivityRunTestsBinding

lateinit var mAdapter: RunTestsExpandableListViewAdapter

@Inject
Expand Down Expand Up @@ -62,14 +67,36 @@ class RunTestsActivity : AbstractActivity() {

mAdapter = RunTestsExpandableListViewAdapter(this, tsGroups, viewModel)

binding.elv.setAdapter(mAdapter)
binding.expandableListView.setAdapter(mAdapter)
binding.selectAll.setOnClickListener {
viewModel.selectedAllBtnStatus.value = SELECT_ALL
mAdapter.notifyDataSetChanged()
updateStatusIndicator()
}

binding.selectNone.setOnClickListener {
viewModel.selectedAllBtnStatus.value = NOT_SELECT_ANY
mAdapter.notifyDataSetChanged()
updateStatusIndicator()
}

// TODO(aanorbel) Update button color from theme
viewModel.selectedAllBtnStatus.observe(this) { selectAllBtnStatus ->
if (!TextUtils.isEmpty(selectAllBtnStatus)) {
if (selectAllBtnStatus == SELECT_ALL) {
binding.ckbSelectAll.isChecked = true
} else {
binding.ckbSelectAll.isChecked = false
when (selectAllBtnStatus) {
SELECT_ALL -> {
binding.selectNone.isActivated = true
binding.selectAll.isActivated = false
}
NOT_SELECT_ANY -> {

binding.selectNone.isActivated = true
binding.selectAll.isActivated = false
}
SELECT_SOME -> {
binding.selectNone.isActivated = true
binding.selectAll.isActivated = true
}
}
mAdapter.notifyDataSetChanged()
updateStatusIndicator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,18 @@ class RunTestsExpandableListViewAdapter(
return mGroupListData.size
}

override fun getChildrenCount(groupPosition: Int): Int {
return mGroupListData[groupPosition].nettests.size
}
override fun getChildrenCount(groupPosition: Int): Int = mGroupListData[groupPosition].nettests.size

override fun getGroup(groupPosition: Int): GroupItem {
return mGroupListData[groupPosition]
}
override fun getGroup(groupPosition: Int): GroupItem = mGroupListData[groupPosition]

override fun getChild(groupPosition: Int, childPosition: Int): ChildItem {
return mGroupListData[groupPosition].nettests[childPosition]
}
override fun getChild(groupPosition: Int, childPosition: Int): ChildItem =
mGroupListData[groupPosition].nettests[childPosition]

override fun getGroupId(groupPosition: Int): Long {
return groupPosition.toLong()
}
override fun getGroupId(groupPosition: Int): Long = groupPosition.toLong()

override fun getChildId(groupPosition: Int, childPosition: Int): Long {
return childPosition.toLong()
}
override fun getChildId(groupPosition: Int, childPosition: Int): Long = childPosition.toLong()

override fun hasStableIds(): Boolean {
return false
}
override fun hasStableIds(): Boolean = false

override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup): View? {
var convertView =
Expand All @@ -73,6 +62,8 @@ class RunTestsExpandableListViewAdapter(
for (childItem in groupItem.nettests) {
childItem.selected = false
}
} else if (isSelectAllChildItems(groupItem.nettests)){
groupItem.selected = true
}
if (groupItem.selected) {
if (isSelectAllChildItems(groupItem.nettests)) {
Expand Down Expand Up @@ -138,12 +129,16 @@ class RunTestsExpandableListViewAdapter(
}
}
}
convertView.findViewById<CheckBox>(R.id.child_select).apply {
isChecked = childItem.selected
setOnCheckedChangeListener { buttonView, isChecked ->

childItem.selected = isChecked
convertView.findViewById<ImageView>(R.id.child_select).apply {
setImageResource(
when (childItem.selected) {
true -> R.drawable.check_box
false -> R.drawable.check_box_outline_blank
}
)
setOnClickListener {
if (childItem.selected) {
childItem.selected = false
if (isNotSelectedAnyChildItems(groupItem.nettests)) {
groupItem.selected = false
}
Expand All @@ -153,6 +148,7 @@ class RunTestsExpandableListViewAdapter(
mViewModel.setSelectedAllBtnStatus(SELECT_SOME)
}
} else {
childItem.selected = true
groupItem.selected = true
if (isSelectedAllItems(mGroupListData)) {
mViewModel.setSelectedAllBtnStatus(SELECT_ALL)
Expand All @@ -166,9 +162,7 @@ class RunTestsExpandableListViewAdapter(
return convertView
}

override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
return false
}
override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = false

private fun isNotSelectedAnyGroupItem(groupItemsList: List<GroupItem>): Boolean {
for (groupItem in groupItemsList) {
Expand All @@ -179,15 +173,6 @@ class RunTestsExpandableListViewAdapter(
return true
}

private fun isSelectedAllGroupItems(groupItemsList: List<GroupItem>): Boolean {
for (groupItem in groupItemsList) {
if (!groupItem.selected) {
return false
}
}
return true
}

private fun isNotSelectedAnyChildItems(childItemList: List<ChildItem>): Boolean {
for (childItem in childItemList) {
if (childItem.selected) {
Expand Down
54 changes: 28 additions & 26 deletions app/src/main/res/layout/activity_run_tests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,51 @@
android:layout_height="match_parent"
tools:context=".activity.runtests.RunTestsActivity">

<RelativeLayout
<LinearLayout
android:id="@+id/control_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/rlSelectAll"
app:layout_constraintTop_toTopOf="parent">

<TextView
<Button
android:id="@+id/select_all"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginStart="62dp"
android:gravity="center_vertical"
android:maxLines="3"
android:text="Select All Btn"
android:textColor="@color/color_base"
android:textSize="16sp"/>
android:layout_height="wrap_content"
android:text="@string/Settings_Websites_Categories_Selection_All"
android:textAllCaps="false"
app:cornerRadius="24dp"
app:rippleColor="@color/ripple_material_dark"
app:strokeColor="@color/color_base" />

<CheckBox
android:id="@+id/ckbSelectAll"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"/>
<Button
android:id="@+id/select_none"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Settings_Websites_Categories_Selection_None"
android:textAllCaps="false"
app:cornerRadius="24dp"
app:rippleColor="@color/ripple_material_dark"
app:strokeColor="@color/color_base" />

</RelativeLayout>
</LinearLayout>


<ExpandableListView
android:id="@+id/elv"
android:id="@+id/expandable_list_view"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:groupIndicator="@null"
android:childDivider="@color/color_gray3"
android:childDivider="@android:color/transparent"
android:divider="@android:color/transparent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/rlSelectAll"
app:layout_constraintBottom_toTopOf="@+id/bottom_appbar_layout">
app:layout_constraintTop_toBottomOf="@id/control_layout"
app:layout_constraintBottom_toTopOf="@+id/bottom_appbar_layout"/>

</ExpandableListView>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/bottom_appbar_layout"
android:layout_width="match_parent"
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/res/layout/run_tests_child_list_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="8dp"
android:layout_marginVertical="16dp"
tools:context=".activity.runtests.adapter.RunTestsExpandableListViewAdapter">

<TextView
Expand All @@ -17,11 +17,15 @@
android:text="@string/Test_Tor_Fullname"
android:textColor="@color/color_black"/>

<CheckBox
<ImageView
android:id="@+id/child_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"/>
android:layout_centerVertical="true"
android:layout_marginVertical="16dp"
android:layout_marginEnd="16dp"
android:src="@drawable/check_box_outline_blank"
android:contentDescription="checkbox"/>

</RelativeLayout>
2 changes: 0 additions & 2 deletions app/src/main/res/layout/run_tests_group_list_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:text="@{ExpandableListViewGroupItem.mGroupName, default=my_default}"
android:textColor="#000000"
android:textSize="16sp"/>

<ImageView
Expand Down

0 comments on commit 51f350e

Please sign in to comment.