From c3e5e308ecab98352b0b7ce48b52ab0df568fd0b Mon Sep 17 00:00:00 2001 From: seven332 Date: Sun, 6 Mar 2016 01:58:39 +0800 Subject: [PATCH] Improve EhDB --- .../main/java/com/hippo/ehviewer/EhDB.java | 229 ++++++------------ .../ehviewer/download/DownloadManager.java | 20 +- .../com/hippo/ehviewer/spider/SpiderDen.java | 8 +- .../hippo/ehviewer/ui/CommonOperations.java | 6 +- .../ehviewer/ui/scene/DownloadLabelScene.java | 10 +- .../ehviewer/ui/scene/DownloadScene.java | 10 +- .../ehviewer/ui/scene/FavoritesScene.java | 2 +- .../ehviewer/daogenerator/EhDaoGenerator.java | 210 +++++++++++++--- 8 files changed, 269 insertions(+), 226 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/EhDB.java b/app/src/main/java/com/hippo/ehviewer/EhDB.java index e5aa20fe8..a4ff962ab 100644 --- a/app/src/main/java/com/hippo/ehviewer/EhDB.java +++ b/app/src/main/java/com/hippo/ehviewer/EhDB.java @@ -28,20 +28,18 @@ import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.client.data.ListUrlBuilder; import com.hippo.ehviewer.dao.DaoMaster; import com.hippo.ehviewer.dao.DaoSession; +import com.hippo.ehviewer.dao.DownloadDirname; import com.hippo.ehviewer.dao.DownloadDirnameDao; -import com.hippo.ehviewer.dao.DownloadDirnameRaw; import com.hippo.ehviewer.dao.DownloadInfo; +import com.hippo.ehviewer.dao.DownloadLabel; import com.hippo.ehviewer.dao.DownloadLabelDao; -import com.hippo.ehviewer.dao.DownloadLabelRaw; import com.hippo.ehviewer.dao.DownloadsDao; -import com.hippo.ehviewer.dao.GalleryInfoDao; -import com.hippo.ehviewer.dao.GalleryInfoRaw; -import com.hippo.ehviewer.dao.HistoryInfoDao; -import com.hippo.ehviewer.dao.HistoryInfoRaw; +import com.hippo.ehviewer.dao.HistoryDao; +import com.hippo.ehviewer.dao.HistoryInfo; +import com.hippo.ehviewer.dao.LocalFavoriteInfo; import com.hippo.ehviewer.dao.LocalFavoritesDao; -import com.hippo.ehviewer.dao.LocalFavoritesRaw; +import com.hippo.ehviewer.dao.QuickSearch; import com.hippo.ehviewer.dao.QuickSearchDao; -import com.hippo.ehviewer.dao.QuickSearchRaw; import com.hippo.yorozuya.sparse.SparseJLArray; import java.util.ArrayList; @@ -152,33 +150,6 @@ public class EhDB { // Ignore } - // Merge gallery info - try { - Cursor cursor = oldDB.rawQuery("select * from " + OldDBHelper.TABLE_GALLERY, null); - if (cursor != null) { - GalleryInfoDao dao = sDaoSession.getGalleryInfoDao(); - if (cursor.moveToFirst()) { - while (!cursor.isAfterLast()) { - GalleryInfoRaw raw = new GalleryInfoRaw(); - raw.setGid((long) cursor.getInt(0)); - raw.setToken(cursor.getString(1)); - raw.setTitle(cursor.getString(2)); - raw.setPosted(cursor.getString(3)); - raw.setCategory(cursor.getInt(4)); - raw.setThumb(cursor.getString(5)); - raw.setUploader(cursor.getString(6)); - raw.setRating(cursor.getFloat(7)); - raw.setReference(cursor.getInt(8)); - dao.insert(raw); - cursor.moveToNext(); - } - } - cursor.close(); - } - } catch (Exception e) { - // Ignore - } - // Merge local favorites try { Cursor cursor = oldDB.rawQuery("select * from " + OldDBHelper.TABLE_LOCAL_FAVOURITE, null); @@ -187,10 +158,18 @@ public class EhDB { if (cursor.moveToFirst()) { long i = 0L; while (!cursor.isAfterLast()) { - LocalFavoritesRaw raw = new LocalFavoritesRaw(); - raw.setGid((long) cursor.getInt(0)); - raw.setDate(i); - dao.insert(raw); + // 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++; } @@ -209,7 +188,7 @@ public class EhDB { QuickSearchDao dao = sDaoSession.getQuickSearchDao(); if (cursor.moveToFirst()) { while (!cursor.isAfterLast()) { - QuickSearchRaw raw = new QuickSearchRaw(); + QuickSearch quickSearch = new QuickSearch(); int mode = cursor.getInt(2); String search = cursor.getString(4); @@ -219,15 +198,15 @@ public class EhDB { search = search.substring("uploader:".length()); } - raw.setDate((long) cursor.getInt(0)); - raw.setName(cursor.getString(1)); - raw.setMode(mode); - raw.setCategory(cursor.getInt(3)); - raw.setKeyword(mode == ListUrlBuilder.MODE_TAG ? tag : search); - raw.setAdvanceSearch(cursor.getInt(5)); - raw.setMinRating(cursor.getInt(6)); + quickSearch.setDate((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(raw); + dao.insert(quickSearch); cursor.moveToNext(); } } @@ -282,14 +261,22 @@ public class EhDB { // Merge history info Cursor cursor = oldDB.rawQuery("select * from " + OldDBHelper.TABLE_HISTORY, null); if (cursor != null) { - HistoryInfoDao dao = sDaoSession.getHistoryInfoDao(); + HistoryDao dao = sDaoSession.getHistoryDao(); if (cursor.moveToFirst()) { while (!cursor.isAfterLast()) { - HistoryInfoRaw raw = new HistoryInfoRaw(); - raw.setGid((long) cursor.getInt(0)); - raw.setMode(cursor.getInt(1)); - raw.setDate(cursor.getLong(2)); - dao.insert(raw); + // 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(); + info.setMode(cursor.getInt(1)); + info.setTime(cursor.getLong(2)); + dao.insert(info); cursor.moveToNext(); } } @@ -306,63 +293,6 @@ public class EhDB { } } - private static synchronized void addGalleryInfo(GalleryInfo galleryInfo) { - GalleryInfoDao dao = sDaoSession.getGalleryInfoDao(); - GalleryInfoRaw raw = dao.load(galleryInfo.gid); - if (raw == null) { - // add new item, set reference 1 - raw = new GalleryInfoRaw(); - raw.setGid(galleryInfo.gid); - raw.setToken(galleryInfo.token); - raw.setTitle(galleryInfo.title); - raw.setThumb(galleryInfo.thumb); - raw.setCategory(galleryInfo.category); - raw.setPosted(galleryInfo.posted); - raw.setUploader(galleryInfo.uploader); - raw.setRating(galleryInfo.rating); - raw.setReference(1); - dao.insert(raw); - } else { - // already in db, add reference - raw.setReference(raw.getReference() + 1); - dao.update(raw); - } - } - - private static synchronized void removeGalleryInfo(int gid) { - GalleryInfoDao dao = sDaoSession.getGalleryInfoDao(); - GalleryInfoRaw raw = dao.load((long) gid); - int reference = raw.getReference(); - if (reference <= 1) { - // No reference, delete it - dao.deleteByKey((long) gid); - } else { - // Still has reference, sub reference - raw.setReference(reference - 1); - dao.update(raw); - } - } - - public static synchronized GalleryInfo getGalleryInfo(long gid) { - GalleryInfoDao dao = sDaoSession.getGalleryInfoDao(); - GalleryInfoRaw raw = dao.load(gid); - if (raw != null) { - GalleryInfo gi = new GalleryInfo(); - gi.gid = raw.getGid(); - gi.token = raw.getToken(); - gi.title = raw.getTitle(); - gi.thumb = raw.getThumb(); - gi.category = raw.getCategory(); - gi.posted = raw.getPosted(); - gi.uploader = raw.getUploader(); - gi.rating = raw.getRating(); - gi.generateSLang(); - return gi; - } else { - return null; - } - } - public static synchronized List getAllDownloadInfos() { DownloadsDao dao = sDaoSession.getDownloadsDao(); List list = dao.queryBuilder().orderAsc(DownloadsDao.Properties.Time).list(); @@ -394,7 +324,7 @@ public class EhDB { @Nullable public static synchronized String getDownloadDirname(long gid) { DownloadDirnameDao dao = sDaoSession.getDownloadDirnameDao(); - DownloadDirnameRaw raw = dao.load(gid); + DownloadDirname raw = dao.load(gid); if (raw != null) { return raw.getDirname(); } else { @@ -407,12 +337,12 @@ public class EhDB { */ public static synchronized void putDownloadDirname(long gid, String dirname) { DownloadDirnameDao dao = sDaoSession.getDownloadDirnameDao(); - DownloadDirnameRaw raw = dao.load(gid); + DownloadDirname raw = dao.load(gid); if (raw != null) { // Update raw.setDirname(dirname); dao.update(raw); } else { // Insert - raw = new DownloadDirnameRaw(); + raw = new DownloadDirname(); raw.setGid(gid); raw.setDirname(dirname); dao.insert(raw); @@ -425,21 +355,21 @@ public class EhDB { } @NonNull - public static synchronized List getAllDownloadLabelList() { + public static synchronized List getAllDownloadLabelList() { DownloadLabelDao dao = sDaoSession.getDownloadLabelDao(); return dao.queryBuilder().orderAsc(DownloadLabelDao.Properties.Time).list(); } - public static synchronized DownloadLabelRaw addDownloadLabel(String label) { + public static synchronized DownloadLabel addDownloadLabel(String label) { DownloadLabelDao dao = sDaoSession.getDownloadLabelDao(); - DownloadLabelRaw raw = new DownloadLabelRaw(); + DownloadLabel raw = new DownloadLabel(); raw.setLabel(label); raw.setTime(System.currentTimeMillis()); raw.setId(dao.insert(raw)); return raw; } - public static synchronized void updateDownloadLabel(DownloadLabelRaw raw) { + public static synchronized void updateDownloadLabel(DownloadLabel raw) { DownloadLabelDao dao = sDaoSession.getDownloadLabelDao(); dao.update(raw); } @@ -454,7 +384,7 @@ public class EhDB { int limit = reverse ? fromPosition - toPosition + 1 : toPosition - fromPosition + 1; DownloadLabelDao dao = sDaoSession.getDownloadLabelDao(); - List list = dao.queryBuilder().orderAsc(DownloadLabelDao.Properties.Time) + List list = dao.queryBuilder().orderAsc(DownloadLabelDao.Properties.Time) .offset(offset).limit(limit).list(); int step = reverse ? 1 : -1; @@ -469,36 +399,25 @@ public class EhDB { dao.updateInTx(list); } - public static synchronized void removeDownloadLabel(DownloadLabelRaw raw) { + public static synchronized void removeDownloadLabel(DownloadLabel raw) { DownloadLabelDao dao = sDaoSession.getDownloadLabelDao(); dao.delete(raw); } public static synchronized List getAllLocalFavorites() { LocalFavoritesDao dao = sDaoSession.getLocalFavoritesDao(); - List list = dao.queryBuilder().orderAsc(LocalFavoritesDao.Properties.Date).list(); - List result = new ArrayList<>(list.size()); - for (LocalFavoritesRaw raw: list) { - GalleryInfo gi = getGalleryInfo(raw.getGid()); - if (null == gi) { - continue; - } - result.add(gi); - } + List list = dao.queryBuilder().orderAsc(LocalFavoritesDao.Properties.Time).list(); + List result = new ArrayList<>(); + result.addAll(list); return result; } public static synchronized List searchLocalFavorites(String query) { LocalFavoritesDao dao = sDaoSession.getLocalFavoritesDao(); - List list = dao.queryBuilder().orderAsc(LocalFavoritesDao.Properties.Date).list(); - List result = new ArrayList<>(list.size()); - for (LocalFavoritesRaw raw: list) { - GalleryInfo gi = getGalleryInfo((int) (long) raw.getGid()); - if (null == gi || !gi.title.toLowerCase().contains(query.toLowerCase())) { - continue; - } - result.add(gi); - } + List list = dao.queryBuilder().orderAsc(LocalFavoritesDao.Properties.Time) + .where(LocalFavoritesDao.Properties.Title.like("%" + query+ "%")).list(); + List result = new ArrayList<>(); + result.addAll(list); return result; } @@ -518,33 +437,23 @@ public class EhDB { return null != dao.load(gid); } - public static synchronized void addLocalFavorites(GalleryInfo galleryInfo) { + public static synchronized void putLocalFavorites(GalleryInfo galleryInfo) { LocalFavoritesDao dao = sDaoSession.getLocalFavoritesDao(); - if (null != dao.load(galleryInfo.gid)) { - // Contained - return; + if (null == dao.load(galleryInfo.gid)) { + LocalFavoriteInfo info; + if (galleryInfo instanceof LocalFavoriteInfo) { + info = (LocalFavoriteInfo) galleryInfo; + } else { + info = new LocalFavoriteInfo(galleryInfo); + info.time = System.currentTimeMillis(); + } + dao.insert(info); } - - addGalleryInfo(galleryInfo); - LocalFavoritesRaw raw = new LocalFavoritesRaw(); - raw.setGid(galleryInfo.gid); - raw.setDate(System.currentTimeMillis()); - dao.insert(raw); } - public static synchronized void addLocalFavorites(List galleryInfoList) { - LocalFavoritesDao dao = sDaoSession.getLocalFavoritesDao(); + public static synchronized void putLocalFavorites(List galleryInfoList) { for (GalleryInfo gi: galleryInfoList) { - if (null != dao.load(gi.gid)) { - // Contained - continue; - } - - addGalleryInfo(gi); - LocalFavoritesRaw raw = new LocalFavoritesRaw(); - raw.setGid(gi.gid); - raw.setDate(System.currentTimeMillis()); - dao.insert(raw); + putLocalFavorites(gi); } } } diff --git a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java index 0f5ed8769..6009bf0e3 100644 --- a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java +++ b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java @@ -24,7 +24,7 @@ import android.util.Log; import com.hippo.ehviewer.EhDB; import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.dao.DownloadInfo; -import com.hippo.ehviewer.dao.DownloadLabelRaw; +import com.hippo.ehviewer.dao.DownloadLabel; import com.hippo.ehviewer.spider.SpiderQueen; import com.hippo.image.Image; import com.hippo.yorozuya.ConcurrentPool; @@ -53,7 +53,7 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { private final Map> mMap; // All labels without default label @NonNull - private final List mLabelList; + private final List mLabelList; // Store download info with default label @NonNull private final LinkedList mDefaultInfoList; @@ -82,13 +82,13 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { public DownloadManager(Context context) { mContext = context; - List labels = EhDB.getAllDownloadLabelList(); + List labels = EhDB.getAllDownloadLabelList(); mLabelList = labels; // Create list for each label HashMap> map = new HashMap<>(); mMap = map; - for (DownloadLabelRaw label : labels) { + for (DownloadLabel label : labels) { map.put(label.getLabel(), new LinkedList()); } // Create default for non tag @@ -136,7 +136,7 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { return false; } - for (DownloadLabelRaw raw: mLabelList) { + for (DownloadLabel raw: mLabelList) { if (label.equals(raw.getLabel())) { return true; } @@ -150,7 +150,7 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { } @NonNull - public List getLabelList() { + public List getLabelList() { return mLabelList; } @@ -585,7 +585,7 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { } public void moveLabel(int fromPosition, int toPosition) { - final DownloadLabelRaw item = mLabelList.remove(fromPosition); + final DownloadLabel item = mLabelList.remove(fromPosition); mLabelList.add(toPosition, item); EhDB.moveDownloadLabel(fromPosition, toPosition); @@ -597,7 +597,7 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { public void renameLabel(@NonNull String from, @NonNull String to) { // Find in label list boolean found = false; - for (DownloadLabelRaw raw: mLabelList) { + for (DownloadLabel raw: mLabelList) { if (from.equals(raw.getLabel())) { found = true; raw.setLabel(to); @@ -633,8 +633,8 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { public void deleteLabel(@NonNull String label) { // Find in label list and remove boolean found = false; - for (Iterator iterator = mLabelList.iterator(); iterator.hasNext();) { - DownloadLabelRaw raw = iterator.next(); + for (Iterator iterator = mLabelList.iterator(); iterator.hasNext();) { + DownloadLabel raw = iterator.next(); if (label.equals(raw.getLabel())) { found = true; iterator.remove(); diff --git a/app/src/main/java/com/hippo/ehviewer/spider/SpiderDen.java b/app/src/main/java/com/hippo/ehviewer/spider/SpiderDen.java index 7763673eb..59cc1f6bb 100644 --- a/app/src/main/java/com/hippo/ehviewer/spider/SpiderDen.java +++ b/app/src/main/java/com/hippo/ehviewer/spider/SpiderDen.java @@ -65,13 +65,9 @@ public final class SpiderDen { // Read from DB String dirname = EhDB.getDownloadDirname(galleryInfo.gid); if (dirname == null) { - GalleryInfo info = EhDB.getGalleryInfo(galleryInfo.gid); - if (info == null) { - info = galleryInfo; - } - dirname = FileUtils.sanitizeFilename(info.gid + "-" + info.title); + dirname = FileUtils.sanitizeFilename(galleryInfo.gid + "-" + galleryInfo.title); // Put into DB - EhDB.putDownloadDirname(info.gid, dirname); + EhDB.putDownloadDirname(galleryInfo.gid, dirname); } return dir.subFile(dirname); } else { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java index d0095d985..a73215e0c 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java @@ -28,7 +28,7 @@ import com.hippo.ehviewer.Settings; import com.hippo.ehviewer.client.EhClient; import com.hippo.ehviewer.client.EhRequest; import com.hippo.ehviewer.client.data.GalleryInfo; -import com.hippo.ehviewer.dao.DownloadLabelRaw; +import com.hippo.ehviewer.dao.DownloadLabel; import com.hippo.ehviewer.download.DownloadManager; import com.hippo.ehviewer.download.DownloadService; @@ -39,7 +39,7 @@ public final class CommonOperations { private static void doAddToFavorites(Activity activity, GalleryInfo galleryInfo, int slot, EhClient.Callback listener) { if (slot == -1) { - EhDB.addLocalFavorites(galleryInfo); + EhDB.putLocalFavorites(galleryInfo); listener.onSuccess(null); } else if (slot >= 0 && slot <= 9) { EhClient client = EhApplication.getEhClient(activity); @@ -110,7 +110,7 @@ public final class CommonOperations { activity.startService(intent); } else { // Let use chose label - List list = dm.getLabelList(); + List list = dm.getLabelList(); final String[] items = new String[list.size() + 1]; items[0] = activity.getString(R.string.default_download_label_name); for (int i = 0, n = list.size(); i < n; i++) { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadLabelScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadLabelScene.java index d01c66c08..f8a179d5a 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadLabelScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadLabelScene.java @@ -49,7 +49,7 @@ import com.hippo.app.EditTextDialogBuilder; import com.hippo.easyrecyclerview.EasyRecyclerView; import com.hippo.ehviewer.EhApplication; import com.hippo.ehviewer.R; -import com.hippo.ehviewer.dao.DownloadLabelRaw; +import com.hippo.ehviewer.dao.DownloadLabel; import com.hippo.util.ActivityHelper; import com.hippo.view.ViewTransition; import com.hippo.yorozuya.ViewUtils; @@ -69,10 +69,10 @@ public class DownloadLabelScene extends ToolbarScene { private RecyclerView.Adapter mAdapter; @Nullable - public List mList = null; + public List mList = null; @NonNull - public final Map mPinState = new HashMap<>(); + public final Map mPinState = new HashMap<>(); @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -216,7 +216,7 @@ public class DownloadLabelScene extends ToolbarScene { return; } - DownloadLabelRaw raw = mList.get(mIndex); + DownloadLabel raw = mList.get(mIndex); EhApplication.getDownloadManager(getContext()).deleteLabel(raw.getLabel()); mPinState.remove(raw); if (mAdapter != null) { @@ -351,7 +351,7 @@ public class DownloadLabelScene extends ToolbarScene { .setPositiveButton(android.R.string.ok, helper) .show(); } else if (label == v) { - DownloadLabelRaw raw = mList.get(index); + DownloadLabel raw = mList.get(index); EditTextDialogBuilder builder = new EditTextDialogBuilder( getContext(), raw.getLabel(), getString(R.string.label)); builder.setTitle(R.string.rename_label_title); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java index 244516cbf..f7f9dba46 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java @@ -59,7 +59,7 @@ import com.hippo.ehviewer.client.EhCacheKeyFactory; import com.hippo.ehviewer.client.EhUtils; import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.dao.DownloadInfo; -import com.hippo.ehviewer.dao.DownloadLabelRaw; +import com.hippo.ehviewer.dao.DownloadLabel; import com.hippo.ehviewer.download.DownloadManager; import com.hippo.ehviewer.download.DownloadService; import com.hippo.ehviewer.spider.SpiderDen; @@ -310,7 +310,7 @@ public class DownloadScene extends ToolbarScene return true; case R.id.action_default_download_label: DownloadManager dm = EhApplication.getDownloadManager(getContext()); - List list = dm.getLabelList(); + List list = dm.getLabelList(); final String[] items = new String[list.size() + 2]; items[0] = getString(R.string.let_me_select); items[1] = getString(R.string.default_download_label_name); @@ -342,11 +342,11 @@ public class DownloadScene extends ToolbarScene } }); - List list = EhApplication.getDownloadManager(getContext()).getLabelList(); + List list = EhApplication.getDownloadManager(getContext()).getLabelList(); final List labels = new ArrayList<>(list.size() + 1); // Add default label name labels.add(getString(R.string.default_download_label_name)); - for (DownloadLabelRaw raw: list) { + for (DownloadLabel raw: list) { labels.add(raw.getLabel()); } @@ -509,7 +509,7 @@ public class DownloadScene extends ToolbarScene break; } case 4: {// Move - List labelRawList = EhApplication.getDownloadManager(getContext()).getLabelList(); + List labelRawList = EhApplication.getDownloadManager(getContext()).getLabelList(); List labelList = new ArrayList<>(labelRawList.size() + 1); labelList.add(getString(R.string.default_download_label_name)); for (int i = 0, n = labelRawList.size(); i < n; i++) { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java index 045278485..fbe629dfd 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java @@ -857,7 +857,7 @@ public class FavoritesScene extends BaseScene implements mModifyAdd = true; mHelper.refresh(); } else if (dstCat == FavListUrlBuilder.FAV_CAT_LOCAL) { // Move from cloud to local - EhDB.addLocalFavorites(mModifyGiList); + EhDB.putLocalFavorites(mModifyGiList); mEnableModify = true; mModifyFavCat = -1; mModifyAdd = false; diff --git a/daogenerator/src/main/java/com/hippo/ehviewer/daogenerator/EhDaoGenerator.java b/daogenerator/src/main/java/com/hippo/ehviewer/daogenerator/EhDaoGenerator.java index ab7b96dce..4dfa5e25e 100644 --- a/daogenerator/src/main/java/com/hippo/ehviewer/daogenerator/EhDaoGenerator.java +++ b/daogenerator/src/main/java/com/hippo/ehviewer/daogenerator/EhDaoGenerator.java @@ -35,6 +35,8 @@ public class EhDaoGenerator { private static final int VERSION = 1; private static final String DOWNLOAD_INFO_PATH = "../app/src/main/java-gen/com/hippo/ehviewer/dao/DownloadInfo.java"; + private static final String HISTORY_INFO_PATH = "../app/src/main/java-gen/com/hippo/ehviewer/dao/HistoryInfo.java"; + private static final String LOCAL_FAVORITE_INFO_PATH = "../app/src/main/java-gen/com/hippo/ehviewer/dao/LocalFavoriteInfo.java"; public static void generate() throws Exception { Utilities.deleteContents(new File(DELETE_DIR)); @@ -43,7 +45,6 @@ public class EhDaoGenerator { outDir.mkdirs(); Schema schema = new Schema(VERSION, PACKAGE); - addGalleryInfo(schema); addDownloads(schema); addDownloadLabel(schema); addDownloadDirname(schema); @@ -52,32 +53,17 @@ public class EhDaoGenerator { addLocalFavorites(schema); new DaoGenerator().generateAll(schema, OUT_DIR); - adjustDownloads(); - } - - private static void addGalleryInfo(Schema schema) { - Entity entity = schema.addEntity("GalleryInfoRaw"); - entity.setTableName("GALLERY_INFO"); - entity.setClassNameDao("GalleryInfoDao"); - entity.addLongProperty("gid").primaryKey(); - entity.addStringProperty("token"); - entity.addStringProperty("title"); - entity.addStringProperty("posted"); - entity.addIntProperty("category"); - entity.addStringProperty("thumb"); - entity.addStringProperty("uploader"); - entity.addFloatProperty("rating"); - entity.addIntProperty("reference"); + adjustDownloadInfo(); + adjustHistoryInfo(); + adjustLocalFavoriteInfo(); } private static void addDownloads(Schema schema) { Entity entity = schema.addEntity("DownloadInfo"); entity.setTableName("DOWNLOADS"); entity.setClassNameDao("DownloadsDao"); - - entity.setSuperclass("com.hippo.ehviewer.client.data.GalleryInfo"); - - // Gallery Info Data + entity.setSuperclass("GalleryInfo"); + // GalleryInfo data entity.addLongProperty("gid").primaryKey().notNull(); entity.addStringProperty("token"); entity.addStringProperty("title"); @@ -88,7 +74,7 @@ public class EhDaoGenerator { entity.addStringProperty("uploader"); entity.addFloatProperty("rating").notNull(); entity.addStringProperty("simpleLanguage"); - // Download Info Data + // DownloadInfo data entity.addIntProperty("state").notNull(); entity.addIntProperty("legacy").notNull(); entity.addLongProperty("time").notNull(); @@ -96,8 +82,8 @@ public class EhDaoGenerator { } private static void addDownloadLabel(Schema schema) { - Entity entity = schema.addEntity("DownloadLabelRaw"); - entity.setTableName("DOWNLOAD_LABEL"); + Entity entity = schema.addEntity("DownloadLabel"); + entity.setTableName("DOWNLOAD_LABELS"); entity.setClassNameDao("DownloadLabelDao"); entity.addIdProperty(); entity.addStringProperty("label"); @@ -105,7 +91,7 @@ public class EhDaoGenerator { } private static void addDownloadDirname(Schema schema) { - Entity entity = schema.addEntity("DownloadDirnameRaw"); + Entity entity = schema.addEntity("DownloadDirname"); entity.setTableName("DOWNLOAD_DIRNAME"); entity.setClassNameDao("DownloadDirnameDao"); entity.addLongProperty("gid").primaryKey(); @@ -113,16 +99,28 @@ public class EhDaoGenerator { } private static void addHistoryInfo(Schema schema) { - Entity entity = schema.addEntity("HistoryInfoRaw"); - entity.setTableName("HISTORY_INFO"); - entity.setClassNameDao("HistoryInfoDao"); - entity.addLongProperty("gid").primaryKey(); - entity.addIntProperty("mode"); - entity.addLongProperty("date"); + Entity entity = schema.addEntity("HistoryInfo"); + entity.setTableName("HISTORY"); + entity.setClassNameDao("HistoryDao"); + entity.setSuperclass("GalleryInfo"); + // GalleryInfo data + entity.addLongProperty("gid").primaryKey().notNull(); + entity.addStringProperty("token"); + entity.addStringProperty("title"); + entity.addStringProperty("titleJpn"); + entity.addStringProperty("thumb"); + entity.addIntProperty("category").notNull(); + entity.addStringProperty("posted"); + entity.addStringProperty("uploader"); + entity.addFloatProperty("rating").notNull(); + entity.addStringProperty("simpleLanguage"); + // HistoryInfo data + entity.addIntProperty("mode").notNull(); + entity.addLongProperty("time").notNull(); } private static void addQuickSearch(Schema schema) { - Entity entity = schema.addEntity("QuickSearchRaw"); + Entity entity = schema.addEntity("QuickSearch"); entity.setTableName("QUICK_SEARCH"); entity.setClassNameDao("QuickSearchDao"); entity.addIdProperty(); @@ -136,14 +134,26 @@ public class EhDaoGenerator { } private static void addLocalFavorites(Schema schema) { - Entity entity = schema.addEntity("LocalFavoritesRaw"); + Entity entity = schema.addEntity("LocalFavoriteInfo"); entity.setTableName("LOCAL_FAVORITES"); entity.setClassNameDao("LocalFavoritesDao"); - entity.addLongProperty("gid").primaryKey(); - entity.addLongProperty("date"); + entity.setSuperclass("GalleryInfo"); + // GalleryInfo data + entity.addLongProperty("gid").primaryKey().notNull(); + entity.addStringProperty("token"); + entity.addStringProperty("title"); + entity.addStringProperty("titleJpn"); + entity.addStringProperty("thumb"); + entity.addIntProperty("category").notNull(); + entity.addStringProperty("posted"); + entity.addStringProperty("uploader"); + entity.addFloatProperty("rating").notNull(); + entity.addStringProperty("simpleLanguage"); + // LocalFavoriteInfo data + entity.addLongProperty("time").notNull(); } - private static void adjustDownloads() throws Exception { + private static void adjustDownloadInfo() throws Exception { JavaClassSource javaClass = Roaster.parse(JavaClassSource.class, new File(DOWNLOAD_INFO_PATH)); // Remove field from GalleryInfo javaClass.removeField(javaClass.getField("gid")); @@ -155,6 +165,7 @@ public class EhDaoGenerator { javaClass.removeField(javaClass.getField("posted")); javaClass.removeField(javaClass.getField("uploader")); javaClass.removeField(javaClass.getField("rating")); + javaClass.removeField(javaClass.getField("simpleLanguage")); // Set all field public javaClass.getField("state").setPublic(); javaClass.getField("legacy").setPublic(); @@ -223,4 +234,131 @@ public class EhDaoGenerator { fileWriter.write(javaClass.toString()); fileWriter.close(); } + + private static void adjustHistoryInfo() throws Exception { + JavaClassSource javaClass = Roaster.parse(JavaClassSource.class, new File(HISTORY_INFO_PATH)); + // Remove field from GalleryInfo + javaClass.removeField(javaClass.getField("gid")); + javaClass.removeField(javaClass.getField("token")); + javaClass.removeField(javaClass.getField("title")); + javaClass.removeField(javaClass.getField("titleJpn")); + javaClass.removeField(javaClass.getField("thumb")); + javaClass.removeField(javaClass.getField("category")); + javaClass.removeField(javaClass.getField("posted")); + javaClass.removeField(javaClass.getField("uploader")); + javaClass.removeField(javaClass.getField("rating")); + javaClass.removeField(javaClass.getField("simpleLanguage")); + // Set all field public + javaClass.getField("mode").setPublic(); + javaClass.getField("time").setPublic(); + // Add Parcelable stuff + javaClass.addMethod("\t@Override\n" + + "\tpublic int describeContents() {\n" + + "\t\treturn 0;\n" + + "\t}"); + javaClass.addMethod("\t@Override\n" + + "\tpublic void writeToParcel(Parcel dest, int flags) {\n" + + "\t\tsuper.writeToParcel(dest, flags);\n" + + "\t\tdest.writeInt(this.mode);\n" + + "\t\tdest.writeLong(this.time);\n" + + "\t}"); + javaClass.addMethod("\tprotected HistoryInfo(Parcel in) {\n" + + "\t\tsuper(in);\n" + + "\t\tthis.mode = in.readInt();\n" + + "\t\tthis.time = in.readLong();\n" + + "\t}").setConstructor(true); + javaClass.addField("\tpublic static final Creator CREATOR = new Creator() {\n" + + "\t\t@Override\n" + + "\t\tpublic HistoryInfo createFromParcel(Parcel source) {\n" + + "\t\t\treturn new HistoryInfo(source);\n" + + "\t\t}\n" + + "\n" + + "\t\t@Override\n" + + "\t\tpublic HistoryInfo[] newArray(int size) {\n" + + "\t\t\treturn new HistoryInfo[size];\n" + + "\t\t}\n" + + "\t};"); + javaClass.addImport("android.os.Parcel"); + // Add from GalleryInfo constructor + javaClass.addMethod("\tpublic HistoryInfo(GalleryInfo galleryInfo) {\n" + + "\t\tthis.gid = galleryInfo.gid;\n" + + "\t\tthis.token = galleryInfo.token;\n" + + "\t\tthis.title = galleryInfo.title;\n" + + "\t\tthis.titleJpn = galleryInfo.titleJpn;\n" + + "\t\tthis.thumb = galleryInfo.thumb;\n" + + "\t\tthis.category = galleryInfo.category;\n" + + "\t\tthis.posted = galleryInfo.posted;\n" + + "\t\tthis.uploader = galleryInfo.uploader;\n" + + "\t\tthis.rating = galleryInfo.rating;\n" + + "\t\tthis.simpleTags = galleryInfo.simpleTags;\n" + + "\t\tthis.simpleLanguage = galleryInfo.simpleLanguage;\n" + + "\t}").setConstructor(true); + javaClass.addImport("com.hippo.ehviewer.client.data.GalleryInfo"); + + FileWriter fileWriter = new FileWriter(HISTORY_INFO_PATH); + fileWriter.write(javaClass.toString()); + fileWriter.close(); + } + + private static void adjustLocalFavoriteInfo() throws Exception { + JavaClassSource javaClass = Roaster.parse(JavaClassSource.class, new File(LOCAL_FAVORITE_INFO_PATH)); + // Remove field from GalleryInfo + javaClass.removeField(javaClass.getField("gid")); + javaClass.removeField(javaClass.getField("token")); + javaClass.removeField(javaClass.getField("title")); + javaClass.removeField(javaClass.getField("titleJpn")); + javaClass.removeField(javaClass.getField("thumb")); + javaClass.removeField(javaClass.getField("category")); + javaClass.removeField(javaClass.getField("posted")); + javaClass.removeField(javaClass.getField("uploader")); + javaClass.removeField(javaClass.getField("rating")); + javaClass.removeField(javaClass.getField("simpleLanguage")); + // Set all field public + javaClass.getField("time").setPublic(); + // Add Parcelable stuff + javaClass.addMethod("\t@Override\n" + + "\tpublic int describeContents() {\n" + + "\t\treturn 0;\n" + + "\t}"); + javaClass.addMethod("\t@Override\n" + + "\tpublic void writeToParcel(Parcel dest, int flags) {\n" + + "\t\tsuper.writeToParcel(dest, flags);\n" + + "\t\tdest.writeLong(this.time);\n" + + "\t}"); + javaClass.addMethod("\tprotected LocalFavoriteInfo(Parcel in) {\n" + + "\t\tsuper(in);\n" + + "\t\tthis.time = in.readLong();\n" + + "\t}").setConstructor(true); + javaClass.addField("\tpublic static final Creator CREATOR = new Creator() {\n" + + "\t\t@Override\n" + + "\t\tpublic LocalFavoriteInfo createFromParcel(Parcel source) {\n" + + "\t\t\treturn new LocalFavoriteInfo(source);\n" + + "\t\t}\n" + + "\n" + + "\t\t@Override\n" + + "\t\tpublic LocalFavoriteInfo[] newArray(int size) {\n" + + "\t\t\treturn new LocalFavoriteInfo[size];\n" + + "\t\t}\n" + + "\t};"); + javaClass.addImport("android.os.Parcel"); + // Add from GalleryInfo constructor + javaClass.addMethod("\tpublic LocalFavoriteInfo(GalleryInfo galleryInfo) {\n" + + "\t\tthis.gid = galleryInfo.gid;\n" + + "\t\tthis.token = galleryInfo.token;\n" + + "\t\tthis.title = galleryInfo.title;\n" + + "\t\tthis.titleJpn = galleryInfo.titleJpn;\n" + + "\t\tthis.thumb = galleryInfo.thumb;\n" + + "\t\tthis.category = galleryInfo.category;\n" + + "\t\tthis.posted = galleryInfo.posted;\n" + + "\t\tthis.uploader = galleryInfo.uploader;\n" + + "\t\tthis.rating = galleryInfo.rating;\n" + + "\t\tthis.simpleTags = galleryInfo.simpleTags;\n" + + "\t\tthis.simpleLanguage = galleryInfo.simpleLanguage;\n" + + "\t}").setConstructor(true); + javaClass.addImport("com.hippo.ehviewer.client.data.GalleryInfo"); + + FileWriter fileWriter = new FileWriter(LOCAL_FAVORITE_INFO_PATH); + fileWriter.write(javaClass.toString()); + fileWriter.close(); + } }