Implement several pieces of UI polish for badges.

This commit is contained in:
Alex Hart
2021-10-28 13:59:05 -03:00
committed by Greyson Parrelli
parent 186bd9db48
commit 755ec672c0
33 changed files with 364 additions and 113 deletions

View File

@@ -4,6 +4,9 @@ import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.BehaviorSubject
import io.reactivex.rxjava3.subjects.Subject
import org.signal.donations.StripeApi
import org.thoughtcrime.securesms.badges.Badges
import org.thoughtcrime.securesms.badges.models.Badge
import org.thoughtcrime.securesms.database.model.databaseprotos.BadgeList
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
import org.thoughtcrime.securesms.payments.currency.CurrencyUtil
import org.thoughtcrime.securesms.subscription.LevelUpdateOperation
@@ -25,11 +28,15 @@ internal class DonationsValues internal constructor(store: KeyValueStore) : Sign
private const val KEY_LEVEL = "donation.level"
private const val KEY_LAST_KEEP_ALIVE_LAUNCH = "donation.last.successful.ping"
private const val KEY_LAST_END_OF_PERIOD = "donation.last.end.of.period"
private const val EXPIRED_BADGE = "donation.expired.badge"
}
override fun onFirstEverAppLaunch() = Unit
override fun getKeysToIncludeInBackup(): MutableList<String> = mutableListOf(KEY_SUBSCRIPTION_CURRENCY_CODE, KEY_LAST_KEEP_ALIVE_LAUNCH)
override fun getKeysToIncludeInBackup(): MutableList<String> = mutableListOf(
KEY_CURRENCY_CODE_BOOST,
KEY_LAST_KEEP_ALIVE_LAUNCH
)
private val subscriptionCurrencyPublisher: Subject<Currency> by lazy { BehaviorSubject.createDefault(getSubscriptionCurrency()) }
val observableSubscriptionCurrency: Observable<Currency> by lazy { subscriptionCurrencyPublisher }
@@ -76,18 +83,13 @@ internal class DonationsValues internal constructor(store: KeyValueStore) : Sign
}
}
fun setSubscriptionCurrency(currency: Currency) {
putString(KEY_SUBSCRIPTION_CURRENCY_CODE, currency.currencyCode)
subscriptionCurrencyPublisher.onNext(currency)
}
fun setBoostCurrency(currency: Currency) {
putString(KEY_CURRENCY_CODE_BOOST, currency.currencyCode)
boostCurrencyPublisher.onNext(currency)
}
fun getSubscriber(): Subscriber? {
val currencyCode = getSubscriptionCurrency().currencyCode
fun getSubscriber(currency: Currency): Subscriber? {
val currencyCode = currency.currencyCode
val subscriberIdBytes = getBlob("$KEY_SUBSCRIBER_ID_PREFIX$currencyCode", null)
return if (subscriberIdBytes == null) {
@@ -97,13 +99,22 @@ internal class DonationsValues internal constructor(store: KeyValueStore) : Sign
}
}
fun getSubscriber(): Subscriber? {
return getSubscriber(getSubscriptionCurrency())
}
fun requireSubscriber(): Subscriber {
return getSubscriber() ?: throw Exception("Subscriber ID is not set.")
}
fun setSubscriber(subscriber: Subscriber) {
val currencyCode = subscriber.currencyCode
putBlob("$KEY_SUBSCRIBER_ID_PREFIX$currencyCode", subscriber.subscriberId.bytes)
store.beginWrite()
.putBlob("$KEY_SUBSCRIBER_ID_PREFIX$currencyCode", subscriber.subscriberId.bytes)
.putString(KEY_SUBSCRIPTION_CURRENCY_CODE, currencyCode)
.apply()
subscriptionCurrencyPublisher.onNext(Currency.getInstance(currencyCode))
}
fun getLevelOperation(): LevelUpdateOperation? {
@@ -133,6 +144,20 @@ internal class DonationsValues internal constructor(store: KeyValueStore) : Sign
}
}
fun setExpiredBadge(badge: Badge?) {
if (badge != null) {
putBlob(EXPIRED_BADGE, Badges.toDatabaseBadge(badge).toByteArray())
} else {
remove(EXPIRED_BADGE)
}
}
fun getExpiredBadge(): Badge? {
val badgeBytes = getBlob(EXPIRED_BADGE, null) ?: return null
return Badges.fromDatabaseBadge(BadgeList.Badge.parseFrom(badgeBytes))
}
private fun clearLevelOperation() {
remove(KEY_IDEMPOTENCY)
remove(KEY_LEVEL)