From da8bcf17cc87ff6a354c4ec5824b5efda5f0184c Mon Sep 17 00:00:00 2001 From: riz081 Date: Tue, 10 Jun 2025 13:24:32 +0700 Subject: [PATCH] fix list --- .../example/bdkipoc/TransactionActivity.java | 182 +++++++++++++++++- .../main/res/layout/activity_transaction.xml | 101 ++++++++-- 2 files changed, 261 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/example/bdkipoc/TransactionActivity.java b/app/src/main/java/com/example/bdkipoc/TransactionActivity.java index 33e1f37..7f0c0ef 100644 --- a/app/src/main/java/com/example/bdkipoc/TransactionActivity.java +++ b/app/src/main/java/com/example/bdkipoc/TransactionActivity.java @@ -9,6 +9,7 @@ import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.ImageButton; +import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.Toast; import android.widget.TextView; @@ -42,6 +43,9 @@ import java.util.Set; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; +import java.util.Calendar; +import android.app.DatePickerDialog; +import android.widget.DatePicker; public class TransactionActivity extends AppCompatActivity implements TransactionAdapter.OnPrintClickListener { private RecyclerView recyclerView; @@ -65,6 +69,11 @@ public class TransactionActivity extends AppCompatActivity implements Transactio private Set processedReferences = new HashSet<>(); private SharedPreferences prefs; + // ✅ DATE FILTER VARIABLES + private String fromDate = ""; + private String toDate = ""; + private TextView filterButtonText; + // ✅ UPDATED PAGINATION VARIABLES private int currentPage = 1; // Start from page 1 instead of 0 private final int itemsPerPage = 15; // ✅ 15 items per page as requested @@ -131,10 +140,16 @@ public class TransactionActivity extends AppCompatActivity implements Transactio private void setupToolbar() { Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); + + // Hide default title since we're using custom layout if (getSupportActionBar() != null) { - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - getSupportActionBar().setDisplayShowHomeEnabled(true); + getSupportActionBar().setDisplayShowTitleEnabled(false); + getSupportActionBar().setDisplayHomeAsUpEnabled(false); } + + // Setup custom back button + ImageView backButton = findViewById(R.id.backButton); + backButton.setOnClickListener(v -> finish()); } private void setupRecyclerView() { @@ -171,8 +186,20 @@ public class TransactionActivity extends AppCompatActivity implements Transactio }); // Filter button click - findViewById(R.id.filterButton).setOnClickListener(v -> { - Toast.makeText(this, "Filter clicked", Toast.LENGTH_SHORT).show(); + LinearLayout filterButton = findViewById(R.id.filterButton); + filterButtonText = (TextView) filterButton.findViewById(R.id.filterButtonText); + + filterButton.setOnClickListener(v -> { + showDateRangePickerDialog(); + }); + + // Long press to clear filter + filterButton.setOnLongClickListener(v -> { + if (!fromDate.isEmpty() || !toDate.isEmpty()) { + clearDateFilter(); + Toast.makeText(this, "Date filter cleared", Toast.LENGTH_SHORT).show(); + } + return true; }); } @@ -203,7 +230,145 @@ public class TransactionActivity extends AppCompatActivity implements Transactio }); } - // ✅ NEW METHOD: Navigate to specific page + // ✅ NEW METHOD: Show date range picker dialog + private void showDateRangePickerDialog() { + Calendar calendar = Calendar.getInstance(); + + // First, pick FROM date + DatePickerDialog fromDatePicker = new DatePickerDialog(this, + (view, year, month, dayOfMonth) -> { + Calendar fromCal = Calendar.getInstance(); + fromCal.set(year, month, dayOfMonth); + + // Format for API (yyyy-MM-dd) + SimpleDateFormat apiFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); + fromDate = apiFormat.format(fromCal.getTime()); + + // Now pick TO date + showToDatePicker(fromCal); + }, + calendar.get(Calendar.YEAR), + calendar.get(Calendar.MONTH), + calendar.get(Calendar.DAY_OF_MONTH) + ); + + fromDatePicker.setTitle("Select From Date"); + fromDatePicker.show(); + } + + // ✅ NEW METHOD: Show TO date picker + private void showToDatePicker(Calendar fromCal) { + Calendar calendar = Calendar.getInstance(); + + DatePickerDialog toDatePicker = new DatePickerDialog(this, + (view, year, month, dayOfMonth) -> { + Calendar toCal = Calendar.getInstance(); + toCal.set(year, month, dayOfMonth); + + // Validate date range + if (toCal.before(fromCal)) { + Toast.makeText(this, "To date cannot be before from date", Toast.LENGTH_SHORT).show(); + return; + } + + // Format for API (yyyy-MM-dd) + SimpleDateFormat apiFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); + toDate = apiFormat.format(toCal.getTime()); + + // Format for display (compact format) + SimpleDateFormat displayFormat = new SimpleDateFormat("dd/MM", Locale.getDefault()); + String fromDisplay = displayFormat.format(fromCal.getTime()); + String toDisplay = displayFormat.format(toCal.getTime()); + + // Add year if different years + SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy", Locale.getDefault()); + String fromYear = yearFormat.format(fromCal.getTime()); + String toYear = yearFormat.format(toCal.getTime()); + + String dateRangeText; + if (fromYear.equals(toYear)) { + // Same year: "01/04 - 30/04" + dateRangeText = fromDisplay + " - " + toDisplay; + } else { + // Different years: "01/04/24 - 30/04/25" + SimpleDateFormat shortYearFormat = new SimpleDateFormat("dd/MM/yy", Locale.getDefault()); + String fromDisplayWithYear = shortYearFormat.format(fromCal.getTime()); + String toDisplayWithYear = shortYearFormat.format(toCal.getTime()); + dateRangeText = fromDisplayWithYear + " - " + toDisplayWithYear; + } + + // Update filter button text + updateFilterButtonText(dateRangeText); + + // Apply filter + applyDateFilter(); + }, + calendar.get(Calendar.YEAR), + calendar.get(Calendar.MONTH), + calendar.get(Calendar.DAY_OF_MONTH) + ); + + // Set minimum date to fromDate + toDatePicker.getDatePicker().setMinDate(fromCal.getTimeInMillis()); + toDatePicker.setTitle("Select To Date"); + toDatePicker.show(); + } + + // ✅ NEW METHOD: Update filter button text with smart formatting + private void updateFilterButtonText(String dateRangeText) { + if (filterButtonText != null) { + // Smart text formatting for better display + String displayText; + + if (dateRangeText.length() > 15) { + // For long date ranges, use compact format + String[] dates = dateRangeText.split(" - "); + if (dates.length == 2) { + // Convert "01/04/2025 - 30/04/2025" to "01/04\n30/04" + String fromShort = dates[0].substring(0, 5); // Get "01/04" + String toShort = dates[1].substring(0, 5); // Get "30/04" + displayText = fromShort + "\n" + toShort; + } else { + displayText = dateRangeText; + } + } else { + displayText = dateRangeText; + } + + filterButtonText.setText(displayText); + filterButtonText.setTextColor(getResources().getColor(android.R.color.holo_blue_dark)); + filterButtonText.setTextSize(12); // Smaller text when filter is active + + Log.d("TransactionActivity", "🎨 Filter button updated: " + displayText); + } + } + + // ✅ NEW METHOD: Apply date filter + private void applyDateFilter() { + Log.d("TransactionActivity", "🗓️ Applying date filter: " + fromDate + " to " + toDate); + + // Reset to first page and reload data + currentPage = 1; + refreshTransactions(); + } + + // ✅ NEW METHOD: Clear date filter + private void clearDateFilter() { + fromDate = ""; + toDate = ""; + + if (filterButtonText != null) { + filterButtonText.setText("Filter"); + filterButtonText.setTextColor(getResources().getColor(android.R.color.darker_gray)); + filterButtonText.setTextSize(14); // Reset to normal size + } + + Log.d("TransactionActivity", "🗓️ Date filter cleared"); + + // Reload data without date filter + currentPage = 1; + refreshTransactions(); + } private void goToPage(int page) { if (page < 1 || page > totalPages || page == currentPage || isLoading) { return; @@ -439,14 +604,15 @@ public class TransactionActivity extends AppCompatActivity implements Transactio protected List doInBackground(Void... voids) { List result = new ArrayList<>(); try { - // ✅ PAGINATION API CALL: Use page-based API call + // ✅ PAGINATION API CALL: Use page-based API call with date filter int apiPage = pageToLoad - 1; // API uses 0-based indexing String urlString = "https://be-edc.msvc.app/transactions?page=" + apiPage + - "&limit=" + itemsPerPage + "&sortOrder=DESC&from_date=&to_date=&location_id=0&merchant_id=0&tid=73001500&mid=71000026521&sortColumn=created_at"; + "&limit=" + itemsPerPage + "&sortOrder=DESC&from_date=" + fromDate + "&to_date=" + toDate + "&location_id=0&merchant_id=0&tid=73001500&mid=71000026521&sortColumn=created_at"; Log.d("TransactionActivity", "🔍 Fetching transactions page " + pageToLoad + - " (API page " + apiPage + ") with limit " + itemsPerPage + " - SORT: DESC by created_at"); + " (API page " + apiPage + ") with limit " + itemsPerPage + " - SORT: DESC by created_at" + + " - Date Filter: " + fromDate + " to " + toDate); URI uri = new URI(urlString); URL url = uri.toURL(); diff --git a/app/src/main/res/layout/activity_transaction.xml b/app/src/main/res/layout/activity_transaction.xml index 09752ad..b1c3e30 100644 --- a/app/src/main/res/layout/activity_transaction.xml +++ b/app/src/main/res/layout/activity_transaction.xml @@ -6,16 +6,84 @@ + android:layout_height="wrap_content" + android:elevation="0dp" + app:elevation="0dp"> + + + + + app:contentInsetStart="0dp" + app:contentInsetLeft="0dp"> + + + + + + + + + + + + + + + + + + + + @@ -31,7 +99,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" - android:padding="20dp" + android:padding="16dp" android:background="@android:color/white" android:gravity="center_vertical"> @@ -45,7 +113,7 @@ android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp" - android:layout_marginEnd="16dp"> + android:layout_marginEnd="12dp"> + android:focusable="true" + android:minWidth="120dp"> + android:textSize="14sp" + android:textColor="#666666" + android:maxLines="2" + android:ellipsize="end" + android:gravity="center" />