]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListAdapter.java
index febca0191ecb02525c8bd47b7205f26dcb8b76f5..bc463ed564661bb6d3da11155158322bbb2b5b29 100644 (file)
 /*
- * Copyright © 2019 Damyan Ivanov.
- * This file is part of Mobile-Ledger.
- * Mobile-Ledger is free software: you can distribute it and/or modify it
+ * Copyright © 2021 Damyan Ivanov.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
  * under the term of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your opinion), any later version.
  *
- * Mobile-Ledger is distributed in the hope that it will be useful,
+ * MoLe is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  * GNU General Public License terms for details.
  *
  * You should have received a copy of the GNU General Public License
- * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
  */
 
 package net.ktnx.mobileledger.ui.transaction_list;
 
-import android.content.Context;
-import android.database.sqlite.SQLiteDatabase;
-import android.graphics.Typeface;
-import android.os.AsyncTask;
-import android.support.annotation.NonNull;
-import android.support.v7.widget.AppCompatTextView;
-import android.support.v7.widget.RecyclerView;
-import android.view.Gravity;
 import android.view.LayoutInflater;
-import android.view.View;
 import android.view.ViewGroup;
-import android.widget.LinearLayout;
-import android.widget.TextView;
 
-import net.ktnx.mobileledger.R;
-import net.ktnx.mobileledger.model.LedgerTransaction;
-import net.ktnx.mobileledger.model.LedgerTransactionAccount;
+import androidx.annotation.NonNull;
+import androidx.recyclerview.widget.AsyncListDiffer;
+import androidx.recyclerview.widget.DiffUtil;
+import androidx.recyclerview.widget.RecyclerView;
+
+import net.ktnx.mobileledger.databinding.LastUpdateLayoutBinding;
+import net.ktnx.mobileledger.databinding.TransactionDelimiterBinding;
+import net.ktnx.mobileledger.databinding.TransactionListRowBinding;
 import net.ktnx.mobileledger.model.TransactionListItem;
-import net.ktnx.mobileledger.utils.Globals;
-import net.ktnx.mobileledger.utils.MLDB;
-
-import java.text.DateFormat;
-import java.util.Date;
-
-import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
-
-public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
-    private String boldAccountName;
-    public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
-        TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
-
-        if (item.getType() == TransactionListItem.Type.TRANSACTION) {
-            holder.vTransaction.setVisibility(View.VISIBLE);
-            holder.vDelimiter.setVisibility(View.GONE);
-            LedgerTransaction tr = item.getTransaction();
-            // in a race when transaction value is reduced, but the model hasn't been notified yet
-            // the view will disappear when the notifications reaches the model, so by simply omitting
-            // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
-            // a bit longer
-            if (tr == null) return;
-
-            LedgerTransaction previous = null;
-            TransactionListItem previousItem = null;
-            if (position > 0)
-                previousItem = TransactionListViewModel.getTransactionListItem(position - 1);
-
-//        Log.d("transactions", String.format("Filling position %d with %d accounts", position,
-//                tr.getAccounts().size()));
-
-            TransactionLoader loader = new TransactionLoader();
-            loader.execute(
-                    new TransactionLoaderParams(tr, previous, holder, position, boldAccountName));
-
-            // WORKAROUND what seems to be a bug in CardHolder somewhere
-            // when a view that was previously holding a delimiter is re-purposed
-            // occasionally it stays too short (not high enough)
-            holder.vTransaction.measure(View.MeasureSpec
-                            .makeMeasureSpec(holder.itemView.getWidth(), View.MeasureSpec.EXACTLY),
-                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
+import net.ktnx.mobileledger.utils.Logger;
+import net.ktnx.mobileledger.utils.Misc;
+
+import java.util.List;
+import java.util.Locale;
+
+public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolderBase> {
+    private final AsyncListDiffer<TransactionListItem> listDiffer;
+    public TransactionListAdapter() {
+        super();
+
+        setHasStableIds(true);
+
+        listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<TransactionListItem>() {
+            @Override
+            public boolean areItemsTheSame(@NonNull TransactionListItem oldItem,
+                                           @NonNull TransactionListItem newItem) {
+                if (oldItem.getType() != newItem.getType())
+                    return false;
+                switch (oldItem.getType()) {
+                    case DELIMITER:
+                        return (oldItem.getDate()
+                                       .equals(newItem.getDate()));
+                    case TRANSACTION:
+                        return oldItem.getTransaction()
+                                      .getLedgerId() == newItem.getTransaction()
+                                                               .getLedgerId();
+                    case HEADER:
+                        return true;    // there can be only one header
+                    default:
+                        throw new IllegalStateException(
+                                String.format(Locale.US, "Unexpected transaction item type %s",
+                                        oldItem.getType()));
+                }
+            }
+            @Override
+            public boolean areContentsTheSame(@NonNull TransactionListItem oldItem,
+                                              @NonNull TransactionListItem newItem) {
+                switch (oldItem.getType()) {
+                    case DELIMITER:
+                        return oldItem.isMonthShown() == newItem.isMonthShown();
+                    case TRANSACTION:
+                        return oldItem.getTransaction()
+                                      .equals(newItem.getTransaction()) &&
+                               Misc.equalStrings(oldItem.getBoldAccountName(),
+                                       newItem.getBoldAccountName()) &&
+                               Misc.equalStrings(oldItem.getRunningTotal(),
+                                       newItem.getRunningTotal());
+                    case HEADER:
+                        // headers don't differ in their contents. they observe the last update
+                        // date and react to its changes
+                        return true;
+                    default:
+                        throw new IllegalStateException(
+                                String.format(Locale.US, "Unexpected transaction item type %s",
+                                        oldItem.getType()));
+
+                }
+            }
+        });
+    }
+    @Override
+    public long getItemId(int position) {
+        TransactionListItem item = listDiffer.getCurrentList()
+                                             .get(position);
+        switch (item.getType()) {
+            case HEADER:
+                return -1;
+            case TRANSACTION:
+                return item.getTransaction()
+                           .getLedgerId();
+            case DELIMITER:
+                return -item.getDate()
+                            .toDate()
+                            .getTime();
+            default:
+                throw new IllegalStateException("Unexpected value: " + item.getType());
         }
-        else {
-            Date date = item.getDate();
-            holder.vTransaction.setVisibility(View.GONE);
-            holder.vDelimiter.setVisibility(View.VISIBLE);
-            holder.tvDelimiterDate.setText(DateFormat.getDateInstance().format(date));
-            holder.tvDelimiterMonth
-                    .setText(item.isMonthShown() ? Globals.monthNames[date.getMonth()] : "");
+    }
+    @Override
+    public int getItemViewType(int position) {
+        return listDiffer.getCurrentList()
+                         .get(position)
+                         .getType()
+                         .ordinal();
+    }
+    public void onBindViewHolder(@NonNull TransactionRowHolderBase holder, int position) {
+        TransactionListItem item = listDiffer.getCurrentList()
+                                             .get(position);
+
+        // in a race when transaction value is reduced, but the model hasn't been notified yet
+        // the view will disappear when the notifications reaches the model, so by simply omitting
+        // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
+        // a bit longer
+        if (item == null)
+            return;
+
+        final TransactionListItem.Type newType = item.getType();
+
+        switch (newType) {
+            case TRANSACTION:
+                holder.asTransaction()
+                      .bind(item, item.getBoldAccountName());
+
+                break;
+            case DELIMITER:
+                holder.asDelimiter()
+                      .bind(item);
+                break;
+            case HEADER:
+                holder.asHeader()
+                      .bind();
+
+                break;
+            default:
+                throw new IllegalStateException("Unexpected value: " + newType);
         }
     }
-
     @NonNull
     @Override
-    public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
-//        Log.d("perf", "onCreateViewHolder called");
-        View row = LayoutInflater.from(parent.getContext())
-                .inflate(R.layout.transaction_list_row, parent, false);
-        return new TransactionRowHolder(row);
+    public TransactionRowHolderBase onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
+//        debug("perf", "onCreateViewHolder called");
+        final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
+
+        switch (TransactionListItem.Type.valueOf(viewType)) {
+            case TRANSACTION:
+                return new TransactionRowHolder(
+                        TransactionListRowBinding.inflate(inflater, parent, false));
+            case DELIMITER:
+                return new TransactionListDelimiterRowHolder(
+                        TransactionDelimiterBinding.inflate(inflater, parent, false));
+            case HEADER:
+                return new TransactionListLastUpdateRowHolder(
+                        LastUpdateLayoutBinding.inflate(inflater, parent, false));
+            default:
+                throw new IllegalStateException("Unexpected value: " + viewType);
+        }
     }
 
     @Override
     public int getItemCount() {
-        return TransactionListViewModel.getTransactionCount();
-    }
-    public void setBoldAccountName(String boldAccountName) {
-        this.boldAccountName = boldAccountName;
-    }
-    public void resetBoldAccountName() {
-        this.boldAccountName = null;
-    }
-
-    enum LoaderStep {HEAD, ACCOUNTS, DONE}
-
-    private static class TransactionLoader
-            extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
-        @Override
-        protected Void doInBackground(TransactionLoaderParams... p) {
-            LedgerTransaction tr = p[0].transaction;
-            LedgerTransaction previous = p[0].previousTransaction;
-
-            SQLiteDatabase db = MLDB.getReadableDatabase();
-            tr.loadData(db);
-
-            boolean showDate;
-            if (previous == null) showDate = true;
-            else {
-                previous.loadData(db);
-                showDate = !previous.getDate().equals(tr.getDate());
-            }
-            publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, showDate));
-
-            int rowIndex = 0;
-            for (LedgerTransactionAccount acc : tr.getAccounts()) {
-//                Log.d(c.getAccountName(), acc.getAmount()));
-                publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
-                        p[0].boldAccountName));
-            }
-
-            publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
-
-            return null;
-        }
-        @Override
-        protected void onProgressUpdate(TransactionLoaderStep... values) {
-            super.onProgressUpdate(values);
-            TransactionLoaderStep step = values[0];
-            TransactionRowHolder holder = step.getHolder();
-
-            switch (step.getStep()) {
-                case HEAD:
-                    holder.tvDescription.setText(step.getTransaction().getDescription());
-
-                    if (step.getPosition() % 2 == 0) {
-                        holder.row.setBackgroundColor(Globals.tableRowEvenBG);
-                    }
-                    else {
-                        holder.row.setBackgroundColor(Globals.tableRowOddBG);
-                    }
-
-                    break;
-                case ACCOUNTS:
-                    int rowIndex = step.getAccountPosition();
-                    Context ctx = holder.row.getContext();
-                    LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
-                    TextView accName, accAmount;
-                    if (row == null) {
-                        row = new LinearLayout(ctx);
-                        row.setLayoutParams(new LinearLayout.LayoutParams(
-                                LinearLayout.LayoutParams.MATCH_PARENT,
-                                LinearLayout.LayoutParams.WRAP_CONTENT));
-                        row.setGravity(Gravity.CENTER_VERTICAL);
-                        row.setOrientation(LinearLayout.HORIZONTAL);
-                        row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
-                        accName = new AppCompatTextView(ctx);
-                        accName.setLayoutParams(new LinearLayout.LayoutParams(0,
-                                LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
-                        accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
-                        row.addView(accName);
-                        accAmount = new AppCompatTextView(ctx);
-                        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
-                                LinearLayout.LayoutParams.WRAP_CONTENT,
-                                LinearLayout.LayoutParams.WRAP_CONTENT);
-                        llp.setMarginEnd(0);
-                        accAmount.setLayoutParams(llp);
-                        accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
-                        accAmount.setMinWidth(dp2px(ctx, 60));
-                        row.addView(accAmount);
-                        holder.tableAccounts.addView(row);
-                    }
-                    else {
-                        accName = (TextView) row.getChildAt(0);
-                        accAmount = (TextView) row.getChildAt(1);
-                    }
-                    LedgerTransactionAccount acc = step.getAccount();
-
-                    accName.setText(acc.getAccountName());
-                    accAmount.setText(acc.toString());
-
-//                    Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
-//                            acc.getAccountName(), acc.getAmount()));
-
-                    String boldAccountName = step.getBoldAccountName();
-                    if ((boldAccountName != null) && boldAccountName.equals(acc.getAccountName())) {
-                        accName.setTypeface(null, Typeface.BOLD);
-                        accAmount.setTypeface(null, Typeface.BOLD);
-                        accName.setTextColor(Globals.primaryDark);
-                        accAmount.setTextColor(Globals.primaryDark);
-                    }
-                    else {
-                        accName.setTypeface(null, Typeface.NORMAL);
-                        accAmount.setTypeface(null, Typeface.NORMAL);
-                        accName.setTextColor(Globals.defaultTextColor);
-                        accAmount.setTextColor(Globals.defaultTextColor);
-                    }
-
-                    break;
-                case DONE:
-                    int accCount = step.getAccountCount();
-                    if (holder.tableAccounts.getChildCount() > accCount) {
-                        holder.tableAccounts.removeViews(accCount,
-                                holder.tableAccounts.getChildCount() - accCount);
-                    }
-
-//                    Log.d("transactions",
-//                            String.format("Position %d fill done", step.getPosition()));
-            }
-        }
+        return listDiffer.getCurrentList()
+                         .size();
     }
-
-    private class TransactionLoaderParams {
-        LedgerTransaction transaction, previousTransaction;
-        TransactionRowHolder holder;
-        int position;
-        String boldAccountName;
-        TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder, int position, String boldAccountName) {
-            this(transaction, null, holder, position, boldAccountName);
-        }
-        TransactionLoaderParams(LedgerTransaction transaction,
-                                LedgerTransaction previousTransaction, TransactionRowHolder holder,
-                                int position, String boldAccountName) {
-            this.transaction = transaction;
-            this.previousTransaction = previousTransaction;
-            this.holder = holder;
-            this.position = position;
-            this.boldAccountName = boldAccountName;
-        }
+    public void setTransactions(List<TransactionListItem> newList) {
+        Logger.debug("transactions",
+                String.format(Locale.US, "Got new transaction list (%d items)", newList.size()));
+        listDiffer.submitList(newList);
     }
 }
\ No newline at end of file