|
1 | 1 | package com.blogspot.e_kanivets.moneytracker.adapter |
2 | 2 |
|
3 | 3 | import android.content.Context |
| 4 | +import android.support.v4.content.ContextCompat |
4 | 5 | import android.support.v7.widget.RecyclerView |
5 | 6 | import android.view.LayoutInflater |
6 | 7 | import android.view.View |
7 | 8 | import android.view.ViewGroup |
| 9 | +import android.widget.TextView |
8 | 10 | import com.blogspot.e_kanivets.moneytracker.R |
9 | 11 | import com.blogspot.e_kanivets.moneytracker.entity.RecordReportItem |
| 12 | +import kotlinx.android.synthetic.main.view_report_item_exp.view.* |
10 | 13 |
|
11 | | -class RecordReportAdapter(private var items: List<RecordReportItem>, private val ctx: Context) : RecyclerView.Adapter<RecordReportAdapter.ViewHolder>() { |
| 14 | +class RecordReportAdapter( |
| 15 | + private var items: MutableList<RecordReportItem>, |
| 16 | + private var data: HashMap<RecordReportItem.ParentRow, List<RecordReportItem.ChildRow>>, |
| 17 | + private val ctx: Context |
| 18 | +) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { |
| 19 | + |
| 20 | + private var red: Int = 0 |
| 21 | + private var green: Int = 0 |
| 22 | + |
| 23 | + init { |
| 24 | + red = ContextCompat.getColor(ctx, R.color.red) |
| 25 | + green = ContextCompat.getColor(ctx, R.color.green) |
| 26 | + } |
| 27 | + |
| 28 | + private lateinit var summaryViewHolder: RecyclerView.ViewHolder |
12 | 29 |
|
13 | 30 | override fun getItemCount(): Int { |
14 | 31 | return items.size + 1 |
15 | 32 | } |
16 | 33 |
|
17 | 34 | override fun getItemViewType(position: Int): Int = when { |
18 | 35 | position == 0 -> TYPE_SUMMARY |
19 | | - items[position - 1] is RecordReportItem.ChildRow -> TYPE_PARENT |
20 | | - else -> TYPE_CHILD |
| 36 | + items[getPosWithoutSummary(position)] is RecordReportItem.ChildRow -> TYPE_CHILD |
| 37 | + else -> TYPE_PARENT |
21 | 38 | } |
22 | 39 |
|
23 | 40 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = |
24 | 41 | when (viewType) { |
25 | | - TYPE_PARENT -> ViewHolder(LayoutInflater.from(ctx).inflate(R.layout.view_report_item_exp, parent, false)) |
26 | | - TYPE_CHILD -> ViewHolder(LayoutInflater.from(ctx).inflate(R.layout.view_report_item, parent, false)) |
27 | | - else -> ViewHolder(LayoutInflater.from(ctx).inflate(R.layout.view_report_item, parent, false)) |
| 42 | + TYPE_PARENT -> ParentViewHolder(LayoutInflater.from(ctx).inflate(R.layout.view_report_item_exp, parent, false)) |
| 43 | + TYPE_CHILD -> ChildViewHolder(LayoutInflater.from(ctx).inflate(R.layout.view_report_item, parent, false)) |
| 44 | + else -> summaryViewHolder |
28 | 45 | } |
29 | 46 |
|
30 | | - override fun onBindViewHolder(holder: ViewHolder, position: Int) { |
| 47 | + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { |
| 48 | + val posWithoutSummary = getPosWithoutSummary(position) |
| 49 | + if (posWithoutSummary < 0) return |
| 50 | + |
| 51 | + if (holder is ChildViewHolder) { |
| 52 | + val row = items[posWithoutSummary] as RecordReportItem.ChildRow |
| 53 | + holder.tvCategory.text = row.title |
| 54 | + holder.tvTotal.text = row.amount |
| 55 | + holder.tvTotal.setTextColor(if (row.amount.first() != '-') green else red) |
| 56 | + } else { |
| 57 | + val parentViewHolder = holder as ParentViewHolder |
| 58 | + val row = items[posWithoutSummary] as RecordReportItem.ParentRow |
| 59 | + parentViewHolder.tvCategory.text = row.category |
| 60 | + parentViewHolder.tvTotal.text = row.totalAmount |
| 61 | + holder.tvTotal.setTextColor(if (row.totalAmount.first() != '-') green else red) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + fun setData(items: MutableList<RecordReportItem>, data: HashMap<RecordReportItem.ParentRow, List<RecordReportItem.ChildRow>>) { |
| 66 | + this.items = items |
| 67 | + this.data = data |
| 68 | + notifyDataSetChanged() |
| 69 | + } |
31 | 70 |
|
| 71 | + fun addSummaryView(summaryView: View) { |
| 72 | + this.summaryViewHolder = summaryView.tag as RecyclerView.ViewHolder |
32 | 73 | } |
33 | 74 |
|
34 | | - class ViewHolder(view: View) : RecyclerView.ViewHolder(view) |
| 75 | + private fun changeItems(position: Int) { |
| 76 | + if (items[position] is RecordReportItem.ParentRow) { |
| 77 | + val parentRow: RecordReportItem.ParentRow = items[position] as RecordReportItem.ParentRow |
| 78 | + |
| 79 | + if (parentRow.isOpen) { |
| 80 | + val item = items.filterIndexed { index, _ -> index > position } |
| 81 | + .find { recordReportItem -> recordReportItem is RecordReportItem.ParentRow } |
| 82 | + |
| 83 | + val lastChildInd = if (item != null) items.indexOf(item) else items.size |
| 84 | + |
| 85 | + items.subList(position + 1, lastChildInd).clear() |
| 86 | + val itemCount = lastChildInd - position - 1 |
| 87 | + notifyItemRangeRemoved(getPosWithSummary(position + 1), itemCount) |
| 88 | + |
| 89 | + parentRow.isOpen = false |
| 90 | + } else { |
| 91 | + data[parentRow]?.let { childRows -> |
| 92 | + var lastChildInd = position + 1 |
| 93 | + for (childRow in childRows) { |
| 94 | + items.add(lastChildInd, childRow) |
| 95 | + lastChildInd++ |
| 96 | + } |
| 97 | + notifyItemRangeInserted(getPosWithSummary(position + 1), childRows.size) |
| 98 | + } |
| 99 | + |
| 100 | + parentRow.isOpen = true |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private fun getPosWithSummary(position: Int): Int { |
| 106 | + return position + 1 |
| 107 | + } |
| 108 | + |
| 109 | + private fun getPosWithoutSummary(position: Int): Int { |
| 110 | + return position - 1 |
| 111 | + } |
| 112 | + |
| 113 | + inner class ParentViewHolder : RecyclerView.ViewHolder { |
| 114 | + |
| 115 | + var tvCategory: TextView |
| 116 | + var tvTotal: TextView |
| 117 | + |
| 118 | + constructor(view: View) : super(view) { |
| 119 | + tvCategory = view.tvCategory |
| 120 | + tvTotal = view.tvTotal |
| 121 | + |
| 122 | + view.setOnClickListener { |
| 123 | + if (view.lowerDivider.visibility == View.VISIBLE) { |
| 124 | + view.lowerDivider.visibility = View.GONE |
| 125 | + view.ivArrow.setImageDrawable(ContextCompat.getDrawable(ctx, R.drawable.ic_arrow_downward_outline)) |
| 126 | + } else { |
| 127 | + view.lowerDivider.visibility = View.VISIBLE |
| 128 | + view.ivArrow.setImageDrawable(ContextCompat.getDrawable(ctx, R.drawable.ic_arrow_upward_outline)) |
| 129 | + } |
| 130 | + |
| 131 | + changeItems(getPosWithoutSummary(adapterPosition)) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + class ChildViewHolder(view: View) : RecyclerView.ViewHolder(view) { |
| 137 | + |
| 138 | + val tvCategory: TextView = view.tvCategory |
| 139 | + val tvTotal: TextView = view.tvTotal |
| 140 | + |
| 141 | + } |
35 | 142 |
|
36 | 143 | companion object { |
| 144 | + |
37 | 145 | private const val TYPE_SUMMARY = 0 |
38 | 146 | private const val TYPE_PARENT = 1 |
39 | 147 | private const val TYPE_CHILD = 2 |
|
0 commit comments