Skip to content

Commit

Permalink
courses: add select all button (fixes #2355) (#2356)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Sep 22, 2023
1 parent ffaffa3 commit 7a50782
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 70 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1052
versionName "0.10.52"
versionCode 1053
versionName "0.10.53"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class AdapterCourses extends RecyclerView.Adapter<RecyclerView.ViewHolder
private Markwon markwon;
private boolean isAscending = true;
private boolean isTitleAscending = true;
private boolean areAllSelected = true;

public AdapterCourses(Context context, List<RealmMyCourse> courseList, HashMap<String, JsonObject> map) {
this.map = map;
this.context = context;
Expand Down Expand Up @@ -178,6 +180,29 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int
}
}

public boolean areAllSelected(){
if (selectedItems.size() != courseList.size()) {
areAllSelected = false;
}
return areAllSelected;
}

public void selectAllItems(boolean selectAll) {
if (selectAll) {
selectedItems.clear();
selectedItems.addAll(courseList);
} else {
selectedItems.clear();
}

notifyDataSetChanged();

if (listener != null) {
listener.onSelectedListChange(selectedItems);
}
}


private void displayTagCloud(FlexboxLayout flexboxDrawable, int position) {
flexboxDrawable.removeAllViews();
final ChipCloud chipCloud = new ChipCloud(context, flexboxDrawable, config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
Expand Down Expand Up @@ -47,10 +48,12 @@ public class CourseFragment extends BaseRecyclerFragment<RealmMyCourse> implemen
ImageView imgSearch;
AdapterCourses adapterCourses;
Button btnRemove, orderByDate, orderByTitle;
CheckBox selectAll;
Spinner spnGrade, spnSubject;
List<RealmTag> searchTags;
Spinner spn;
AlertDialog confirmation;
private boolean allItemsSelected = false;

public CourseFragment() {
}
Expand Down Expand Up @@ -88,9 +91,22 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
KeyboardUtils.hideSoftKeyboard(getActivity());
});

btnRemove.setOnClickListener(V -> new AlertDialog.Builder(this.getContext()).setMessage(R.string.are_you_sure_you_want_to_delete_these_courses).setPositiveButton(R.string.yes, (dialogInterface, i) -> {
deleteSelected(true);
}).setNegativeButton(R.string.no, null).show());
btnRemove.setOnClickListener(V -> new AlertDialog.Builder(this.getContext())
.setMessage(R.string.are_you_sure_you_want_to_delete_these_courses)
.setPositiveButton(R.string.yes, (dialogInterface, i) -> {
deleteSelected(true);
if (adapterCourses.getCourseList().size() == 0) {
selectAll.setVisibility(View.GONE);
etSearch.setVisibility(View.GONE);
imgSearch.setVisibility(View.GONE);
tvAddToLib.setVisibility(View.GONE);
getView().findViewById(R.id.filter).setVisibility(View.GONE);
spn.setVisibility(View.GONE);
btnRemove.setVisibility(View.GONE);
tvSelected.setVisibility(View.GONE);
}
})
.setNegativeButton(R.string.no, null).show());
getView().findViewById(R.id.btn_collections).setOnClickListener(view -> {
CollectionsFragment f = CollectionsFragment.getInstance(searchTags, "courses");
f.setListener(this);
Expand Down Expand Up @@ -140,6 +156,16 @@ private void initializeView() {
addToMyList();
selectedItems.clear();
tvAddToLib.setEnabled(false); // selectedItems will always have a size of 0
if (adapterCourses.getCourseList().size() == 0) {
selectAll.setVisibility(View.GONE);
etSearch.setVisibility(View.GONE);
imgSearch.setVisibility(View.GONE);
tvAddToLib.setVisibility(View.GONE);
getView().findViewById(R.id.filter).setVisibility(View.GONE);
spn.setVisibility(View.GONE);
btnRemove.setVisibility(View.GONE);
tvSelected.setVisibility(View.GONE);
}
}
});
etSearch = getView().findViewById(R.id.et_search);
Expand All @@ -153,6 +179,22 @@ private void initializeView() {
tvFragmentInfo = getView().findViewById(R.id.tv_fragment_info);
spnGrade.setOnItemSelectedListener(itemSelectedListener);
spnSubject.setOnItemSelectedListener(itemSelectedListener);
selectAll = getView().findViewById(R.id.selectAll);
if (adapterCourses.getCourseList().size() == 0) {
selectAll.setVisibility(View.GONE);
etSearch.setVisibility(View.GONE);
imgSearch.setVisibility(View.GONE);
tvAddToLib.setVisibility(View.GONE);
getView().findViewById(R.id.filter).setVisibility(View.GONE);
spn.setVisibility(View.GONE);
btnRemove.setVisibility(View.GONE);
tvSelected.setVisibility(View.GONE);
}
selectAll.setOnClickListener(view -> {
boolean allSelected = selectedItems.size() == adapterCourses.getCourseList().size();
adapterCourses.selectAllItems(!allSelected);
selectAll.setText(allSelected ? getString(R.string.select_all) : getString(R.string.unselect_all));
});
}

private AdapterView.OnItemSelectedListener itemSelectedListener = new AdapterView.OnItemSelectedListener() {
Expand Down Expand Up @@ -218,6 +260,7 @@ public void onTagClicked(RealmTag tag) {

private void changeButtonStatus() {
tvAddToLib.setEnabled(selectedItems.size() > 0);
selectAll.setText(adapterCourses.areAllSelected() ? getString(R.string.unselect_all) : getString(R.string.select_all));
}

@Override
Expand Down
93 changes: 53 additions & 40 deletions app/src/main/res/layout/fragment_my_course.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
Expand All @@ -6,7 +7,6 @@
android:layout_height="match_parent"
tools:context=".ui.library.LibraryFragment">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -16,24 +16,25 @@
android:id="@+id/ll_actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_gravity="end"
android:background="@color/colorPrimary"
android:gravity="right"
android:gravity="end"
android:orientation="vertical"
android:padding="@dimen/padding_large">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:orientation="horizontal"
android:padding="@dimen/padding_normal">

<TextView
android:id="@+id/tv_fragment_info"
android:layout_width="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="@dimen/_10dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="@dimen/padding_normal"
Expand All @@ -42,21 +43,6 @@
android:textSize="@dimen/text_size_large"
android:textStyle="bold" />

<TextView
android:id="@+id/tv_add"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/padding_small"
android:drawablePadding="4dp"
android:gravity="center"
android:text="@string/join_selected"
android:textColor="@drawable/tv_color"
android:textSize="@dimen/text_size_mid"
app:drawableLeftCompat="@drawable/ic_add_library" />


<Button
android:id="@+id/btn_remove"
android:layout_width="wrap_content"
Expand All @@ -68,26 +54,21 @@
android:text="@string/leave"
android:textColor="@color/md_white_1000"
android:textSize="@dimen/text_size_mid"
android:visibility="gone"/>

</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:padding="@dimen/padding_small">
android:visibility="gone" />

<TextView
android:id="@+id/tv_selected"
android:id="@+id/tv_add"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/padding_small"
android:drawablePadding="4dp"
android:gravity="center"
android:padding="@dimen/padding_normal"
android:textColor="@color/md_white_1000"
android:textSize="@dimen/text_size_mid" />
android:text="@string/join_selected"
android:textColor="@drawable/tv_color"
android:textSize="@dimen/text_size_mid"
app:drawableLeftCompat="@drawable/ic_add_library" />

<Spinner
android:id="@+id/spn_sort"
Expand All @@ -98,19 +79,32 @@
android:padding="@dimen/padding_normal" />

<TextView
android:id="@+id/tv_delete"
android:id="@+id/tv_selected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/_10dp"
android:background="?attr/selectableItemBackground"
android:gravity="start|center_vertical"
android:visibility="gone"
android:text="@string/remove_selected"
android:textColor="@drawable/tv_color"
android:textSize="@dimen/text_size_mid"
android:visibility="gone"
app:drawableLeftCompat="@drawable/ic_delete" />

<TextView
android:id="@+id/tv_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/_10dp"
android:background="?attr/selectableItemBackground"
android:gravity="start|center_vertical"
android:text="@string/remove_selected"
android:textColor="@drawable/tv_color"
android:textSize="@dimen/text_size_mid"
android:visibility="gone"
app:drawableLeftCompat="@drawable/ic_delete" />

<ImageButton
android:id="@+id/filter"
Expand All @@ -126,10 +120,31 @@

</LinearLayout>

</LinearLayout>

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

<CheckBox
android:id="@+id/selectAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10.5dp"
android:padding="8dp"
android:layout_weight="1"
android:text="@string/select_all"
android:textSize="@dimen/text_size_mid" />

<include
layout="@layout/layout_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" /> <!-- Set layout_width to 0dp and layout_weight to 1 for equal distribution -->
</LinearLayout>

<include layout="@layout/layout_search" />


<FrameLayout
android:layout_width="match_parent"
Expand All @@ -150,7 +165,6 @@

</LinearLayout>


<androidx.cardview.widget.CardView
android:id="@+id/card_filter"
android:layout_width="match_parent"
Expand Down Expand Up @@ -280,8 +294,7 @@
android:textColor="@color/md_white_1000" />
</LinearLayout>


</LinearLayout>
</androidx.cardview.widget.CardView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Loading

0 comments on commit 7a50782

Please sign in to comment.