mirror of
https://github.com/oxen-io/session-android.git
synced 2025-06-11 02:28:34 +00:00
Fix possible crash around loading initial conversation pages.
This commit is contained in:
parent
eaf5280d99
commit
1b2f964f32
@ -61,28 +61,16 @@ class ConversationDataSource extends PositionalDataSource<MessageRecord> {
|
|||||||
|
|
||||||
MmsSmsDatabase db = DatabaseFactory.getMmsSmsDatabase(context);
|
MmsSmsDatabase db = DatabaseFactory.getMmsSmsDatabase(context);
|
||||||
List<MessageRecord> records = new ArrayList<>(params.requestedLoadSize);
|
List<MessageRecord> records = new ArrayList<>(params.requestedLoadSize);
|
||||||
|
int totalCount = db.getConversationCount(threadId);
|
||||||
|
int effectiveCount = params.requestedStartPosition;
|
||||||
|
|
||||||
if (!isInvalid()) {
|
|
||||||
try (MmsSmsDatabase.Reader reader = db.readerFor(db.getConversation(threadId, params.requestedStartPosition, params.requestedLoadSize))) {
|
try (MmsSmsDatabase.Reader reader = db.readerFor(db.getConversation(threadId, params.requestedStartPosition, params.requestedLoadSize))) {
|
||||||
MessageRecord record;
|
MessageRecord record;
|
||||||
while ((record = reader.getNext()) != null && !isInvalid()) {
|
while ((record = reader.getNext()) != null && effectiveCount < totalCount && !isInvalid()) {
|
||||||
records.add(record);
|
records.add(record);
|
||||||
|
effectiveCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Log.i(TAG, "[Initial Load] Invalidated before we could even query!");
|
|
||||||
}
|
|
||||||
|
|
||||||
int effectiveCount = records.size() + params.requestedStartPosition;
|
|
||||||
int totalCount = db.getConversationCount(threadId);
|
|
||||||
|
|
||||||
if (effectiveCount > totalCount) {
|
|
||||||
Log.w(TAG, String.format(Locale.ENGLISH, "Miscalculation! Records: %d, Start Position: %d, Total: %d. Adjusting total.",
|
|
||||||
records.size(),
|
|
||||||
params.requestedStartPosition,
|
|
||||||
totalCount));
|
|
||||||
totalCount = effectiveCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
records = ensureMultipleOfPageSize(records, params.pageSize, totalCount);
|
records = ensureMultipleOfPageSize(records, params.pageSize, totalCount);
|
||||||
|
|
||||||
@ -99,16 +87,12 @@ class ConversationDataSource extends PositionalDataSource<MessageRecord> {
|
|||||||
MmsSmsDatabase db = DatabaseFactory.getMmsSmsDatabase(context);
|
MmsSmsDatabase db = DatabaseFactory.getMmsSmsDatabase(context);
|
||||||
List<MessageRecord> records = new ArrayList<>(params.loadSize);
|
List<MessageRecord> records = new ArrayList<>(params.loadSize);
|
||||||
|
|
||||||
if (!isInvalid()) {
|
|
||||||
try (MmsSmsDatabase.Reader reader = db.readerFor(db.getConversation(threadId, params.startPosition, params.loadSize))) {
|
try (MmsSmsDatabase.Reader reader = db.readerFor(db.getConversation(threadId, params.startPosition, params.loadSize))) {
|
||||||
MessageRecord record;
|
MessageRecord record;
|
||||||
while ((record = reader.getNext()) != null && !isInvalid()) {
|
while ((record = reader.getNext()) != null && !isInvalid()) {
|
||||||
records.add(record);
|
records.add(record);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Log.i(TAG, "[Update] Invalidated before we could even query!");
|
|
||||||
}
|
|
||||||
|
|
||||||
callback.onResult(records);
|
callback.onResult(records);
|
||||||
Util.runOnMain(dataUpdateCallback::onDataUpdated);
|
Util.runOnMain(dataUpdateCallback::onDataUpdated);
|
||||||
@ -117,7 +101,7 @@ class ConversationDataSource extends PositionalDataSource<MessageRecord> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static @NonNull List<MessageRecord> ensureMultipleOfPageSize(@NonNull List<MessageRecord> records, int pageSize, int total) {
|
private static @NonNull List<MessageRecord> ensureMultipleOfPageSize(@NonNull List<MessageRecord> records, int pageSize, int total) {
|
||||||
if (records.size() != total && records.size() % pageSize != 0) {
|
if (records.size() != total && records.size() > pageSize && records.size() % pageSize != 0) {
|
||||||
int overflow = records.size() % pageSize;
|
int overflow = records.size() % pageSize;
|
||||||
return records.subList(0, records.size() - overflow);
|
return records.subList(0, records.size() - overflow);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user