-
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
30 changed files
with
658 additions
and
358 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/dgomesdev/exchangeapp/domain/useCases/CoinConversionUseCase.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,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) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
.../java/com/dgomesdev/exchangeapp/domain/useCases/GetLastUpdatedValuesIfNoNetworkUseCase.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,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)) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/dgomesdev/exchangeapp/domain/useCases/GetTodayValuesUseCase.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,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)) | ||
} | ||
} |
76 changes: 54 additions & 22 deletions
76
app/src/main/java/com/dgomesdev/exchangeapp/presentation/ExchangeViewModel.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,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() | ||
} |
Oops, something went wrong.