discord-jadx/app/src/main/java/com/discord/stores/StoreLocalMessagesHolder.java

240 lines
9.3 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.message.Message;
import com.discord.utilities.cache.SharedPreferencesProvider;
import com.discord.utilities.logging.Logger;
import com.discord.utilities.message.MessageUtils;
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<Message>> cacheSnapshot = h0.emptyMap();
private final Gson gson;
private final HashMap<Long, TreeMap<Long, Message>> messages = new HashMap<>();
private final Subject<Map<Long, List<Message>>, Map<Long, List<Message>>> messagesPublisher = new SerializedSubject(BehaviorSubject.k0());
private Map<Long, ? extends List<Message>> 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, Message>> entry : this.messages.entrySet()) {
Long key = entry.getKey();
Collection<Message> 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_V11", m).apply();
}
}
}
private final void publishIfUpdated(boolean z2) {
Collection<Message> 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, Message> 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(Message message) {
m.checkNotNullParameter(message, "message");
long channelId = message.getChannelId();
TreeMap<Long, Message> treeMap = this.messages.get(Long.valueOf(channelId));
if (treeMap == null) {
treeMap = new TreeMap<>();
}
treeMap.put(Long.valueOf(message.getId()), message);
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, Message>> 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, Message> treeMap = this.messages.get(Long.valueOf(j));
if (treeMap != null) {
m.checkNotNullExpressionValue(treeMap, "messages[channelId] ?: return");
Collection<Message> values = treeMap.values();
m.checkNotNullExpressionValue(values, "messagesForChannel.values");
Iterator<T> it = values.iterator();
while (true) {
if (!it.hasNext()) {
obj = null;
break;
}
obj = it.next();
if (m.areEqual(((Message) obj).getNonce(), str)) {
break;
}
}
Message message = (Message) obj;
if (message != null) {
m.checkNotNullExpressionValue(message, "messagesForChannel.value…once == nonce } ?: return");
deleteMessage(message);
}
}
}
@StoreThread
public final void deleteMessage(Message message) {
m.checkNotNullParameter(message, "message");
long id2 = message.getId();
long channelId = message.getChannelId();
TreeMap<Long, Message> 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<Message> getFlattenedMessages() {
return o.flatten(this.messagesSnapshot.values());
}
@StoreThread
public final Message getMessage(long j, String str) {
Collection<Message> values;
m.checkNotNullParameter(str, "nonce");
TreeMap<Long, Message> 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();
if (m.areEqual(((Message) next).getNonce(), str)) {
obj = next;
break;
}
}
return (Message) obj;
}
public final Observable<Map<Long, List<Message>>> 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_V11", 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<>(MessageUtils.getSORT_BY_IDS_COMPARATOR()));
TreeMap<Long, Message> treeMap = this.messages.get(Long.valueOf(longValue));
if (treeMap == null) {
treeMap = new TreeMap<>();
}
for (Message message : (List) entry.getValue()) {
treeMap.put(Long.valueOf(message.getId()), message);
}
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);
}
}