Skip to content

Commit 3f8c11e

Browse files
committed
在下载列表略缩图失效或丢失时,尝试自动获取新的略缩图连接
1 parent d2b4433 commit 3f8c11e

File tree

5 files changed

+135
-57
lines changed

5 files changed

+135
-57
lines changed

app/src/main/java/com/hippo/conaco/ConacoTask.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ protected V doInBackground(Void... params) {
408408
Response response = mCall.execute();
409409
ResponseBody body = response.body();
410410
if (body == null) {
411+
411412
return null;
412413
}
413414
is = body.byteStream();

app/src/main/java/com/hippo/ehviewer/client/EhClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.hippo.ehviewer.client;
1818

19+
import android.annotation.SuppressLint;
1920
import android.content.Context;
2021
import android.os.AsyncTask;
2122

@@ -88,6 +89,7 @@ public void execute(EhRequest request) {
8889
}
8990
}
9091

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

9395
private final int mMethod;

app/src/main/java/com/hippo/ehviewer/client/EhUrl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
package com.hippo.ehviewer.client;
1818

1919
import androidx.annotation.NonNull;
20+
import androidx.annotation.Nullable;
21+
2022
import com.hippo.ehviewer.Settings;
23+
import com.hippo.ehviewer.client.data.GalleryInfo;
2124
import com.hippo.network.UrlBuilder;
2225
import java.util.List;
2326
import java.util.ListIterator;
@@ -287,7 +290,7 @@ public static String getFixedPreviewThumbUrl(String originUrl) {
287290
HttpUrl url = HttpUrl.parse(originUrl);
288291
if (url == null) return originUrl;
289292
List<String> pathSegments = url.pathSegments();
290-
if (pathSegments == null || pathSegments.size() < 3) return originUrl;
293+
if (pathSegments.size() < 3) return originUrl;
291294

292295
ListIterator<String> iterator = pathSegments.listIterator(pathSegments.size());
293296
// The last segments, like
@@ -312,4 +315,5 @@ public static String getFixedPreviewThumbUrl(String originUrl) {
312315
return originUrl;
313316
}
314317
}
318+
315319
}
Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,73 @@
1-
package com.hippo.ehviewer.ui.scene.gallery.detail;
1+
package com.hippo.ehviewer.ui.scene.gallery.detail
22

3-
import android.content.Context;
4-
5-
import com.hippo.ehviewer.EhApplication;
6-
import com.hippo.ehviewer.EhDB;
7-
import com.hippo.ehviewer.client.EhUtils;
8-
import com.hippo.ehviewer.client.data.GalleryDetail;
9-
import com.hippo.ehviewer.sync.GalleryDetailTagsSyncTask;
10-
import com.hippo.ehviewer.ui.scene.EhCallback;
11-
import com.hippo.scene.SceneFragment;
12-
import com.hippo.lib.yorozuya.FileUtils;
13-
14-
public class GetGalleryDetailListener extends EhCallback<GalleryDetailScene, GalleryDetail> {
15-
16-
public static int RESULT_DETAIL = 1;
17-
public static int RESULT_UPDATE = 0;
18-
19-
private final int resultMode;
20-
21-
public GetGalleryDetailListener(Context context, int stageId, String sceneTag, int resultMode) {
22-
super(context, stageId, sceneTag);
23-
this.resultMode = resultMode;
24-
}
25-
26-
@Override
27-
public void onSuccess(GalleryDetail result) {
28-
getApplication().removeGlobalStuff(this);
3+
import android.content.Context
4+
import com.hippo.ehviewer.EhApplication
5+
import com.hippo.ehviewer.EhDB
6+
import com.hippo.ehviewer.client.EhUtils
7+
import com.hippo.ehviewer.client.data.GalleryDetail
8+
import com.hippo.ehviewer.sync.GalleryDetailTagsSyncTask
9+
import com.hippo.ehviewer.ui.scene.EhCallback
10+
import com.hippo.lib.yorozuya.FileUtils
11+
import com.hippo.scene.SceneFragment
2912

13+
class GetGalleryDetailListener(
14+
context: Context?,
15+
stageId: Int,
16+
sceneTag: String?,
17+
private val resultMode: Int
18+
) : EhCallback<GalleryDetailScene?, GalleryDetail?>(context, stageId, sceneTag) {
19+
override fun onSuccess(result: GalleryDetail?) {
20+
application.removeGlobalStuff(this)
21+
if (result==null){
22+
return
23+
}
3024
// Put gallery detail to cache
31-
EhApplication.getGalleryDetailCache(getApplication()).put(result.gid, result);
25+
EhApplication.getGalleryDetailCache(application).put(result.gid, result)
3226

3327
// Add history
34-
EhDB.putHistoryInfo(result);
28+
EhDB.putHistoryInfo(result)
3529

3630
// Save tags
37-
GalleryDetailTagsSyncTask syncTask = new GalleryDetailTagsSyncTask(result);
38-
syncTask.start();
31+
val syncTask = GalleryDetailTagsSyncTask(result)
32+
syncTask.start()
3933

4034
// Notify success
41-
GalleryDetailScene scene = getScene();
42-
if (scene != null) {
43-
scene.onGetGalleryDetailSuccess(result);
44-
// if (resultMode == RESULT_DETAIL) {
45-
// scene.onGetGalleryDetailSuccess(result);
46-
// return;
47-
// }
48-
// scene.onGetGalleryDetailUpdateSuccess(result, SpiderInfo.getSpiderInfo(result), newPath(result));
49-
}
35+
val scene = scene
36+
scene?.onGetGalleryDetailSuccess(result)
5037
}
5138

52-
@Override
53-
public void onFailure(Exception e) {
54-
getApplication().removeGlobalStuff(this);
55-
GalleryDetailScene scene = getScene();
39+
override fun onFailure(e: Exception) {
40+
application.removeGlobalStuff(this)
41+
val scene = scene
5642
if (scene != null) {
5743
if (resultMode == RESULT_DETAIL) {
58-
scene.onGetGalleryDetailFailure(e);
59-
return;
44+
scene.onGetGalleryDetailFailure(e)
45+
return
6046
}
61-
scene.onGetGalleryDetailUpdateFailure(e);
47+
scene.onGetGalleryDetailUpdateFailure(e)
6248
}
6349
}
6450

65-
@Override
66-
public void onCancel() {
67-
getApplication().removeGlobalStuff(this);
51+
override fun onCancel() {
52+
application.removeGlobalStuff(this)
53+
}
54+
55+
override fun isInstance(scene: SceneFragment): Boolean {
56+
return scene is GalleryDetailScene
6857
}
6958

70-
@Override
71-
public boolean isInstance(SceneFragment scene) {
72-
return scene instanceof GalleryDetailScene;
59+
private fun newPath(result: GalleryDetail): String {
60+
return FileUtils.sanitizeFilename(
61+
result.gid.toString() + "-" + EhUtils.getSuitableTitle(
62+
result
63+
)
64+
)
7365
}
7466

75-
private String newPath(GalleryDetail result) {
76-
return FileUtils.sanitizeFilename(result.gid + "-" + EhUtils.getSuitableTitle(result));
67+
companion object {
68+
@JvmField
69+
var RESULT_DETAIL: Int = 1
70+
@JvmField
71+
var RESULT_UPDATE: Int = 0
7772
}
7873
}

app/src/main/java/com/hippo/widget/LoadImageView.java

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,23 @@
3737
import androidx.annotation.DrawableRes;
3838
import androidx.annotation.IntDef;
3939
import androidx.annotation.NonNull;
40+
import androidx.annotation.Nullable;
4041

4142
import com.hippo.conaco.Conaco;
4243
import com.hippo.conaco.ConacoTask;
4344
import com.hippo.conaco.Unikery;
4445
import com.hippo.drawable.PreciselyClipDrawable;
4546
import com.hippo.ehviewer.EhApplication;
47+
import com.hippo.ehviewer.EhDB;
4648
import com.hippo.ehviewer.R;
49+
import com.hippo.ehviewer.client.EhCacheKeyFactory;
50+
import com.hippo.ehviewer.client.EhClient;
51+
import com.hippo.ehviewer.client.EhRequest;
52+
import com.hippo.ehviewer.client.EhUrl;
53+
import com.hippo.ehviewer.client.data.GalleryDetail;
54+
import com.hippo.ehviewer.dao.DownloadInfo;
4755
import com.hippo.lib.image.Image;
56+
import com.hippo.lib.yorozuya.IntIdGenerator;
4857
import com.hippo.util.DrawableManager;
4958

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

65+
public enum LoadSource {
66+
DOWNLOAD_LIST, DEFAULT
67+
}
68+
69+
5670
public static final int RETRY_TYPE_NONE = 0;
5771
public static final int RETRY_TYPE_CLICK = 1;
5872
public static final int RETRY_TYPE_LONG_CLICK = 2;
@@ -69,6 +83,11 @@ public class LoadImageView extends FixedAspectImageView implements Unikery<Image
6983
private int mRetryType;
7084
public boolean mFailed;
7185
private boolean mLoadFromDrawable;
86+
private boolean secondTry = false;
87+
@Nullable
88+
private DownloadInfo downloadInfo = null;
89+
90+
private int mRequestId = IntIdGenerator.INVALID_ID;
7291

7392
public LoadImageView(Context context) {
7493
super(context);
@@ -206,6 +225,11 @@ public void load(String key, String url) {
206225
load(key, url, true);
207226
}
208227

228+
public void load(String key, String url, boolean useNetwork, DownloadInfo downloadInfo) {
229+
this.downloadInfo = downloadInfo;
230+
load(key, url, useNetwork);
231+
}
232+
209233
public void load(String key, String url, boolean useNetwork) {
210234
if (url == null || key == null) {
211235
return;
@@ -282,7 +306,7 @@ public boolean onGetValue(@NonNull Image value, int source) {
282306

283307
if (Integer.MIN_VALUE != mOffsetX) {
284308
Drawable.ConstantState state = value.getDrawable().getConstantState();
285-
if (state!=null){
309+
if (state != null) {
286310
drawable = state.newDrawable();
287311
}
288312
drawable = new PreciselyClipDrawable(drawable, mOffsetX, mOffsetY, mClipWidth, mClipHeight);
@@ -321,10 +345,26 @@ public void onFailure() {
321345
} else if (mRetryType == RETRY_TYPE_LONG_CLICK) {
322346
setOnLongClickListener(this);
323347
} else {
348+
if (secondTry && downloadInfo != null) {
349+
Context context = this.getContext();
350+
if (EhApplication.getInstance().containGlobalStuff(mRequestId)) {
351+
// request exist
352+
return;
353+
}
354+
String detailUrl = EhUrl.getGalleryDetailUrl(downloadInfo.gid, downloadInfo.token);
355+
GalleryDetailCallback callback = new GalleryDetailCallback(context);
356+
mRequestId = ((EhApplication) context.getApplicationContext()).putGlobalStuff(callback);
357+
EhRequest request = new EhRequest()
358+
.setMethod(EhClient.METHOD_GET_GALLERY_DETAIL)
359+
.setArgs(detailUrl)
360+
.setCallback(callback);
361+
EhApplication.getEhClient(context).execute(request);
362+
}
324363
// Can't retry, so release
325364
mKey = null;
326365
mUrl = null;
327366
}
367+
328368
}
329369

330370
@Override
@@ -384,4 +424,40 @@ public void onPreSetImageResource(int resId, boolean isTarget) {
384424
@Retention(RetentionPolicy.SOURCE)
385425
private @interface RetryType {
386426
}
427+
428+
private class GalleryDetailCallback implements EhClient.Callback<GalleryDetail> {
429+
private final Context context;
430+
private final EhApplication ehApplication;
431+
432+
private GalleryDetailCallback(Context context) {
433+
this.context = context;
434+
this.ehApplication = (EhApplication) context.getApplicationContext();
435+
}
436+
437+
@Override
438+
public void onSuccess(GalleryDetail result) {
439+
ehApplication.removeGlobalStuff(this);
440+
secondTry = true;
441+
if (context == null||result == null || downloadInfo == null) {
442+
return;
443+
}
444+
// Put gallery detail to cache
445+
EhApplication.getGalleryDetailCache(context).put(result.gid, result);
446+
if (downloadInfo.gid == result.gid && !downloadInfo.thumb.equals(result.thumb)) {
447+
downloadInfo.updateInfo(result);
448+
EhDB.putDownloadInfo(downloadInfo);
449+
load(EhCacheKeyFactory.getThumbKey(downloadInfo.gid), downloadInfo.thumb, true);
450+
}
451+
}
452+
453+
@Override
454+
public void onFailure(Exception e) {
455+
ehApplication.removeGlobalStuff(this);
456+
}
457+
458+
@Override
459+
public void onCancel() {
460+
ehApplication.removeGlobalStuff(this);
461+
}
462+
}
387463
}

0 commit comments

Comments
 (0)