Skip to content

Commit

Permalink
[feat] #40 피드백 뷰 api 연동
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan2dani committed Dec 5, 2023
1 parent ed6b59b commit 37144a9
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.howdroid.data.datasource.remote

import com.example.howdroid.data.model.response.ResponseFeedBack
import com.example.howdroid.data.model.response.ResponseStatistic
import com.example.howdroid.data.service.ChartService
import javax.inject.Inject
Expand All @@ -9,4 +10,7 @@ class ChartDataSource @Inject constructor(
) {
suspend fun fetchStatistic(selectedDate: String): ResponseStatistic =
chartService.fetchStatistic(selectedDate)

suspend fun fetchFeedBack(selectedDate: String): ResponseFeedBack =
chartService.fetchFeedBack(selectedDate)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.howdroid.data.model.response

import kotlinx.serialization.Serializable

@Serializable
data class ResponseFeedBack(
val delayDetailMessage: String,
val delayMessage: String,
val firstPriPercent: Int,
val priorityDetailMessage: String,
val priorityMessage: String,
val rateDetailMessage: String,
val rateMessage: String,
val secondPriPercent: Int,
val thirdPriPercent: Int
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.howdroid.data.repository

import com.example.howdroid.data.datasource.remote.ChartDataSource
import com.example.howdroid.data.model.response.ResponseFeedBack
import com.example.howdroid.data.model.response.ResponseStatistic
import com.example.howdroid.domain.repository.ChartRepository
import javax.inject.Inject
Expand All @@ -12,4 +13,9 @@ class ChartRepositoryImpl @Inject constructor(
runCatching {
chartDataSource.fetchStatistic(selectedDate)
}

override suspend fun fetchFeedBack(selectedDate: String): Result<ResponseFeedBack> =
runCatching {
chartDataSource.fetchFeedBack(selectedDate)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.howdroid.data.service

import com.example.howdroid.data.model.response.ResponseFeedBack
import com.example.howdroid.data.model.response.ResponseStatistic
import retrofit2.http.GET
import retrofit2.http.Path
Expand All @@ -9,4 +10,9 @@ interface ChartService {
suspend fun fetchStatistic(
@Path("selectedDate") selectedDate: String,
): ResponseStatistic

@GET("feedback/{selectedDate}")
suspend fun fetchFeedBack(
@Path("selectedDate") selectedDate: String,
): ResponseFeedBack
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.howdroid.domain.repository

import com.example.howdroid.data.model.response.ResponseFeedBack
import com.example.howdroid.data.model.response.ResponseStatistic

interface ChartRepository {
suspend fun fetchStatistic(selectedDate: String): Result<ResponseStatistic>
suspend fun fetchFeedBack(selectedDate: String): Result<ResponseFeedBack>
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,154 @@
package com.example.howdroid.presentation.chart

import android.graphics.Typeface
import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.example.howdroid.R
import com.example.howdroid.databinding.FragmentChartStatisticBinding
import com.example.howdroid.databinding.FragmentChartFeedbackBinding
import com.example.howdroid.presentation.type.PriorityType
import com.example.howdroid.util.binding.BindingFragment
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.formatter.ValueFormatter
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

class ChartFeedbackFragment :
BindingFragment<FragmentChartStatisticBinding>(R.layout.fragment_chart_feedback) {
BindingFragment<FragmentChartFeedbackBinding>(R.layout.fragment_chart_feedback) {
private val viewModel: ChartViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

collectData()
}

private fun collectData() {
viewModel.feedBackContent.flowWithLifecycle(lifecycle).onEach { feedBackContent ->
with(binding) {
if (feedBackContent?.rateMessage != null) {
feedBackContent.also {
tvChartRateTitle.text = it.rateMessage
tvChartRateDes.text = it.rateDetailMessage
}
tvChartRateTitle.text = feedBackContent.rateMessage
tvChartRateDes.text = feedBackContent.rateDetailMessage
}
if (feedBackContent?.priorityMessage != null) {
tvChartPriorityTitle.text = feedBackContent.priorityMessage
tvChartPriorityDes.text = feedBackContent.priorityDetailMessage
}
if (feedBackContent?.delayMessage != null) {
tvChartDelayedTitle.text = feedBackContent.delayMessage
tvChartDelayedDes.text = feedBackContent.delayDetailMessage
}
}

val priorityRateChart: BarChart = binding.barChartPriorityRate
val totalPercent = feedBackContent?.let {
it.firstPriPercent + it.secondPriPercent + it.thirdPriPercent
}
val barList =
if (feedBackContent != null && totalPercent != null) {
setPriorityRateData(
feedBackContent.firstPriPercent.toFloat() / totalPercent.toFloat() * 100,
feedBackContent.secondPriPercent.toFloat() / totalPercent.toFloat() * 100,
feedBackContent.thirdPriPercent.toFloat() / totalPercent.toFloat() * 100
)
} else {
return@onEach
}
setPriorityRateBarChart(priorityRateChart, barList!!)
}.launchIn(lifecycleScope)
}

private fun setPriorityRateBarChart(barChart: BarChart, barList: ArrayList<BarEntry>) {
barChart.run {
setDrawBarShadow(false)
setTouchEnabled(false)
setDrawValueAboveBar(true)
setPinchZoom(false)
setDrawGridBackground(false)
setMaxVisibleValueCount(2)
description.isEnabled = false
legend.isEnabled = false

xAxis.run {
isEnabled = true
position = XAxis.XAxisPosition.BOTTOM
valueFormatter = LabelFeedBackCustomFormatter()
setDrawGridLines(false)
granularity = 1f
setDrawAxisLine(false)
textSize = 13f
typeface = Typeface.DEFAULT_BOLD
}

axisLeft.run {
isEnabled = false
axisMinimum = 0f
axisMaximum = 100f
}
axisRight.run {
isEnabled = false
}

animateY(1500)
}

val barDataSet = BarDataSet(barList, "")
.apply {
setDrawValues(true)
colors = context?.let {
listOf(
it.getColor(R.color.Orange),
it.getColor(R.color.Green_400),
it.getColor(R.color.Blue)
)
}
valueFormatter = LabelFeedBackCustomFormatter()
}

val data = BarData(barDataSet)
data.barWidth = 0.35f
barChart.data = data
barChart.setFitBars(true)

barChart.notifyDataSetChanged()
barChart.invalidate()
}

private fun setPriorityRateData(
mostImportant: Float,
important: Float,
notImportant: Float
): ArrayList<BarEntry> {
val barList = ArrayList<BarEntry>()
barList.add(BarEntry(1f, mostImportant))
barList.add(BarEntry(2f, important))
barList.add(BarEntry(3f, notImportant))

return barList
}
}

class LabelFeedBackCustomFormatter : ValueFormatter() {
override fun getFormattedValue(value: Float): String {
val index = value.toInt()
return when (index) {
1 -> PriorityType.MOST_IMPORTANT.typeString
2 -> PriorityType.IMPORTANT.typeString
3 -> PriorityType.NOT_IMPORTANT.typeString
else -> throw IndexOutOfBoundsException("index out")
}
}

override fun getBarStackedLabel(value: Float, stackedEntry: BarEntry?): String {
return super.getBarStackedLabel(value, stackedEntry)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.example.howdroid.presentation.chart

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.howdroid.data.model.response.ResponseFeedBack
import com.example.howdroid.data.model.response.ResponseStatistic
import com.example.howdroid.domain.repository.ChartRepository
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -36,8 +37,13 @@ class ChartViewModel @Inject constructor(
private val _nowWorstFailTag = MutableStateFlow("")
val nowWorstFailTag get() = _nowWorstFailTag.asStateFlow()

private val _feedBackContent = MutableStateFlow<ResponseFeedBack?>(null)
val feedBackContent get() = _feedBackContent.asStateFlow()


init {
fetchStatistic()
fetchFeedBack()
}

// TODO 초기화 유사한 거 끼리 함수화
Expand Down Expand Up @@ -66,4 +72,17 @@ class ChartViewModel @Inject constructor(
}
}

private fun fetchFeedBack() {
val today = LocalDate.now().toString()
viewModelScope.launch {
chartRepository.fetchFeedBack(today)
.onSuccess { feedBackContent ->
_feedBackContent.value = feedBackContent
}
.onFailure { throwable ->
Timber.e(throwable.message)
}
}
}

}

0 comments on commit 37144a9

Please sign in to comment.