11package com.seok.gfd.views
22
3+ import android.graphics.Color
34import android.os.Bundle
45import android.view.LayoutInflater
56import android.view.View
67import android.view.ViewGroup
8+ import android.widget.LinearLayout
9+ import android.widget.TextView
10+ import android.widget.Toast
711import androidx.fragment.app.Fragment
812import com.ogaclejapan.smarttablayout.utils.v4.Bundler
913import com.seok.gfd.R
1014import com.seok.gfd.retrofit.domain.resopnse.CommitsResponseDto
1115import com.seok.gfd.utils.CommonUtils
16+ import kotlinx.android.synthetic.main.fragment_main.*
17+ import kotlinx.android.synthetic.main.fragment_main_sub.*
18+ import org.jetbrains.anko.backgroundColor
19+ import org.jetbrains.anko.backgroundColorResource
20+ import org.jetbrains.anko.textColor
21+ import java.time.DayOfWeek
22+ import java.time.LocalDate
23+ import java.time.Month
24+ import java.time.format.DateTimeFormatter
25+ import kotlin.math.ceil
26+ import kotlin.math.roundToInt
1227
1328class MainSub : Fragment () {
1429
15- companion object {
16- fun arguments (param : CommitsResponseDto ) : Bundle {
30+ private lateinit var commitResponse: CommitsResponseDto
31+
32+ companion object {
33+ fun arguments (param : CommitsResponseDto ): Bundle {
1734 val str = CommonUtils .gson.toJson(param)
18- return Bundler ().putString(" key " , str).get()
35+ return Bundler ().putString(" year " , str).get()
1936 }
2037 }
2138
@@ -24,18 +41,99 @@ class MainSub : Fragment() {
2441 container : ViewGroup ? ,
2542 savedInstanceState : Bundle ?
2643 ): View ? {
27- return inflater.inflate(R .layout.fragment_main_sub1 , container, false )
44+ return inflater.inflate(R .layout.fragment_main_sub , container, false )
2845 }
2946
3047 override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
3148 super .onViewCreated(view, savedInstanceState)
49+
50+ initUI()
51+
3252 }
3353
3454
3555 override fun onCreate (savedInstanceState : Bundle ? ) {
3656 super .onCreate(savedInstanceState)
37- val t = arguments?.getString(" key" )
38- val user = CommonUtils .gson.fromJson(t, CommitsResponseDto ::class .java)
39- user
57+ init ()
58+ }
59+
60+ private fun init () {
61+ val yearContribution = arguments?.getString(" year" )
62+ commitResponse = CommonUtils .gson.fromJson(yearContribution, CommitsResponseDto ::class .java)
63+ }
64+
65+ private fun initUI () {
66+
67+ val date = commitResponse.contributions!! [commitResponse.contributions!! .size - 1 ].date
68+ val localDate = LocalDate .parse(date, DateTimeFormatter .ISO_DATE )
69+ val startDay = getDayStartCount(localDate.dayOfWeek)
70+
71+
72+ var weekText = TextView (activity)
73+ var weekTop = 23f
74+ for (index in 1 .. ceil(commitResponse.contributions!! .size.toDouble() / 7 ).toInt() step 2 ){
75+ weekText = TextView (activity)
76+ weekText.textColor = activity!! .getColor(R .color.userRankPos)
77+ weekText.text = index.toString() + " W"
78+ main_sub_scalable_layout.addView(weekText, 20f , weekTop, 100f , 50f )
79+ main_sub_scalable_layout.setScale_TextSize(weekText, 40f )
80+ weekTop + = 55f
81+ }
82+ weekTop = 23f
83+ for (index in 2 .. commitResponse.contributions!! .size / 7 step 2 ){
84+ weekText = TextView (activity)
85+ weekText.textColor = activity!! .getColor(R .color.userRankPos)
86+ weekText.text = index.toString() + " W"
87+ main_sub_scalable_layout.addView(weekText, 570f , weekTop, 100f , 50f )
88+ main_sub_scalable_layout.setScale_TextSize(weekText, 40f )
89+ weekTop + = 55f
90+ }
91+
92+ var layoutSize = 45f
93+ var layoutPScaleTop = 30f
94+ var layoutPScaleLeft = 110f
95+ var layoutPScaleWidth = layoutSize
96+ var layoutPScaleHeight = layoutSize
97+ var lineChange = 0
98+
99+ for (index in 0 until startDay) {
100+ lineChange++
101+ layoutPScaleLeft + = 55f
102+ }
103+ for (index in commitResponse.contributions?.size!! - 1 downTo 0 ) {
104+ val linearLayout = LinearLayout (activity)
105+ main_sub_scalable_layout.addView(
106+ linearLayout,
107+ layoutPScaleLeft,
108+ layoutPScaleTop,
109+ layoutPScaleWidth,
110+ layoutPScaleHeight
111+ )
112+ linearLayout.backgroundColor =
113+ Color .parseColor(commitResponse.contributions!! [index].color)
114+ layoutPScaleLeft + = 55f
115+ lineChange++
116+ if (lineChange % 7 == 0 ) {
117+ layoutPScaleLeft + = 170f
118+ }
119+ if (lineChange % 14 == 0 ) {
120+ layoutPScaleTop + = 55f
121+ layoutPScaleLeft = 110f
122+ }
123+ linearLayout.setOnClickListener {
124+ // 존Toast
125+ }
126+ }
127+ }
128+
129+ private fun getDayStartCount (dayOfWeek : DayOfWeek ): Int = when (dayOfWeek) {
130+ DayOfWeek .MONDAY -> 0
131+ DayOfWeek .TUESDAY -> 1
132+ DayOfWeek .WEDNESDAY -> 2
133+ DayOfWeek .THURSDAY -> 3
134+ DayOfWeek .FRIDAY -> 4
135+ DayOfWeek .SATURDAY -> 5
136+ DayOfWeek .SUNDAY -> 6
137+ else -> 0
40138 }
41- }
139+ }
0 commit comments