mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-29 11:06:23 +00:00
Migrated to locally-assigned RecipientId's.
Oh boy.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package org.thoughtcrime.securesms.recipients;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.annotation.AnyThread;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.WorkerThread;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase;
|
||||
import org.thoughtcrime.securesms.util.LRUCache;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public final class LiveRecipientCache {
|
||||
|
||||
|
||||
private final Context context;
|
||||
private final RecipientDatabase recipientDatabase;
|
||||
private final Map<RecipientId, LiveRecipient> recipients;
|
||||
private final LiveRecipient unknown;
|
||||
|
||||
private RecipientId localRecipientId;
|
||||
|
||||
@SuppressLint("UseSparseArrays")
|
||||
public LiveRecipientCache(@NonNull Context context) {
|
||||
this.context = context.getApplicationContext();
|
||||
this.recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
|
||||
this.recipients = new LRUCache<>(1000);
|
||||
this.unknown = new LiveRecipient(context, new MutableLiveData<>(), Recipient.UNKNOWN);
|
||||
}
|
||||
|
||||
@AnyThread
|
||||
synchronized @NonNull LiveRecipient getLive(@NonNull RecipientId id) {
|
||||
if (id.isUnknown()) return unknown;
|
||||
|
||||
LiveRecipient live = recipients.get(id);
|
||||
|
||||
if (live == null) {
|
||||
final LiveRecipient newLive = new LiveRecipient(context, new MutableLiveData<>(), new Recipient(id));
|
||||
|
||||
recipients.put(id, newLive);
|
||||
SignalExecutors.BOUNDED.execute(newLive::resolve);
|
||||
|
||||
live = newLive;
|
||||
}
|
||||
|
||||
return live;
|
||||
}
|
||||
|
||||
synchronized @NonNull Recipient getSelf() {
|
||||
if (localRecipientId == null) {
|
||||
localRecipientId = recipientDatabase.getOrInsertFromE164(TextSecurePreferences.getLocalNumber(context));
|
||||
}
|
||||
|
||||
return getLive(localRecipientId).resolve();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user