Added calendar view to adoptions in andriod
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
package com.example.petstoremobile.fragments.listfragments;
|
package com.example.petstoremobile.fragments.listfragments;
|
||||||
|
|
||||||
|
import android.graphics.Color;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.text.*;
|
import android.text.*;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.*;
|
import android.view.*;
|
||||||
import android.widget.*;
|
import android.widget.*;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
@@ -17,7 +19,14 @@ import com.example.petstoremobile.dtos.AdoptionDTO;
|
|||||||
import com.example.petstoremobile.dtos.PageResponse;
|
import com.example.petstoremobile.dtos.PageResponse;
|
||||||
import com.example.petstoremobile.fragments.ListFragment;
|
import com.example.petstoremobile.fragments.ListFragment;
|
||||||
import com.example.petstoremobile.fragments.listfragments.detailfragments.AdoptionDetailFragment;
|
import com.example.petstoremobile.fragments.listfragments.detailfragments.AdoptionDetailFragment;
|
||||||
|
import com.example.petstoremobile.utils.EventDecorator;
|
||||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||||
|
import com.prolificinteractive.materialcalendarview.CalendarDay;
|
||||||
|
import com.prolificinteractive.materialcalendarview.CalendarMode;
|
||||||
|
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
|
||||||
|
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import retrofit2.*;
|
import retrofit2.*;
|
||||||
|
|
||||||
@@ -30,6 +39,11 @@ public class AdoptionFragment extends Fragment implements AdoptionAdapter.OnAdop
|
|||||||
private SwipeRefreshLayout swipeRefresh;
|
private SwipeRefreshLayout swipeRefresh;
|
||||||
private EditText etSearch;
|
private EditText etSearch;
|
||||||
private ImageButton hamburger;
|
private ImageButton hamburger;
|
||||||
|
private ImageButton btnToggleCalendarMode;
|
||||||
|
private MaterialCalendarView calendarView;
|
||||||
|
private CalendarDay selectedCalendarDay;
|
||||||
|
private boolean isMonthMode = false;
|
||||||
|
private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
@@ -38,10 +52,13 @@ public class AdoptionFragment extends Fragment implements AdoptionAdapter.OnAdop
|
|||||||
|
|
||||||
api = RetrofitClient.getAdoptionApi(requireContext());
|
api = RetrofitClient.getAdoptionApi(requireContext());
|
||||||
hamburger = view.findViewById(R.id.btnHamburgerAdoption);
|
hamburger = view.findViewById(R.id.btnHamburgerAdoption);
|
||||||
|
calendarView = view.findViewById(R.id.calendarViewAdoption);
|
||||||
|
btnToggleCalendarMode = view.findViewById(R.id.btnToggleCalendarModeAdoption);
|
||||||
|
|
||||||
setupRecyclerView(view);
|
setupRecyclerView(view);
|
||||||
setupSearch(view);
|
setupSearch(view);
|
||||||
setupSwipeRefresh(view);
|
setupSwipeRefresh(view);
|
||||||
|
setupCalendar();
|
||||||
loadAdoptions();
|
loadAdoptions();
|
||||||
|
|
||||||
FloatingActionButton fab = view.findViewById(R.id.fabAddAdoption);
|
FloatingActionButton fab = view.findViewById(R.id.fabAddAdoption);
|
||||||
@@ -52,9 +69,57 @@ public class AdoptionFragment extends Fragment implements AdoptionAdapter.OnAdop
|
|||||||
if (lf != null) lf.openDrawer();
|
if (lf != null) lf.openDrawer();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
btnToggleCalendarMode.setOnClickListener(v -> toggleCalendarMode());
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void toggleCalendarMode() {
|
||||||
|
isMonthMode = !isMonthMode;
|
||||||
|
calendarView.state().edit()
|
||||||
|
.setCalendarDisplayMode(isMonthMode ? CalendarMode.MONTHS : CalendarMode.WEEKS)
|
||||||
|
.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupCalendar() {
|
||||||
|
calendarView.setOnDateChangedListener(new OnDateSelectedListener() {
|
||||||
|
@Override
|
||||||
|
public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
|
||||||
|
if (selected) {
|
||||||
|
if (date.equals(selectedCalendarDay)) {
|
||||||
|
selectedCalendarDay = null;
|
||||||
|
calendarView.clearSelection();
|
||||||
|
} else {
|
||||||
|
selectedCalendarDay = date;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
selectedCalendarDay = null;
|
||||||
|
}
|
||||||
|
filter(etSearch.getText().toString());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateCalendarDecorators() {
|
||||||
|
HashSet<CalendarDay> datesWithAdoptions = new HashSet<>();
|
||||||
|
for (AdoptionDTO adoption : adoptionList) {
|
||||||
|
try {
|
||||||
|
if (adoption.getAdoptionDate() != null) {
|
||||||
|
Date date = dateFormat.parse(adoption.getAdoptionDate());
|
||||||
|
if (date != null) {
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.setTime(date);
|
||||||
|
datesWithAdoptions.add(CalendarDay.from(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (ParseException e) {
|
||||||
|
Log.e("AdoptionFragment", "Error parsing date: " + adoption.getAdoptionDate());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
calendarView.removeDecorators();
|
||||||
|
calendarView.addDecorator(new EventDecorator(Color.RED, datesWithAdoptions));
|
||||||
|
}
|
||||||
|
|
||||||
private void setupRecyclerView(View view) {
|
private void setupRecyclerView(View view) {
|
||||||
RecyclerView rv = view.findViewById(R.id.recyclerViewAdoptions);
|
RecyclerView rv = view.findViewById(R.id.recyclerViewAdoptions);
|
||||||
adapter = new AdoptionAdapter(filteredList, this);
|
adapter = new AdoptionAdapter(filteredList, this);
|
||||||
@@ -80,16 +145,25 @@ public class AdoptionFragment extends Fragment implements AdoptionAdapter.OnAdop
|
|||||||
|
|
||||||
private void filter(String query) {
|
private void filter(String query) {
|
||||||
filteredList.clear();
|
filteredList.clear();
|
||||||
if (query.isEmpty()) {
|
String lowerQuery = query.toLowerCase();
|
||||||
filteredList.addAll(adoptionList);
|
|
||||||
} else {
|
String selectedDateString = null;
|
||||||
String lower = query.toLowerCase();
|
if (selectedCalendarDay != null) {
|
||||||
for (AdoptionDTO a : adoptionList) {
|
selectedDateString = String.format(Locale.getDefault(), "%04d-%02d-%02d",
|
||||||
if ((a.getCustomerName() != null && a.getCustomerName().toLowerCase().contains(lower))
|
selectedCalendarDay.getYear(), selectedCalendarDay.getMonth(), selectedCalendarDay.getDay());
|
||||||
|| (a.getPetName() != null && a.getPetName().toLowerCase().contains(lower))
|
}
|
||||||
|| (a.getAdoptionStatus() != null && a.getAdoptionStatus().toLowerCase().contains(lower))) {
|
|
||||||
filteredList.add(a);
|
for (AdoptionDTO a : adoptionList) {
|
||||||
}
|
boolean matchesSearch = query.isEmpty() ||
|
||||||
|
(a.getCustomerName() != null && a.getCustomerName().toLowerCase().contains(lowerQuery)) ||
|
||||||
|
(a.getPetName() != null && a.getPetName().toLowerCase().contains(lowerQuery)) ||
|
||||||
|
(a.getAdoptionStatus() != null && a.getAdoptionStatus().toLowerCase().contains(lowerQuery));
|
||||||
|
|
||||||
|
boolean matchesDate = (selectedDateString == null) ||
|
||||||
|
(a.getAdoptionDate() != null && a.getAdoptionDate().startsWith(selectedDateString));
|
||||||
|
|
||||||
|
if (matchesSearch && matchesDate) {
|
||||||
|
filteredList.add(a);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
adapter.notifyDataSetChanged();
|
adapter.notifyDataSetChanged();
|
||||||
@@ -97,13 +171,14 @@ public class AdoptionFragment extends Fragment implements AdoptionAdapter.OnAdop
|
|||||||
|
|
||||||
private void loadAdoptions() {
|
private void loadAdoptions() {
|
||||||
if (swipeRefresh != null) swipeRefresh.setRefreshing(true);
|
if (swipeRefresh != null) swipeRefresh.setRefreshing(true);
|
||||||
api.getAllAdoptions(0, 100).enqueue(new Callback<PageResponse<AdoptionDTO>>() {
|
api.getAllAdoptions(0, 500).enqueue(new Callback<PageResponse<AdoptionDTO>>() {
|
||||||
public void onResponse(Call<PageResponse<AdoptionDTO>> c,
|
public void onResponse(Call<PageResponse<AdoptionDTO>> c,
|
||||||
Response<PageResponse<AdoptionDTO>> r) {
|
Response<PageResponse<AdoptionDTO>> r) {
|
||||||
if (swipeRefresh != null) swipeRefresh.setRefreshing(false);
|
if (swipeRefresh != null) swipeRefresh.setRefreshing(false);
|
||||||
if (r.isSuccessful() && r.body() != null) {
|
if (r.isSuccessful() && r.body() != null) {
|
||||||
adoptionList.clear();
|
adoptionList.clear();
|
||||||
adoptionList.addAll(r.body().getContent());
|
adoptionList.addAll(r.body().getContent());
|
||||||
|
updateCalendarDecorators();
|
||||||
filter(etSearch != null ? etSearch.getText().toString() : "");
|
filter(etSearch != null ? etSearch.getText().toString() : "");
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(getContext(), "Failed to load adoptions", Toast.LENGTH_SHORT).show();
|
Toast.makeText(getContext(), "Failed to load adoptions", Toast.LENGTH_SHORT).show();
|
||||||
|
|||||||
@@ -28,15 +28,35 @@
|
|||||||
android:contentDescription="Open menu"/>
|
android:contentDescription="Open menu"/>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="match_parent"
|
android:layout_width="0dp"
|
||||||
|
android:layout_weight="1"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="Adoptions"
|
android:text="Adoptions"
|
||||||
android:textColor="@color/white"
|
android:textColor="@color/white"
|
||||||
android:textSize="20sp"
|
android:textSize="20sp"
|
||||||
android:textStyle="bold"/>
|
android:textStyle="bold"/>
|
||||||
|
|
||||||
|
<ImageButton
|
||||||
|
android:id="@+id/btnToggleCalendarModeAdoption"
|
||||||
|
android:layout_width="48dp"
|
||||||
|
android:layout_height="48dp"
|
||||||
|
android:src="@android:drawable/ic_menu_today"
|
||||||
|
android:background="?attr/selectableItemBackgroundBorderless"
|
||||||
|
app:tint="@color/white"
|
||||||
|
android:contentDescription="Toggle Calendar Mode"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
<com.prolificinteractive.materialcalendarview.MaterialCalendarView
|
||||||
|
android:id="@+id/calendarViewAdoption"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@android:color/white"
|
||||||
|
app:mcv_showOtherDates="all"
|
||||||
|
app:mcv_selectionColor="@color/accent_blue"
|
||||||
|
app:mcv_calendarMode="week"
|
||||||
|
app:mcv_tileHeight="40dp" />
|
||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
android:id="@+id/etSearchAdoption"
|
android:id="@+id/etSearchAdoption"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
|||||||
Reference in New Issue
Block a user