Closes #3; show different icon for different favorite state.

This commit is contained in:
seven332 committed 2015-04-14 21:49:34 +08:00
1 parent 85ea7550f4
commit 2fdc42acc4
10 files changed
+142 -40

No files matched your search

@@ -32,7 +32,7 @@ import com.hippo.ehviewer.cache.ImageCache;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.Crash;
import com.hippo.ehviewer.util.EhUtils;
import com.hippo.ehviewer.util.Favorite;
import com.hippo.ehviewer.miscellaneous.FavoriteHelper;
import com.hippo.ehviewer.util.Log;
import com.hippo.ehviewer.util.Ui;
import com.hippo.ehviewer.util.Utils;
@@ -72,7 +72,7 @@ public class AppContext extends Application implements UncaughtExceptionHandler
Log.init(mContextThemeWrapper);
Crash.init(mContextThemeWrapper);
EhClient.createInstance(mContextThemeWrapper);
Favorite.init(mContextThemeWrapper);
FavoriteHelper.init(mContextThemeWrapper);
Data.createInstance(mContextThemeWrapper);
ExDownloaderManager.createInstance(mContextThemeWrapper);
MaterialToast.setContext(mContextThemeWrapper);
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.hippo.ehviewer.util;
package com.hippo.ehviewer.miscellaneous;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
@@ -31,9 +31,11 @@ import com.hippo.ehviewer.app.MaterialAlertDialog;
import com.hippo.ehviewer.data.Data;
import com.hippo.ehviewer.data.GalleryInfo;
import com.hippo.ehviewer.ehclient.EhClient;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.ViewUtils;
import com.hippo.ehviewer.widget.MaterialToast;
public class Favorite {
public class FavoriteHelper {
private static Context mContext;
private static Resources mResources;
@@ -53,15 +55,19 @@ public class Favorite {
}
}
public static void addToFavorite(final Context context, GalleryInfo gi) {
int defaultFavorite = Config.getDefaultFavorite();
public static void addToFavorite(final Context context, final GalleryInfo gi,
final OnAddToFavoriteListener listener) {
final int defaultFavorite = Config.getDefaultFavorite();
switch (defaultFavorite) {
case -2:
Favorite.getAddToFavoriteDialog(context, gi).show();
FavoriteHelper.getAddToFavoriteDialog(context, gi, listener).show();
break;
case -1:
Data.getInstance().addLocalFavourite(gi);
MaterialToast.showToast(R.string.toast_add_favourite);
if (listener != null) {
listener.onSuccess(gi, -1);
}
break;
default:
EhClient.getInstance().addToFavorite(gi.gid,
@@ -69,19 +75,24 @@ public class Favorite {
@Override
public void onSuccess() {
MaterialToast.showToast(R.string.toast_add_favourite);
if (listener != null) {
listener.onSuccess(gi, defaultFavorite);
}
}
@Override
public void onFailure(String eMsg) {
MaterialToast.showToast(R.string.failed_to_add);
if (listener != null) {
listener.onFailure(eMsg, gi, defaultFavorite);
}
}
});
}
}
@SuppressLint("InflateParams")
public static AlertDialog getAddToFavoriteDialog(final Context context, final GalleryInfo gi) {
// TODO List do not work final
public static AlertDialog getAddToFavoriteDialog(final Context context, final GalleryInfo gi,
final OnAddToFavoriteListener listener) {
View view = ViewUtils.inflateDialogView(R.layout.add_to_favorite, false);
final CheckBox cb = (CheckBox) view.findViewById(R.id.set_default);
@@ -97,10 +108,14 @@ public class Favorite {
if (cb.isChecked())
Config.setDefaultFavorite(position - 1);
final int favoriteSlot = position - 1;
switch (position) {
case 0:
Data.getInstance().addLocalFavourite(gi);
if (listener != null) {
listener.onSuccess(gi, favoriteSlot);
}
MaterialToast.showToast(R.string.toast_add_favourite);
break;
default:
@@ -109,10 +124,16 @@ public class Favorite {
@Override
public void onSuccess() {
MaterialToast.showToast(R.string.toast_add_favourite);
if (listener != null) {
listener.onSuccess(gi, favoriteSlot);
}
}
@Override
public void onFailure(String eMsg) {
MaterialToast.showToast(R.string.failed_to_add);
if (listener != null) {
listener.onFailure(eMsg, gi, favoriteSlot);
}
}
});
}
@@ -124,4 +145,9 @@ public class Favorite {
.setView(view, false).setNegativeButton(android.R.string.cancel)
.setPositiveButton(android.R.string.ok).create();
}
public interface OnAddToFavoriteListener {
void onSuccess(GalleryInfo gi, int slot);
void onFailure(String eMsg, GalleryInfo gi, int slot);
}
}
@@ -59,7 +59,7 @@ import com.hippo.ehviewer.drawable.MaterialIndicatorDrawable;
import com.hippo.ehviewer.drawable.MaterialIndicatorDrawable.Stroke;
import com.hippo.ehviewer.ehclient.EhClient;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.Favorite;
import com.hippo.ehviewer.miscellaneous.FavoriteHelper;
import com.hippo.ehviewer.util.Ui;
import com.hippo.ehviewer.widget.ActionableToastBar;
import com.hippo.ehviewer.widget.ActionableToastBar.ActionClickedListener;
@@ -127,14 +127,14 @@ public class FavouriteActivity extends AbsTranslucentActivity
private void initLocalFavorite() {
mGalleryListView.setEnabledHeader(false);
mGalleryListView.setEnabledFooter(false);
setTitle(Favorite.FAVORITE_TITLES[mMenuIndex]);
setTitle(FavoriteHelper.FAVORITE_TITLES[mMenuIndex]);
mGalleryListView.refresh();
}
private void initFavorite() {
mGalleryListView.setEnabledHeader(true);
mGalleryListView.setEnabledFooter(true);
setTitle(Favorite.FAVORITE_TITLES[mMenuIndex]);
setTitle(FavoriteHelper.FAVORITE_TITLES[mMenuIndex]);
mGalleryListView.refresh();
}
@@ -211,7 +211,7 @@ public class FavouriteActivity extends AbsTranslucentActivity
if (convertView == null)
convertView = LayoutInflater.from(FavouriteActivity.this).inflate(R.layout.menu_item, parent, false);
TextView tv = (TextView)convertView;
tv.setText(Favorite.FAVORITE_TITLES[position]);
tv.setText(FavoriteHelper.FAVORITE_TITLES[position]);
if (position == 0) {
Drawable dr = mResources.getDrawable(R.drawable.ic_setting_panda_light);
dr.setBounds(0, 0, Ui.dp2pix(36), Ui.dp2pix(36));
@@ -236,7 +236,7 @@ public class FavouriteActivity extends AbsTranslucentActivity
return;
mMenuIndex = position;
setTitle(Favorite.FAVORITE_TITLES[mMenuIndex]);
setTitle(FavoriteHelper.FAVORITE_TITLES[mMenuIndex]);
if (mMenuIndex == 0)
initLocalFavorite();
else
@@ -417,7 +417,7 @@ public class FavouriteActivity extends AbsTranslucentActivity
private AlertDialog createMoveDialog(final ActionMode mode) {
return new MaterialAlertDialog.Builder(this).setTitle(R.string.where_to_move)
.setItems(Favorite.FAVORITE_TITLES, new MaterialAlertDialog.OnClickListener() {
.setItems(FavoriteHelper.FAVORITE_TITLES, new MaterialAlertDialog.OnClickListener() {
@Override
public boolean onClick(MaterialAlertDialog dialog, int position) {
if (mMenuIndex == position) {
@@ -86,12 +86,11 @@ import com.hippo.ehviewer.ehclient.DetailUrlParser;
import com.hippo.ehviewer.ehclient.EhClient;
import com.hippo.ehviewer.miscellaneous.ActivityHelper;
import com.hippo.ehviewer.miscellaneous.Downloader;
import com.hippo.ehviewer.miscellaneous.FavoriteHelper;
import com.hippo.ehviewer.service.DownloadService;
import com.hippo.ehviewer.util.AppConfig;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.Constants;
import com.hippo.ehviewer.util.Favorite;
import com.hippo.ehviewer.util.Log;
import com.hippo.ehviewer.util.Ui;
import com.hippo.ehviewer.util.ViewUtils;
import com.hippo.ehviewer.widget.AutoWrapLayout;
@@ -123,17 +122,22 @@ public class GalleryDetailActivity extends AbsTranslucentActivity
ProgressiveRatingBar.OnUserRateListener, PreviewList.PreviewHolder,
View.OnLayoutChangeListener, EasyRecyclerView.OnItemClickListener,
EasyRecyclerView.OnItemLongClickListener, RefreshTextView.OnRefreshListener,
FitWindowView.OnFitSystemWindowsListener {
FitWindowView.OnFitSystemWindowsListener, FavoriteHelper.OnAddToFavoriteListener {
@SuppressWarnings("unused")
private static final String TAG = GalleryDetailActivity.class.getSimpleName();
public static final String KEY_G_INFO = "gallery_info";
private static final int FAVORITE_NONE = 0x0;
private static final int FAVORITE_LOCAL = 0x1;
private static final int FAVORITE_ACCOUNT = 0x2;
private AppContext mAppContext;
private Resources mResources;
private EhClient mClient;
private WindowsAnimate mWindowsAnimate;
private Data mData;
private GalleryInfo mGalleryInfo;
private int mRunningAnimateNum = 0;
@@ -187,6 +191,8 @@ public class GalleryDetailActivity extends AbsTranslucentActivity
private FloatingActionButton mReply;
private OvalDrawable mCategoryDrawable;
private Drawable mFavoriteDrawable;
private Drawable mFavoriteOutlineDrawable;
private Drawable mRateDrawable;
private Drawable mCheckmarkDrawable;
@@ -201,6 +207,9 @@ public class GalleryDetailActivity extends AbsTranslucentActivity
private String mOldReply;
private boolean mReplyPositive;
private int mFavoriteState = FAVORITE_NONE;
private boolean mIsDoingFavorite = false;
private AlertDialog createGoToDialog() {
CharSequence[] items = new CharSequence[((PreviewImpl)mGalleryInfo).getPreviewPageNum()];
for (int i = 0; i < items.length; i++)
@@ -307,6 +316,7 @@ public class GalleryDetailActivity extends AbsTranslucentActivity
mClient = EhClient.getInstance();
mWindowsAnimate = new WindowsAnimate();
mWindowsAnimate.init(this);
mData = Data.getInstance();
mResources = getResources();
@@ -377,9 +387,10 @@ public class GalleryDetailActivity extends AbsTranslucentActivity
Drawable similarDrawable = mResources.getDrawable(R.drawable.ic_similar);
similarDrawable.setBounds(rect);
mSimilar.setCompoundDrawables(null, similarDrawable, null, null);
Drawable favoriteDrawable = mResources.getDrawable(R.drawable.ic_favorite);
favoriteDrawable.setBounds(rect);
mFavorite.setCompoundDrawables(null, favoriteDrawable, null, null);
mFavoriteDrawable = mResources.getDrawable(R.drawable.ic_favorite);
mFavoriteDrawable.setBounds(rect);
mFavoriteOutlineDrawable = mResources.getDrawable(R.drawable.ic_favorite_outline);
mFavoriteOutlineDrawable.setBounds(rect);
Drawable shareDrawable = mResources.getDrawable(R.drawable.ic_share);
shareDrawable.setBounds(rect);
mShare.setCompoundDrawables(null, shareDrawable, null, null);
@@ -626,6 +637,16 @@ public class GalleryDetailActivity extends AbsTranslucentActivity
}
}
private void updateFavoriteButton() {
if (mFavoriteState == FAVORITE_NONE) {
mFavorite.setText(R.string.add_to_favorites);
mFavorite.setCompoundDrawables(null, mFavoriteOutlineDrawable, null, null);
} else {
mFavorite.setText(R.string.remove_from_favorites);
mFavorite.setCompoundDrawables(null, mFavoriteDrawable, null, null);
}
}
/**
* GalleryDetail
* LofiGalleryDetail
@@ -663,21 +684,26 @@ public class GalleryDetailActivity extends AbsTranslucentActivity
// Detail
mDividerAM.setVisibility(View.VISIBLE);
mDetailMore.setVisibility(View.VISIBLE);
// Favorite
if (mData.containsLocalFavourite(mGalleryInfo.gid)) {
mFavoriteState |= FAVORITE_LOCAL;
}
if (mGalleryInfo instanceof GalleryDetail) {
GalleryDetail galleryDetail = (GalleryDetail)mGalleryInfo;
// TODO handle favorite
Log.d("TAG", "favorite " + galleryDetail.isFavorite);
if (galleryDetail.isFavorite) {
mFavoriteState |= FAVORITE_ACCOUNT;
}
if (galleryDetail.torrents.length > 0) {
mTorrent.setVisibility(View.VISIBLE);
}
((TextView)mDetailMore.findViewById(R.id.language)).setText(
getString(R.string.language) + ": " + galleryDetail.language);
((TextView)mDetailMore.findViewById(R.id.posted)).setText(galleryDetail.posted);
((TextView)mDetailMore.findViewById(R.id.pages)).setText(
getString(R.string.pages) + ": " + String.valueOf(galleryDetail.pages));
((TextView)mDetailMore.findViewById(R.id.language)).setText(getString(R.string.language) + ": " + galleryDetail.language);
((TextView) mDetailMore.findViewById(R.id.posted)).setText(galleryDetail.posted);
((TextView) mDetailMore.findViewById(R.id.pages)).setText(getString(R.string.pages) + ": " + String.valueOf(galleryDetail.pages));
((TextView)mDetailMore.findViewById(R.id.size)).setText(
getString(R.string.size) + ": " + galleryDetail.size);
StringBuilder sb = new StringBuilder();
@@ -700,9 +726,10 @@ public class GalleryDetailActivity extends AbsTranslucentActivity
((TextView)mMoreDetailScroll.findViewById(R.id.more_detail)).setText(sb.toString());
} else if (mGalleryInfo instanceof LofiGalleryDetail) {
LofiGalleryDetail lofiGalleryDetail = (LofiGalleryDetail)mGalleryInfo;
((TextView)mDetailMore.findViewById(R.id.language)).setVisibility(View.GONE);
((TextView)mDetailMore.findViewById(R.id.posted)).setVisibility(View.GONE);
((TextView)mDetailMore.findViewById(R.id.pages)).setVisibility(View.GONE);
(mDetailMore.findViewById(R.id.language)).setVisibility(View.GONE);
(mDetailMore.findViewById(R.id.posted)).setVisibility(View.GONE);
(mDetailMore.findViewById(R.id.pages)).setVisibility(View.GONE);
((TextView)mDetailMore.findViewById(R.id.size)).setText(lofiGalleryDetail.posted);
StringBuilder sb = new StringBuilder();
sb.append("gid: ").append(lofiGalleryDetail.gid).append("\n\n")
@@ -719,6 +746,8 @@ public class GalleryDetailActivity extends AbsTranslucentActivity
// TODO
}
updateFavoriteButton();
// Rate
mDividerMR.setVisibility(View.VISIBLE);
mDetailRate.setVisibility(View.VISIBLE);
@@ -906,7 +935,36 @@ public class GalleryDetailActivity extends AbsTranslucentActivity
intent.putExtra(GalleryListActivity.KEY_IMAGE_URL, mGalleryInfo.thumb);
startActivity(intent);
} else if (v == mFavorite) {
Favorite.addToFavorite(this, mGalleryInfo);
if (!mIsDoingFavorite) {
if (mFavoriteState == FAVORITE_NONE) {
mIsDoingFavorite = true;
FavoriteHelper.addToFavorite(this, mGalleryInfo, this);
} else {
if ((mFavoriteState & FAVORITE_LOCAL) != 0) {
mData.deleteLocalFavourite(mGalleryInfo.gid);
mFavoriteState &= ~FAVORITE_LOCAL;
updateFavoriteButton();
}
if ((mFavoriteState & FAVORITE_ACCOUNT) != 0) {
mIsDoingFavorite = true;
mClient.addToFavorite(mGalleryInfo.gid, mGalleryInfo.token, -1, null, new EhClient.OnAddToFavoriteListener() {
@Override
public void onSuccess() {
mIsDoingFavorite = false;
mFavoriteState &= ~FAVORITE_ACCOUNT;
updateFavoriteButton();
}
@Override
public void onFailure(String eMsg) {
mIsDoingFavorite = false;
updateFavoriteButton();
}
});
}
}
}
} else if (v == mShare) {
ActivityHelper.share(this, EhClient.getDetailUrl(mGalleryInfo.gid, mGalleryInfo.token));
} else if (v == mRate) {
@@ -1288,6 +1346,18 @@ public class GalleryDetailActivity extends AbsTranslucentActivity
}
}
@Override
public void onSuccess(GalleryInfo gi, int slot) {
mIsDoingFavorite = false;
mFavoriteState |= slot == -1 ? FAVORITE_LOCAL : FAVORITE_ACCOUNT;
updateFavoriteButton();
}
@Override
public void onFailure(String eMsg, GalleryInfo gi, int slot) {
mIsDoingFavorite = false;
}
private class GDetailGetListener
implements EhClient.OnGetGDetailListener {
@Override
@@ -1418,7 +1488,5 @@ public class GalleryDetailActivity extends AbsTranslucentActivity
return 0;
}
}
}
}
@@ -73,7 +73,7 @@ import com.hippo.ehviewer.network.HttpHelper;
import com.hippo.ehviewer.service.DownloadService;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.DialogUtils;
import com.hippo.ehviewer.util.Favorite;
import com.hippo.ehviewer.miscellaneous.FavoriteHelper;
import com.hippo.ehviewer.util.Ui;
import com.hippo.ehviewer.util.Utils;
import com.hippo.ehviewer.util.ViewUtils;
@@ -551,7 +551,7 @@ public class GalleryListActivity extends AbsTranslucentActivity implements View.
final GalleryInfo gi = mGalleryListView.getGalleryInfo(longClickItemIndex);
switch (position) {
case 0: // Add favourite item
Favorite.addToFavorite(GalleryListActivity.this, gi);
FavoriteHelper.addToFavorite(GalleryListActivity.this, gi, null);
break;
case 1:
Intent it = new Intent(GalleryListActivity.this, DownloadService.class);
@@ -65,7 +65,7 @@ import com.hippo.ehviewer.util.BgThread;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.DialogUtils;
import com.hippo.ehviewer.util.EhUtils;
import com.hippo.ehviewer.util.Favorite;
import com.hippo.ehviewer.miscellaneous.FavoriteHelper;
import com.hippo.ehviewer.util.Secret;
import com.hippo.ehviewer.util.Ui;
import com.hippo.ehviewer.util.Utils;
@@ -234,9 +234,9 @@ public class SettingsActivity extends AbsPreferenceActivity {
mHAHProxyConfig.setOnPreferenceClickListener(this);
int i = 0;
String[] entrise = new String[Favorite.FAVORITE_TITLES.length + 1];
String[] entrise = new String[FavoriteHelper.FAVORITE_TITLES.length + 1];
entrise[i++] = getString(R.string.none);
for (String str : Favorite.FAVORITE_TITLES)
for (String str : FavoriteHelper.FAVORITE_TITLES)
entrise[i++] = str;
mDefaultFavorite.setEntries(entrise);
}
@@ -94,6 +94,8 @@
<string name="start_saving_torrent">开始保存种子</string>
<string name="save_torrent_successful">保存种子至 %s</string>
<string name="save_torrent_failed">保存种子失败</string>
<string name="add_to_favorites">添加至收藏</string>
<string name="remove_from_favorites">从收藏中移除</string>
<!-- AbsGalleryActivity -->
<string name="click_retry">点击重试</string>
@@ -82,6 +82,8 @@
<string name="start_saving_torrent">開始保存種子</string>
<string name="save_torrent_successful">保存種子至 %s</string>
<string name="save_torrent_failed">保存種子失敗</string>
<string name="add_to_favorites">添加至收藏</string>
<string name="remove_from_favorites">從收藏中移除</string>
<!-- AbsGalleryActivity -->
<string name="click_retry">點擊重試</string>
@@ -82,6 +82,8 @@
<string name="start_saving_torrent">開始儲存種子</string>
<string name="save_torrent_successful">儲存種子至 %s</string>
<string name="save_torrent_failed">儲存種子失敗</string>
<string name="add_to_favorites">新增至收藏</string>
<string name="remove_from_favorites">從收藏中移除</string>
<!-- AbsGalleryActivity -->
<string name="click_retry">點選重試</string>
+2
View File
@@ -98,6 +98,8 @@
<string name="start_saving_torrent">Start saving torrent</string>
<string name="save_torrent_successful">Save torrent to %s</string>
<string name="save_torrent_failed">Failed to save torrent</string>
<string name="add_to_favorites">Add to favorites</string>
<string name="remove_from_favorites">Remove from Favorites</string>
<!-- AbsGalleryActivity -->
<string name="click_retry">Click to retry</string>