diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java index e83033244..16ca9ca73 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java @@ -193,6 +193,39 @@ public final class EhClient { */ public static final int METHOD_GET_PREVIEW_SET = 6; + /** + * Rate gallery + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
paraminfo
gidgid of the gallery
tokentoken of the gallery
ratingyour rating of the gallery
sourcethe source, one of {@link EhUrl#SOURCE_G}, + * {@link EhUrl#SOURCE_EX} and {@link EhUrl#SOURCE_LOFI}
listenerthe listener for callback
+ */ + public static final int METHOD_RATE_GALLERY = 7; + private final ThreadPoolExecutor mRequestThreadPool; private final HttpClient mHttpClient; @@ -315,6 +348,8 @@ public final class EhClient { return EhEngine.getGalleryDetail(mHttpClient, mEhHttpRequest, (String) params[0], (Integer) params[1]); case METHOD_GET_PREVIEW_SET: return EhEngine.getPreviewSet(mHttpClient, mEhHttpRequest, (String) params[0], (Integer) params[1]); + case METHOD_RATE_GALLERY: + return EhEngine.rateGallery(mHttpClient, mEhHttpRequest, (Integer) params[0], (String) params[1], (Float) params[2], (Integer) params[3]); default: return new IllegalStateException("Can't detect method"); } diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java index 247fb347e..e4c3e512d 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java @@ -183,4 +183,33 @@ class EhEngine { httpRequest.disconnect(); } } + + public static RateGalleryParser.Result rateGallery(HttpClient httpClient, + HttpRequest httpRequest, int gid, String token, float rating, int source) throws Exception { + try { + final JSONObject json = new JSONObject(); + json.put("method", "rategallery"); + json.put("apiuid", APIUID); + json.put("apikey", APIKEY); + json.put("gid", gid); + json.put("token", token); + json.put("rating", rating); + + JsonPoster jsonPoster = new JsonPoster(json); + httpRequest.setUrl(EhUrl.getApiUrl(source)); + httpRequest.setHttpImpl(jsonPoster); + HttpResponse response = httpClient.execute(httpRequest); + String body = response.getString(); + + return RateGalleryParser.parse(body); + } catch (Exception e) { + if (httpRequest.getState() == HttpRequest.STATE_DISCONNECT) { + throw new CanceledException(); + } else { + throw e; + } + } finally { + httpRequest.disconnect(); + } + } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/RateGalleryParser.java b/app/src/main/java/com/hippo/ehviewer/client/RateGalleryParser.java new file mode 100644 index 000000000..f10c1e1e4 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/client/RateGalleryParser.java @@ -0,0 +1,46 @@ +/* + * Copyright 2015 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hippo.ehviewer.client; + +import org.json.JSONException; +import org.json.JSONObject; + +public class RateGalleryParser { + + public static class Result { + public float rating; + public int ratedTimes; + } + + public static Result parse(String body) throws Exception { + if (body == null) { + throw new EhException("Empty body"); + } + + try { + JSONObject jsonObject = new JSONObject(body); + Result result = new Result(); + result.rating = (float)jsonObject.getDouble("rating_avg"); + result.ratedTimes = jsonObject.getInt("rating_cnt"); + return result; + } catch (JSONException e) { + Exception exception = new ParseException("Can't parse rate gallery", body); + exception.initCause(e); + throw exception; + } + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java index 9ed7858fa..a315f3fbe 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java @@ -29,6 +29,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; +import android.widget.Toast; import com.hippo.effect.ViewTransition; import com.hippo.ehviewer.EhApplication; @@ -39,11 +40,13 @@ import com.hippo.ehviewer.client.EhConfig; import com.hippo.ehviewer.client.EhRequest; import com.hippo.ehviewer.client.EhUrl; import com.hippo.ehviewer.client.GalleryDetailUrlParser; +import com.hippo.ehviewer.client.RateGalleryParser; import com.hippo.ehviewer.client.data.Comment; import com.hippo.ehviewer.client.data.GalleryBase; import com.hippo.ehviewer.client.data.GalleryDetail; import com.hippo.ehviewer.client.data.PreviewSet; import com.hippo.ehviewer.client.data.TagGroup; +import com.hippo.ehviewer.ui.ContentActivity; import com.hippo.ehviewer.ui.GalleryActivity; import com.hippo.ehviewer.util.EhUtils; import com.hippo.ehviewer.util.Settings; @@ -166,6 +169,8 @@ public class GalleryDetailScene extends Scene implements View.OnClickListener, mEhConfig = EhApplication.getEhHttpClient(getStageActivity()).getEhConfigClone(); mSource = Settings.getEhSource(); + + ((ContentActivity) getStageActivity()).setDrawerListActivatedPosition(ContentActivity.DRAWER_LIST_NONE); } @Override @@ -254,6 +259,13 @@ public class GalleryDetailScene extends Scene implements View.OnClickListener, mPreview.setPreviewHelper(new SimplePreviewHelper()); } + @Override + protected void onResume() { + super.onResume(); + + ((ContentActivity) getStageActivity()).setDrawerListActivatedPosition(ContentActivity.DRAWER_LIST_NONE); + } + private void requestGalleryDetail(int gid, String token) { EhClient client = EhApplication.getEhClient(getStageActivity()); int source = Settings.getEhSource(); @@ -275,7 +287,7 @@ public class GalleryDetailScene extends Scene implements View.OnClickListener, mCategory.setTextColor(EhUtils.getCategoryColor(gb.category)); } - private String getRatingText(float rating) { + private static String getRatingText(float rating, Resources resources) { String undefine = "(´_ゝ`)"; if (Float.isNaN(rating)) { return undefine; @@ -310,12 +322,12 @@ public class GalleryDetailScene extends Scene implements View.OnClickListener, if (resId == 0) { return undefine; } else { - return getStageActivity().getString(resId); + return resources.getString(resId); } } private String getAllRatingText(float rating, int ratedTimes) { - return String.format(getStageActivity().getString(R.string.we_feel), getRatingText(rating)) + + return String.format(getStageActivity().getString(R.string.we_feel), getRatingText(rating, getStageActivity().getResources())) + " (" + rating + ", " + ratedTimes + ")"; } @@ -548,13 +560,19 @@ public class GalleryDetailScene extends Scene implements View.OnClickListener, } else if (mDownload == v) { // TODO } else if (mRead == v) { - Intent intent = new Intent(getStageActivity(), GalleryActivity.class); - intent.setAction(GalleryActivity.ACTION_GALLERY_FROM_GALLERY_BASE); - intent.putExtra(GalleryActivity.KEY_GALLERY_BASE, mGalleryDetail); - //intent.setAction(GalleryActivity.ACTION_GALLERY_FROM_ARCHIVE); - //intent.putExtra(GalleryActivity.KEY_ARCHIVE_URI, - // Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "a.zip"))); - getStageActivity().startActivity(intent); + GalleryBase galleryBase = mGalleryDetail; + if (galleryBase == null) { + galleryBase = mGalleryBase; + } + if (galleryBase != null) { + Intent intent = new Intent(getStageActivity(), GalleryActivity.class); + intent.setAction(GalleryActivity.ACTION_GALLERY_FROM_GALLERY_BASE); + intent.putExtra(GalleryActivity.KEY_GALLERY_BASE, galleryBase); + //intent.setAction(GalleryActivity.ACTION_GALLERY_FROM_ARCHIVE); + //intent.putExtra(GalleryActivity.KEY_ARCHIVE_URI, + // Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "a.zip"))); + getStageActivity().startActivity(intent); + } } else if (mFavorite == v) { // TODO } else if (mTorrent == v) { @@ -565,7 +583,54 @@ public class GalleryDetailScene extends Scene implements View.OnClickListener, StartActivityHelper.share(getStageActivity(), EhUrl.getDetailUrl(Settings.getEhSource(), mGalleryDetail.gid, mGalleryDetail.token, 0)); } else if (mRate == v) { - // TODO + int[] position = new int[2]; + ViewUtils.getLocationInAncestor(mRate, position, getSceneView()); + RateDialogHelper helper = new RateDialogHelper(); + new SimpleDialog.Builder(getStageActivity()) + .setTitle(R.string.rate) + .setCustomView(R.layout.dialog_rate, helper) + .setPositiveButton(android.R.string.ok) + .setOnButtonClickListener(helper) + .setStartPoint(position[0], position[1]).show(this); + } + } + + class RateDialogHelper implements SimpleDialog.OnCreateCustomViewListener, + SimpleDialog.OnClickListener { + + private RatingView mRatingView; + + @Override + public void onCreateCustomView(final SimpleDialog dialog, View view) { + final Resources resources = dialog.getStageActivity().getResources(); + final TextView textView = (TextView) view.findViewById(R.id.text); + mRatingView = (RatingView) view.findViewById(R.id.rating_view); + mRatingView.setOnRatingChangeListener(new RatingView.OnRatingChangeListener() { + @Override + public void onRatingChange(float oldRating, float newRating, boolean byUser, boolean confirm) { + textView.setText(getAllRatingText(resources, newRating)); + } + }); + mRatingView.setRating(mGalleryDetail.rating); + } + + public String getAllRatingText(Resources resources, float rating) { + return String.format(resources.getString(R.string.we_feel), getRatingText(rating, resources)); + } + + @Override + public boolean onClick(SimpleDialog dialog, int which) { + if (which == SimpleDialog.POSITIVE) { + GalleryDetail galleryDetail = mGalleryDetail; + EhClient client = EhApplication.getEhClient(getStageActivity()); + EhRequest request = new EhRequest(); + request.setEhConfig(mEhConfig); + request.setMethod(EhClient.METHOD_RATE_GALLERY); + request.setArgs(galleryDetail.gid, galleryDetail.token, mRatingView.getRating(), mSource); + request.setEhListener(new RateGalleryListener()); + client.execute(request); + } + return true; } } @@ -649,6 +714,29 @@ public class GalleryDetailScene extends Scene implements View.OnClickListener, } } + private class RateGalleryListener extends EhClient.EhListener { + + @Override + public void onSuccess(RateGalleryParser.Result result) { + mGalleryDetail.rating = result.rating; + mGalleryDetail.ratedTimes = result.ratedTimes; + // Update UI + mRating.setRating(result.rating); + mRatingText.setText(getAllRatingText(result.rating, result.ratedTimes)); + // Toast + Toast.makeText(getStageActivity(), R.string.rate_successfully, Toast.LENGTH_SHORT).show(); + } + + @Override + public void onFailure(Exception e) { + Toast.makeText(getStageActivity(), R.string.rate_failed, Toast.LENGTH_SHORT).show(); + } + + @Override + public void onCanceled() { + } + } + private class DetailHolder extends RecyclerView.ViewHolder { public DetailHolder(View itemView) { diff --git a/app/src/main/res/layout/dialog_rate.xml b/app/src/main/res/layout/dialog_rate.xml new file mode 100644 index 000000000..d87b94598 --- /dev/null +++ b/app/src/main/res/layout/dialog_rate.xml @@ -0,0 +1,46 @@ + + + + + + + + + diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index ebf426038..6b5063b62 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -125,7 +125,7 @@ 一般 较大 - 高级 + 高级 显示应用状态 在屏幕右上角显示设备状态 清理图片内存缓存 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9d136a6e1..668fb0ed0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -87,6 +87,8 @@ No comments No more comments More comments + Rate successfully + Rate failed Copied to clipboard @@ -146,6 +148,7 @@ Only work for Android 4.4 and above Immersive full-screen mode Do not hide nav bar + Volume keys to turn pages EH EH source