Skip to content

Commit 20389d6

Browse files
Revert "Add line chart to Flow Use Case 1"
This reverts commit 94a663b.
1 parent b0e8e2c commit 20389d6

File tree

12 files changed

+67
-80
lines changed

12 files changed

+67
-80
lines changed

.idea/jarRepositories.xml

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ dependencies {
9393
implementation "androidx.room:room-ktx:$room_version"
9494
kapt "androidx.room:room-compiler:$room_version"
9595

96-
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
97-
9896
testImplementation project(path: ':app')
9997

10098
testImplementation 'junit:junit:4.13.2'

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/base/UseCase.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ const val useCase16Description =
6060
const val useCase17Description =
6161
"#17 Perform heavy calculation on Main Thread without freezing the UI"
6262

63-
const val flowUseCase1Description = "#1 Read from database that exposes Flow, ViewModel exposes LiveData"
64-
6563
private val coroutinesUseCases =
6664
UseCaseCategory(
6765
"Coroutine Use Cases", listOf(
@@ -166,7 +164,7 @@ private val flowUseCases =
166164
"Flow Use Cases",
167165
listOf(
168166
UseCase(
169-
flowUseCase1Description,
167+
"Flow Use Case 1",
170168
FlowUseCase1Activity::class.java
171169
)
172170
)

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/flow/mock/FlowMockApi.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import retrofit2.http.GET
88

99
interface FlowMockApi {
1010

11-
@GET("current-google-stock-price")
12-
suspend fun getCurrentGoogleStockPrice(): GoogleStock
11+
@GET("current-bitcoin-price")
12+
suspend fun getCurrentBitcoinPrice(): BitcoinPrice
13+
14+
@GET("current-etherium-price")
15+
suspend fun getCurrentEtheriumPrice(): EtheriumPrice
1316
}
1417

1518
fun createFlowMockApi(interceptor: MockNetworkInterceptor): FlowMockApi {

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/flow/mock/FlowMockData.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package com.lukaslechner.coroutineusecasesonandroid.usecases.flow.mock
22

33
import kotlin.random.Random
44

5-
data class GoogleStock(val currentPriceUsd: Float)
5+
data class BitcoinPrice(val usd: Float)
66

7-
fun fakeCurrentGoogleStockPrice(): GoogleStock {
8-
val initialPrice = 2_000f
9-
val increase = Random.nextInt(100)
7+
data class EtheriumPrice(val usd: Float)
8+
9+
fun fakeCurrentBitcoinPrice(): BitcoinPrice {
10+
val initialPrice = 50_000f
11+
val increase = Random.nextInt(1000)
1012
val currentPrice = initialPrice + increase
11-
return GoogleStock(currentPrice)
13+
return BitcoinPrice(currentPrice)
1214
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package com.lukaslechner.coroutineusecasesonandroid.usecases.flow.usecase1
22

33
import android.os.Bundle
4+
import android.view.View
45
import androidx.activity.viewModels
5-
import androidx.core.content.ContextCompat
6-
import com.github.mikephil.charting.data.Entry
7-
import com.github.mikephil.charting.data.LineData
8-
import com.github.mikephil.charting.data.LineDataSet
9-
import com.lukaslechner.coroutineusecasesonandroid.R
106
import com.lukaslechner.coroutineusecasesonandroid.base.BaseActivity
11-
import com.lukaslechner.coroutineusecasesonandroid.base.flowUseCase1Description
127
import com.lukaslechner.coroutineusecasesonandroid.databinding.ActivityFlowUsecase1Binding
8+
import java.time.Instant
139

1410
class FlowUseCase1Activity : BaseActivity() {
1511

@@ -21,60 +17,23 @@ class FlowUseCase1Activity : BaseActivity() {
2117
super.onCreate(savedInstanceState)
2218
setContentView(binding.root)
2319

24-
initChart()
25-
2620
viewModel.whileTrueInCoroutine()
27-
viewModel.currentGoogleStockPrice.observe(this) { uiState ->
21+
viewModel.bitcoinPrice.observe(this) { uiState ->
2822
if (uiState != null) {
2923
render(uiState)
3024
}
3125
}
3226
}
3327

34-
private fun initChart() {
35-
val entries: ArrayList<Entry> = ArrayList()
36-
entries.add(Entry(0f, 2000f))
37-
38-
val data = LineDataSet(entries, "Google Stock Price")
39-
40-
val colorPrimary = ContextCompat.getColor(this, R.color.colorPrimary)
41-
data.color = colorPrimary
42-
data.setCircleColor(colorPrimary)
43-
44-
val lineData = LineData(data)
45-
46-
with(binding.googleStockChart) {
47-
this.data = lineData
48-
setDrawGridBackground(false)
49-
axisRight.isEnabled = false
50-
xAxis.isEnabled = false
51-
}
52-
53-
binding.googleStockChart.axisLeft.apply {
54-
axisMinimum = 1990f
55-
axisMaximum = 2100f
56-
}
57-
}
58-
5928
private fun render(uiState: UiState) {
6029
when (uiState) {
6130
is UiState.Success -> {
62-
addNewChartEntry(uiState)
31+
binding.progressBar.visibility = View.GONE
32+
binding.lastFetch.text = Instant.now().epochSecond.toString()
33+
binding.bitcoinPrice.text = uiState.bitcoinPrice.usd.toString()
6334
}
6435
}
6536
}
6637

67-
private fun addNewChartEntry(uiState: UiState.Success) {
68-
val currentLineData = binding.googleStockChart.data
69-
currentLineData.addEntry(
70-
Entry(
71-
currentLineData.entryCount.toFloat(),
72-
uiState.googleStock.currentPriceUsd
73-
), 0
74-
)
75-
binding.googleStockChart.data = currentLineData
76-
binding.googleStockChart.invalidate()
77-
}
78-
79-
override fun getToolbarTitle() = flowUseCase1Description
38+
override fun getToolbarTitle() = "Flow Use Case 1 Activity"
8039
}

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/flow/usecase1/FlowUseCase1ViewModel.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ class FlowUseCase1ViewModel(
1111
private val mockApi: FlowMockApi = mockApi()
1212
) : BaseViewModel<UiState>() {
1313

14-
val currentGoogleStockPrice = MutableLiveData<UiState>(UiState.Loading)
14+
val bitcoinPrice = MutableLiveData<UiState>(UiState.Loading)
1515

1616
fun whileTrueInCoroutine() {
1717
viewModelScope.launch {
1818
while (true) {
19-
val currentGoogleStockPrice = mockApi.getCurrentGoogleStockPrice()
20-
this@FlowUseCase1ViewModel.currentGoogleStockPrice.value = UiState.Success(currentGoogleStockPrice)
21-
delay(2000)
19+
val currentBitcoinPrice = mockApi.getCurrentBitcoinPrice()
20+
bitcoinPrice.value = UiState.Success(currentBitcoinPrice)
21+
delay(5_000)
2222
}
2323
}
2424
}

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/flow/usecase1/MockApi.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package com.lukaslechner.coroutineusecasesonandroid.usecases.flow.usecase1
22

33
import com.google.gson.Gson
44
import com.lukaslechner.coroutineusecasesonandroid.usecases.flow.mock.createFlowMockApi
5-
import com.lukaslechner.coroutineusecasesonandroid.usecases.flow.mock.fakeCurrentGoogleStockPrice
5+
import com.lukaslechner.coroutineusecasesonandroid.usecases.flow.mock.fakeCurrentBitcoinPrice
66
import com.lukaslechner.coroutineusecasesonandroid.utils.MockNetworkInterceptor
77

88
fun mockApi() =
99
createFlowMockApi(
1010
MockNetworkInterceptor()
1111
.mock(
12-
path = "http://localhost/current-google-stock-price",
13-
body = { Gson().toJson(fakeCurrentGoogleStockPrice()) },
12+
path = "http://localhost/current-bitcoin-price",
13+
body = { Gson().toJson(fakeCurrentBitcoinPrice()) },
1414
status = 200,
15-
delayInMs = 100,
15+
delayInMs = 1500,
1616
)
1717
)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.lukaslechner.coroutineusecasesonandroid.usecases.flow.usecase1
22

3-
import com.lukaslechner.coroutineusecasesonandroid.usecases.flow.mock.GoogleStock
3+
import com.lukaslechner.coroutineusecasesonandroid.usecases.flow.mock.BitcoinPrice
44

55
sealed class UiState {
66
object Loading : UiState()
7-
data class Success(val googleStock: GoogleStock) : UiState()
7+
data class Success(val bitcoinPrice: BitcoinPrice) : UiState()
88
data class Error(val message: String) : UiState()
99
}

0 commit comments

Comments
 (0)