2020-04-24 21:43:49 +00:00
|
|
|
package org.thoughtcrime.securesms.conversation;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2020-05-07 01:03:00 +00:00
|
|
|
import androidx.lifecycle.LiveData;
|
|
|
|
import androidx.lifecycle.MutableLiveData;
|
2020-04-24 21:43:49 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientUtil;
|
|
|
|
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;
|
|
|
|
import org.whispersystems.libsignal.util.Pair;
|
|
|
|
|
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
|
2020-05-07 01:03:00 +00:00
|
|
|
class ConversationRepository {
|
2020-04-24 21:43:49 +00:00
|
|
|
|
|
|
|
private final Context context;
|
|
|
|
private final Executor executor;
|
|
|
|
|
2020-05-07 01:03:00 +00:00
|
|
|
ConversationRepository() {
|
2020-04-24 21:43:49 +00:00
|
|
|
this.context = ApplicationDependencies.getApplication();
|
|
|
|
this.executor = SignalExecutors.BOUNDED;
|
|
|
|
}
|
|
|
|
|
2020-05-07 01:03:00 +00:00
|
|
|
LiveData<ConversationData> getConversationData(long threadId, long lastSeen, int jumpToPosition) {
|
|
|
|
MutableLiveData<ConversationData> liveData = new MutableLiveData<>();
|
|
|
|
|
|
|
|
executor.execute(() -> {
|
|
|
|
liveData.postValue(getConversationDataInternal(threadId, lastSeen, jumpToPosition));
|
|
|
|
});
|
|
|
|
|
|
|
|
return liveData;
|
2020-04-24 21:43:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 01:03:00 +00:00
|
|
|
private @NonNull ConversationData getConversationDataInternal(long threadId, long lastSeen, int jumpToPosition) {
|
2020-04-24 21:43:49 +00:00
|
|
|
Pair<Long, Boolean> lastSeenAndHasSent = DatabaseFactory.getThreadDatabase(context).getLastSeenAndHasSent(threadId);
|
|
|
|
|
|
|
|
boolean hasSent = lastSeenAndHasSent.second();
|
|
|
|
|
|
|
|
if (lastSeen == -1) {
|
|
|
|
lastSeen = lastSeenAndHasSent.first();
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isMessageRequestAccepted = RecipientUtil.isMessageRequestAccepted(context, threadId);
|
|
|
|
boolean hasPreMessageRequestMessages = RecipientUtil.isPreMessageRequestThread(context, threadId);
|
|
|
|
|
2020-05-07 01:03:00 +00:00
|
|
|
return new ConversationData(lastSeen, hasSent, isMessageRequestAccepted, hasPreMessageRequestMessages, jumpToPosition);
|
2020-04-24 21:43:49 +00:00
|
|
|
}
|
|
|
|
}
|