Search in sources :

Example 6 with PageTitle

use of org.wikipedia.page.PageTitle in project apps-android-wikipedia by wikimedia.

the class ReadingListFragment method addSelectedPagesToList.

private void addSelectedPagesToList() {
    List<ReadingListPage> selectedPages = getSelectedPages();
    if (!selectedPages.isEmpty()) {
        List<PageTitle> titles = new ArrayList<>();
        for (ReadingListPage page : selectedPages) {
            titles.add(ReadingListPage.toPageTitle(page));
        }
        bottomSheetPresenter.show(getChildFragmentManager(), AddToReadingListDialog.newInstance(titles, AddToReadingListDialog.InvokeSource.READING_LIST_ACTIVITY));
        update();
    }
}
Also used : PageTitle(org.wikipedia.page.PageTitle) ArrayList(java.util.ArrayList) ReadingListPage(org.wikipedia.readinglist.database.ReadingListPage)

Example 7 with PageTitle

use of org.wikipedia.page.PageTitle in project apps-android-wikipedia by wikimedia.

the class ReadingListDbHelper method addPagesToListIfNotExist.

public int addPagesToListIfNotExist(@NonNull ReadingList list, @NonNull List<PageTitle> titles) {
    SQLiteDatabase db = getWritableDatabase();
    db.beginTransaction();
    int numAdded = 0;
    try {
        for (PageTitle title : titles) {
            if (getPageByTitle(db, list, title) != null) {
                continue;
            }
            addPageToList(db, list, title);
            numAdded++;
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
    if (numAdded > 0) {
        SavedPageSyncService.enqueue();
        ReadingListSyncAdapter.manualSync();
    }
    return numAdded;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) PageTitle(org.wikipedia.page.PageTitle)

Example 8 with PageTitle

use of org.wikipedia.page.PageTitle 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();
    }
}
Also used : SplitLargeListsEvent(org.wikipedia.events.SplitLargeListsEvent) PageTitle(org.wikipedia.page.PageTitle) Cursor(android.database.Cursor) WikiSite(org.wikipedia.dataclient.WikiSite)

Example 9 with PageTitle

use of org.wikipedia.page.PageTitle in project apps-android-wikipedia by wikimedia.

the class SavedPageSyncService method savePageFor.

private long savePageFor(@NonNull ReadingListPage page) throws IOException, InterruptedException {
    PageTitle pageTitle = ReadingListPage.toPageTitle(page);
    long totalSize = 0;
    Call<PageLead> leadCall = reqPageLead(CacheControl.FORCE_NETWORK, OfflineCacheInterceptor.SAVE_HEADER_SAVE, pageTitle);
    Call<PageRemaining> sectionsCall = reqPageSections(CacheControl.FORCE_NETWORK, OfflineCacheInterceptor.SAVE_HEADER_SAVE, pageTitle);
    retrofit2.Response<PageLead> leadRsp = leadCall.execute();
    totalSize += responseSize(leadRsp);
    retrofit2.Response<PageRemaining> sectionsRsp = sectionsCall.execute();
    totalSize += responseSize(sectionsRsp);
    Set<String> imageUrls = new HashSet<>(pageImageUrlParser.parse(leadRsp.body()));
    imageUrls.addAll(pageImageUrlParser.parse(sectionsRsp.body()));
    if (!TextUtils.isEmpty(leadRsp.body().getThumbUrl())) {
        page.thumbUrl(UriUtil.resolveProtocolRelativeUrl(pageTitle.getWikiSite(), leadRsp.body().getThumbUrl()));
        persistPageThumbnail(pageTitle, page.thumbUrl());
        imageUrls.add(page.thumbUrl());
    }
    page.description(leadRsp.body().getDescription());
    if (Prefs.isImageDownloadEnabled()) {
        totalSize += reqSaveImages(pageTitle.getWikiSite(), imageUrls);
    }
    String title = pageTitle.getPrefixedText();
    L.i("Saved page " + title + " (" + totalSize + ")");
    return totalSize;
}
Also used : PageTitle(org.wikipedia.page.PageTitle) PageLead(org.wikipedia.dataclient.page.PageLead) PageRemaining(org.wikipedia.dataclient.page.PageRemaining) HashSet(java.util.HashSet)

Example 10 with PageTitle

use of org.wikipedia.page.PageTitle in project apps-android-wikipedia by wikimedia.

the class SearchResults method filter.

/**
 * Filter the list of results to make sure the original page title isn't one of them
 * and the suggestions have thumbnails if required.
 *
 * @param searchResults original results from server
 * @return filtered results
 */
@NonNull
public static SearchResults filter(SearchResults searchResults, String title, boolean requireThumbnail) {
    final boolean verbose = ReleaseUtil.isDevRelease();
    List<SearchResult> filteredResults = new ArrayList<>();
    List<SearchResult> results = searchResults.getResults();
    for (int i = 0; i < results.size() && filteredResults.size() < Constants.MAX_SUGGESTION_RESULTS; i++) {
        final SearchResult res = results.get(i);
        final PageTitle pageTitle = res.getPageTitle();
        if (verbose) {
            L.v(pageTitle.getPrefixedText());
        }
        if (!title.equalsIgnoreCase(pageTitle.getPrefixedText()) && (!requireThumbnail || pageTitle.getThumbUrl() != null) && !(pageTitle.isMainPage() || pageTitle.isDisambiguationPage())) {
            filteredResults.add(res);
        }
    }
    return new SearchResults(filteredResults, null, null);
}
Also used : ArrayList(java.util.ArrayList) PageTitle(org.wikipedia.page.PageTitle) NonNull(android.support.annotation.NonNull)

Aggregations

PageTitle (org.wikipedia.page.PageTitle)60 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)15 WikiSite (org.wikipedia.dataclient.WikiSite)11 HistoryEntry (org.wikipedia.history.HistoryEntry)11 NonNull (android.support.annotation.NonNull)6 OnClick (butterknife.OnClick)5 ReadingListPage (org.wikipedia.readinglist.database.ReadingListPage)5 IOException (java.io.IOException)4 Date (java.util.Date)4 WikipediaApp (org.wikipedia.WikipediaApp)3 PageBackStackItem (org.wikipedia.page.PageBackStackItem)3 Tab (org.wikipedia.page.tabs.Tab)3 ProgressDialog (android.app.ProgressDialog)2 Intent (android.content.Intent)2 AssetManager (android.content.res.AssetManager)2 Configuration (android.content.res.Configuration)2 Resources (android.content.res.Resources)2 Bundle (android.os.Bundle)2 DisplayMetrics (android.util.DisplayMetrics)2