|
| 1 | +package com.seok.gfd.views |
| 2 | + |
| 3 | + |
| 4 | +import android.annotation.SuppressLint |
| 5 | +import android.graphics.Color |
| 6 | +import android.os.Bundle |
| 7 | +import android.view.LayoutInflater |
| 8 | +import android.view.View |
| 9 | +import android.view.ViewGroup |
| 10 | +import android.widget.LinearLayout |
| 11 | +import androidx.fragment.app.Fragment |
| 12 | +import androidx.lifecycle.Observer |
| 13 | +import androidx.lifecycle.ViewModelProviders |
| 14 | +import com.bumptech.glide.Glide |
| 15 | +import com.bumptech.glide.request.RequestOptions |
| 16 | +import com.seok.gfd.R |
| 17 | +import com.seok.gfd.retrofit.domain.User |
| 18 | +import com.seok.gfd.retrofit.domain.request.CommitRequestDto |
| 19 | +import com.seok.gfd.retrofit.domain.resopnse.CommitsResponseDto |
| 20 | +import com.seok.gfd.utils.ProgressbarDialog |
| 21 | +import com.seok.gfd.utils.SharedPreference |
| 22 | +import com.seok.gfd.viewmodel.GithubCommitDataViewModel |
| 23 | +import com.seok.gfd.viewmodel.UserViewModel |
| 24 | +import kotlinx.android.synthetic.main.fragment_main3.* |
| 25 | +import org.jetbrains.anko.backgroundColor |
| 26 | +import org.jetbrains.anko.margin |
| 27 | +import java.time.LocalDate |
| 28 | + |
| 29 | +class Main3Fragment : Fragment() { |
| 30 | + private lateinit var githubCommitDataViewModel: GithubCommitDataViewModel |
| 31 | + private lateinit var userViewModel: UserViewModel |
| 32 | + private lateinit var sharedPreference: SharedPreference |
| 33 | + private lateinit var progressbar : ProgressbarDialog |
| 34 | + private lateinit var user: User |
| 35 | + |
| 36 | + override fun onCreateView( |
| 37 | + inflater: LayoutInflater, |
| 38 | + container: ViewGroup?, |
| 39 | + savedInstanceState: Bundle? |
| 40 | + ): View? { |
| 41 | + return inflater.inflate(R.layout.fragment_main3, container, false) |
| 42 | + } |
| 43 | + |
| 44 | + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| 45 | + super.onViewCreated(view, savedInstanceState) |
| 46 | + init() |
| 47 | + initViewModelFun() |
| 48 | + } |
| 49 | + |
| 50 | + private fun init() { |
| 51 | +// // Github 그래프 웹뷰, 줌 가능 |
| 52 | +// val settings = wv_mv_graph.settings |
| 53 | +// settings.builtInZoomControls = true |
| 54 | + |
| 55 | + // Progress Bar |
| 56 | + progressbar = ProgressbarDialog(this.activity!!) |
| 57 | + progressbar.show() |
| 58 | + |
| 59 | + // SharedPreference 에 저장된 User 정보 가져오기 |
| 60 | + sharedPreference = SharedPreference(this.activity!!.application) |
| 61 | + user = sharedPreference.getValueObject(getString(R.string.user_info)) |
| 62 | + |
| 63 | + githubCommitDataViewModel = |
| 64 | + ViewModelProviders.of(this).get(GithubCommitDataViewModel::class.java) |
| 65 | + userViewModel = ViewModelProviders.of(this).get(UserViewModel::class.java) |
| 66 | + // Login Activity 에서 저장한 User 정보 가져오기 |
| 67 | + |
| 68 | + setUserInfoUI(user) |
| 69 | + |
| 70 | + githubCommitDataViewModel.getCommitsInfo(user.login) |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + private fun initViewModelFun() { |
| 75 | + // 금일 커밋 가져오기 |
| 76 | + githubCommitDataViewModel.todayCommit.observe(this, Observer { |
| 77 | + today_commit.text = it |
| 78 | + sharedPreference.setValue(getString(R.string.user_today), it) |
| 79 | + // 금일 커밋 서버에 저장 |
| 80 | + val commit = CommitRequestDto(user.login, it.toInt()) |
| 81 | + userViewModel.enrollCommit(commit) |
| 82 | + }) |
| 83 | + // 금년 총 커밋 가져오기 |
| 84 | + githubCommitDataViewModel.yearCommit.observe(this, Observer { |
| 85 | + year_commit.text = it |
| 86 | + sharedPreference.setValue(getString(R.string.user_year), it) |
| 87 | + }) |
| 88 | + // 커밋 최대값 가져오기 |
| 89 | + githubCommitDataViewModel.maxCommit.observe(this, Observer { |
| 90 | + max_commit.text = it |
| 91 | + sharedPreference.setValue(getString(R.string.user_max), it) |
| 92 | + }) |
| 93 | + // 총 커밋 가져오기 |
| 94 | + githubCommitDataViewModel.commits.observe(this, Observer { |
| 95 | + setCommitUI(it) |
| 96 | + }) |
| 97 | + } |
| 98 | + |
| 99 | + @SuppressLint("NewApi") |
| 100 | + private fun setCommitUI(it: List<CommitsResponseDto.Contribution>) { |
| 101 | + val lastDateTime = LocalDate.now().minusDays(366).toString() |
| 102 | + val nowDateTime = LocalDate.now().toString() |
| 103 | + val lastCommitIndex = it.indexOf(it.find { it.date == lastDateTime }) |
| 104 | + val nowCommitIndex = it.indexOf(it.find { it.date == nowDateTime }) |
| 105 | + val maxCommit = sharedPreference.getValue(getString(R.string.user_max)) |
| 106 | + |
| 107 | + this.activity?.runOnUiThread { |
| 108 | + contribute.removeAllViews() |
| 109 | + contribute.columnCount = 53 |
| 110 | + contribute.rowCount = 7 |
| 111 | + for (index in lastCommitIndex downTo nowCommitIndex) { |
| 112 | + val layout = LinearLayout(activity) |
| 113 | + val param = LinearLayout.LayoutParams(65, 65) |
| 114 | + val commit = it[index] |
| 115 | + param.margin = 4 |
| 116 | + layout.layoutParams = param |
| 117 | +// val txt = TextView(this.activity) |
| 118 | +// txt.text = commit.count.toString() |
| 119 | +// layout.gravity = Gravity.CENTER |
| 120 | +// layout.addView(txt) |
| 121 | + layout.backgroundColor = Color.parseColor(commit.color) |
| 122 | + if (commit.count == maxCommit.toInt()) { |
| 123 | + layout.background = activity?.getDrawable(R.drawable.rect_background) |
| 124 | + } |
| 125 | + contribute.addView(layout) |
| 126 | + } |
| 127 | + } |
| 128 | + progressbar.hide() |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + private fun setUserInfoUI(user: User) { |
| 133 | +// wv_mv_graph.loadUrl("https://ghchart.rshah.org/${user.login}") |
| 134 | + user_id.text = user.login |
| 135 | + user_location.text = user.location |
| 136 | + year_commit.text = sharedPreference.getValue(getString(R.string.user_year)) |
| 137 | + max_commit.text = sharedPreference.getValue(getString(R.string.user_max)) |
| 138 | + Glide.with(this).load(user.avatar_url).apply(RequestOptions.circleCropTransform()) |
| 139 | + .into(img_mv_user_profile) |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + /** 오리지날 잔디 그래프 출력 |
| 144 | + private fun setCommitUI(it: List<Commits>) { |
| 145 | + val maxCommit = it.maxBy { it.dataCount } |
| 146 | + this.activity?.runOnUiThread { |
| 147 | + contribute.removeAllViews() |
| 148 | + contribute.columnCount = 53 |
| 149 | + contribute.rowCount = 7 |
| 150 | + for (commit in it) { |
| 151 | + val layout = LinearLayout(this.activity) |
| 152 | + val param = LinearLayout.LayoutParams(65, 65) |
| 153 | + param.margin = 4 |
| 154 | + layout.layoutParams = param |
| 155 | + val txt = TextView(this.activity) |
| 156 | + txt.text = commit.dataCount.toString() |
| 157 | + layout.gravity = Gravity.CENTER |
| 158 | + layout.addView(txt) |
| 159 | + layout.backgroundColor = Color.parseColor(commit.fill) |
| 160 | + if (commit.dataCount == maxCommit!!.dataCount) { |
| 161 | + layout.background = this.activity?.getDrawable(R.drawable.rect_background) |
| 162 | + max_commit.text = commit.dataCount.toString() |
| 163 | + } |
| 164 | + contribute.addView(layout) |
| 165 | + } |
| 166 | + val todayCommit = it[it.size - 1].dataCount |
| 167 | + today_commit.text = todayCommit.toString() |
| 168 | + } |
| 169 | + } |
| 170 | + */ |
| 171 | +} |
0 commit comments