Skip to content

Commit

Permalink
feat: conform to material design checkbox patter
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Aug 23, 2024
1 parent 4ef50f4 commit da47ebe
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import androidx.work.WorkInfo;
import androidx.work.WorkManager;

import com.google.android.material.checkbox.MaterialCheckBox;
import com.google.android.material.snackbar.Snackbar;

import org.openobservatory.engine.BaseNettest;
Expand Down Expand Up @@ -104,41 +103,37 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
binding.expandableListView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
if (binding.expandableListView.getChildCount() > 0) {
if (adapter.isSelectedAllItems()) {
binding.switchTests.setCheckedState(MaterialCheckBox.STATE_CHECKED);
binding.switchTests.setImageResource(R.drawable.check_box);
} else if (adapter.isNotSelectedAnyGroupItem()) {
binding.switchTests.setCheckedState(MaterialCheckBox.STATE_UNCHECKED);
binding.switchTests.setImageResource(R.drawable.check_box_outline_blank);
} else {
binding.switchTests.setCheckedState(MaterialCheckBox.STATE_INDETERMINATE);
binding.switchTests.setImageResource(R.drawable.check_box_indeterminate);
}
}
});

binding.switchTests.addOnCheckedStateChangedListener((checkBox, state) -> {
switch (state) {
case MaterialCheckBox.STATE_CHECKED -> {
viewModel.setSelectedAllBtnStatus(SELECT_ALL);
adapter.notifyDataSetChanged();
}
case MaterialCheckBox.STATE_UNCHECKED -> {
viewModel.setSelectedAllBtnStatus(SELECT_NONE);
adapter.notifyDataSetChanged();
}
case MaterialCheckBox.STATE_INDETERMINATE -> {
viewModel.setSelectedAllBtnStatus(SELECT_SOME);
adapter.notifyDataSetChanged();
}
binding.switchTests.setOnClickListener( view -> {
if (adapter.isSelectedAllItems()){
viewModel.setSelectedAllBtnStatus(SELECT_NONE);
adapter.notifyDataSetChanged();
} else{
viewModel.setSelectedAllBtnStatus(SELECT_ALL);
adapter.notifyDataSetChanged();
}
});

if (descriptor.getName().equals(OONITests.EXPERIMENTAL.getLabel())) {
binding.switchTests.setChecked(resolveStatus(preferenceManager, descriptor.getName(), descriptor.preferencePrefix(), true));
binding.switchTests.setImageResource(
resolveStatus(preferenceManager, descriptor.getName(), descriptor.preferencePrefix(), true)?
R.drawable.check_box : R.drawable.check_box_outline_blank
);
} else {
if (adapter.isSelectedAllItems()) {
binding.switchTests.setCheckedState(MaterialCheckBox.STATE_CHECKED);
binding.switchTests.setImageResource(R.drawable.check_box);
} else if (adapter.isNotSelectedAnyGroupItem()) {
binding.switchTests.setCheckedState(MaterialCheckBox.STATE_UNCHECKED);
binding.switchTests.setImageResource(R.drawable.check_box_outline_blank);
} else {
binding.switchTests.setCheckedState(MaterialCheckBox.STATE_INDETERMINATE);
binding.switchTests.setImageResource(R.drawable.check_box_indeterminate);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.activity.viewModels
import androidx.databinding.BindingAdapter
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.checkbox.MaterialCheckBox
import io.noties.markwon.Markwon
import org.openobservatory.ooniprobe.R
import org.openobservatory.ooniprobe.activity.AbstractActivity
Expand Down Expand Up @@ -160,13 +161,26 @@ class AddDescriptorActivity : AbstractActivity() {
}

binding.expandableListView.viewTreeObserver.addOnGlobalLayoutListener {
binding.testsCheckbox.checkedState = viewModel.selectedAllBtnStatus.value!!
if (binding.expandableListView.childCount > 0) {
if (adapter.isSelectedAllItems()) {
binding.testsCheckbox.setImageResource(R.drawable.check_box)
} else if (adapter.isNotSelectedAnyGroupItem()) {
binding.testsCheckbox.setImageResource(R.drawable.check_box_outline_blank)
} else {
binding.testsCheckbox.setImageResource(R.drawable.check_box_indeterminate)
}
}
}

// This observer is used to change the state of the "Select All" button when a checkbox is clicked.
binding.testsCheckbox.addOnCheckedStateChangedListener { checkBox, state ->
viewModel.setSelectedAllBtnStatus(state)
adapter.notifyDataSetChanged()
binding.testsCheckbox.setOnClickListener { _ ->
if (adapter.isSelectedAllItems()) {
viewModel.setSelectedAllBtnStatus(MaterialCheckBox.STATE_UNCHECKED)
adapter.notifyDataSetChanged()
} else {
viewModel.setSelectedAllBtnStatus(MaterialCheckBox.STATE_CHECKED)
adapter.notifyDataSetChanged()
}
}

// This observer is used to finish the activity when the descriptor is added.
Expand Down
22 changes: 14 additions & 8 deletions app/src/main/res/layout/activity_add_descriptor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,32 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/settings" />

<TextView

<ImageView
android:id="@+id/tests_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/AddDescriptor_AutoRun"
app:layout_constraintBottom_toBottomOf="@id/tests_checkbox"
android:src="@drawable/check_box_outline_blank"
app:tint="@color/color_blue4"
android:padding="@dimen/item_padding_small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/tests_checkbox" />
app:layout_constraintTop_toBottomOf="@id/automatic_updates_switch" />

<com.google.android.material.checkbox.MaterialCheckBox
android:id="@+id/tests_checkbox"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/automatic_updates_switch" />
android:text="@string/AddDescriptor_AutoRun"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="@id/tests_checkbox"
app:layout_constraintStart_toEndOf="@id/tests_checkbox"
app:layout_constraintTop_toTopOf="@id/tests_checkbox" />

<org.openobservatory.ooniprobe.common.views.CustomExpandableListView
android:id="@+id/expandable_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:groupIndicator="@null"
android:paddingStart="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tests_checkbox"
Expand Down
32 changes: 18 additions & 14 deletions app/src/main/res/layout/activity_overview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
Expand All @@ -175,20 +174,25 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="32dp"
android:layout_marginStart="@dimen/item_padding_large"
android:layout_marginEnd="@dimen/item_padding_large"
app:richText="@{viewmodel.description}"
app:testName="@{viewmodel.descriptor.name}"/>

<TextView
android:id="@+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/item_padding_large"
android:text="@string/AddDescriptor_Settings"
android:layout_below="@+id/desc"
style="?attr/textAppearanceHeadline6"/>

<LinearLayout
android:id="@+id/automatic_updates_container"
android:layout_width="match_parent"
android:layout_marginStart="@dimen/item_padding_large"
android:layout_marginEnd="@dimen/item_padding_large"
android:layout_height="wrap_content"
android:layout_below="@+id/settings"
android:paddingBottom="16dp">
Expand All @@ -213,25 +217,25 @@
android:id="@+id/header_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/item_padding_small"
android:layout_marginEnd="@dimen/item_padding_small"
android:layout_below="@+id/automatic_updates_container">

<TextView
<ImageView
android:id="@+id/switch_tests"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/AddDescriptor_AutoRun" />

<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
android:padding="@dimen/item_padding_small"
android:src="@drawable/check_box_outline_blank"
app:tint="@color/color_blue4"
android:visibility="@{viewmodel.automaticRunSwitchVisibility ,default = visible}"/>

<com.google.android.material.checkbox.MaterialCheckBox
android:id="@+id/switch_tests"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/check_box_custom_button"
app:checkedState="indeterminate"
android:visibility="@{viewmodel.automaticRunSwitchVisibility ,default = visible}"/>
android:layout_marginTop="@dimen/item_padding_small"
android:text="@string/AddDescriptor_AutoRun"
android:textSize="16sp"/>
</LinearLayout>
</RelativeLayout>

Expand All @@ -242,7 +246,7 @@
android:childDivider="@android:color/transparent"
android:divider="@android:color/transparent"
android:groupIndicator="@null"
android:paddingStart="16dp"
android:paddingStart="38dp"
app:layout_constraintTop_toBottomOf="@id/header"
tools:listitem="@layout/overview_test_group_list_item" />

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_run_tests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
android:childDivider="@android:color/transparent"
android:divider="@android:color/transparent"
android:groupIndicator="@null"
android:paddingHorizontal="@dimen/item_padding_small"
android:nestedScrollingEnabled="true"
android:listSelector="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
Expand Down
26 changes: 12 additions & 14 deletions app/src/main/res/layout/nettest_group_list_item.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
android:id="@+id/groupCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/item_padding_small"
app:paddingEnd="10dp"
android:src="@drawable/check_box_outline_blank"
app:tint="@color/color_blue4"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginTop="@dimen/item_padding_small"
android:layout_marginBottom="4dp">

<TextView
Expand All @@ -32,15 +41,4 @@

</LinearLayout>


<ImageView
android:id="@+id/groupCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/item_padding_small"
app:paddingEnd="10dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:src="@drawable/check_box_outline_blank"
app:tint="@color/color_blue4"/>
</RelativeLayout>
</LinearLayout>
28 changes: 13 additions & 15 deletions app/src/main/res/layout/overview_test_group_list_item.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".activity.overview.OverviewTestsExpandableListViewAdapter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingEnd="16dp" >
android:orientation="horizontal">

<ImageView
android:id="@+id/groupCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/item_padding_small"
android:src="@drawable/check_box_outline_blank"
app:tint="@color/color_blue4"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginTop="@dimen/item_padding_small"
android:layout_marginBottom="4dp">

<ImageView
Expand Down Expand Up @@ -39,15 +47,5 @@
app:tint="@color/color_black" />

</LinearLayout>
<ImageView
android:id="@+id/groupCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/item_padding_small"
android:paddingEnd="10dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:src="@drawable/check_box_outline_blank"
app:tint="@color/color_blue4"/>

</RelativeLayout>
</LinearLayout>
40 changes: 18 additions & 22 deletions app/src/main/res/layout/run_tests_child_list_item.xml
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".activity.runtests.adapter.RunTestsExpandableListViewAdapter">

<TextView
android:id="@+id/child_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="48dp"
android:maxLines="1"
android:text="@string/Test_Tor_Fullname"
android:textColor="@color/color_black" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".activity.runtests.adapter.RunTestsExpandableListViewAdapter">

<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_marginVertical="8dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="32dp"
android:padding="@dimen/item_padding_small"
android:src="@drawable/check_box_outline_blank"
app:tint="@color/color_base" />
app:tint="@color/color_blue4" />

<TextView
android:id="@+id/child_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:layout_marginTop="@dimen/item_padding_small"
android:text="@string/Test_Tor_Fullname"
android:textColor="@color/color_black" />

</RelativeLayout>
</LinearLayout>
Loading

0 comments on commit da47ebe

Please sign in to comment.