-
Notifications
You must be signed in to change notification settings - Fork 2
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
17 changed files
with
1,241 additions
and
102 deletions.
There are no files selected for viewing
955 changes: 955 additions & 0 deletions
955
app/schemas/com.gemwallet.android.data.database.GemDatabase/34.json
Large diffs are not rendered by default.
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
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/gemwallet/android/data/database/PriceAlertsDao.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,36 @@ | ||
package com.gemwallet.android.data.database | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import com.gemwallet.android.data.database.entities.DbPriceAlert | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
@Dao | ||
interface PriceAlertsDao { | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun put(alerts: List<DbPriceAlert>) | ||
|
||
@Query( | ||
""" | ||
SELECT * FROM price_alerts WHERE enabled = 1 | ||
""" | ||
) | ||
fun getAlerts(): Flow<List<DbPriceAlert>> | ||
|
||
@Query( | ||
""" | ||
SELECT * FROM price_alerts WHERE asset_id = :assetId | ||
""" | ||
) | ||
fun getAlert(assetId: String): Flow<DbPriceAlert?> | ||
|
||
@Query( | ||
""" | ||
UPDATE price_alerts SET enabled = :enabled WHERE asset_id = :assetId | ||
""" | ||
) | ||
suspend fun enabled(assetId: String, enabled: Boolean) | ||
} |
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
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/gemwallet/android/data/database/entities/DbPriceAlert.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,17 @@ | ||
package com.gemwallet.android.data.database.entities | ||
|
||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import com.wallet.core.primitives.PriceAlertDirection | ||
|
||
@Entity(tableName = "price_alerts") | ||
data class DbPriceAlert( | ||
@PrimaryKey @ColumnInfo("asset_id") val assetId: String, | ||
val price: Double? = null, | ||
@ColumnInfo("price_percent_change") | ||
val pricePercentChange: Double? = null, | ||
@ColumnInfo("price_direction") | ||
val priceDirection: PriceAlertDirection? = null, | ||
val enabled: Boolean, | ||
) |
25 changes: 25 additions & 0 deletions
25
app/src/main/java/com/gemwallet/android/data/database/mappers/PriceAlertMapper.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.gemwallet.android.data.database.mappers | ||
|
||
import com.gemwallet.android.data.database.entities.DbPriceAlert | ||
import com.wallet.core.primitives.PriceAlert | ||
|
||
class PriceAlertMapper : Mapper<PriceAlert, DbPriceAlert> { | ||
override fun asDomain(entity: PriceAlert): DbPriceAlert { | ||
return DbPriceAlert( | ||
assetId = entity.assetId, | ||
price = entity.price, | ||
pricePercentChange = entity.pricePercentChange, | ||
priceDirection = entity.priceDirection, | ||
enabled = true, | ||
) | ||
} | ||
|
||
override fun asEntity(domain: DbPriceAlert): PriceAlert { | ||
return PriceAlert( | ||
assetId = domain.assetId, | ||
price = domain.price, | ||
priceDirection = domain.priceDirection, | ||
pricePercentChange = domain.pricePercentChange, | ||
) | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...src/main/java/com/gemwallet/android/data/repositories/pricealerts/PriceAlertRepository.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,89 @@ | ||
package com.gemwallet.android.data.repositories.pricealerts | ||
|
||
import com.gemwallet.android.cases.pricealerts.EnablePriceAlertCase | ||
import com.gemwallet.android.cases.pricealerts.GetPriceAlertsCase | ||
import com.gemwallet.android.cases.pricealerts.PutPriceAlertCase | ||
import com.gemwallet.android.data.config.ConfigRepository | ||
import com.gemwallet.android.data.database.PriceAlertsDao | ||
import com.gemwallet.android.data.database.entities.DbPriceAlert | ||
import com.gemwallet.android.data.database.mappers.PriceAlertMapper | ||
import com.gemwallet.android.ext.toIdentifier | ||
import com.gemwallet.android.services.GemApiClient | ||
import com.wallet.core.primitives.AssetId | ||
import com.wallet.core.primitives.PriceAlert | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.filterNotNull | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import kotlin.collections.map | ||
|
||
class PriceAlertRepository( | ||
private val gemClient: GemApiClient, | ||
private val priceAlertsDao: PriceAlertsDao, | ||
private val configRepository: ConfigRepository, | ||
private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO), | ||
) : GetPriceAlertsCase, PutPriceAlertCase, EnablePriceAlertCase { | ||
|
||
private val mapper = PriceAlertMapper() | ||
|
||
init { | ||
scope.launch(Dispatchers.IO) { sync() } | ||
} | ||
|
||
override fun getPriceAlerts(): Flow<List<PriceAlert>> { | ||
return priceAlertsDao.getAlerts().map { items -> items.map(mapper::asEntity) } | ||
} | ||
|
||
override fun getPriceAlert(assetId: AssetId): Flow<PriceAlert?> { | ||
return priceAlertsDao.getAlert(assetId.toIdentifier()).filterNotNull().map(mapper::asEntity) | ||
} | ||
|
||
override suspend fun enabled(assetId: AssetId, enabled: Boolean): Unit = withContext(Dispatchers.IO) { | ||
val assetIdentifier = assetId.toIdentifier() | ||
val priceAlert = priceAlertsDao.getAlert(assetIdentifier).firstOrNull().let { | ||
if (it == null) { | ||
priceAlertsDao.put(listOf(DbPriceAlert(assetIdentifier, enabled = true))) | ||
listOf(PriceAlert(assetIdentifier)) | ||
} else { | ||
listOf(mapper.asEntity(it)) | ||
} | ||
} | ||
priceAlertsDao.enabled(assetIdentifier, enabled) | ||
|
||
launch(Dispatchers.IO) { | ||
if (enabled) { | ||
gemClient.includePriceAlert(getDeviceId(), priceAlert) | ||
} else { | ||
gemClient.excludePriceAlert(getDeviceId(), priceAlert) | ||
} | ||
} | ||
} | ||
|
||
override suspend fun putPriceAlert(alert: PriceAlert) = withContext(Dispatchers.IO) { | ||
priceAlertsDao.put(listOf(mapper.asDomain(alert))) | ||
launch(Dispatchers.IO) { | ||
gemClient.includePriceAlert(getDeviceId(), listOf(alert)) | ||
} | ||
Unit | ||
} | ||
|
||
override fun enabled(assetId: AssetId): Flow<Boolean> { | ||
return priceAlertsDao.getAlert(assetId.toIdentifier()).map { it != null && it.enabled } | ||
} | ||
|
||
private suspend fun sync() { | ||
gemClient.includePriceAlert(getDeviceId(), getPriceAlerts().firstOrNull() ?: emptyList()) | ||
val local = priceAlertsDao.getAlerts().firstOrNull() ?: emptyList() | ||
val remote = gemClient.getPriceAlerts(getDeviceId()).getOrNull() ?: return | ||
val toExclude = remote.filter { remote -> | ||
local.firstOrNull { it.assetId == remote.assetId } == null | ||
} | ||
gemClient.excludePriceAlert(getDeviceId(), toExclude) | ||
} | ||
|
||
private fun getDeviceId() = configRepository.getDeviceId() | ||
} |
Oops, something went wrong.