Add scene result, update gallery comments for post comment, add isViewCreated in BaseScene

This commit is contained in:
seven332 committed 2016-02-06 16:08:08 +08:00
1 parent 875509be68
commit 67e3c8dc60
10 files changed
+161 -20

No files matched your search

@@ -16,6 +16,9 @@
package com.hippo.ehviewer.client.parser;
import android.os.Environment;
import android.util.Log;
import com.hippo.ehviewer.client.EhUtils;
import com.hippo.ehviewer.client.data.GalleryComment;
import com.hippo.ehviewer.client.data.GalleryDetail;
@@ -27,6 +30,9 @@ import com.hippo.ehviewer.client.exception.ParseException;
import com.hippo.ehviewer.client.exception.PiningException;
import com.hippo.yorozuya.NumberUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -260,6 +266,18 @@ public class GalleryDetailParser {
largePreviewSet.addItem(ParserUtils.parseInt(m.group(2)) - 1,
ParserUtils.trim(m.group(3)), ParserUtils.trim(m.group(1)));
}
if (largePreviewSet.size() == 0) {
Log.d("TAG", "largePreviewSet.size() == 0");
try {
OutputStream os = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "empty.txt"));
os.write(body.getBytes("utf-8"));
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return largePreviewSet;
}
}
@@ -18,13 +18,17 @@ package com.hippo.ehviewer.ui.scene;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import com.hippo.ehviewer.ui.MainActivity;
import com.hippo.scene.SceneFragment;
public class BaseScene extends SceneFragment {
private boolean mViewCreated;
public void setDrawerLayoutEnable(boolean enable) {
FragmentActivity activity = getActivity();
if (activity instanceof MainActivity) {
@@ -49,6 +53,22 @@ public class BaseScene extends SceneFragment {
}
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViewCreated = true;
}
@Override
public void onDestroyView() {
mViewCreated = false;
super.onDestroyView();
}
public boolean isViewCreated() {
return mViewCreated;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
@@ -406,6 +406,9 @@ public final class GalleryCommentsScene extends ToolbarScene
private void onCommentGallerySuccess(GalleryComment[] result) {
mComments = result;
mAdapter.notifyDataSetChanged();
Bundle re = new Bundle();
re.putParcelableArray(KEY_COMMENTS, result);
setResult(SceneFragment.RESULT_OK, re);
}
private static class CommentGalleryListener implements EhClient.Callback<GalleryComment[]> {
@@ -23,6 +23,7 @@ import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
@@ -89,6 +90,8 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene
@Retention(RetentionPolicy.SOURCE)
private @interface State {}
private static final int REQUEST_CODE_COMMENT_GALLERY = 0;
private static final int STATE_INIT = -1;
private static final int STATE_NORMAL = 0;
private static final int STATE_REFRESH = 1;
@@ -883,7 +886,9 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene
args.putInt(GalleryCommentsScene.KEY_GID, mGalleryDetail.gid);
args.putString(GalleryCommentsScene.KEY_TOKEN, mGalleryDetail.token);
args.putParcelableArray(GalleryCommentsScene.KEY_COMMENTS, mGalleryDetail.comments);
startScene(new Announcer(GalleryCommentsScene.class).setArgs(args));
startScene(new Announcer(GalleryCommentsScene.class)
.setArgs(args)
.setRequestCode(this, REQUEST_CODE_COMMENT_GALLERY));
} else if (mPreviews == v) {
int gid = getGid();
String token = getToken();
@@ -922,6 +927,32 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene
finish();
}
@Override
protected void onSceneResult(int requestCode, int resultCode, Bundle data) {
switch (requestCode) {
case REQUEST_CODE_COMMENT_GALLERY:
if (resultCode != RESULT_OK || data == null){
break;
}
Parcelable[] array = data.getParcelableArray(GalleryCommentsScene.KEY_COMMENTS);
if (!(array instanceof GalleryComment[])) {
break;
}
GalleryComment[] comments = (GalleryComment[]) array;
if (mGalleryDetail == null) {
break;
}
mGalleryDetail.comments = comments;
if (!isViewCreated()) {
break;
}
bindComments(comments);
break;
default:
super.onSceneResult(requestCode, resultCode, data);
}
}
private static class EnterGalleryListTransaction implements TransitionHelper {
private View mThumb;
@@ -962,15 +993,19 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene
private void onGetGalleryDetailSuccess(GalleryDetail result) {
mGalleryDetail = result;
adjustViewVisibility(STATE_NORMAL, true);
bindViewSecond();
if (isViewCreated()) {
adjustViewVisibility(STATE_NORMAL, true);
bindViewSecond();
}
}
private void onGetGalleryDetailFailure(Exception e) {
e.printStackTrace();
String error = ExceptionUtils.getReadableString(getContext(), e);
mFailedText.setText(error);
adjustViewVisibility(STATE_FAILED, true);
if (isViewCreated()) {
String error = ExceptionUtils.getReadableString(getContext(), e);
mFailedText.setText(error);
adjustViewVisibility(STATE_FAILED, true);
}
}
private void onRateGallerySuccess(RateGalleryParser.Result result) {
@@ -980,8 +1015,10 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene
}
// Update UI
mRatingText.setText(getAllRatingText(result.rating, result.ratedTimes));
mRating.setRating(result.rating);
if (isViewCreated()) {
mRatingText.setText(getAllRatingText(result.rating, result.ratedTimes));
mRating.setRating(result.rating);
}
}
private static class GetGalleryDetailListener implements EhClient.Callback<GalleryDetail> {
@@ -1006,7 +1006,7 @@ public final class GalleryListScene extends BaseScene
private void onGetGalleryListSuccess(GalleryListParser.Result result, int taskId) {
if (mHelper != null && mSearchBarMoveHelper != null &&
mHelper.isCurrentTask(taskId)) {
mHelper.isCurrentTask(taskId) && isViewCreated()) {
mHelper.setPages(taskId, result.pages);
mHelper.onGetPageData(taskId, result.galleryInfos);
mSearchBarMoveHelper.returnSearchBarPosition();
@@ -1015,7 +1015,7 @@ public final class GalleryListScene extends BaseScene
private void onGetGalleryListFailure(Exception e, int taskId) {
if (mHelper != null && mSearchBarMoveHelper != null &&
mHelper.isCurrentTask(taskId)) {
mHelper.isCurrentTask(taskId) && isViewCreated()) {
mHelper.onGetExpection(taskId, e);
mSearchBarMoveHelper.returnSearchBarPosition();
}
@@ -53,6 +53,7 @@ import com.hippo.widget.ContentLayout;
import com.hippo.widget.LoadImageView;
import com.hippo.widget.Slider;
import com.hippo.yorozuya.LayoutUtils;
import com.hippo.yorozuya.SimpleHandler;
import java.util.ArrayList;
import java.util.Locale;
@@ -221,17 +222,22 @@ public class GalleryPreviewsScene extends ToolbarScene {
private class GalleryPreviewHelper extends ContentLayout.ContentHelper<GalleryPreview> {
@Override
protected void getPageData(int taskId, int type, int page) {
protected void getPageData(final int taskId, int type, int page) {
if (mGid == -1 || mToken == null) {
onGetExpection(taskId, new EhException(getString(R.string.error_cannot_find_gallery)));
return;
}
LargePreviewSet previewSet = EhApplication.getLargePreviewSetCache(
final LargePreviewSet previewSet = EhApplication.getLargePreviewSetCache(
getContext()).get(EhCacheKeyFactory.getLargePreviewSetKey(mGid, page));
Integer pages = EhApplication.getPreviewPagesCache(getContext()).get(mGid);
final Integer pages = EhApplication.getPreviewPagesCache(getContext()).get(mGid);
if (previewSet != null && pages != null) {
onGetLargePreviewSetSuccess(Pair.create(previewSet, pages), taskId);
SimpleHandler.getInstance().post(new Runnable() {
@Override
public void run() {
onGetLargePreviewSetSuccess(Pair.create(previewSet, pages), taskId);
}
});
return;
}
@@ -266,7 +272,7 @@ public class GalleryPreviewsScene extends ToolbarScene {
}
private void onGetLargePreviewSetSuccess(Pair<LargePreviewSet, Integer> result, int taskId) {
if (mHelper != null && mHelper.isCurrentTask(taskId)) {
if (mHelper != null && mHelper.isCurrentTask(taskId) && isViewCreated()) {
LargePreviewSet previewSet = result.first;
int size = previewSet.size();
ArrayList<GalleryPreview> list = new ArrayList<>(size);
@@ -284,7 +290,7 @@ public class GalleryPreviewsScene extends ToolbarScene {
}
private void onGetLargePreviewSetFailure(Exception e, int taskId) {
if (mHelper != null && mHelper.isCurrentTask(taskId)) {
if (mHelper != null && mHelper.isCurrentTask(taskId) && isViewCreated()) {
mHelper.onGetExpection(taskId, e);
}
}
@@ -185,6 +185,7 @@ public final class LoginScene extends BaseScene implements EditText.OnEditorActi
// Clean up for sign in
mCookieStore.cleanUpForSignIn();
// TODO Make callback static
mSignInRequest = new EhRequest()
.setMethod(EhClient.METHOD_SIGN_IN)
.setArgs(username, password)
@@ -24,7 +24,8 @@ public final class Announcer {
Bundle args;
TransitionHelper tranHelper;
int flag;
int requestId;
SceneFragment requestFrom;
int requestCode;
public Announcer(Class<?> clazz) {
this.clazz = clazz;
@@ -45,8 +46,9 @@ public final class Announcer {
return this;
}
public Announcer setRequestId(int requestId) {
this.requestId = requestId;
public Announcer setRequestCode(SceneFragment requestFrom, int requestCode) {
this.requestFrom = requestFrom;
this.requestCode = requestCode;
return this;
}
}
@@ -25,9 +25,12 @@ import android.support.v4.app.FragmentActivity;
import android.view.View;
import com.hippo.ehviewer.R;
import com.hippo.yorozuya.IntList;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
public class SceneFragment extends Fragment {
@@ -40,6 +43,17 @@ public class SceneFragment extends Fragment {
public static final int FLAG_REMOVE_ALL_THE_OTHER_SCENES = 0x1;
/** Standard scene result: operation canceled. */
public static final int RESULT_CANCELED = 0;
/** Standard scene result: operation succeeded. */
public static final int RESULT_OK = -1;
int resultCode = RESULT_CANCELED;
Bundle result = null;
List<String> mRequestSceneTagList = new ArrayList<>(0);
IntList mRequestCodeList = new IntList(0);
@LaunchMode
public int getLaunchMode() {
return LAUNCH_MODE_STANDARD;
@@ -100,4 +114,30 @@ public class SceneFragment extends Fragment {
view.setTag(R.id.fragment_tag, getTag());
view.setBackgroundColor(getContext().getResources().getColor(R.color.background_light));
}
void addRequest(String requestSceneTag, int requestCode) {
mRequestSceneTagList.add(requestSceneTag);
mRequestCodeList.add(requestCode);
}
void returnResult(StageActivity stage) {
for (int i = 0, size = Math.min(mRequestSceneTagList.size(), mRequestCodeList.size()); i < size; i++) {
String tag = mRequestSceneTagList.get(i);
int code = mRequestCodeList.get(i);
SceneFragment scene = stage.findSceneByTag(tag);
if (scene != null) {
scene.onSceneResult(code, resultCode, result);
}
}
mRequestSceneTagList.clear();
mRequestCodeList.clear();
}
protected void onSceneResult(int requestCode, int resultCode, Bundle data) {
}
public void setResult(int resultCode, Bundle result) {
this.resultCode = resultCode;
this.result = result;
}
}
@@ -147,6 +147,10 @@ public abstract class StageActivity extends AppCompatActivity {
currentScene.onNewArguments(args);
}
if (!removeAllTheOthers) {
// Check request
if (announcer.requestFrom != null) {
currentScene.addRequest(announcer.requestFrom.getTag(), announcer.requestCode);
}
// Done!
return;
}
@@ -221,6 +225,11 @@ public abstract class StageActivity extends AppCompatActivity {
}
transaction.commit();
// Check request
if (newScene != null && announcer.requestFrom != null) {
newScene.addRequest(announcer.requestFrom.getTag(), announcer.requestCode);
}
// Update SoftInputMode
if (newScene != null) {
getWindow().setSoftInputMode(newScene.getSoftInputMode());
@@ -305,9 +314,14 @@ public abstract class StageActivity extends AppCompatActivity {
// Remove tag
mSceneTagList.remove(index);
// Return result
if (scene instanceof SceneFragment) {
((SceneFragment) scene).returnResult(this);
}
// Update SoftInputMode
if (next instanceof SceneFragment) {
getWindow().setSoftInputMode(((SceneFragment) scene).getSoftInputMode());
getWindow().setSoftInputMode(((SceneFragment) next).getSoftInputMode());
}
}