3737import androidx .annotation .DrawableRes ;
3838import androidx .annotation .IntDef ;
3939import androidx .annotation .NonNull ;
40+ import androidx .annotation .Nullable ;
4041
4142import com .hippo .conaco .Conaco ;
4243import com .hippo .conaco .ConacoTask ;
4344import com .hippo .conaco .Unikery ;
4445import com .hippo .drawable .PreciselyClipDrawable ;
4546import com .hippo .ehviewer .EhApplication ;
47+ import com .hippo .ehviewer .EhDB ;
4648import 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 ;
4755import com .hippo .lib .image .Image ;
56+ import com .hippo .lib .yorozuya .IntIdGenerator ;
4857import com .hippo .util .DrawableManager ;
4958
5059import java .lang .annotation .Retention ;
5362public 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