Skip to content

Commit

Permalink
Merge pull request #53 from RyanKoech/bug/31
Browse files Browse the repository at this point in the history
Fixes #31
  • Loading branch information
RyanKoech authored Jun 8, 2023
2 parents ecd05f4 + a85bcf8 commit c81972e
Show file tree
Hide file tree
Showing 25 changed files with 104 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fun CoinDetailsScreen(
try {
viewState.dayMarketChart.toPairList().subList(0, 11)
} catch (e : Throwable){
Timber.d(e.localizedMessage)
Timber.w(e.localizedMessage)
viewState.dayMarketChart.toPairList()
}
}
Expand All @@ -71,7 +71,7 @@ fun CoinDetailsScreen(
try {
viewState.threeMonthMarketChart.toPairList().subList(0, 167)
} catch (e : Throwable){
Timber.d(e.localizedMessage)
Timber.w(e.localizedMessage)
viewState.threeMonthMarketChart.toPairList()
}
}
Expand All @@ -80,7 +80,7 @@ fun CoinDetailsScreen(
try {
viewState.threeMonthMarketChart.toPairList().subList(0, 719)
} catch (e : Throwable){
Timber.d(e.localizedMessage)
Timber.w(e.localizedMessage)
viewState.threeMonthMarketChart.toPairList()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.ryankoech.krypto.feature_home.core.ktx
package com.ryankoech.krypto.feature_coin_list.core.ktx

import com.ryankoech.krypto.common.presentation.util.DisplayCurrency
import com.ryankoech.krypto.feature_home.data.dto.display_currency.DisplayCurrencyDto
import com.ryankoech.krypto.feature_coin_list.data.dto.display_currency.DisplayCurrencyDto

fun HashMap<DisplayCurrency, Double>.toDisplayCurrencyList() : List<DisplayCurrencyDto> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package com.ryankoech.krypto.feature_home.data.data_source.local.shared_pref
package com.ryankoech.krypto.feature_coin_list.data.data_source.local.sharedl_pref

import android.content.Context
import android.content.SharedPreferences
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.ryankoech.krypto.common.presentation.util.DisplayCurrency
import com.ryankoech.krypto.feature_home.core.ktx.toDisplayCurrencyList
import com.ryankoech.krypto.feature_home.data.dto.display_currency.DisplayCurrencyDto
import com.ryankoech.krypto.feature_coin_list.data.dto.display_currency.DisplayCurrencyDto
import com.ryankoech.krypto.feature_coin_list.core.ktx.toDisplayCurrencyList
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.coroutineScope
import timber.log.Timber
import javax.inject.Inject

class HomeLocalPref @Inject constructor(
class CoinsLocalPref @Inject constructor(
@ApplicationContext private val context : Context
) {

companion object {
private const val PREF_NAME = "feature_home_local_pref"
private const val PREF_NAME = "feature_coin_local_pref"
private const val DISPLAY_CURRENCY_KEY = "display_currency_data"
}

Expand Down Expand Up @@ -50,7 +50,7 @@ class HomeLocalPref @Inject constructor(
gson.fromJson(json, object : TypeToken<List<DisplayCurrencyDto>>() {}.type)
displayCurrencyData
}catch (e : Exception) {
Timber.e(e)
Timber.w(e)
null
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ryankoech.krypto.feature_home.data.dto.display_currency
package com.ryankoech.krypto.feature_coin_list.data.dto.display_currency

import com.ryankoech.krypto.common.presentation.util.DisplayCurrency

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.ryankoech.krypto.feature_coin_list.data.repository

import com.ryankoech.krypto.common.presentation.util.DisplayCurrency
import com.ryankoech.krypto.feature_coin_list.data.data_source.local.db.CoinDao
import com.ryankoech.krypto.feature_coin_list.data.data_source.local.sharedl_pref.CoinsLocalPref
import com.ryankoech.krypto.feature_coin_list.data.data_source.remote.CoinServiceApi
import com.ryankoech.krypto.feature_coin_list.data.dto.CoinDto
import com.ryankoech.krypto.feature_coin_list.data.dto.CoinLocalDto
import com.ryankoech.krypto.feature_coin_list.data.dto.display_currency.DisplayCurrencyDto
import com.ryankoech.krypto.feature_coin_list.domain.repository.CoinRepository
import kotlinx.coroutines.coroutineScope
import retrofit2.Response
import javax.inject.Inject

class CoinRepositoryImpl @Inject constructor(
private val api : CoinServiceApi,
private val coinsLocalPref: CoinsLocalPref,
private val dao : CoinDao
) : CoinRepository {

Expand All @@ -23,5 +28,18 @@ class CoinRepositoryImpl @Inject constructor(

override suspend fun getCoin(coinId: String): CoinLocalDto? = dao.getCoin(coinId)

override suspend fun saveDisplayCurrencyData(displayCurrencyDataMap: HashMap<DisplayCurrency, Double>) {
return coroutineScope {
coinsLocalPref.saveDisplayCurrencyData(displayCurrencyDataMap)
}
}


override suspend fun getDisplayCurrencyData(): List<DisplayCurrencyDto>? {
return coroutineScope {
coinsLocalPref.getDisplayCurrencyData()
}
}


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.ryankoech.krypto.feature_coin_list.data.repository

import com.ryankoech.krypto.common.presentation.util.DisplayCurrency
import com.ryankoech.krypto.feature_coin_list.data.dto.CoinDto
import com.ryankoech.krypto.feature_coin_list.data.dto.CoinLocalDto
import com.ryankoech.krypto.feature_coin_list.data.dto.Roi
import com.ryankoech.krypto.feature_coin_list.data.dto.display_currency.DisplayCurrencyDto
import com.ryankoech.krypto.feature_coin_list.data.dto.toLocalCoinDto
import com.ryankoech.krypto.feature_coin_list.domain.repository.CoinRepository
import retrofit2.Response
Expand Down Expand Up @@ -212,6 +214,15 @@ val FAKE_COIN_LIST = listOf(
)
)

val FakeDisplayCurrencies = listOf(
DisplayCurrencyDto(DisplayCurrency.BNB, 262.4234),
DisplayCurrencyDto(DisplayCurrency.USD, 1.0),
DisplayCurrencyDto(DisplayCurrency.ETH, 1274.1234),
DisplayCurrencyDto(DisplayCurrency.BTC, 17505.54256),
DisplayCurrencyDto(DisplayCurrency.LTC, 74.13),
).sortedBy { it.currency.ordinal }


class FakeCoinRepositoryImpl @Inject constructor() : CoinRepository {
override suspend fun getCoins(): Response<List<CoinDto>> {
return Response.success(FAKE_COIN_LIST)
Expand All @@ -224,4 +235,12 @@ class FakeCoinRepositoryImpl @Inject constructor() : CoinRepository {
}

override suspend fun getCoin(coinId: String): CoinLocalDto = FAKE_COIN_LIST.toLocalCoinDto().first()

override suspend fun saveDisplayCurrencyData(displayCurrencyDataMap: HashMap<DisplayCurrency, Double>) {
Timber.d("Saved Local DisplayCurrency Data")
}

override suspend fun getDisplayCurrencyData(): List<DisplayCurrencyDto>?{
return FakeDisplayCurrencies
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.ryankoech.krypto.feature_coin_list.domain.repository

import com.ryankoech.krypto.common.presentation.util.DisplayCurrency
import com.ryankoech.krypto.feature_coin_list.data.dto.CoinDto
import com.ryankoech.krypto.feature_coin_list.data.dto.CoinLocalDto
import com.ryankoech.krypto.feature_coin_list.data.dto.display_currency.DisplayCurrencyDto
import retrofit2.Response

interface CoinRepository {
Expand All @@ -13,4 +15,8 @@ interface CoinRepository {
suspend fun saveCoins(coins : List<CoinLocalDto>)

suspend fun getCoin(coinId : String) : CoinLocalDto?

suspend fun saveDisplayCurrencyData(displayCurrencyDataMap : HashMap<DisplayCurrency, Double>)

suspend fun getDisplayCurrencyData() : List<DisplayCurrencyDto>?
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ryankoech.krypto.feature_coin_list.domain.usecase

import com.ryankoech.krypto.common.core.util.Resource
import com.ryankoech.krypto.common.presentation.util.DisplayCurrency
import com.ryankoech.krypto.feature_coin_list.core.di.HILT_NAME_REPO_FOR_ALL
import com.ryankoech.krypto.feature_coin_list.data.dto.CoinDto
import com.ryankoech.krypto.feature_coin_list.data.dto.toCoinEntity
Expand Down Expand Up @@ -43,6 +44,7 @@ class GetCoinsUseCase @Inject constructor(
// Save to local
if(!cacheCoin.isNullOrEmpty()) {
repository.saveCoins(cacheCoin!!.toLocalCoinDto())
saveDisplayCurrencies(cacheCoin!!)
}
response
} else {
Expand Down Expand Up @@ -72,6 +74,20 @@ class GetCoinsUseCase @Inject constructor(
emit(Resource.Error(e.localizedMessage ?: "Unexpected Error Occurred."))
}
}

private suspend fun saveDisplayCurrencies(coins : List<CoinDto>) {
val displayCurrenciesMap = hashMapOf<DisplayCurrency, Double>()
displayCurrenciesMap[DisplayCurrency.USD] = 1.0
val displayCurrencies = DisplayCurrency.values().map { it.toString() }
for (coin in coins) {
val coinSymbol = coin.symbol.uppercase(Locale.ROOT)
if (displayCurrencies.contains(coinSymbol)) {
displayCurrenciesMap[DisplayCurrency.valueOf(coinSymbol)] = coin.current_price
}
}

repository.saveDisplayCurrencyData(displayCurrenciesMap)
}
companion object{
@VisibleForTesting
fun List<Coin>.sortData(sortInfo: SortInfo): List<Coin> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.ryankoech.krypto.feature_home.domain.usecase
package com.ryankoech.krypto.feature_coin_list.domain.usecase

import com.ryankoech.krypto.common.core.util.Resource
import com.ryankoech.krypto.common.presentation.util.DisplayCurrency
import com.ryankoech.krypto.feature_home.core.di.HILT_NAME_REPO_FOR_ALL
import com.ryankoech.krypto.feature_home.data.dto.display_currency.DisplayCurrencyDto
import com.ryankoech.krypto.feature_home.domain.repository.OwnedCoinsRepository
import com.ryankoech.krypto.feature_coin_list.core.di.HILT_NAME_REPO_FOR_ALL
import com.ryankoech.krypto.feature_coin_list.data.dto.display_currency.DisplayCurrencyDto
import com.ryankoech.krypto.feature_coin_list.domain.repository.CoinRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
Expand All @@ -16,14 +16,15 @@ import javax.inject.Inject
import javax.inject.Named

class GetDisplayCurrencyDataUseCase @Inject constructor(
@Named(HILT_NAME_REPO_FOR_ALL) private val repository: OwnedCoinsRepository
@Named(HILT_NAME_REPO_FOR_ALL) private val repository: CoinRepository
) {

@VisibleForTesting
internal val defaultList = listOf( DisplayCurrencyDto(DisplayCurrency.USD, 1.0))

operator fun invoke() = flow<Resource<List<DisplayCurrencyDto>>> {
val currencyData = repository.getDisplayCurrencyData() ?: defaultList
val displayCurrenciesDto = repository.getDisplayCurrencyData()
val currencyData = if (displayCurrenciesDto.isNullOrEmpty()) defaultList else displayCurrenciesDto
emit(Resource.Success(currencyData))
}.onStart {
emit(Resource.Success(defaultList))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.ryankoech.krypto.feature_home.domain.usecase
package com.ryankoech.krypto.feature_coin_list.domain.usecase

import com.ryankoech.krypto.common.core.util.Resource
import com.ryankoech.krypto.common.presentation.util.DisplayCurrency
import com.ryankoech.krypto.feature_home.core.di.HILT_NAME_REPO_FOR_ALL
import com.ryankoech.krypto.feature_home.domain.repository.OwnedCoinsRepository
import com.ryankoech.krypto.feature_coin_list.core.di.HILT_NAME_REPO_FOR_ALL
import com.ryankoech.krypto.feature_coin_list.domain.repository.CoinRepository
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Named

class SaveDisplayCurrencyDataUseCase @Inject constructor(
@Named(HILT_NAME_REPO_FOR_ALL) private val repository: OwnedCoinsRepository
@Named(HILT_NAME_REPO_FOR_ALL) private val repository: CoinRepository
) {

suspend operator fun invoke(displayCurrencyData: HashMap<DisplayCurrency, Double>) : Resource<Unit> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import com.ryankoech.krypto.common.presentation.util.DisplayCurrency
import com.ryankoech.krypto.feature_home.core.ktx.toDisplayCurrencyList
import com.ryankoech.krypto.feature_coin_list.data.data_source.local.sharedl_pref.CoinsLocalPref
import com.ryankoech.krypto.feature_coin_list.core.ktx.toDisplayCurrencyList
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
Expand All @@ -16,18 +17,18 @@ import org.junit.runner.RunWith

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class HomeLocalPrefTest {
class CoinsLocalPrefTest {

private var homeLocalPref: HomeLocalPref? = null
private var coinsLocalPref: CoinsLocalPref? = null

@Before
fun setUp() {
homeLocalPref = HomeLocalPref(ApplicationProvider.getApplicationContext<Context?>().applicationContext)
coinsLocalPref = CoinsLocalPref(ApplicationProvider.getApplicationContext<Context?>().applicationContext)
}

@After
fun tearDown() {
homeLocalPref = null
coinsLocalPref = null
}

@Test
Expand All @@ -39,10 +40,10 @@ class HomeLocalPrefTest {
DisplayCurrency.BTC to 4.0,
DisplayCurrency.BNB to 5.0,
)
homeLocalPref!!.saveDisplayCurrencyData(fakeDisplayCurrencyMap)
coinsLocalPref!!.saveDisplayCurrencyData(fakeDisplayCurrencyMap)

runBlocking {
val retrievedDisplayCurrencyList = homeLocalPref!!.getDisplayCurrencyData()
val retrievedDisplayCurrencyList = coinsLocalPref!!.getDisplayCurrencyData()
assertThat(retrievedDisplayCurrencyList).isEqualTo(fakeDisplayCurrencyMap.toDisplayCurrencyList())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import com.ryankoech.krypto.common.presentation.theme.KryptoTheme
import com.ryankoech.krypto.feature_home.data.dto.display_currency.DisplayCurrencyDto
import com.ryankoech.krypto.feature_coin_list.data.dto.display_currency.DisplayCurrencyDto
import com.ryankoech.krypto.feature_home.data.repository.FakeDisplayCurrencies
import com.ryankoech.krypto.feature_home.data.repository.FakeOwnedCoins
import com.ryankoech.krypto.feature_home.presentation.components.success.TEST_TAG_HOME_SCREEN_SUCCESS_NO_DATA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.ryankoech.krypto.feature_home.R
import com.ryankoech.krypto.feature_home.core.util.EXCEPTION_MESSAGE
import com.ryankoech.krypto.feature_home.data.dto.owned_coin.OwnedCoinDto
import com.ryankoech.krypto.feature_home.data.repository.FakeOwnedCoinsRepositoryImpl
import com.ryankoech.krypto.feature_home.domain.usecase.GetDisplayCurrencyDataUseCase
import com.ryankoech.krypto.feature_coin_list.domain.usecase.GetDisplayCurrencyDataUseCase
import com.ryankoech.krypto.feature_home.domain.usecase.GetOwnedCoinsUseCase
import com.ryankoech.krypto.feature_home.domain.usecase.WipeDatabaseUseCase
import com.ryankoech.krypto.feature_home.presentation.components.success.HOME_SCREEN_ACTION_ITEM_WIPE_WALLET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.compose.ui.test.junit4.createAndroidComposeRule
import com.ryankoech.krypto.common.presentation.theme.KryptoTheme
import com.ryankoech.krypto.common.presentation.util.DisplayCurrency
import com.ryankoech.krypto.common.presentation.util.getFormattedBalance
import com.ryankoech.krypto.feature_home.data.dto.display_currency.DisplayCurrencyDto
import com.ryankoech.krypto.feature_coin_list.data.dto.display_currency.DisplayCurrencyDto
import com.ryankoech.krypto.feature_home.data.dto.owned_coin.OwnedCoinDto
import com.ryankoech.krypto.feature_home.data.dto.owned_coin.getBalance
import com.ryankoech.krypto.feature_home.R
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.ryankoech.krypto.feature_home.data.dto.owned_coin

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.ryankoech.krypto.feature_home.data.dto.display_currency.DisplayCurrencyDto
import com.ryankoech.krypto.feature_coin_list.data.dto.display_currency.DisplayCurrencyDto

const val OWNED_COIN_DTO_TABLENAME = "owned_coin_table"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.ryankoech.krypto.feature_home.data.repository

import com.ryankoech.krypto.common.presentation.util.DisplayCurrency
import com.ryankoech.krypto.feature_home.data.dto.display_currency.DisplayCurrencyDto
import com.ryankoech.krypto.feature_coin_list.data.dto.display_currency.DisplayCurrencyDto
import com.ryankoech.krypto.feature_home.data.dto.owned_coin.OwnedCoinDto
import com.ryankoech.krypto.feature_home.domain.repository.OwnedCoinsRepository
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -61,14 +61,6 @@ val FakeOwnedCoins = listOf(
),
)

val FakeDisplayCurrencies = listOf(
DisplayCurrencyDto(DisplayCurrency.BNB, 262.4234),
DisplayCurrencyDto(DisplayCurrency.USD, 1.0),
DisplayCurrencyDto(DisplayCurrency.ETH, 1274.1234),
DisplayCurrencyDto(DisplayCurrency.BTC, 17505.54256),
DisplayCurrencyDto(DisplayCurrency.LTC, 74.13),
).sortedBy { it.currency.ordinal }

class FakeOwnedCoinsRepositoryImpl @Inject constructor() : OwnedCoinsRepository {

override suspend fun saveOwnedCoin(coin: OwnedCoinDto): String {
Expand All @@ -90,14 +82,6 @@ class FakeOwnedCoinsRepositoryImpl @Inject constructor() : OwnedCoinsRepository
}
}

override suspend fun saveDisplayCurrencyData(displayCurrencyDataMap: HashMap<DisplayCurrency, Double>) {
Timber.d("Saved Local DisplayCurrency Data")
}

override suspend fun getDisplayCurrencyData(): List<DisplayCurrencyDto>?{
return FakeDisplayCurrencies
}

override suspend fun wipeDatabase() {
Timber.d("Database cleared")
}
Expand Down
Loading

0 comments on commit c81972e

Please sign in to comment.