Skip to content

Commit

Permalink
New version
Browse files Browse the repository at this point in the history
  • Loading branch information
dgomesdev committed Sep 29, 2023
1 parent 142a146 commit fed1828
Show file tree
Hide file tree
Showing 30 changed files with 658 additions and 358 deletions.
45 changes: 45 additions & 0 deletions .idea/appInsightsSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/migrations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ plugins {

android {
namespace = "com.dgomesdev.exchangeapp"
compileSdk = 33
compileSdk = 34

defaultConfig {
applicationId = "com.dgomesdev.exchangeapp"
minSdk = 26
targetSdk = 33
targetSdk = 34
versionCode = 1
versionName = "1.0"

Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<activity
android:name=".presentation.MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.ExchangeApp">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ interface ExchangeRepository {

suspend fun save(exchangeValues: ExchangeValues)

fun list(): Flow<List<ExchangeValues>>
fun getLastUpdatedValues(): Flow<List<ExchangeValues>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class ExchangeRepositoryImpl @Inject constructor(
class ExchangeRepositoryImpl @Inject constructor(
private val service: ExchangeService,
private val exchangeDao: ExchangeDao
) : ExchangeRepository {
Expand All @@ -19,5 +19,5 @@ class ExchangeRepositoryImpl @Inject constructor(
exchangeDao.save(exchangeValues)
}

override fun list(): Flow<List<ExchangeValues>> = exchangeDao.getAll()
override fun getLastUpdatedValues(): Flow<List<ExchangeValues>> = exchangeDao.getAll()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package com.dgomesdev.exchangeapp.domain.model

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.google.gson.annotations.SerializedName

typealias ExchangeResponse = HashMap<String, ExchangeValues>

@Entity(tableName = "coin_table")
data class ExchangeValues (
@PrimaryKey(autoGenerate = true)
val id: Int,
val code: String,
val codein: String,
@PrimaryKey
val name: String,
val bid: Double,
val convertedAmount: Double
@SerializedName("create_date")
val createDate: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.dgomesdev.exchangeapp.domain.useCases

import com.dgomesdev.exchangeapp.data.data.ExchangeRepositoryImpl
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class CoinConversionUseCase @Inject constructor(
private val repository: ExchangeRepositoryImpl,
) {

suspend operator fun invoke(coin: String) = execute(coin)

private suspend fun execute(coin: String): Flow<List<Double>> = flow {

val valuesList: MutableList<Double> = mutableListOf()
when (coin) {
"BRL" -> {
valuesList.add(1.0)
valuesList.add(repository.getExchangeValues("BRL-USD").first().bid)
valuesList.add(repository.getExchangeValues("BRL-EUR").first().bid)
}

"USD" -> {
valuesList.add(repository.getExchangeValues("USD-BRL").first().bid)
valuesList.add(1.0)
valuesList.add(repository.getExchangeValues("USD-EUR").first().bid)
}

"EUR" -> {
valuesList.add(repository.getExchangeValues("EUR-BRL").first().bid)
valuesList.add(repository.getExchangeValues("EUR-USD").first().bid)
valuesList.add(1.0)
}

else -> {
valuesList.add(1.0)
valuesList.add(1.0)
valuesList.add(1.0)
}
}
emit(valuesList)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dgomesdev.exchangeapp.domain.useCases

import com.dgomesdev.exchangeapp.data.data.ExchangeRepositoryImpl
import com.dgomesdev.exchangeapp.domain.model.ExchangeValues
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class GetLastUpdatedValuesIfNoNetworkUseCase @Inject constructor(
private val repository: ExchangeRepositoryImpl
) {

suspend operator fun invoke() = flow {

val valuesList: List<ExchangeValues> = repository.getLastUpdatedValues().first()

val bidValuesList = mutableListOf<Double>()

for (value in valuesList) {
bidValuesList.add(value.bid)
}

emit (Pair(bidValuesList, valuesList.first().createDate))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.dgomesdev.exchangeapp.domain.useCases

import android.util.Log
import com.dgomesdev.exchangeapp.data.data.ExchangeRepositoryImpl
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class GetTodayValuesUseCase @Inject constructor(
private val repository: ExchangeRepositoryImpl,
) {

suspend operator fun invoke() = flow {

val valuesList = mutableListOf<Double>()

val exchangeValuesList = listOf(
repository.getExchangeValues("USD-BRL").first(),
repository.getExchangeValues("EUR-BRL").first(),
repository.getExchangeValues("BRL-USD").first(),
repository.getExchangeValues("EUR-USD").first(),
repository.getExchangeValues("BRL-EUR").first(),
repository.getExchangeValues("USD-EUR").first()
)

for (exchangeValue in exchangeValuesList) {
try{ repository.save(exchangeValue) }
catch (error: Throwable) { Log.e("ROOM ERROR", error.message ?: "No error") }
finally { valuesList.add(exchangeValue.bid) }
}

emit(Pair(valuesList, exchangeValuesList.first().createDate))
}
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,81 @@
package com.dgomesdev.exchangeapp.presentation

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.dgomesdev.exchangeapp.data.data.ExchangeRepositoryImpl
import com.dgomesdev.exchangeapp.domain.model.ExchangeValues
import com.dgomesdev.exchangeapp.domain.useCases.CoinConversionUseCase
import com.dgomesdev.exchangeapp.domain.useCases.GetLastUpdatedValuesIfNoNetworkUseCase
import com.dgomesdev.exchangeapp.domain.useCases.GetTodayValuesUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class ExchangeViewModel @Inject constructor(
private val exchangeRepository: ExchangeRepositoryImpl
private val coinConversionUseCase: CoinConversionUseCase,
private val getTodayValuesUseCase: GetTodayValuesUseCase,
private val getLastUpdatedValuesIfNoNetworkUseCase: GetLastUpdatedValuesIfNoNetworkUseCase,
) : ViewModel() {

private val _mainState = MutableStateFlow<ExchangeValues?>(null)
val mainState: StateFlow<ExchangeValues?> = _mainState
private val _conversionValuesList = MutableStateFlow<State?>(null)
val conversionValuesList: StateFlow<State?> = _conversionValuesList

private val _historyState = MutableStateFlow<List<ExchangeValues>>(emptyList())
val historyState: StateFlow<List<ExchangeValues>> = _historyState
private val _todayValuesList = MutableStateFlow(listOf(0.0, 0.0, 0.0, 0.0, 0.0, 0.0))
val todayValuesList: StateFlow<List<Double>> = _todayValuesList

private val _lastUpdateDate = MutableStateFlow("")
val lastUpdateDate: StateFlow<String> = _lastUpdateDate

init {
getExchangeValues("USD")
getTodayValues()
}

fun getExchangeValues(coin: String) {
viewModelScope.launch {
exchangeRepository.list()
.collect{
_historyState.value = it
coinConversionUseCase(coin)
.onStart {
_conversionValuesList.value = State.Loading
}.catch {
_conversionValuesList.value = State.Failure(it)
}.collect {
_conversionValuesList.value = State.Success(it)
}
}
}

fun getExchangeValues(coins: String) = viewModelScope.launch {
exchangeRepository.getExchangeValues(coins)
.collect {
_mainState.value = it
}
}

fun saveExchangeValues(exchangeValues: ExchangeValues, convertedAmount: Double) {
private fun getTodayValues() {
viewModelScope.launch {
exchangeRepository.save(exchangeValues.copy(convertedAmount = convertedAmount))
exchangeRepository.list()
.collect {valuesList ->
_historyState.value = valuesList
getTodayValuesUseCase()
.catch {firstError ->
Log.e("FIRST ERROR", firstError.message ?: "NO FIRST ERROR")
getLastUpdatedValuesIfNoNetworkUseCase()
.catch {secondError ->
Log.e("SECOND ERROR", secondError.message ?: "NO SECOND ERROR")
_todayValuesList.value = listOf(1.0, 1.0, 1.0, 1.0, 1.0, 1.0)
}.collect {
_todayValuesList.value = it.first
_lastUpdateDate.value = it.second
}
}.collect {
_todayValuesList.value = it.first
_lastUpdateDate.value = it.second
}
}
}
}

sealed class State {
object Loading : State()
data class Success(
val valuesList: List<Double>,
) : State()

data class Failure(
val error: Throwable,
) : State()
}
Loading

0 comments on commit fed1828

Please sign in to comment.