-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/example/howdroid/data/model/response/ResponseFeedBack.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
app/src/main/java/com/example/howdroid/domain/repository/ChartRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
142 changes: 140 additions & 2 deletions
142
app/src/main/java/com/example/howdroid/presentation/chart/ChartFeedbackFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters