use of org.wikipedia.events.SplitLargeListsEvent in project apps-android-wikipedia by wikimedia.
the class ReadingListPageTable method importOldLists.
// TODO: Remove in Dec 2018
private void importOldLists(@NonNull SQLiteDatabase db, @NonNull List<ReadingList> lists) {
try {
try (Cursor cursor = db.query(OldReadingListContract.TABLE, null, null, null, null, null, null)) {
while (cursor.moveToNext()) {
ReadingList list = ReadingListDbHelper.instance().createList(db, OldReadingListContract.Col.TITLE.val(cursor), OldReadingListContract.Col.DESCRIPTION.val(cursor));
lists.add(list);
}
}
boolean shouldShowLargeSplitMessage = false;
try (Cursor cursor = db.rawQuery("SELECT * FROM " + OldReadingListPageContract.TABLE_PAGE + " JOIN " + OldReadingListPageContract.TABLE_DISK + " ON (" + OldReadingListPageContract.Col.KEY.qualifiedName() + " = " + OldReadingListPageContract.Col.DISK_KEY.qualifiedName() + ")", null)) {
while (cursor.moveToNext()) {
Set<String> listKeys = OldReadingListPageContract.Col.LIST_KEYS.val(cursor);
String lang = OldReadingListPageContract.Col.LANG.val(cursor);
String site = OldReadingListPageContract.Col.SITE.val(cursor);
PageTitle title = new PageTitle(OldReadingListPageContract.Col.NAMESPACE.val(cursor).toLegacyString(), OldReadingListPageContract.Col.TITLE.val(cursor), lang == null ? new WikiSite(site) : new WikiSite(site, lang));
title.setThumbUrl(OldReadingListPageContract.Col.THUMBNAIL_URL.val(cursor));
title.setDescription(OldReadingListPageContract.Col.DESCRIPTION.val(cursor));
ReadingListPage page = new ReadingListPage(title);
page.atime(OldReadingListPageContract.Col.ATIME.val(cursor));
page.mtime(OldReadingListPageContract.Col.MTIME.val(cursor));
page.sizeBytes(OldReadingListPageContract.Col.LOGICAL_SIZE.val(cursor));
long diskStatus = OldReadingListPageContract.Col.DISK_STATUS.val(cursor);
if (diskStatus == OldReadingListPageContract.STATUS_DELETED) {
continue;
} else if (diskStatus == OldReadingListPageContract.STATUS_ONLINE) {
page.offline(false);
page.status(ReadingListPage.STATUS_QUEUE_FOR_SAVE);
} else if (diskStatus == OldReadingListPageContract.STATUS_UNSAVED) {
page.offline(false);
page.status(ReadingListPage.STATUS_SAVED);
} else if (diskStatus == OldReadingListPageContract.STATUS_OUTDATED) {
page.offline(true);
page.status(ReadingListPage.STATUS_QUEUE_FOR_SAVE);
} else if (diskStatus == OldReadingListPageContract.STATUS_SAVED) {
page.offline(true);
page.status(ReadingListPage.STATUS_SAVED);
}
ReadingList origList = null;
for (ReadingList list : lists) {
if (listKeys.contains(getListKey(list.dbTitle()))) {
origList = list;
break;
}
}
if (origList == null) {
continue;
}
int splitListIndex = 0;
ReadingList newList = origList;
while (newList.pages().size() >= SiteInfoClient.getMaxPagesPerReadingList()) {
shouldShowLargeSplitMessage = true;
newList = null;
String newListName = origList.dbTitle() + " (" + Integer.toString(++splitListIndex) + ")";
for (ReadingList list : lists) {
if (list.dbTitle().equals(newListName)) {
newList = list;
break;
}
}
if (newList == null) {
newList = ReadingListDbHelper.instance().createList(db, newListName, origList.description());
lists.add(newList);
}
}
newList.pages().add(page);
}
for (ReadingList list : lists) {
ReadingListDbHelper.instance().addPagesToList(db, list, list.pages());
}
}
if (shouldShowLargeSplitMessage) {
WikipediaApp.getInstance().getBus().post(new SplitLargeListsEvent());
}
db.execSQL("DROP TABLE IF EXISTS " + OldReadingListContract.TABLE);
db.execSQL("DROP TABLE IF EXISTS " + OldReadingListPageContract.TABLE_PAGE);
db.execSQL("DROP TABLE IF EXISTS " + OldReadingListPageContract.TABLE_DISK);
db.execSQL("DROP TABLE IF EXISTS " + OldReadingListPageContract.TABLE_HTTP);
} catch (Exception e) {
L.d("Error while importing old reading lists.");
e.printStackTrace();
}
}
Aggregations