|
| 1 | +package com.seok.gfd.adapter |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.content.Context |
| 5 | +import android.graphics.Color |
| 6 | +import android.view.Gravity |
| 7 | +import android.view.LayoutInflater |
| 8 | +import android.view.View |
| 9 | +import android.view.ViewGroup |
| 10 | +import android.widget.LinearLayout |
| 11 | +import android.widget.TextView |
| 12 | +import androidx.gridlayout.widget.GridLayout |
| 13 | +import androidx.recyclerview.widget.RecyclerView |
| 14 | +import com.seok.gfd.R |
| 15 | +import com.seok.gfd.utils.Contribution |
| 16 | +import org.jetbrains.anko.backgroundColor |
| 17 | +import org.jetbrains.anko.margin |
| 18 | +import java.time.LocalDate |
| 19 | +import java.time.Month |
| 20 | + |
| 21 | +class ContributionsAdapter(list: ArrayList<Contribution>, context: Context) : |
| 22 | + RecyclerView.Adapter<ContributionsAdapter.MyViewHolder>() { |
| 23 | + var list = list |
| 24 | + var context: Context = context |
| 25 | + |
| 26 | + override fun getItemCount() = list.size |
| 27 | + |
| 28 | + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { |
| 29 | + val v = LayoutInflater.from(parent.context) |
| 30 | + .inflate(R.layout.contributions_layout, parent, false) |
| 31 | + return MyViewHolder(v) |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + @SuppressLint("SetTextI18n") |
| 36 | + override fun onBindViewHolder(holder: MyViewHolder, position: Int) { |
| 37 | + val contribution = list[position] |
| 38 | + val formatYear = |
| 39 | + String.format("%s: %d Contributions", contribution.year, contribution.total) |
| 40 | + holder.yearContribution.text = formatYear |
| 41 | + |
| 42 | + var monthFlag = "" |
| 43 | + val size = contribution.list!!.size |
| 44 | + var lineLayout = LinearLayout(this.context) |
| 45 | + lineLayout.orientation = LinearLayout.VERTICAL |
| 46 | + val params = LinearLayout.LayoutParams(35, 35) |
| 47 | + params.margin = 4 |
| 48 | + params.gravity = Gravity.CENTER |
| 49 | + for (index in 0 until size) { |
| 50 | + var tempLayout = LinearLayout(this.context) |
| 51 | + if (index % 7 == 0) { |
| 52 | + val monthDataStr = getDayStartCount(LocalDate.parse(contribution.list!![index].date).month) |
| 53 | + lineLayout = LinearLayout(this.context) |
| 54 | + lineLayout.orientation = LinearLayout.VERTICAL |
| 55 | + tempLayout.backgroundColor = Color.WHITE |
| 56 | + tempLayout.layoutParams = params |
| 57 | + if(monthFlag != monthDataStr){ |
| 58 | + val month = TextView(this.context) |
| 59 | + month.textSize = 7f |
| 60 | + month.text = monthDataStr |
| 61 | + tempLayout.addView(month) |
| 62 | + monthFlag = monthDataStr |
| 63 | + } |
| 64 | + lineLayout.addView(tempLayout) |
| 65 | + tempLayout = LinearLayout(this.context) |
| 66 | + tempLayout.backgroundColor = Color.parseColor(contribution.list!![index].color) |
| 67 | + tempLayout.layoutParams = params |
| 68 | + lineLayout.addView(tempLayout) |
| 69 | + } else { |
| 70 | + tempLayout.backgroundColor = Color.parseColor(contribution.list!![index].color) |
| 71 | + tempLayout.layoutParams = params |
| 72 | + lineLayout.addView(tempLayout) |
| 73 | + } |
| 74 | + if (index % 7 == 0) { |
| 75 | + holder.contributionCanvas.addView(lineLayout) |
| 76 | + } |
| 77 | + if (index == size) { |
| 78 | + holder.contributionCanvas.addView(lineLayout) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { |
| 84 | + var yearContribution: TextView = itemView.findViewById(R.id.guest_year_contribution) |
| 85 | + var contributionCanvas: GridLayout = itemView.findViewById(R.id.canvas) |
| 86 | + } |
| 87 | + |
| 88 | + private fun getDayStartCount(dayOfMonth: Month): String = when (dayOfMonth) { |
| 89 | + Month.JANUARY -> "Jan" |
| 90 | + Month.FEBRUARY -> "Feb" |
| 91 | + Month.MARCH -> "Mar" |
| 92 | + Month.APRIL -> "Apr" |
| 93 | + Month.MAY -> "May" |
| 94 | + Month.JUNE -> "Jun" |
| 95 | + Month.JULY -> "Jul" |
| 96 | + Month.AUGUST-> "Aug" |
| 97 | + Month.SEPTEMBER-> "Sep" |
| 98 | + Month.OCTOBER -> "Oct" |
| 99 | + Month.NOVEMBER-> "Nov" |
| 100 | + Month.DECEMBER -> "Dec" |
| 101 | + else -> "X" |
| 102 | + } |
| 103 | +} |
| 104 | + |
0 commit comments