Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

field_search #1125

Merged
merged 3 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.OpenableColumns;
import android.text.Html;
import android.text.Spanned;
Expand All @@ -38,6 +39,7 @@
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.view.ActionMode;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.documentfile.provider.DocumentFile;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DividerItemDecoration;
Expand Down Expand Up @@ -70,6 +72,7 @@
import com.fieldbook.tracker.utilities.SnackbarUtils;
import com.fieldbook.tracker.utilities.TapTargetUtil;
import com.fieldbook.tracker.utilities.Utils;
import com.fieldbook.tracker.views.SearchBar;
import com.getkeepsafe.taptargetview.TapTarget;
import com.getkeepsafe.taptargetview.TapTargetSequence;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
Expand Down Expand Up @@ -102,6 +105,7 @@ public class FieldEditorActivity extends ThemedActivity
private static final int REQUEST_FILE_EXPLORER_CODE = 1;
private static final int REQUEST_CLOUD_FILE_CODE = 5;
private static final int REQUEST_BRAPI_IMPORT_ACTIVITY = 10;

private ArrayList<FieldObject> fieldList;
public FieldAdapter mAdapter;
public EditText trait;
Expand All @@ -116,6 +120,7 @@ public class FieldEditorActivity extends ThemedActivity
private ActionMode actionMode;
private TextView customTitleView;
public ExportUtil exportUtil;
private SearchBar searchBar;

@Inject
DataHelper database;
Expand Down Expand Up @@ -182,6 +187,8 @@ public void onFieldSelected(int fieldId) {
FloatingActionButton fab = findViewById(R.id.newField);
fab.setOnClickListener(v -> handleImportAction());

searchBar = findViewById(R.id.act_fields_sb);

queryAndLoadFields();

}
Expand Down Expand Up @@ -1062,6 +1069,9 @@ public void queryAndLoadFields() {
fieldList = database.getAllFieldObjects(); // Fetch data from the database
mAdapter.submitList(new ArrayList<>(fieldList), () -> recyclerView.scrollToPosition(0));
mAdapter.notifyDataSetChanged();

new Handler(Looper.getMainLooper()).postDelayed(this::setupSearchBar, 100);

} catch (Exception e) {
Log.e(TAG, "Error updating fields list", e);
}
Expand All @@ -1085,4 +1095,34 @@ public FieldSwitcher getFieldSwitcher() {
return fieldSwitcher;
}

private void setupSearchBar() {

if (recyclerView.canScrollVertically(1) || recyclerView.canScrollVertically(-1)) {

searchBar.setVisibility(View.VISIBLE);

searchBar.editText.addTextChangedListener(new android.text.TextWatcher() {

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do nothing
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mAdapter.setTextFilter(s.toString());
}

@Override
public void afterTextChanged(android.text.Editable s) {
// Do nothing
}
});

} else {

searchBar.setVisibility(View.GONE);

}
}
}
23 changes: 23 additions & 0 deletions app/src/main/java/com/fieldbook/tracker/adapters/FieldAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
Expand Down Expand Up @@ -39,6 +40,8 @@ public class FieldAdapter extends ListAdapter<FieldObject, FieldAdapter.ViewHold
private final FieldSwitcher fieldSwitcher;
private AdapterCallback callback;
private OnFieldSelectedListener listener;
private String filterText = "";
private final List<FieldObject> fullFieldList = new ArrayList<>();

public interface OnFieldSelectedListener {
void onFieldSelected(int itemId);
Expand Down Expand Up @@ -219,4 +222,24 @@ public void onBindViewHolder(ViewHolder holder, int position) {
holder.sourceIcon.setBackground(null);
}
}

@Override
public void submitList(@Nullable List<FieldObject> list, @Nullable Runnable commitCallback) {
super.submitList(list, commitCallback);
fullFieldList.clear();
if (list != null) {
fullFieldList.addAll(list);
}
}

public void setTextFilter(String filter) {
this.filterText = filter;
List<FieldObject> filterFields = new ArrayList<>(fullFieldList);
for (FieldObject field : fullFieldList) {
if (!filter.isEmpty() && !field.getExp_name().toLowerCase().contains(filter.toLowerCase())) {
filterFields.remove(field);
}
}
submitList(filterFields);
}
}
20 changes: 12 additions & 8 deletions app/src/main/java/com/fieldbook/tracker/views/SearchBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ package com.fieldbook.tracker.views
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AutoCompleteTextView
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.TextView
import androidx.core.widget.addTextChangedListener
import android.widget.ImageView
import com.fieldbook.tracker.R

class SearchBar: FrameLayout {
class SearchBar : FrameLayout {

lateinit var editText: AutoCompleteTextView
lateinit var clearButton: ImageView

constructor(context: Context) : super(context) {
init()
Expand All @@ -25,7 +21,11 @@ class SearchBar: FrameLayout {
init()
}

constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
init()
}

Expand All @@ -35,6 +35,10 @@ class SearchBar: FrameLayout {
val view = LayoutInflater.from(context).inflate(R.layout.view_search_bar, this, true)

editText = view.findViewById(R.id.search)
clearButton = view.findViewById(R.id.clear)

clearButton.setOnClickListener {
editText.text.clear()
}
}
}
14 changes: 13 additions & 1 deletion app/src/main/res/layout/activity_fields.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="?attr/fb_color_background"
android:id="@+id/field_editor_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

Expand All @@ -21,6 +23,14 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.fieldbook.tracker.views.SearchBar
android:visibility="gone"
android:id="@+id/act_fields_sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/act_list_filter_tb" />

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
Expand All @@ -30,7 +40,9 @@
android:id="@+id/fieldRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" />
android:padding="5dp"
tools:listitem="@layout/list_item_field_recycler"
tools:itemCount="3"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/res/layout/list_item_field_recycler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
android:layout_width="44dp"
android:layout_height="44dp"
android:gravity="center"
android:padding="8dp"/>
android:padding="8dp"
tools:src="@drawable/ic_file_csv"/>

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
Expand Down Expand Up @@ -43,7 +44,8 @@
android:ellipsize="end"
android:gravity="start"
android:minWidth="40dp"
android:singleLine="true"/>
android:singleLine="true"
tools:text="200"/>

</LinearLayout>

Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/view_search_bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
android:background="?selectableItemBackgroundBorderless"
android:contentDescription="@string/searchpreference_clear"
android:padding="4dp"
android:visibility="gone"
app:srcCompat="@drawable/searchpreference_ic_clear"
app:tint="?android:attr/textColorPrimary" />

Expand Down