Skip to content

Commit

Permalink
在下载列表略缩图失效或丢失时,尝试自动获取新的略缩图连接
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojieonly committed Nov 22, 2024
1 parent d2b4433 commit 3f8c11e
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 57 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/hippo/conaco/ConacoTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ protected V doInBackground(Void... params) {
Response response = mCall.execute();
ResponseBody body = response.body();
if (body == null) {

return null;
}
is = body.byteStream();
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/hippo/ehviewer/client/EhClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.hippo.ehviewer.client;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;

Expand Down Expand Up @@ -88,6 +89,7 @@ public void execute(EhRequest request) {
}
}

@SuppressLint("StaticFieldLeak")
public class Task extends AsyncTask<Object, Void, Object> {

private final int mMethod;
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/hippo/ehviewer/client/EhUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package com.hippo.ehviewer.client;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hippo.ehviewer.Settings;
import com.hippo.ehviewer.client.data.GalleryInfo;
import com.hippo.network.UrlBuilder;
import java.util.List;
import java.util.ListIterator;
Expand Down Expand Up @@ -287,7 +290,7 @@ public static String getFixedPreviewThumbUrl(String originUrl) {
HttpUrl url = HttpUrl.parse(originUrl);
if (url == null) return originUrl;
List<String> pathSegments = url.pathSegments();
if (pathSegments == null || pathSegments.size() < 3) return originUrl;
if (pathSegments.size() < 3) return originUrl;

ListIterator<String> iterator = pathSegments.listIterator(pathSegments.size());
// The last segments, like
Expand All @@ -312,4 +315,5 @@ public static String getFixedPreviewThumbUrl(String originUrl) {
return originUrl;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,78 +1,73 @@
package com.hippo.ehviewer.ui.scene.gallery.detail;
package com.hippo.ehviewer.ui.scene.gallery.detail

import android.content.Context;

import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.EhDB;
import com.hippo.ehviewer.client.EhUtils;
import com.hippo.ehviewer.client.data.GalleryDetail;
import com.hippo.ehviewer.sync.GalleryDetailTagsSyncTask;
import com.hippo.ehviewer.ui.scene.EhCallback;
import com.hippo.scene.SceneFragment;
import com.hippo.lib.yorozuya.FileUtils;

public class GetGalleryDetailListener extends EhCallback<GalleryDetailScene, GalleryDetail> {

public static int RESULT_DETAIL = 1;
public static int RESULT_UPDATE = 0;

private final int resultMode;

public GetGalleryDetailListener(Context context, int stageId, String sceneTag, int resultMode) {
super(context, stageId, sceneTag);
this.resultMode = resultMode;
}

@Override
public void onSuccess(GalleryDetail result) {
getApplication().removeGlobalStuff(this);
import android.content.Context
import com.hippo.ehviewer.EhApplication
import com.hippo.ehviewer.EhDB
import com.hippo.ehviewer.client.EhUtils
import com.hippo.ehviewer.client.data.GalleryDetail
import com.hippo.ehviewer.sync.GalleryDetailTagsSyncTask
import com.hippo.ehviewer.ui.scene.EhCallback
import com.hippo.lib.yorozuya.FileUtils
import com.hippo.scene.SceneFragment

class GetGalleryDetailListener(
context: Context?,
stageId: Int,
sceneTag: String?,
private val resultMode: Int
) : EhCallback<GalleryDetailScene?, GalleryDetail?>(context, stageId, sceneTag) {
override fun onSuccess(result: GalleryDetail?) {
application.removeGlobalStuff(this)
if (result==null){
return
}
// Put gallery detail to cache
EhApplication.getGalleryDetailCache(getApplication()).put(result.gid, result);
EhApplication.getGalleryDetailCache(application).put(result.gid, result)

// Add history
EhDB.putHistoryInfo(result);
EhDB.putHistoryInfo(result)

// Save tags
GalleryDetailTagsSyncTask syncTask = new GalleryDetailTagsSyncTask(result);
syncTask.start();
val syncTask = GalleryDetailTagsSyncTask(result)
syncTask.start()

// Notify success
GalleryDetailScene scene = getScene();
if (scene != null) {
scene.onGetGalleryDetailSuccess(result);
// if (resultMode == RESULT_DETAIL) {
// scene.onGetGalleryDetailSuccess(result);
// return;
// }
// scene.onGetGalleryDetailUpdateSuccess(result, SpiderInfo.getSpiderInfo(result), newPath(result));
}
val scene = scene
scene?.onGetGalleryDetailSuccess(result)
}

@Override
public void onFailure(Exception e) {
getApplication().removeGlobalStuff(this);
GalleryDetailScene scene = getScene();
override fun onFailure(e: Exception) {
application.removeGlobalStuff(this)
val scene = scene
if (scene != null) {
if (resultMode == RESULT_DETAIL) {
scene.onGetGalleryDetailFailure(e);
return;
scene.onGetGalleryDetailFailure(e)
return
}
scene.onGetGalleryDetailUpdateFailure(e);
scene.onGetGalleryDetailUpdateFailure(e)
}
}

@Override
public void onCancel() {
getApplication().removeGlobalStuff(this);
override fun onCancel() {
application.removeGlobalStuff(this)
}

override fun isInstance(scene: SceneFragment): Boolean {
return scene is GalleryDetailScene
}

@Override
public boolean isInstance(SceneFragment scene) {
return scene instanceof GalleryDetailScene;
private fun newPath(result: GalleryDetail): String {
return FileUtils.sanitizeFilename(
result.gid.toString() + "-" + EhUtils.getSuitableTitle(
result
)
)
}

private String newPath(GalleryDetail result) {
return FileUtils.sanitizeFilename(result.gid + "-" + EhUtils.getSuitableTitle(result));
companion object {
@JvmField
var RESULT_DETAIL: Int = 1
@JvmField
var RESULT_UPDATE: Int = 0
}
}
78 changes: 77 additions & 1 deletion app/src/main/java/com/hippo/widget/LoadImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,23 @@
import androidx.annotation.DrawableRes;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hippo.conaco.Conaco;
import com.hippo.conaco.ConacoTask;
import com.hippo.conaco.Unikery;
import com.hippo.drawable.PreciselyClipDrawable;
import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.EhDB;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.client.EhCacheKeyFactory;
import com.hippo.ehviewer.client.EhClient;
import com.hippo.ehviewer.client.EhRequest;
import com.hippo.ehviewer.client.EhUrl;
import com.hippo.ehviewer.client.data.GalleryDetail;
import com.hippo.ehviewer.dao.DownloadInfo;
import com.hippo.lib.image.Image;
import com.hippo.lib.yorozuya.IntIdGenerator;
import com.hippo.util.DrawableManager;

import java.lang.annotation.Retention;
Expand All @@ -53,6 +62,11 @@
public class LoadImageView extends FixedAspectImageView implements Unikery<Image>,
View.OnClickListener, View.OnLongClickListener, Animatable {

public enum LoadSource {
DOWNLOAD_LIST, DEFAULT
}


public static final int RETRY_TYPE_NONE = 0;
public static final int RETRY_TYPE_CLICK = 1;
public static final int RETRY_TYPE_LONG_CLICK = 2;
Expand All @@ -69,6 +83,11 @@ public class LoadImageView extends FixedAspectImageView implements Unikery<Image
private int mRetryType;
public boolean mFailed;
private boolean mLoadFromDrawable;
private boolean secondTry = false;
@Nullable
private DownloadInfo downloadInfo = null;

private int mRequestId = IntIdGenerator.INVALID_ID;

public LoadImageView(Context context) {
super(context);
Expand Down Expand Up @@ -206,6 +225,11 @@ public void load(String key, String url) {
load(key, url, true);
}

public void load(String key, String url, boolean useNetwork, DownloadInfo downloadInfo) {
this.downloadInfo = downloadInfo;
load(key, url, useNetwork);
}

public void load(String key, String url, boolean useNetwork) {
if (url == null || key == null) {
return;
Expand Down Expand Up @@ -282,7 +306,7 @@ public boolean onGetValue(@NonNull Image value, int source) {

if (Integer.MIN_VALUE != mOffsetX) {
Drawable.ConstantState state = value.getDrawable().getConstantState();
if (state!=null){
if (state != null) {
drawable = state.newDrawable();
}
drawable = new PreciselyClipDrawable(drawable, mOffsetX, mOffsetY, mClipWidth, mClipHeight);
Expand Down Expand Up @@ -321,10 +345,26 @@ public void onFailure() {
} else if (mRetryType == RETRY_TYPE_LONG_CLICK) {
setOnLongClickListener(this);
} else {
if (secondTry && downloadInfo != null) {
Context context = this.getContext();
if (EhApplication.getInstance().containGlobalStuff(mRequestId)) {
// request exist
return;
}
String detailUrl = EhUrl.getGalleryDetailUrl(downloadInfo.gid, downloadInfo.token);
GalleryDetailCallback callback = new GalleryDetailCallback(context);
mRequestId = ((EhApplication) context.getApplicationContext()).putGlobalStuff(callback);
EhRequest request = new EhRequest()
.setMethod(EhClient.METHOD_GET_GALLERY_DETAIL)
.setArgs(detailUrl)
.setCallback(callback);
EhApplication.getEhClient(context).execute(request);
}
// Can't retry, so release
mKey = null;
mUrl = null;
}

}

@Override
Expand Down Expand Up @@ -384,4 +424,40 @@ public void onPreSetImageResource(int resId, boolean isTarget) {
@Retention(RetentionPolicy.SOURCE)
private @interface RetryType {
}

private class GalleryDetailCallback implements EhClient.Callback<GalleryDetail> {
private final Context context;
private final EhApplication ehApplication;

private GalleryDetailCallback(Context context) {
this.context = context;
this.ehApplication = (EhApplication) context.getApplicationContext();
}

@Override
public void onSuccess(GalleryDetail result) {
ehApplication.removeGlobalStuff(this);
secondTry = true;
if (context == null||result == null || downloadInfo == null) {
return;
}
// Put gallery detail to cache
EhApplication.getGalleryDetailCache(context).put(result.gid, result);
if (downloadInfo.gid == result.gid && !downloadInfo.thumb.equals(result.thumb)) {
downloadInfo.updateInfo(result);
EhDB.putDownloadInfo(downloadInfo);
load(EhCacheKeyFactory.getThumbKey(downloadInfo.gid), downloadInfo.thumb, true);
}
}

@Override
public void onFailure(Exception e) {
ehApplication.removeGlobalStuff(this);
}

@Override
public void onCancel() {
ehApplication.removeGlobalStuff(this);
}
}
}

0 comments on commit 3f8c11e

Please sign in to comment.