243 lines
9.7 KiB
Java
243 lines
9.7 KiB
Java
|
package com.discord.stores;
|
||
|
|
||
|
import android.content.SharedPreferences;
|
||
|
import c.a.b.a;
|
||
|
import c.i.d.e;
|
||
|
import com.discord.app.AppLog;
|
||
|
import com.discord.models.domain.ModelMessage;
|
||
|
import com.discord.utilities.cache.SharedPreferencesProvider;
|
||
|
import com.discord.utilities.logging.Logger;
|
||
|
import com.google.gson.Gson;
|
||
|
import d0.t.h0;
|
||
|
import d0.t.n;
|
||
|
import d0.t.o;
|
||
|
import d0.t.u;
|
||
|
import d0.z.d.m;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Collection;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.HashSet;
|
||
|
import java.util.Iterator;
|
||
|
import java.util.List;
|
||
|
import java.util.Map;
|
||
|
import java.util.Set;
|
||
|
import java.util.TreeMap;
|
||
|
import rx.Observable;
|
||
|
import rx.subjects.BehaviorSubject;
|
||
|
import rx.subjects.SerializedSubject;
|
||
|
import rx.subjects.Subject;
|
||
|
/* compiled from: StoreLocalMessagesHolder.kt */
|
||
|
public final class StoreLocalMessagesHolder {
|
||
|
private boolean cacheEnabled;
|
||
|
private Map<Long, ? extends List<? extends ModelMessage>> cacheSnapshot = h0.emptyMap();
|
||
|
private final Gson gson;
|
||
|
private final HashMap<Long, TreeMap<Long, ModelMessage>> messages = new HashMap<>();
|
||
|
private final Subject<Map<Long, List<ModelMessage>>, Map<Long, List<ModelMessage>>> messagesPublisher = new SerializedSubject(BehaviorSubject.l0());
|
||
|
private Map<Long, ? extends List<? extends ModelMessage>> messagesSnapshot = h0.emptyMap();
|
||
|
private SharedPreferences sharedPreferences;
|
||
|
private final Set<Long> updatedChannels = new HashSet();
|
||
|
|
||
|
public StoreLocalMessagesHolder() {
|
||
|
e eVar = new e();
|
||
|
a.a(eVar);
|
||
|
this.gson = eVar.a();
|
||
|
}
|
||
|
|
||
|
public static /* synthetic */ void init$default(StoreLocalMessagesHolder storeLocalMessagesHolder, boolean z2, int i, Object obj) {
|
||
|
if ((i & 1) != 0) {
|
||
|
z2 = true;
|
||
|
}
|
||
|
storeLocalMessagesHolder.init(z2);
|
||
|
}
|
||
|
|
||
|
@StoreThread
|
||
|
private final void messageCacheTryPersist() {
|
||
|
if (this.cacheEnabled) {
|
||
|
HashMap hashMap = new HashMap();
|
||
|
for (Map.Entry<Long, TreeMap<Long, ModelMessage>> entry : this.messages.entrySet()) {
|
||
|
Long key = entry.getKey();
|
||
|
Collection<ModelMessage> values = entry.getValue().values();
|
||
|
m.checkNotNullExpressionValue(values, "entry.value.values");
|
||
|
hashMap.put(key, u.toList(values));
|
||
|
}
|
||
|
if (!m.areEqual(this.cacheSnapshot, hashMap)) {
|
||
|
this.cacheSnapshot = hashMap;
|
||
|
String m = this.gson.m(hashMap);
|
||
|
SharedPreferences sharedPreferences = this.sharedPreferences;
|
||
|
if (sharedPreferences == null) {
|
||
|
m.throwUninitializedPropertyAccessException("sharedPreferences");
|
||
|
}
|
||
|
sharedPreferences.edit().putString("STORE_LOCAL_MESSAGES_CACHE_V7", m).apply();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private final void publishIfUpdated(boolean z2) {
|
||
|
Collection<ModelMessage> collection;
|
||
|
if (!this.updatedChannels.isEmpty() || z2) {
|
||
|
HashMap hashMap = new HashMap(this.messagesSnapshot);
|
||
|
for (Long l : this.updatedChannels) {
|
||
|
long longValue = l.longValue();
|
||
|
Long valueOf = Long.valueOf(longValue);
|
||
|
TreeMap<Long, ModelMessage> treeMap = this.messages.get(Long.valueOf(longValue));
|
||
|
if (treeMap == null || (collection = treeMap.values()) == null) {
|
||
|
collection = n.emptyList();
|
||
|
}
|
||
|
hashMap.put(valueOf, new ArrayList(collection));
|
||
|
}
|
||
|
this.updatedChannels.clear();
|
||
|
this.messagesSnapshot = hashMap;
|
||
|
this.messagesPublisher.onNext(hashMap);
|
||
|
messageCacheTryPersist();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static /* synthetic */ void publishIfUpdated$default(StoreLocalMessagesHolder storeLocalMessagesHolder, boolean z2, int i, Object obj) {
|
||
|
if ((i & 1) != 0) {
|
||
|
z2 = false;
|
||
|
}
|
||
|
storeLocalMessagesHolder.publishIfUpdated(z2);
|
||
|
}
|
||
|
|
||
|
@StoreThread
|
||
|
public final void addMessage(ModelMessage modelMessage) {
|
||
|
m.checkNotNullParameter(modelMessage, "message");
|
||
|
long channelId = modelMessage.getChannelId();
|
||
|
TreeMap<Long, ModelMessage> treeMap = this.messages.get(Long.valueOf(channelId));
|
||
|
if (treeMap == null) {
|
||
|
treeMap = new TreeMap<>();
|
||
|
}
|
||
|
treeMap.put(Long.valueOf(modelMessage.getId()), modelMessage);
|
||
|
this.messages.put(Long.valueOf(channelId), treeMap);
|
||
|
this.updatedChannels.add(Long.valueOf(channelId));
|
||
|
publishIfUpdated$default(this, false, 1, null);
|
||
|
}
|
||
|
|
||
|
@StoreThread
|
||
|
public final void clearCache() {
|
||
|
for (Map.Entry<Long, TreeMap<Long, ModelMessage>> entry : this.messages.entrySet()) {
|
||
|
Long key = entry.getKey();
|
||
|
Set<Long> set = this.updatedChannels;
|
||
|
m.checkNotNullExpressionValue(key, "channelId");
|
||
|
set.add(key);
|
||
|
entry.getValue().clear();
|
||
|
}
|
||
|
publishIfUpdated$default(this, false, 1, null);
|
||
|
}
|
||
|
|
||
|
@StoreThread
|
||
|
public final void deleteMessage(long j, String str) {
|
||
|
Object obj;
|
||
|
m.checkNotNullParameter(str, "nonce");
|
||
|
TreeMap<Long, ModelMessage> treeMap = this.messages.get(Long.valueOf(j));
|
||
|
if (treeMap != null) {
|
||
|
m.checkNotNullExpressionValue(treeMap, "messages[channelId] ?: return");
|
||
|
Collection<ModelMessage> values = treeMap.values();
|
||
|
m.checkNotNullExpressionValue(values, "messagesForChannel.values");
|
||
|
Iterator<T> it = values.iterator();
|
||
|
while (true) {
|
||
|
if (!it.hasNext()) {
|
||
|
obj = null;
|
||
|
break;
|
||
|
}
|
||
|
obj = it.next();
|
||
|
ModelMessage modelMessage = (ModelMessage) obj;
|
||
|
m.checkNotNullExpressionValue(modelMessage, "message");
|
||
|
if (m.areEqual(modelMessage.getNonce(), str)) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
ModelMessage modelMessage2 = (ModelMessage) obj;
|
||
|
if (modelMessage2 != null) {
|
||
|
m.checkNotNullExpressionValue(modelMessage2, "messagesForChannel.value…once == nonce } ?: return");
|
||
|
deleteMessage(modelMessage2);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@StoreThread
|
||
|
public final void deleteMessage(ModelMessage modelMessage) {
|
||
|
m.checkNotNullParameter(modelMessage, "message");
|
||
|
long id2 = modelMessage.getId();
|
||
|
long channelId = modelMessage.getChannelId();
|
||
|
TreeMap<Long, ModelMessage> treeMap = this.messages.get(Long.valueOf(channelId));
|
||
|
if (treeMap != null) {
|
||
|
m.checkNotNullExpressionValue(treeMap, "messages[channelId] ?: return");
|
||
|
if (treeMap.containsKey(Long.valueOf(id2))) {
|
||
|
treeMap.remove(Long.valueOf(id2));
|
||
|
this.updatedChannels.add(Long.valueOf(channelId));
|
||
|
if (treeMap.isEmpty()) {
|
||
|
this.messages.remove(Long.valueOf(channelId));
|
||
|
}
|
||
|
}
|
||
|
publishIfUpdated$default(this, false, 1, null);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@StoreThread
|
||
|
public final List<ModelMessage> getFlattenedMessages() {
|
||
|
return o.flatten(this.messagesSnapshot.values());
|
||
|
}
|
||
|
|
||
|
@StoreThread
|
||
|
public final ModelMessage getMessage(long j, String str) {
|
||
|
Collection<ModelMessage> values;
|
||
|
m.checkNotNullParameter(str, "nonce");
|
||
|
TreeMap<Long, ModelMessage> treeMap = this.messages.get(Long.valueOf(j));
|
||
|
Object obj = null;
|
||
|
if (treeMap == null || (values = treeMap.values()) == null) {
|
||
|
return null;
|
||
|
}
|
||
|
Iterator<T> it = values.iterator();
|
||
|
while (true) {
|
||
|
if (!it.hasNext()) {
|
||
|
break;
|
||
|
}
|
||
|
Object next = it.next();
|
||
|
ModelMessage modelMessage = (ModelMessage) next;
|
||
|
m.checkNotNullExpressionValue(modelMessage, "it");
|
||
|
if (m.areEqual(modelMessage.getNonce(), str)) {
|
||
|
obj = next;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
return (ModelMessage) obj;
|
||
|
}
|
||
|
|
||
|
public final Observable<Map<Long, List<ModelMessage>>> getMessagesPublisher() {
|
||
|
return this.messagesPublisher;
|
||
|
}
|
||
|
|
||
|
@StoreThread
|
||
|
public final void init(boolean z2) {
|
||
|
if (z2) {
|
||
|
try {
|
||
|
SharedPreferences sharedPreferences = SharedPreferencesProvider.INSTANCE.get();
|
||
|
this.sharedPreferences = sharedPreferences;
|
||
|
if (sharedPreferences == null) {
|
||
|
m.throwUninitializedPropertyAccessException("sharedPreferences");
|
||
|
}
|
||
|
String string = sharedPreferences.getString("STORE_LOCAL_MESSAGES_CACHE_V7", null);
|
||
|
for (Map.Entry entry : (string != null ? (Map) this.gson.g(string, new StoreLocalMessagesHolder$init$type$1().getType()) : h0.emptyMap()).entrySet()) {
|
||
|
long longValue = ((Number) entry.getKey()).longValue();
|
||
|
this.messages.put(Long.valueOf(longValue), new TreeMap<>(ModelMessage.getSortByIds()));
|
||
|
TreeMap<Long, ModelMessage> treeMap = this.messages.get(Long.valueOf(longValue));
|
||
|
if (treeMap == null) {
|
||
|
treeMap = new TreeMap<>();
|
||
|
}
|
||
|
for (ModelMessage modelMessage : (List) entry.getValue()) {
|
||
|
treeMap.put(Long.valueOf(modelMessage.getId()), modelMessage);
|
||
|
}
|
||
|
this.messages.put(Long.valueOf(longValue), treeMap);
|
||
|
this.updatedChannels.add(Long.valueOf(longValue));
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
clearCache();
|
||
|
Logger.e$default(AppLog.g, "Error restoring cached local messages", e, null, 4, null);
|
||
|
}
|
||
|
}
|
||
|
this.cacheEnabled = z2;
|
||
|
publishIfUpdated(true);
|
||
|
}
|
||
|
}
|