Skip to content

Instantly share code, notes, and snippets.

@albka1986
Created December 16, 2019 10:59
Show Gist options
  • Save albka1986/874253dff2ca03cad93bde2706bf88e4 to your computer and use it in GitHub Desktop.
Save albka1986/874253dff2ca03cad93bde2706bf88e4 to your computer and use it in GitHub Desktop.
Adapter: group by date
class TransactionAdapter(private val context: Context) : RecyclerView.Adapter<ViewHolder>() {
companion object {
const val tag: String = "TransactionAdapter"
}
private var transactions: List<TransactionEntity?>? = null
private var isNewDate: Boolean = true
private var lastDate: String? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.transaction_item, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return transactions?.size ?: 0
}
private fun isNextDay(date1: String?, date2: String?): Boolean {
val cal1 = Calendar.getInstance()
val cal2 = Calendar.getInstance()
val parsedDate1 = date1?.let { Utility.stringToDateServer(it) }
val parsedDate2 = date2?.let { Utility.stringToDateServer(it) }
cal1.time = parsedDate1
cal2.time = parsedDate2
return cal1.get(Calendar.DAY_OF_YEAR) != cal2.get(Calendar.DAY_OF_YEAR) && cal1.get(Calendar.YEAR) == cal2.get(
Calendar.YEAR
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
transactions?.get(position)?.apply {
holder.cardName.text = agentName
holder.sum.text = context.getString(R.string.current_balance, sum.toString())
holder.date.text = date ?: ""
setType(holder)
isNewDate = position == 0 || isNextDay(lastDate, date)
Log.d(tag, "isNewDate: $isNewDate ")
if (isNewDate) {
try {
val dateFormat = date?.let { Utility.stringToDateServer(it) }
val stringDate = Utility.transactionGroupDataFormat.format(dateFormat)
holder.setTag(stringDate)
} catch (e: Exception) {
logE { "Couldn't parse a date" }
}
} else {
holder.setTag(null)
}
lastDate = date
}
}
override fun getItemViewType(position: Int): Int {
return 1
}
private fun TransactionEntity.setType(holder: ViewHolder) {
when (typeEntity) {
TransactionTypeRequest.WITHDRAW.toString() -> {
holder.transactionTypeTV.text = TransactionTypeEntity.WITHDRAW.toString()
setTypeImage(holder, R.drawable.ic_arrow_down_orange)
}
TransactionTypeRequest.DEPOSIT.toString() -> {
holder.transactionTypeTV.text = TransactionTypeEntity.DEPOSIT.toString()
setTypeImage(holder, R.drawable.ic_arrow_up_green)
}
TransactionTypeRequest.IN_PROGRESS.toString() -> {
holder.transactionTypeTV.text = TransactionTypeEntity.IN_PROGRESS.toString()
setTypeImage(holder, R.drawable.ic_pending)
}
TransactionTypeRequest.ABORTED.toString() -> {
holder.transactionTypeTV.text = TransactionTypeEntity.ABORTED.toString()
setTypeImage(holder, R.drawable.ic_aborted)
}
}
}
private fun setTypeImage(holder: ViewHolder, imageResource: Int) {
holder.transactionTypeIV.setImageDrawable(context.getDrawable(imageResource))
}
fun updateData(newTransactions: List<TransactionEntity?>?) {
this.transactions = newTransactions
notifyDataSetChanged()
}
}
class ViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
val cardName: TextView = view.card_name_tv
val transactionTypeTV: TextView = view.type_tv
val sum: TextView = view.sum_tv
val transactionTypeIV: ImageView = view.transaction_type_iv
val date: TextView = view.date_tv
fun setTag(tag: String?) {
view.tag = tag
Log.e(TransactionAdapter.tag, tag.toString())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment