Add rating gallery
This commit is contained in:
1 parent
a1c11a4472
commit
e9c1eac800
7 files changed
+259
-12
No files matched your search
@@ -193,6 +193,39 @@ public final class EhClient {
|
||||
*/
|
||||
public static final int METHOD_GET_PREVIEW_SET = 6;
|
||||
|
||||
/**
|
||||
* Rate gallery
|
||||
*
|
||||
* <table>
|
||||
* <tr>
|
||||
* <th>param</th>
|
||||
* <th>info</th>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>gid</td>
|
||||
* <td>gid of the gallery</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>token</td>
|
||||
* <td>token of the gallery</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>rating</td>
|
||||
* <td>your rating of the gallery</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>source</td>
|
||||
* <td>the source, one of {@link EhUrl#SOURCE_G},
|
||||
* {@link EhUrl#SOURCE_EX} and {@link EhUrl#SOURCE_LOFI}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>listener</td>
|
||||
* <td>the listener for callback</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*/
|
||||
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");
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<RateGalleryParser.Result> {
|
||||
|
||||
@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) {
|
||||
|
||||
Reference in new issue
Block a user