Search in sources :

Example 1 with QuickSearch

use of com.hippo.ehviewer.dao.QuickSearch in project EhViewer by seven332.

the class GalleryListScene method showAddQuickSearchDialog.

private void showAddQuickSearchDialog(final List<QuickSearch> list, final ArrayAdapter<QuickSearch> adapter, final ListView listView, final TextView tip) {
    Context context = getContext2();
    final ListUrlBuilder urlBuilder = mUrlBuilder;
    if (null == context || null == urlBuilder) {
        return;
    }
    // Can't add image search as quick search
    if (ListUrlBuilder.MODE_IMAGE_SEARCH == urlBuilder.getMode()) {
        showTip(R.string.image_search_not_quick_search, LENGTH_SHORT);
        return;
    }
    // Check duplicate
    for (QuickSearch q : list) {
        if (urlBuilder.equalsQuickSearch(q)) {
            showTip(getString(R.string.duplicate_quick_search, q.name), LENGTH_SHORT);
            return;
        }
    }
    final EditTextDialogBuilder builder = new EditTextDialogBuilder(context, getSuitableTitleForUrlBuilder(context.getResources(), urlBuilder, false), getString(R.string.quick_search));
    builder.setTitle(R.string.add_quick_search_dialog_title);
    builder.setPositiveButton(android.R.string.ok, null);
    final AlertDialog dialog = builder.show();
    dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String text = builder.getText().trim();
            // Check name empty
            if (TextUtils.isEmpty(text)) {
                builder.setError(getString(R.string.name_is_empty));
                return;
            }
            // Check name duplicate
            for (QuickSearch q : list) {
                if (text.equals(q.name)) {
                    builder.setError(getString(R.string.duplicate_name));
                    return;
                }
            }
            builder.setError(null);
            dialog.dismiss();
            QuickSearch quickSearch = urlBuilder.toQuickSearch();
            quickSearch.name = text;
            EhDB.insertQuickSearch(quickSearch);
            list.add(quickSearch);
            adapter.notifyDataSetChanged();
            if (0 == list.size()) {
                tip.setVisibility(View.VISIBLE);
                listView.setVisibility(View.GONE);
            } else {
                tip.setVisibility(View.GONE);
                listView.setVisibility(View.VISIBLE);
            }
        }
    });
}
Also used : Context(android.content.Context) AlertDialog(android.support.v7.app.AlertDialog) ListUrlBuilder(com.hippo.ehviewer.client.data.ListUrlBuilder) QuickSearch(com.hippo.ehviewer.dao.QuickSearch) EditTextDialogBuilder(com.hippo.app.EditTextDialogBuilder) EasyRecyclerView(com.hippo.easyrecyclerview.EasyRecyclerView) ShowcaseView(com.github.amlcurran.showcaseview.ShowcaseView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) RecyclerView(android.support.v7.widget.RecyclerView)

Example 2 with QuickSearch

use of com.hippo.ehviewer.dao.QuickSearch in project EhViewer by seven332.

the class EhDB method importDB.

/**
 * @param file The db file
 * @return error string, null for no error
 */
public static synchronized String importDB(Context context, File file) {
    try {
        SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        int newVersion = DaoMaster.SCHEMA_VERSION;
        int oldVersion = db.getVersion();
        if (oldVersion < newVersion) {
            upgradeDB(db, oldVersion);
            db.setVersion(newVersion);
        } else if (oldVersion > newVersion) {
            return context.getString(R.string.cant_read_the_file);
        }
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession session = daoMaster.newSession();
        // Downloads
        DownloadManager manager = EhApplication.getDownloadManager(context);
        List<DownloadInfo> downloadInfoList = session.getDownloadsDao().queryBuilder().list();
        manager.addDownload(downloadInfoList);
        // Download label
        List<DownloadLabel> downloadLabelList = session.getDownloadLabelDao().queryBuilder().list();
        manager.addDownloadLabel(downloadLabelList);
        // Download dirname
        List<DownloadDirname> downloadDirnameList = session.getDownloadDirnameDao().queryBuilder().list();
        for (DownloadDirname dirname : downloadDirnameList) {
            putDownloadDirname(dirname.getGid(), dirname.getDirname());
        }
        // History
        List<HistoryInfo> historyInfoList = session.getHistoryDao().queryBuilder().list();
        putHistoryInfo(historyInfoList);
        // QuickSearch
        List<QuickSearch> quickSearchList = session.getQuickSearchDao().queryBuilder().list();
        List<QuickSearch> currentQuickSearchList = sDaoSession.getQuickSearchDao().queryBuilder().list();
        for (QuickSearch quickSearch : quickSearchList) {
            String name = quickSearch.name;
            for (QuickSearch q : currentQuickSearchList) {
                if (ObjectUtils.equal(q.name, name)) {
                    // The same name
                    name = null;
                    break;
                }
            }
            if (null == name) {
                continue;
            }
            insertQuickSearch(quickSearch);
        }
        // LocalFavorites
        List<LocalFavoriteInfo> localFavoriteInfoList = session.getLocalFavoritesDao().queryBuilder().list();
        for (LocalFavoriteInfo info : localFavoriteInfoList) {
            putLocalFavorites(info);
        }
        // Bookmarks
        // TODO
        // Filter
        List<Filter> filterList = session.getFilterDao().queryBuilder().list();
        List<Filter> currentFilterList = sDaoSession.getFilterDao().queryBuilder().list();
        for (Filter filter : filterList) {
            if (!currentFilterList.contains(filter)) {
                addFilter(filter);
            }
        }
        return null;
    } catch (Exception e) {
        // Ignore
        return context.getString(R.string.cant_read_the_file);
    }
}
Also used : LocalFavoriteInfo(com.hippo.ehviewer.dao.LocalFavoriteInfo) DownloadDirname(com.hippo.ehviewer.dao.DownloadDirname) DownloadManager(com.hippo.ehviewer.download.DownloadManager) IOException(java.io.IOException) DaoMaster(com.hippo.ehviewer.dao.DaoMaster) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Filter(com.hippo.ehviewer.dao.Filter) DownloadInfo(com.hippo.ehviewer.dao.DownloadInfo) QuickSearch(com.hippo.ehviewer.dao.QuickSearch) DownloadLabel(com.hippo.ehviewer.dao.DownloadLabel) HistoryInfo(com.hippo.ehviewer.dao.HistoryInfo) DaoSession(com.hippo.ehviewer.dao.DaoSession)

Example 3 with QuickSearch

use of com.hippo.ehviewer.dao.QuickSearch in project EhViewer by seven332.

the class EhDB method mergeOldDB.

public static void mergeOldDB(Context context) {
    sNewDB = false;
    OldDBHelper oldDBHelper = new OldDBHelper(context);
    SQLiteDatabase oldDB;
    try {
        oldDB = oldDBHelper.getReadableDatabase();
    } catch (Exception e) {
        return;
    }
    // Get GalleryInfo list
    SparseJLArray<GalleryInfo> map = new SparseJLArray<>();
    try {
        Cursor cursor = oldDB.rawQuery("select * from " + OldDBHelper.TABLE_GALLERY, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                while (!cursor.isAfterLast()) {
                    GalleryInfo gi = new GalleryInfo();
                    gi.gid = cursor.getInt(0);
                    gi.token = cursor.getString(1);
                    gi.title = cursor.getString(2);
                    gi.posted = cursor.getString(3);
                    gi.category = cursor.getInt(4);
                    gi.thumb = cursor.getString(5);
                    gi.uploader = cursor.getString(6);
                    try {
                        // In 0.6.x version, NaN is stored
                        gi.rating = cursor.getFloat(7);
                    } catch (Exception e) {
                        gi.rating = -1.0f;
                    }
                    map.put(gi.gid, gi);
                    cursor.moveToNext();
                }
            }
            cursor.close();
        }
    } catch (Exception e) {
    // Ignore
    }
    // Merge local favorites
    try {
        Cursor cursor = oldDB.rawQuery("select * from " + OldDBHelper.TABLE_LOCAL_FAVOURITE, null);
        if (cursor != null) {
            LocalFavoritesDao dao = sDaoSession.getLocalFavoritesDao();
            if (cursor.moveToFirst()) {
                long i = 0L;
                while (!cursor.isAfterLast()) {
                    // Get GalleryInfo first
                    long gid = cursor.getInt(0);
                    GalleryInfo gi = map.get(gid);
                    if (gi == null) {
                        Log.e(TAG, "Can't get GalleryInfo with gid: " + gid);
                        cursor.moveToNext();
                        continue;
                    }
                    LocalFavoriteInfo info = new LocalFavoriteInfo(gi);
                    info.setTime(i);
                    dao.insert(info);
                    cursor.moveToNext();
                    i++;
                }
            }
            cursor.close();
        }
    } catch (Exception e) {
    // Ignore
    }
    // Merge quick search
    try {
        Cursor cursor = oldDB.rawQuery("select * from " + OldDBHelper.TABLE_TAG, null);
        if (cursor != null) {
            QuickSearchDao dao = sDaoSession.getQuickSearchDao();
            if (cursor.moveToFirst()) {
                while (!cursor.isAfterLast()) {
                    QuickSearch quickSearch = new QuickSearch();
                    int mode = cursor.getInt(2);
                    String search = cursor.getString(4);
                    String tag = cursor.getString(7);
                    if (mode == ListUrlBuilder.MODE_UPLOADER && search != null && search.startsWith("uploader:")) {
                        search = search.substring("uploader:".length());
                    }
                    quickSearch.setTime((long) cursor.getInt(0));
                    quickSearch.setName(cursor.getString(1));
                    quickSearch.setMode(mode);
                    quickSearch.setCategory(cursor.getInt(3));
                    quickSearch.setKeyword(mode == ListUrlBuilder.MODE_TAG ? tag : search);
                    quickSearch.setAdvanceSearch(cursor.getInt(5));
                    quickSearch.setMinRating(cursor.getInt(6));
                    dao.insert(quickSearch);
                    cursor.moveToNext();
                }
            }
            cursor.close();
        }
    } catch (Exception e) {
    // Ignore
    }
    // Merge download info
    try {
        Cursor cursor = oldDB.rawQuery("select * from " + OldDBHelper.TABLE_DOWNLOAD, null);
        if (cursor != null) {
            DownloadsDao dao = sDaoSession.getDownloadsDao();
            if (cursor.moveToFirst()) {
                long i = 0L;
                while (!cursor.isAfterLast()) {
                    // Get GalleryInfo first
                    long gid = cursor.getInt(0);
                    GalleryInfo gi = map.get(gid);
                    if (gi == null) {
                        Log.e(TAG, "Can't get GalleryInfo with gid: " + gid);
                        cursor.moveToNext();
                        continue;
                    }
                    DownloadInfo info = new DownloadInfo(gi);
                    int state = cursor.getInt(2);
                    int legacy = cursor.getInt(3);
                    if (state == DownloadInfo.STATE_FINISH && legacy > 0) {
                        state = DownloadInfo.STATE_FAILED;
                    }
                    info.setState(state);
                    info.setLegacy(legacy);
                    if (cursor.getColumnCount() == 5) {
                        info.setTime(cursor.getLong(4));
                    } else {
                        info.setTime(i);
                    }
                    dao.insert(info);
                    cursor.moveToNext();
                    i++;
                }
            }
            cursor.close();
        }
    } catch (Exception e) {
    // Ignore
    }
    try {
        // Merge history info
        Cursor cursor = oldDB.rawQuery("select * from " + OldDBHelper.TABLE_HISTORY, null);
        if (cursor != null) {
            HistoryDao dao = sDaoSession.getHistoryDao();
            if (cursor.moveToFirst()) {
                while (!cursor.isAfterLast()) {
                    // Get GalleryInfo first
                    long gid = cursor.getInt(0);
                    GalleryInfo gi = map.get(gid);
                    if (gi == null) {
                        Log.e(TAG, "Can't get GalleryInfo with gid: " + gid);
                        cursor.moveToNext();
                        continue;
                    }
                    HistoryInfo info = new HistoryInfo(gi);
                    info.setMode(cursor.getInt(1));
                    info.setTime(cursor.getLong(2));
                    dao.insert(info);
                    cursor.moveToNext();
                }
            }
            cursor.close();
        }
    } catch (Exception e) {
    // Ignore
    }
    try {
        oldDBHelper.close();
    } catch (Exception e) {
    // Ignore
    }
}
Also used : QuickSearchDao(com.hippo.ehviewer.dao.QuickSearchDao) LocalFavoriteInfo(com.hippo.ehviewer.dao.LocalFavoriteInfo) LocalFavoritesDao(com.hippo.ehviewer.dao.LocalFavoritesDao) HistoryDao(com.hippo.ehviewer.dao.HistoryDao) GalleryInfo(com.hippo.ehviewer.client.data.GalleryInfo) Cursor(android.database.Cursor) IOException(java.io.IOException) DownloadsDao(com.hippo.ehviewer.dao.DownloadsDao) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) DownloadInfo(com.hippo.ehviewer.dao.DownloadInfo) QuickSearch(com.hippo.ehviewer.dao.QuickSearch) SparseJLArray(com.hippo.yorozuya.collect.SparseJLArray) HistoryInfo(com.hippo.ehviewer.dao.HistoryInfo)

Example 4 with QuickSearch

use of com.hippo.ehviewer.dao.QuickSearch in project EhViewer by seven332.

the class GalleryListScene method onCreateDrawerView.

@Override
public View onCreateDrawerView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.drawer_list, container, false);
    Toolbar toolbar = (Toolbar) ViewUtils.$$(view, R.id.toolbar);
    final TextView tip = (TextView) ViewUtils.$$(view, R.id.tip);
    final ListView listView = (ListView) ViewUtils.$$(view, R.id.list_view);
    Context context = getContext2();
    Assert.assertNotNull(context);
    final List<QuickSearch> list = EhDB.getAllQuickSearch();
    final ArrayAdapter<QuickSearch> adapter = new ArrayAdapter<>(context, R.layout.item_simple_list, list);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (null == mHelper || null == mUrlBuilder) {
                return;
            }
            mUrlBuilder.set(list.get(position));
            mUrlBuilder.setPageIndex(0);
            onUpdateUrlBuilder();
            mHelper.refresh();
            setState(STATE_NORMAL);
            closeDrawer(Gravity.RIGHT);
        }
    });
    tip.setText(R.string.quick_search_tip);
    toolbar.setTitle(R.string.quick_search);
    toolbar.inflateMenu(R.menu.drawer_gallery_list);
    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            int id = item.getItemId();
            switch(id) {
                case R.id.action_add:
                    if (Settings.getQuickSearchTip()) {
                        showQuickSearchTipDialog(list, adapter, listView, tip);
                    } else {
                        showAddQuickSearchDialog(list, adapter, listView, tip);
                    }
                    break;
                case R.id.action_settings:
                    startScene(new Announcer(QuickSearchScene.class));
                    break;
            }
            return true;
        }
    });
    if (0 == list.size()) {
        tip.setVisibility(View.VISIBLE);
        listView.setVisibility(View.GONE);
    } else {
        tip.setVisibility(View.GONE);
        listView.setVisibility(View.VISIBLE);
    }
    return view;
}
Also used : Context(android.content.Context) MenuItem(android.view.MenuItem) EasyRecyclerView(com.hippo.easyrecyclerview.EasyRecyclerView) ShowcaseView(com.github.amlcurran.showcaseview.ShowcaseView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) RecyclerView(android.support.v7.widget.RecyclerView) Point(android.graphics.Point) ListView(android.widget.ListView) Announcer(com.hippo.scene.Announcer) QuickSearch(com.hippo.ehviewer.dao.QuickSearch) TextView(android.widget.TextView) AdapterView(android.widget.AdapterView) ArrayAdapter(android.widget.ArrayAdapter) Toolbar(android.support.v7.widget.Toolbar)

Example 5 with QuickSearch

use of com.hippo.ehviewer.dao.QuickSearch in project EhViewer by seven332.

the class EhDB method moveQuickSearch.

public static synchronized void moveQuickSearch(int fromPosition, int toPosition) {
    if (fromPosition == toPosition) {
        return;
    }
    boolean reverse = fromPosition > toPosition;
    int offset = reverse ? toPosition : fromPosition;
    int limit = reverse ? fromPosition - toPosition + 1 : toPosition - fromPosition + 1;
    QuickSearchDao dao = sDaoSession.getQuickSearchDao();
    List<QuickSearch> list = dao.queryBuilder().orderAsc(QuickSearchDao.Properties.Time).offset(offset).limit(limit).list();
    int step = reverse ? 1 : -1;
    int start = reverse ? limit - 1 : 0;
    int end = reverse ? 0 : limit - 1;
    long toTime = list.get(end).getTime();
    for (int i = end; reverse ? i < start : i > start; i += step) {
        list.get(i).setTime(list.get(i + step).getTime());
    }
    list.get(start).setTime(toTime);
    dao.updateInTx(list);
}
Also used : QuickSearchDao(com.hippo.ehviewer.dao.QuickSearchDao) QuickSearch(com.hippo.ehviewer.dao.QuickSearch)

Aggregations

QuickSearch (com.hippo.ehviewer.dao.QuickSearch)6 Context (android.content.Context)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 RecyclerView (android.support.v7.widget.RecyclerView)2 View (android.view.View)2 AdapterView (android.widget.AdapterView)2 ListView (android.widget.ListView)2 TextView (android.widget.TextView)2 ShowcaseView (com.github.amlcurran.showcaseview.ShowcaseView)2 EasyRecyclerView (com.hippo.easyrecyclerview.EasyRecyclerView)2 DownloadInfo (com.hippo.ehviewer.dao.DownloadInfo)2 HistoryInfo (com.hippo.ehviewer.dao.HistoryInfo)2 LocalFavoriteInfo (com.hippo.ehviewer.dao.LocalFavoriteInfo)2 QuickSearchDao (com.hippo.ehviewer.dao.QuickSearchDao)2 IOException (java.io.IOException)2 Cursor (android.database.Cursor)1 Point (android.graphics.Point)1 AlertDialog (android.support.v7.app.AlertDialog)1 Toolbar (android.support.v7.widget.Toolbar)1 MenuItem (android.view.MenuItem)1