/*
* 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.
*
* 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 MoLe. If not, see .
*/
package net.ktnx.mobileledger.ui.account_summary;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;
import net.ktnx.mobileledger.R;
import net.ktnx.mobileledger.dao.BaseDAO;
import net.ktnx.mobileledger.databinding.AccountListRowBinding;
import net.ktnx.mobileledger.databinding.AccountListSummaryRowBinding;
import net.ktnx.mobileledger.db.Account;
import net.ktnx.mobileledger.db.DB;
import net.ktnx.mobileledger.model.AccountListItem;
import net.ktnx.mobileledger.model.LedgerAccount;
import net.ktnx.mobileledger.ui.activity.MainActivity;
import net.ktnx.mobileledger.utils.Logger;
import net.ktnx.mobileledger.utils.Misc;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Locale;
import static net.ktnx.mobileledger.utils.Logger.debug;
public class AccountSummaryAdapter extends RecyclerView.Adapter {
public static final int AMOUNT_LIMIT = 3;
private static final int ITEM_TYPE_HEADER = 1;
private static final int ITEM_TYPE_ACCOUNT = 2;
private final AsyncListDiffer listDiffer;
AccountSummaryAdapter() {
setHasStableIds(true);
listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback() {
@Nullable
@Override
public Object getChangePayload(@NonNull AccountListItem oldItem,
@NonNull AccountListItem newItem) {
Change changes = new Change();
final LedgerAccount oldAcc = oldItem.getAccount();
final LedgerAccount newAcc = newItem.getAccount();
if (!Misc.equalStrings(oldAcc.getName(), newAcc.getName()))
changes.add(Change.NAME);
if (oldAcc.getLevel() != newAcc.getLevel())
changes.add(Change.LEVEL);
if (oldAcc.isExpanded() != newAcc.isExpanded())
changes.add(Change.EXPANDED);
if (oldAcc.amountsExpanded() != newAcc.amountsExpanded())
changes.add(Change.EXPANDED_AMOUNTS);
if (!oldAcc.getAmountsString()
.equals(newAcc.getAmountsString()))
changes.add(Change.AMOUNTS);
return changes.toPayload();
}
@Override
public boolean areItemsTheSame(@NotNull AccountListItem oldItem,
@NotNull AccountListItem newItem) {
final AccountListItem.Type oldType = oldItem.getType();
final AccountListItem.Type newType = newItem.getType();
if (oldType != newType)
return false;
if (oldType == AccountListItem.Type.HEADER)
return true;
return oldItem.getAccount()
.getId() == newItem.getAccount()
.getId();
}
@Override
public boolean areContentsTheSame(@NotNull AccountListItem oldItem,
@NotNull AccountListItem newItem) {
return oldItem.sameContent(newItem);
}
});
}
@Override
public long getItemId(int position) {
if (position == 0)
return 0;
return listDiffer.getCurrentList()
.get(position)
.getAccount()
.getId();
}
@Override
public void onBindViewHolder(@NonNull RowHolder holder, int position,
@NonNull List