Skip to content

Commit

Permalink
refactor chart improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
root14 committed Mar 25, 2024
1 parent b224e5f commit 2d38d50
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
17 changes: 10 additions & 7 deletions app/src/main/java/com/root14/hoopoe/AssetDetailActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.formatter.IFillFormatter
import com.root14.hoopoe.data.model.Interval
import com.root14.hoopoe.databinding.ActivityAssetDetailBinding
import com.root14.hoopoe.viewmodel.DetailViewModel
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -34,6 +35,11 @@ class AssetDetailActivity : AppCompatActivity() {
detailViewModel.getIntervalData("bitcoin").observe(this) { changeRate ->
binding.changeRate = changeRate
}

detailViewModel.getChartIntervalData("bitcoin", 30).observe(this) { interval ->
setData(interval)

}
}

//TODO make search icon work again -> show mainScreen search bottom sheet
Expand All @@ -43,11 +49,9 @@ class AssetDetailActivity : AppCompatActivity() {
val chart = binding.chartAsset


setData(30, 10f)

}

private fun setData(count: Int, range: Float) {
private fun setData(interval: Interval) {
val chart = binding.chartAsset
chart.setViewPortOffsets(0f, 0f, 0f, 0f)
chart.setBackgroundColor(Color.BLACK)
Expand Down Expand Up @@ -80,10 +84,10 @@ class AssetDetailActivity : AppCompatActivity() {
chart.invalidate()

val values = ArrayList<Entry>()
for (i in 0 until count) {
val `val` = (Math.random() * (range + 1)).toFloat() + 20
for (i in 0 until interval.data.size) {

//put time and price
values.add(Entry(i.toFloat(), `val`))
values.add(Entry(i.toFloat(), interval.data[i].priceUsd!!.toFloat()))
}
val set1: LineDataSet
if (chart.data != null && chart.data.getDataSetCount() > 0) {
Expand All @@ -92,7 +96,6 @@ class AssetDetailActivity : AppCompatActivity() {
chart.data.notifyDataChanged()
chart.notifyDataSetChanged()
} else {
// create a dataset and give it a type
set1 = LineDataSet(values, "DataSet")
set1.mode = LineDataSet.Mode.HORIZONTAL_BEZIER
set1.setCubicIntensity(0.2f)
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/root14/hoopoe/utils/TimeStamp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.root14.hoopoe.utils

import java.text.SimpleDateFormat
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.util.Date

fun String.timeStampToDate(): LocalDateTime? {
val instant: Instant = Instant.ofEpochSecond(this.toLong())
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault())
}
32 changes: 30 additions & 2 deletions app/src/main/java/com/root14/hoopoe/viewmodel/DetailViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.lifecycle.viewModelScope
import com.root14.hoopoe.data.model.AssetById
import com.root14.hoopoe.data.model.ChangeRate
import com.root14.hoopoe.data.model.Interval
import com.root14.hoopoe.data.model.IntervalData
import com.root14.hoopoe.data.repository.MainRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
Expand All @@ -17,6 +18,33 @@ import javax.inject.Inject
@HiltViewModel
class DetailViewModel @Inject constructor(private val mainRepository: MainRepository) :
ViewModel() {


private var _interval = MutableLiveData<Interval>()
val interval: LiveData<Interval>
get() = _interval

fun getChartIntervalData(id: String, period: Int): LiveData<Interval> {
viewModelScope.launch(Dispatchers.IO) {
mainRepository.getAssetsHistoryById(id = id, interval = "d1").let { interval ->
withContext(Dispatchers.Default) {
val result = calculateHistoryData(interval!!, period)
withContext(Dispatchers.Main) {
_interval.value = result
}
}
}
}
return interval
}

private fun calculateHistoryData(interval: Interval, period: Int): Interval {
val data = interval.data
val result = ArrayList(data.subList((data.size - period), data.size))
return Interval(data = result)
}


//TODO handle slow bandwidth loading state
private val _assets = MutableLiveData<AssetById>()
val assets: LiveData<AssetById>
Expand Down Expand Up @@ -45,8 +73,8 @@ class DetailViewModel @Inject constructor(private val mainRepository: MainReposi
val dataDaily = mainRepository.getAssetsHistoryById(id = assetId, interval = "d1")

val change7d = calculateChangePercentage(dataDaily!!, 7)
val change1m = calculateChangePercentage(dataDaily!!, 30)
val change1y = calculateChangePercentage(dataDaily!!, 364)
val change1m = calculateChangePercentage(dataDaily, 30)
val change1y = calculateChangePercentage(dataDaily, 364)
val change1h = _assets.value?.data?.changePercent24Hr

withContext(Dispatchers.Main) {
Expand Down

0 comments on commit 2d38d50

Please sign in to comment.