Fix BlacklistActivity crash after deleting items

This commit is contained in:
xynngh 2020-10-23 16:24:46 +04:00
parent 71be0cba7b
commit 88ee6b2634
2 changed files with 34 additions and 10 deletions

View File

@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixed
- Fixed importing blacklist files larger than 4000 bytes.
- Fixed occasional crash after deleting blacklist items.
## [0.5.9] - 2020-10-20

View File

@ -53,17 +53,36 @@ public class BlacklistDataSource extends PositionalDataSource<BlacklistItem> {
@NonNull LoadInitialCallback<BlacklistItem> callback) {
LOG.debug("loadInitial({}, {})", params.requestedStartPosition, params.requestedLoadSize);
List<BlacklistItem> items = getQueryBuilder()
.offset(params.requestedStartPosition)
.limit(params.requestedLoadSize)
.list();
int offset = params.requestedStartPosition;
items = blacklistDao.detach(items); // for DiffUtil to work
List<BlacklistItem> items = loadItems(offset, params.requestedLoadSize);
Integer totalCount = null;
if (items.isEmpty()) {
totalCount = (int) blacklistDao.countAll();
if (totalCount > 0) {
LOG.debug("loadInitial() initial range is empty: totalCount={}, offset={}",
totalCount, offset);
offset = (totalCount - params.requestedLoadSize)
/ params.pageSize * params.pageSize; // align to pageSize using integer math
if (offset < 0) offset = 0;
LOG.debug("loadInitial() reloading with offset={}", offset);
items = loadItems(offset, params.requestedLoadSize);
} else {
offset = 0;
}
}
if (params.placeholdersEnabled) {
callback.onResult(items, params.requestedStartPosition, (int) blacklistDao.countAll());
if (totalCount == null) totalCount = (int) blacklistDao.countAll();
callback.onResult(items, offset, totalCount);
} else {
callback.onResult(items, params.requestedStartPosition);
callback.onResult(items, offset);
}
}
@ -72,12 +91,16 @@ public class BlacklistDataSource extends PositionalDataSource<BlacklistItem> {
@NonNull LoadRangeCallback<BlacklistItem> callback) {
LOG.debug("loadRange({}, {})", params.startPosition, params.loadSize);
callback.onResult(loadItems(params.startPosition, params.loadSize));
}
private List<BlacklistItem> loadItems(int offset, int limit) {
List<BlacklistItem> items = getQueryBuilder()
.offset(params.startPosition)
.limit(params.loadSize)
.offset(offset)
.limit(limit)
.list();
callback.onResult(blacklistDao.detach(items));
return blacklistDao.detach(items); // for DiffUtil to work
}
private QueryBuilder<BlacklistItem> getQueryBuilder() {