Skip to content

Commit

Permalink
Merge pull request #228 from dhruv2295/feature/sensorSettings
Browse files Browse the repository at this point in the history
add select all button for sensor list
  • Loading branch information
thias15 authored Sep 26, 2021
2 parents 0160f31 + 540c05a commit d74a821
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@
import java.util.HashMap;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.openbot.R;
import org.openbot.databinding.ItemSensorBinding;
import org.openbot.env.SharedPreferencesManager;

public class SensorListAdapter extends RecyclerView.Adapter<SensorListAdapter.ViewHolder> {

private final List<String> names;
private final List<Boolean> status;
private List<Boolean> status;
private final SharedPreferencesManager preferencesManager;
private CheckBox allSelector;

public SensorListAdapter(
HashMap<String, Boolean> items, SharedPreferencesManager preferencesManager) {
HashMap<String, Boolean> items,
CheckBox allSelector,
SharedPreferencesManager preferencesManager) {
names = new ArrayList<>(items.keySet());
status = new ArrayList<>(items.values());
this.preferencesManager = preferencesManager;
this.allSelector = allSelector;
}

@NotNull
Expand All @@ -36,7 +41,24 @@ public void onBindViewHolder(final ViewHolder holder, int position) {
holder.checkBox.setText(names.get(position));
holder.checkBox.setChecked(status.get(position));
holder.checkBox.setOnClickListener(
v -> preferencesManager.setSensorStatus(holder.checkBox.isChecked(), names.get(position)));
v -> {
status.set(position, holder.checkBox.isChecked());
preferencesManager.setSensorStatus(holder.checkBox.isChecked(), names.get(position));
allSelector.setChecked(getStatusChange());
allSelector.setText(
allSelector.isChecked()
? allSelector.getContext().getResources().getString(R.string.clearAll)
: allSelector.getContext().getResources().getString(R.string.selectAll));
});
}

private boolean getStatusChange() {
for (boolean b : status) if (!b) return false;
return true;
}

public void updateStatusValue(boolean value) {
for (int i = 0; i < status.size(); i++) status.set(i, value);
}

@Override
Expand Down
36 changes: 35 additions & 1 deletion android/app/src/main/java/org/openbot/logging/SensorsDialog.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openbot.logging;

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
Expand All @@ -11,9 +12,11 @@
import android.view.ViewGroup;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.openbot.R;
import org.openbot.databinding.DialogSensorsBinding;
Expand All @@ -30,6 +33,7 @@ public void onCreate(Bundle savedInstanceState) {
setStyle(DialogFragment.STYLE_NORMAL, R.style.FullScreenDialog);
}

@SuppressLint("NotifyDataSetChanged")
@Override
public View onCreateView(
@NotNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Expand Down Expand Up @@ -57,12 +61,42 @@ public View onCreateView(
}
}

SensorListAdapter adapter = new SensorListAdapter(list, preferencesManager);
SensorListAdapter adapter =
new SensorListAdapter(list, binding.selectAllCheck, preferencesManager);

binding.listView.setLayoutManager(new LinearLayoutManager(requireContext()));
binding.listView.setAdapter(adapter);
binding.dismiss.setOnClickListener(v -> dismiss());

int sensorStatusCount = 0;

for (boolean b : new ArrayList<>(list.values()))
if (b) sensorStatusCount++;
else break;

if (list.values().size() == sensorStatusCount) {
binding.selectAllCheck.setChecked(true);
binding.selectAllCheck.setText(getResources().getText(R.string.clearAll));
} else {
binding.selectAllCheck.setChecked(false);
binding.selectAllCheck.setText(getResources().getText(R.string.selectAll));
}

binding.selectAllCheck.setOnClickListener(
v -> {
for (Map.Entry<String, Boolean> entry : list.entrySet())
preferencesManager.setSensorStatus(binding.selectAllCheck.isChecked(), entry.getKey());

binding.selectAllCheck.setChecked(binding.selectAllCheck.isChecked());
binding.selectAllCheck.setText(
binding.selectAllCheck.isChecked()
? getResources().getText(R.string.clearAll)
: getResources().getText(R.string.selectAll));

adapter.updateStatusValue(binding.selectAllCheck.isChecked());
adapter.notifyDataSetChanged();
});

binding.delay.setText(String.valueOf(preferencesManager.getDelay()));
binding.delay.addTextChangedListener(
new TextWatcher() {
Expand Down
67 changes: 49 additions & 18 deletions android/app/src/main/res/layout-land/dialog_sensors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,47 +36,78 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/listView"
android:layout_width="match_parent"
<androidx.core.widget.NestedScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/textView"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
app:layout_constraintBottom_toTopOf="@+id/delayContainer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/delayContainer"
tools:listitem="@layout/item_sensor" />
app:layout_constraintTop_toBottomOf="@+id/textView">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<CheckBox
android:id="@+id/selectAllCheck"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="16dp"
android:textColor="@android:color/black"
android:textSize="16sp"
tools:text="@string/selectAll" />

<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:background="@android:color/darker_gray" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listitem="@layout/item_sensor" />

</LinearLayout>

</androidx.core.widget.NestedScrollView>

<LinearLayout
android:id="@+id/delayContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="16dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
app:layout_constraintStart_toStartOf="parent">

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="@android:color/black"
android:text="Delay (ms)" />
android:text="Delay (ms)"
android:textColor="@android:color/black" />

<EditText
android:id="@+id/delay"
android:layout_width="0dp"
android:inputType="number"
android:digits="0123456789"
android:layout_marginEnd="16dp"
android:paddingHorizontal="16dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
android:layout_marginEnd="16dp"
android:layout_weight="2"
android:digits="0123456789"
android:inputType="number"
android:paddingHorizontal="16dp" />

</LinearLayout>

Expand Down
47 changes: 39 additions & 8 deletions android/app/src/main/res/layout/dialog_sensors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,50 @@

</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/listView"
android:layout_width="match_parent"
<androidx.core.widget.NestedScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
app:layout_constraintBottom_toTopOf="@+id/delayContainer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView"
app:layout_constraintBottom_toTopOf="@+id/delayContainer"
tools:listitem="@layout/item_sensor" />
app:layout_constraintTop_toBottomOf="@+id/textView">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<CheckBox
android:id="@+id/selectAllCheck"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="16dp"
android:textColor="@android:color/black"
android:textSize="16sp"
android:text="@string/selectAll" />

<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:background="@android:color/darker_gray" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listitem="@layout/item_sensor" />

</LinearLayout>

</androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

Expand All @@ -94,7 +126,6 @@
app:layout_constraintEnd_toEndOf="@+id/cardView4"
app:layout_constraintStart_toStartOf="@+id/cardView4">


<Button
android:id="@+id/dismiss"
android:layout_width="0dp"
Expand Down
3 changes: 2 additions & 1 deletion android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,6 @@
<string name="confirm_title">Are you sure?</string>
<string name="stream_change_body">The app needs to be restarted for this setting to take effect</string>


<string name="selectAll">Select All</string>
<string name="clearAll">Clear All</string>
</resources>

0 comments on commit d74a821

Please sign in to comment.