-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate conversation rendering to the paging library.
- Loading branch information
1 parent
9ac1897
commit b750888
Showing
13 changed files
with
657 additions
and
710 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
689 changes: 376 additions & 313 deletions
689
app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationAdapter.java
Large diffs are not rendered by default.
Oops, something went wrong.
79 changes: 23 additions & 56 deletions
79
app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,49 @@ | ||
package org.thoughtcrime.securesms.conversation; | ||
|
||
import android.database.Cursor; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
public final class ConversationData { | ||
private final Cursor cursor; | ||
private final int offset; | ||
private final int limit; | ||
/** | ||
* Represents metadata about a conversation. | ||
*/ | ||
final class ConversationData { | ||
private final long lastSeen; | ||
private final int previousOffset; | ||
private final boolean firstLoad; | ||
private final boolean hasSent; | ||
private final boolean isMessageRequestAccepted; | ||
private final boolean hasPreMessageRequestMessages; | ||
private final int jumpToPosition; | ||
|
||
public ConversationData(Cursor cursor, | ||
int offset, | ||
int limit, | ||
long lastSeen, | ||
int previousOffset, | ||
boolean firstLoad, | ||
boolean hasSent, | ||
boolean isMessageRequestAccepted, | ||
boolean hasPreMessageRequestMessages) | ||
ConversationData(long lastSeen, | ||
boolean hasSent, | ||
boolean isMessageRequestAccepted, | ||
boolean hasPreMessageRequestMessages, | ||
int jumpToPosition) | ||
{ | ||
this.cursor = cursor; | ||
this.offset = offset; | ||
this.limit = limit; | ||
this.lastSeen = lastSeen; | ||
this.previousOffset = previousOffset; | ||
this.firstLoad = firstLoad; | ||
this.hasSent = hasSent; | ||
this.isMessageRequestAccepted = isMessageRequestAccepted; | ||
this.hasPreMessageRequestMessages = hasPreMessageRequestMessages; | ||
this.jumpToPosition = jumpToPosition; | ||
} | ||
|
||
public @NonNull Cursor getCursor() { | ||
return cursor; | ||
} | ||
|
||
public boolean hasLimit() { | ||
return limit > 0; | ||
} | ||
|
||
public int getLimit() { | ||
return limit; | ||
} | ||
|
||
public boolean hasOffset() { | ||
return offset > 0; | ||
} | ||
|
||
public int getOffset() { | ||
return offset; | ||
} | ||
|
||
public int getPreviousOffset() { | ||
return previousOffset; | ||
} | ||
|
||
public long getLastSeen() { | ||
long getLastSeen() { | ||
return lastSeen; | ||
} | ||
|
||
public boolean isFirstLoad() { | ||
return firstLoad; | ||
} | ||
|
||
public boolean hasSent() { | ||
boolean hasSent() { | ||
return hasSent; | ||
} | ||
|
||
public boolean isMessageRequestAccepted() { | ||
boolean isMessageRequestAccepted() { | ||
return isMessageRequestAccepted; | ||
} | ||
|
||
public boolean hasPreMessageRequestMessages() { | ||
boolean hasPreMessageRequestMessages() { | ||
return hasPreMessageRequestMessages; | ||
} | ||
|
||
boolean shouldJumpToMessage() { | ||
return jumpToPosition >= 0; | ||
} | ||
|
||
int getJumpToPosition() { | ||
return jumpToPosition; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.thoughtcrime.securesms.conversation; | ||
|
||
import android.content.Context; | ||
import android.database.ContentObserver; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.paging.DataSource; | ||
import androidx.paging.PositionalDataSource; | ||
|
||
import org.thoughtcrime.securesms.database.DatabaseContentProviders; | ||
import org.thoughtcrime.securesms.database.DatabaseFactory; | ||
import org.thoughtcrime.securesms.database.MmsSmsDatabase; | ||
import org.thoughtcrime.securesms.database.model.MessageRecord; | ||
import org.thoughtcrime.securesms.logging.Log; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Core data source for loading an individual conversation. | ||
*/ | ||
class ConversationDataSource extends PositionalDataSource<MessageRecord> { | ||
|
||
private static final String TAG = Log.tag(ConversationDataSource.class); | ||
|
||
private final Context context; | ||
private final long threadId; | ||
|
||
private ConversationDataSource(@NonNull Context context, long threadId) { | ||
this.context = context; | ||
this.threadId = threadId; | ||
|
||
ContentObserver contentObserver = new ContentObserver(null) { | ||
@Override | ||
public void onChange(boolean selfChange) { | ||
invalidate(); | ||
context.getContentResolver().unregisterContentObserver(this); | ||
} | ||
}; | ||
|
||
context.getContentResolver().registerContentObserver(DatabaseContentProviders.Conversation.getUriForThread(threadId), true, contentObserver); | ||
} | ||
|
||
@Override | ||
public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback<MessageRecord> callback) { | ||
long start = System.currentTimeMillis(); | ||
|
||
MmsSmsDatabase db = DatabaseFactory.getMmsSmsDatabase(context); | ||
List<MessageRecord> records = new ArrayList<>(params.requestedLoadSize); | ||
|
||
try (MmsSmsDatabase.Reader reader = db.readerFor(db.getConversation(threadId, params.requestedStartPosition, params.requestedLoadSize))) { | ||
MessageRecord record; | ||
while ((record = reader.getNext()) != null && !isInvalid()) { | ||
records.add(record); | ||
} | ||
} | ||
|
||
callback.onResult(records, params.requestedStartPosition, db.getConversationCount(threadId)); | ||
Log.d(TAG, "[Initial Load] " + (System.currentTimeMillis() - start) + " ms" + (isInvalid() ? " -- invalidated" : "")); | ||
} | ||
|
||
@Override | ||
public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback<MessageRecord> callback) { | ||
long start = System.currentTimeMillis(); | ||
|
||
MmsSmsDatabase db = DatabaseFactory.getMmsSmsDatabase(context); | ||
List<MessageRecord> records = new ArrayList<>(params.loadSize); | ||
|
||
try (MmsSmsDatabase.Reader reader = db.readerFor(db.getConversation(threadId, params.startPosition, params.loadSize))) { | ||
MessageRecord record; | ||
while ((record = reader.getNext()) != null && !isInvalid()) { | ||
records.add(record); | ||
} | ||
} | ||
|
||
callback.onResult(records); | ||
Log.d(TAG, "[Update] " + (System.currentTimeMillis() - start) + " ms" + (isInvalid() ? " -- invalidated" : "")); | ||
} | ||
|
||
static class Factory extends DataSource.Factory<Integer, MessageRecord> { | ||
|
||
private final Context context; | ||
private final long threadId; | ||
|
||
Factory(Context context, long threadId) { | ||
this.context = context; | ||
this.threadId = threadId; | ||
} | ||
|
||
@Override | ||
public @NonNull DataSource<Integer, MessageRecord> create() { | ||
return new ConversationDataSource(context, threadId); | ||
} | ||
} | ||
} |
Oops, something went wrong.