Skip to content

Commit

Permalink
Use the new FormListSortingBottomSheetDialog in all lists
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Nov 15, 2022
1 parent 86c1385 commit 67b7d9d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,14 @@
import android.widget.ProgressBar;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.SearchView;
import androidx.core.content.ContextCompat;
import androidx.core.view.MenuItemCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetDialog;

import org.odk.collect.android.R;
import org.odk.collect.android.database.instances.DatabaseInstanceColumns;
import org.odk.collect.android.formlists.sorting.FormListSortingAdapter;
import org.odk.collect.android.formlists.sorting.FormListSortingBottomSheetDialog;
import org.odk.collect.android.formlists.sorting.FormListSortingOption;
import org.odk.collect.android.utilities.SnackbarUtils;
import org.odk.collect.androidshared.ui.multiclicksafe.MultiClickGuard;

import java.util.ArrayList;
Expand All @@ -66,17 +59,13 @@ abstract class AppListActivity extends CollectAbstractActivity {
protected ListView listView;
protected LinearLayout llParent;
protected ProgressBar progressBar;
private BottomSheetDialog bottomSheetDialog;

private String filterText;
private String savedFilterText;
private boolean isSearchBoxShown;

private SearchView searchView;

private boolean canHideProgressBar;
private boolean progressBarVisible;

// toggles to all checked or all unchecked
// returns:
// true if result is all checked
Expand Down Expand Up @@ -221,20 +210,22 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
}

switch (item.getItemId()) {
case R.id.menu_sort:
showBottomSheetDialog();
return true;
if (item.getItemId() == R.id.menu_sort) {
new FormListSortingBottomSheetDialog(
this,
sortingOptions,
selectedSortingOrder,
selectedOption -> {
saveSelectedSortingOrder(selectedOption);
updateAdapter();
}
).show();
return true;
}

return super.onOptionsItemSelected(item);
}

private void performSelectedSearch(int position) {
saveSelectedSortingOrder(position);
updateAdapter();
}

protected void checkPreviouslyCheckedItems() {
listView.clearChoices();
List<Integer> selectedPositions = new ArrayList<>();
Expand Down Expand Up @@ -291,46 +282,15 @@ protected void clearSearchView() {
searchView.setQuery("", false);
}

private void showBottomSheetDialog() {
bottomSheetDialog = new BottomSheetDialog(this);
final View sheetView = getLayoutInflater().inflate(R.layout.bottom_sheet, null);
final RecyclerView recyclerView = sheetView.findViewById(R.id.recyclerView);

final FormListSortingAdapter adapter = new FormListSortingAdapter(sortingOptions, getSelectedSortingOrder(), position -> {
performSelectedSearch(position);
bottomSheetDialog.dismiss();
});
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());

bottomSheetDialog.setContentView(sheetView);
bottomSheetDialog.show();
}

protected void showSnackbar(@NonNull String result) {
SnackbarUtils.showShortSnackbar(llParent, result);
}

protected void hideProgressBarIfAllowed() {
if (canHideProgressBar && progressBarVisible) {
hideProgressBar();
}
}

protected void hideProgressBarAndAllow() {
this.canHideProgressBar = true;
hideProgressBar();
}

private void hideProgressBar() {
progressBar.setVisibility(View.GONE);
progressBarVisible = false;
}

protected void showProgressBar() {
progressBar.setVisibility(View.VISIBLE);
progressBarVisible = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog
import org.odk.collect.android.R
import java.util.function.Consumer

class FormListSortingBottomSheetDialog(
context: Context,
private val options: List<FormListSortingOption>,
private val selectedOption: Int,
private val onSelectedOptionChanged: (option: Int) -> Unit
private val onSelectedOptionChanged: Consumer<Int>
) : BottomSheetDialog(context) {

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -26,7 +27,7 @@ class FormListSortingBottomSheetDialog(
options,
selectedOption
) { position ->
onSelectedOptionChanged(position)
onSelectedOptionChanged.accept(position)
dismiss()
}
layoutManager = LinearLayoutManager(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,9 @@
import androidx.core.content.ContextCompat;
import androidx.core.view.MenuItemCompat;
import androidx.fragment.app.ListFragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetDialog;

import org.odk.collect.android.R;
import org.odk.collect.android.activities.CollectAbstractActivity;
import org.odk.collect.android.database.instances.DatabaseInstanceColumns;
import org.odk.collect.android.formlists.sorting.FormListSortingAdapter;
import org.odk.collect.android.formlists.sorting.FormListSortingBottomSheetDialog;
import org.odk.collect.android.formlists.sorting.FormListSortingOption;
import org.odk.collect.android.injection.DaggerUtils;
import org.odk.collect.androidshared.ui.multiclicksafe.MultiClickGuard;
Expand All @@ -51,8 +44,6 @@

import javax.inject.Inject;

import timber.log.Timber;

public abstract class AppListFragment extends ListFragment {

@Inject
Expand All @@ -63,7 +54,6 @@ public abstract class AppListFragment extends ListFragment {
protected LinkedHashSet<Long> selectedInstances = new LinkedHashSet<>();
protected View rootView;
private Integer selectedSortingOrder;
private BottomSheetDialog bottomSheetDialog;
private String filterText;

// toggles to all checked or all unchecked
Expand Down Expand Up @@ -169,50 +159,21 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
}

switch (item.getItemId()) {
case R.id.menu_sort:
bottomSheetDialog.show();
return true;
if (item.getItemId() == R.id.menu_sort) {
new FormListSortingBottomSheetDialog(
requireContext(),
sortingOptions,
selectedSortingOrder,
selectedOption -> {
saveSelectedSortingOrder(selectedOption);
updateAdapter();
}
).show();
return true;
}
return super.onOptionsItemSelected(item);
}

private void performSelectedSearch(int position) {
saveSelectedSortingOrder(position);
updateAdapter();
}

@Override
public void onResume() {
super.onResume();
if (bottomSheetDialog == null) {
setupBottomSheet();
}
}

private void setupBottomSheet() {
CollectAbstractActivity activity = (CollectAbstractActivity) getActivity();
if (activity == null) {
Timber.e(new Error("Activity is null"));
return;
}

bottomSheetDialog = new BottomSheetDialog(activity);
View sheetView = getActivity().getLayoutInflater().inflate(R.layout.bottom_sheet, null);
final RecyclerView recyclerView = sheetView.findViewById(R.id.recyclerView);

final FormListSortingAdapter adapter = new FormListSortingAdapter(sortingOptions, getSelectedSortingOrder(), position -> {
performSelectedSearch(position);
bottomSheetDialog.dismiss();
});
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());

bottomSheetDialog.setContentView(sheetView);
}

protected void checkPreviouslyCheckedItems() {
getListView().clearChoices();
List<Integer> selectedPositions = new ArrayList<>();
Expand Down

0 comments on commit 67b7d9d

Please sign in to comment.