kopia lustrzana https://github.com/ryukoposting/Signal-Android
Consolidate local badge writes.
rodzic
ec3540e200
commit
ce2418ce9f
|
@ -1,41 +1,83 @@
|
||||||
package org.thoughtcrime.securesms.badges
|
package org.thoughtcrime.securesms.badges
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import androidx.annotation.WorkerThread
|
||||||
import io.reactivex.rxjava3.core.Completable
|
import io.reactivex.rxjava3.core.Completable
|
||||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||||
|
import org.signal.core.util.logging.Log
|
||||||
import org.thoughtcrime.securesms.badges.models.Badge
|
import org.thoughtcrime.securesms.badges.models.Badge
|
||||||
import org.thoughtcrime.securesms.database.RecipientDatabase
|
import org.thoughtcrime.securesms.database.RecipientDatabase
|
||||||
import org.thoughtcrime.securesms.database.SignalDatabase
|
import org.thoughtcrime.securesms.database.SignalDatabase
|
||||||
|
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
|
||||||
|
import org.thoughtcrime.securesms.jobs.MultiDeviceProfileContentUpdateJob
|
||||||
|
import org.thoughtcrime.securesms.jobs.RefreshOwnProfileJob
|
||||||
import org.thoughtcrime.securesms.keyvalue.SignalStore
|
import org.thoughtcrime.securesms.keyvalue.SignalStore
|
||||||
import org.thoughtcrime.securesms.recipients.Recipient
|
import org.thoughtcrime.securesms.recipients.Recipient
|
||||||
import org.thoughtcrime.securesms.storage.StorageSyncHelper
|
import org.thoughtcrime.securesms.storage.StorageSyncHelper
|
||||||
import org.thoughtcrime.securesms.util.ProfileUtil
|
import org.thoughtcrime.securesms.util.ProfileUtil
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
class BadgeRepository(context: Context) {
|
class BadgeRepository(context: Context) {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val TAG = Log.tag(BadgeRepository::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
private val context = context.applicationContext
|
private val context = context.applicationContext
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the visibility for each badge on a user's profile, and uploads them to the server.
|
||||||
|
* Does not write to the local database. The caller must either do that themselves or schedule
|
||||||
|
* a refresh own profile job.
|
||||||
|
*
|
||||||
|
* @return A list of the badges, properly modified to either visible or not visible, according to user preferences.
|
||||||
|
*/
|
||||||
|
@Throws(IOException::class)
|
||||||
|
@WorkerThread
|
||||||
|
fun setVisibilityForAllBadgesSync(
|
||||||
|
displayBadgesOnProfile: Boolean,
|
||||||
|
selfBadges: List<Badge>
|
||||||
|
): List<Badge> {
|
||||||
|
Log.d(TAG, "[setVisibilityForAllBadgesSync] Setting badge visibility...", true)
|
||||||
|
|
||||||
|
val recipientDatabase: RecipientDatabase = SignalDatabase.recipients
|
||||||
|
val badges = selfBadges.map { it.copy(visible = displayBadgesOnProfile) }
|
||||||
|
|
||||||
|
Log.d(TAG, "[setVisibilityForAllBadgesSync] Uploading profile...", true)
|
||||||
|
ProfileUtil.uploadProfileWithBadges(context, badges)
|
||||||
|
SignalStore.donationsValues().setDisplayBadgesOnProfile(displayBadgesOnProfile)
|
||||||
|
recipientDatabase.markNeedsSync(Recipient.self().id)
|
||||||
|
|
||||||
|
Log.d(TAG, "[setVisibilityForAllBadgesSync] Requesting data change sync...", true)
|
||||||
|
StorageSyncHelper.scheduleSyncForDataChange()
|
||||||
|
|
||||||
|
return badges
|
||||||
|
}
|
||||||
|
|
||||||
fun setVisibilityForAllBadges(
|
fun setVisibilityForAllBadges(
|
||||||
displayBadgesOnProfile: Boolean,
|
displayBadgesOnProfile: Boolean,
|
||||||
selfBadges: List<Badge> = Recipient.self().badges
|
selfBadges: List<Badge> = Recipient.self().badges
|
||||||
): Completable = Completable.fromAction {
|
): Completable = Completable.fromAction {
|
||||||
val recipientDatabase: RecipientDatabase = SignalDatabase.recipients
|
setVisibilityForAllBadgesSync(displayBadgesOnProfile, selfBadges)
|
||||||
val badges = selfBadges.map { it.copy(visible = displayBadgesOnProfile) }
|
|
||||||
|
|
||||||
ProfileUtil.uploadProfileWithBadges(context, badges)
|
Log.d(TAG, "[setVisibilityForAllBadges] Enqueueing profile refresh...", true)
|
||||||
SignalStore.donationsValues().setDisplayBadgesOnProfile(displayBadgesOnProfile)
|
ApplicationDependencies.getJobManager()
|
||||||
recipientDatabase.markNeedsSync(Recipient.self().id)
|
.startChain(RefreshOwnProfileJob())
|
||||||
StorageSyncHelper.scheduleSyncForDataChange()
|
.then(MultiDeviceProfileContentUpdateJob())
|
||||||
|
.enqueue()
|
||||||
recipientDatabase.setBadges(Recipient.self().id, badges)
|
|
||||||
}.subscribeOn(Schedulers.io())
|
}.subscribeOn(Schedulers.io())
|
||||||
|
|
||||||
fun setFeaturedBadge(featuredBadge: Badge): Completable = Completable.fromAction {
|
fun setFeaturedBadge(featuredBadge: Badge): Completable = Completable.fromAction {
|
||||||
val badges = Recipient.self().badges
|
val badges = Recipient.self().badges
|
||||||
val reOrderedBadges = listOf(featuredBadge.copy(visible = true)) + (badges.filterNot { it.id == featuredBadge.id })
|
val reOrderedBadges = listOf(featuredBadge.copy(visible = true)) + (badges.filterNot { it.id == featuredBadge.id })
|
||||||
|
|
||||||
|
Log.d(TAG, "[setFeaturedBadge] Uploading profile with reordered badges...", true)
|
||||||
ProfileUtil.uploadProfileWithBadges(context, reOrderedBadges)
|
ProfileUtil.uploadProfileWithBadges(context, reOrderedBadges)
|
||||||
|
|
||||||
val recipientDatabase: RecipientDatabase = SignalDatabase.recipients
|
Log.d(TAG, "[setFeaturedBadge] Enqueueing profile refresh...", true)
|
||||||
recipientDatabase.setBadges(Recipient.self().id, reOrderedBadges)
|
ApplicationDependencies.getJobManager()
|
||||||
|
.startChain(RefreshOwnProfileJob())
|
||||||
|
.then(MultiDeviceProfileContentUpdateJob())
|
||||||
|
.enqueue()
|
||||||
}.subscribeOn(Schedulers.io())
|
}.subscribeOn(Schedulers.io())
|
||||||
}
|
}
|
||||||
|
|
|
@ -234,7 +234,7 @@ public class RefreshOwnProfileJob extends BaseJob {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setProfileBadges(@Nullable List<SignalServiceProfile.Badge> badges) {
|
private void setProfileBadges(@Nullable List<SignalServiceProfile.Badge> badges) throws IOException {
|
||||||
if (badges == null) {
|
if (badges == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -334,7 +334,8 @@ public class RefreshOwnProfileJob extends BaseJob {
|
||||||
" visible.", true);
|
" visible.", true);
|
||||||
|
|
||||||
BadgeRepository badgeRepository = new BadgeRepository(context);
|
BadgeRepository badgeRepository = new BadgeRepository(context);
|
||||||
badgeRepository.setVisibilityForAllBadges(displayBadgesOnProfile, appBadges).blockingSubscribe();
|
List<Badge> updatedBadges = badgeRepository.setVisibilityForAllBadgesSync(displayBadgesOnProfile, appBadges);
|
||||||
|
SignalDatabase.recipients().setBadges(Recipient.self().getId(), updatedBadges);
|
||||||
} else {
|
} else {
|
||||||
SignalDatabase.recipients().setBadges(Recipient.self().getId(), appBadges);
|
SignalDatabase.recipients().setBadges(Recipient.self().getId(), appBadges);
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue