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

add back donation filters #59

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
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,7 +17,6 @@

import io.saytheirnames.R;
import io.saytheirnames.activity.DonationDetailsActivity;
import io.saytheirnames.activity.PetitionDetailsActivity;
import io.saytheirnames.models.Donation;
import io.saytheirnames.network.DonationsPager;
import kotlinx.coroutines.Dispatchers;
Expand All @@ -42,7 +41,6 @@ public DonationAdapter.DonationItemHolder onCreateViewHolder(@NonNull ViewGroup
@Override
public void onBindViewHolder(@NonNull DonationAdapter.DonationItemHolder holder, final int position) {

holder.setIsRecyclable(false);
Donation donation = getItem(position);
if (donation != null) {
Glide.with(holder.itemView.getContext())
Expand Down
59 changes: 15 additions & 44 deletions app/src/main/java/io/saytheirnames/fragments/DonationFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;
Expand All @@ -29,7 +27,6 @@
import io.saytheirnames.models.Donation;
import io.saytheirnames.models.DonationType;
import io.saytheirnames.models.DonationTypesData;
import io.saytheirnames.models.DonationsData;
import io.saytheirnames.network.BackendInterface;
import io.saytheirnames.network.DonationsPager;
import io.saytheirnames.network.Utils;
Expand All @@ -38,7 +35,7 @@
import retrofit2.Callback;
import retrofit2.Response;

public class DonationFragment extends Fragment {
public class DonationFragment extends Fragment implements DonationFilterAdapter.DonationFilterListener {
private View view;
//private TextView mTextView;

Expand All @@ -56,6 +53,7 @@ public class DonationFragment extends Fragment {
private ProgressBar progressBar;
private ImageView imageView;
private NestedScrollView nestedScrollView;
private DonationsPager donationsPager;

BackendInterface backendInterface;

Expand Down Expand Up @@ -95,27 +93,30 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);

/*donationRecyclerView.setLayoutManager(layoutManager);
donationRecyclerView.setLayoutManager(layoutManager);


donationRecyclerView.setAdapter(donationAdapter);*/
donationRecyclerView.setAdapter(donationAdapter);

progressBar = view.findViewById(R.id.progressBar);
donationsPager = new DonationsPager(donationAdapter);

initializeRecyclerView();
loadData();
setupDonationFilters();
donationsPager.setFilterType("All");
donationsPager.loadDonationsFromPagination();

return view;
}

// donation filter will not be shown in MVP so this method is unused for now
public void setupDonationFilters() {
/*recyclerView = view.findViewById(R.id.recyclerView);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager1);
donationFilterAdapter = new DonationFilterAdapter(donationTypeList, this, nestedScrollView);
recyclerView.setAdapter(donationFilterAdapter);
getDonationFilterItems();*/
getDonationFilterItems();
}

private void visitPage(String url) {
Expand Down Expand Up @@ -154,11 +155,6 @@ public void onBindViewHolder(@NotNull RecyclerView.ViewHolder viewHolder, @NotNu
donationRecyclerView.setAdapter(donationAdapter);
}

private void loadData() {
DonationsPager petitionsPager = new DonationsPager(donationAdapter);
petitionsPager.loadDonationsFromPagination();
}

public void getDonationFilterItems(){
showProgress(true);
backendInterface.getDonationsTypes().enqueue(new Callback<DonationTypesData>() {
Expand Down Expand Up @@ -188,36 +184,6 @@ public void onFailure(Call<DonationTypesData> call, Throwable throwable) {
});
}

public void filterDonation(String type){
if(type.equals("All")){
loadData();
}else{
doDonationFilter(type);
}
}

private void doDonationFilter(String type) {
showProgress(true);
backendInterface.getFilteredDonations(type.toLowerCase()).enqueue(new Callback<DonationsData>() {
@Override
public void onResponse(@NonNull Call<DonationsData> call, @NonNull Response<DonationsData> response) {
donationArrayList.clear();
Log.d("API_Response", response.body().toString());
List<Donation> body = response.body().getData();
donationArrayList.addAll(body);
donationRecyclerView.setVisibility(View.VISIBLE);

donationAdapter.notifyDataSetChanged();
showProgress(false);
}

@Override
public void onFailure(Call<DonationsData> call, Throwable t) {
showProgress(false);
}
});
}

private void showProgress(Boolean show) {
progressBar.setVisibility(show ? View.VISIBLE : View.GONE);
}
Expand Down Expand Up @@ -256,4 +222,9 @@ public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}

@Override
public void onDonationFilterSelected(DonationType donationType) {
donationAdapter.refresh();
donationsPager.setFilterType(donationType.getType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@ public interface BackendInterface {
@GET("/api/donations")
Call<DonationsData> getFilteredDonations(@Query("type") String type);

@GET("/api/donations")
Call<DonationsData> getDonationsWithFilter(@Query("page") Integer page,
@Query("type") String type);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,38 @@

import io.saytheirnames.models.Donation;
import io.saytheirnames.models.DonationsData;
import io.saytheirnames.models.Petition;
import kotlin.coroutines.Continuation;

public class DonationsDataSource extends PagingSource<Integer, Donation> {

private BackendInterface backendInterface = Utils.getBackendService();
private String type;

public DonationsDataSource(String type) {
this.type = type;
}

@Nullable
@Override
public Object load(@NotNull LoadParams<Integer> loadParams, @NotNull Continuation<? super LoadResult<Integer, Donation>> continuation) {

DonationsData petitionData;
DonationsData donationsData;

try {
petitionData = backendInterface.getDonations(loadParams.getKey()).execute().body();
if (type.equals("All")) {
donationsData = backendInterface.getDonations(loadParams.getKey()).execute().body();
} else {
donationsData = backendInterface.getDonationsWithFilter(loadParams.getKey(), type).execute().body();
}
} catch (IOException exception) {
exception.printStackTrace();
//TODO: We could use a better error state.
return new LoadResult.Page<Integer, Petition>(Collections.emptyList(), null, null, 0, 0);
return new LoadResult.Page<Integer, Donation>(Collections.emptyList(), null, null, 0, 0);
}

int next = petitionData.getPagination().getCurrentPage() + 1;
int next = donationsData.getPagination().getCurrentPage() + 1;

return new LoadResult.Page<Integer, Donation>(petitionData.getData(), null, next > petitionData.getPagination().getLastPage() ? null : next, 0, 0);
return new LoadResult.Page<>(donationsData.getData(), null, next > donationsData.getPagination().getLastPage() ? null : next, 0, 0);
}

}
30 changes: 18 additions & 12 deletions app/src/main/java/io/saytheirnames/network/DonationsPager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@ import androidx.paging.PagingConfig
import androidx.recyclerview.widget.DiffUtil
import io.saytheirnames.adapters.DonationAdapter
import io.saytheirnames.models.Donation
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class DonationsPager(val adapter: DonationAdapter): Any() {
class DonationsPager(val adapter: DonationAdapter) {

private var type = ""
private val pagingData = Pager<Int, Donation>(
config = PagingConfig(
pageSize = 12,
prefetchDistance = 12
),
initialKey = 1,
pagingSourceFactory = { DonationsDataSource(type) }
).flow

fun setFilterType(type: String) {
this.type = type
}

@ExperimentalCoroutinesApi
fun loadDonationsFromPagination() {
GlobalScope.launch {
getDonationsPager().collectLatest {
pagingData.collectLatest {
try {
adapter.submitData(it)
} catch(e: Exception) {
Expand All @@ -23,16 +39,6 @@ class DonationsPager(val adapter: DonationAdapter): Any() {
}
}

private fun getDonationsPager() = Pager<Int, Donation>(
config = PagingConfig(
pageSize = 12,
prefetchDistance = 12
),
initialKey = 1
) {
DonationsDataSource()
}.flow

companion object {
fun getDiffItemCallback() = object: DiffUtil.ItemCallback<Donation>() {
override fun areItemsTheSame(oldItem: Donation, newItem: Donation): Boolean {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/fragment_donation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>

<!--<LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
Expand All @@ -57,7 +57,7 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

</LinearLayout>-->
</LinearLayout>

</com.google.android.material.appbar.AppBarLayout>

Expand Down