-
Notifications
You must be signed in to change notification settings - Fork 10
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
65 changed files
with
435 additions
and
251 deletions.
There are no files selected for viewing
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
82 changes: 82 additions & 0 deletions
82
...src/androidTest/java/dev/shorthouse/coinwatch/data/FavouritesPreferencesRepositoryTest.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,82 @@ | ||
package dev.shorthouse.coinwatch.data | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStoreFactory | ||
import androidx.datastore.dataStoreFile | ||
import androidx.test.core.app.ApplicationProvider | ||
import com.google.common.truth.Truth.assertThat | ||
import dev.shorthouse.coinwatch.data.preferences.favourites.FavouritesPreferencesRepository | ||
import dev.shorthouse.coinwatch.data.preferences.favourites.FavouritesPreferencesSerializer | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.test.StandardTestDispatcher | ||
import kotlinx.coroutines.test.TestScope | ||
import kotlinx.coroutines.test.resetMain | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.coroutines.test.setMain | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class FavouritesPreferencesRepositoryTest { | ||
|
||
private val testContext: Context = ApplicationProvider.getApplicationContext() | ||
private val testCoroutineDispatcher = StandardTestDispatcher() | ||
private val testCoroutineScope = TestScope(testCoroutineDispatcher + Job()) | ||
|
||
private val testDataStore = DataStoreFactory.create( | ||
serializer = FavouritesPreferencesSerializer, | ||
scope = testCoroutineScope, | ||
produceFile = { testContext.dataStoreFile("test_datastore") } | ||
) | ||
|
||
// Class under test | ||
private val favouritesPreferencesRepository = FavouritesPreferencesRepository(testDataStore) | ||
|
||
@Before | ||
fun setup() { | ||
Dispatchers.setMain(testCoroutineDispatcher) | ||
} | ||
|
||
@After | ||
fun cleanup() { | ||
Dispatchers.resetMain() | ||
testCoroutineScope.cancel() | ||
} | ||
|
||
@Test | ||
fun when_isFavouritesCondensedTrue_should_updateFavouritesPreferences() = | ||
testCoroutineScope.runTest { | ||
val isCondensed = true | ||
|
||
favouritesPreferencesRepository.updateIsFavouritesCondensed( | ||
isCondensed = isCondensed | ||
) | ||
|
||
val favouritesPreferences = favouritesPreferencesRepository | ||
.favouritesPreferencesFlow | ||
.first() | ||
|
||
assertThat(favouritesPreferences.isFavouritesCondensed).isTrue() | ||
} | ||
|
||
@Test | ||
fun when_isFavouritesCondensedFalse_should_updateFavouritesPreferences() = | ||
testCoroutineScope.runTest { | ||
val isCondensed = false | ||
|
||
favouritesPreferencesRepository.updateIsFavouritesCondensed( | ||
isCondensed = isCondensed | ||
) | ||
|
||
val favouritesPreferences = favouritesPreferencesRepository | ||
.favouritesPreferencesFlow | ||
.first() | ||
|
||
assertThat(favouritesPreferences.isFavouritesCondensed).isFalse() | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
app/src/main/java/dev/shorthouse/coinwatch/data/mapper/CoinDetailsMapper.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
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
39 changes: 39 additions & 0 deletions
39
app/src/main/java/dev/shorthouse/coinwatch/data/preferences/BasePreferencesSerializer.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,39 @@ | ||
package dev.shorthouse.coinwatch.data.preferences | ||
|
||
import androidx.datastore.core.Serializer | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.json.Json | ||
import timber.log.Timber | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
|
||
abstract class BasePreferencesSerializer<T>( | ||
private val defaultInstance: () -> T, | ||
private val serializer: KSerializer<T>, | ||
) : Serializer<T> { | ||
|
||
override val defaultValue: T | ||
get() = defaultInstance() | ||
|
||
override suspend fun readFrom(input: InputStream): T { | ||
return try { | ||
Json.decodeFromString( | ||
deserializer = serializer, | ||
string = input.readBytes().decodeToString() | ||
) | ||
} catch (exception: SerializationException) { | ||
Timber.e("Error serializing preferences with ${serializer.descriptor}", exception) | ||
defaultValue | ||
} | ||
} | ||
|
||
override suspend fun writeTo(t: T, output: OutputStream) { | ||
output.write( | ||
Json.encodeToString( | ||
serializer = serializer, | ||
value = t | ||
).encodeToByteArray() | ||
) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...c/main/java/dev/shorthouse/coinwatch/data/preferences/favourites/FavouritesPreferences.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,8 @@ | ||
package dev.shorthouse.coinwatch.data.preferences.favourites | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class FavouritesPreferences( | ||
val isFavouritesCondensed: Boolean = false | ||
) |
31 changes: 31 additions & 0 deletions
31
...a/dev/shorthouse/coinwatch/data/preferences/favourites/FavouritesPreferencesRepository.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,31 @@ | ||
package dev.shorthouse.coinwatch.data.preferences.favourites | ||
|
||
import androidx.datastore.core.DataStore | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.first | ||
import timber.log.Timber | ||
import java.io.IOException | ||
import javax.inject.Inject | ||
|
||
class FavouritesPreferencesRepository @Inject constructor( | ||
private val favouritesPreferencesDataStore: DataStore<FavouritesPreferences> | ||
) { | ||
val favouritesPreferencesFlow: Flow<FavouritesPreferences> = favouritesPreferencesDataStore.data | ||
.catch { exception -> | ||
if (exception is IOException) { | ||
Timber.e("Error reading favourites preferences", exception) | ||
emit(FavouritesPreferences()) | ||
} else { | ||
throw exception | ||
} | ||
} | ||
|
||
suspend fun updateIsFavouritesCondensed(isCondensed: Boolean) { | ||
if (isCondensed != favouritesPreferencesFlow.first().isFavouritesCondensed) { | ||
favouritesPreferencesDataStore.updateData { currentPreferences -> | ||
currentPreferences.copy(isFavouritesCondensed = isCondensed) | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...a/dev/shorthouse/coinwatch/data/preferences/favourites/FavouritesPreferencesSerializer.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,8 @@ | ||
package dev.shorthouse.coinwatch.data.preferences.favourites | ||
|
||
import dev.shorthouse.coinwatch.data.preferences.BasePreferencesSerializer | ||
|
||
object FavouritesPreferencesSerializer : BasePreferencesSerializer<FavouritesPreferences>( | ||
defaultInstance = { FavouritesPreferences() }, | ||
serializer = FavouritesPreferences.serializer(), | ||
) |
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
8 changes: 8 additions & 0 deletions
8
...c/main/java/dev/shorthouse/coinwatch/data/preferences/global/UserPreferencesSerializer.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,8 @@ | ||
package dev.shorthouse.coinwatch.data.preferences.global | ||
|
||
import dev.shorthouse.coinwatch.data.preferences.BasePreferencesSerializer | ||
|
||
object UserPreferencesSerializer : BasePreferencesSerializer<UserPreferences>( | ||
defaultInstance = { UserPreferences() }, | ||
serializer = UserPreferences.serializer(), | ||
) |
2 changes: 1 addition & 1 deletion
2
app/src/main/java/dev/shorthouse/coinwatch/data/repository/chart/CoinChartRepository.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
2 changes: 1 addition & 1 deletion
2
app/src/main/java/dev/shorthouse/coinwatch/data/repository/chart/CoinChartRepositoryImpl.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
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
2 changes: 1 addition & 1 deletion
2
app/src/main/java/dev/shorthouse/coinwatch/data/repository/details/CoinDetailsRepository.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
2 changes: 1 addition & 1 deletion
2
...c/main/java/dev/shorthouse/coinwatch/data/repository/details/CoinDetailsRepositoryImpl.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
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
Oops, something went wrong.