Skip to content

Commit

Permalink
🔨 Restore onReadyLoad to onStarted
Browse files Browse the repository at this point in the history
  • Loading branch information
Flyge committed Sep 21, 2017
1 parent 4778791 commit 341827d
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 29 deletions.
14 changes: 7 additions & 7 deletions docs/wiki/listener.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Sketch 支持对 `准备加载`、`完成`、`失败`、`取消` 以及 `下载

注意:
* listener 默认在主线程回调,但是当 Sketch.load() 和 Sketch.download() 开启了同步后其 listener 就在运行线程回调,可能是主线程,也可能是非主线程
* onReadyLoad() 方法只有在需要进入非主线程加载图片时才会被回调,因此有可能不回调 onReadyLoad() 方法而直接回调其它方法
* onStarted() 方法只有在需要进入非主线程加载或下载图片时才会被回调,因此有可能不回调 onStarted() 方法而直接回调其它方法

#### SketchImageView

Expand All @@ -14,8 +14,8 @@ SketchImageView sketchImageView = ...;
// setDisplayListener() 一定要在 displayImage() 之前
sketchImageView.setDisplayListener(new DisplayListener() {
@Override
public void onReadyLoad() {
// 只有在需要进入非主线程加载图片时才会回调 onReadyLoad() 方法
public void onStarted() {
// 只有在需要进入非主线程加载图片时才会回调 onStarted() 方法
}

@Override
Expand Down Expand Up @@ -52,8 +52,8 @@ sketchImageView.displayImage("http://b.zol-img.com.cn/desk/bizhi/image/4/1366x76
```java
Sketch.with(context).load("http://t.cn/RShdS1f", new LoadListener() {
@Override
public void onReadyLoad() {
// 只有在需要进入非主线程加载图片时才会回调 onReadyLoad() 方法
public void onStarted() {
// 只有在需要进入非主线程加载图片时才会回调 onStarted() 方法
}

@Override
Expand Down Expand Up @@ -83,8 +83,8 @@ Sketch.with(context).load("http://t.cn/RShdS1f", new LoadListener() {
```java
Sketch.with(context).download("http://t.cn/RShdS1f", new DownloadListener() {
@Override
public void onReadyLoad() {
// 只有在需要进入非主线程加载图片时才会回调 onReadyLoad() 方法
public void onStarted() {
// 只有在需要进入非主线程下载图片时才会回调 onStarted() 方法
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private void initOptions() {
}

@Override
public void onReadyLoad() {
public void onStarted() {
hintView.loading(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public boolean handleMessage(Message msg) {
break;

case WHAT_CALLBACK_STARTED:
((Listener) msg.obj).onReadyLoad();
((Listener) msg.obj).onStarted();
break;
case WHAT_CALLBACK_FAILED:
((Listener) msg.obj).onError(ErrorCause.valueOf(msg.getData().getString(PARAM_FAILED_CAUSE)));
Expand Down Expand Up @@ -124,7 +124,7 @@ static void postRunUpdateProgress(@NonNull AsyncRequest request, int totalLength
static void postCallbackStarted(@Nullable Listener listener, boolean sync) {
if (listener != null) {
if (sync || SketchUtils.isMainThread()) {
listener.onReadyLoad();
listener.onStarted();
} else {
handler.obtainMessage(WHAT_CALLBACK_STARTED, listener).sendToTarget();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@
* 显示监听器,值的注意的是DisplayListener中所有的方法都会在主线中执行,所以实现着不必考虑异步线程中刷新UI的问题
*/
public interface DisplayListener extends Listener {
/**
* 开始转入异步线程加载图片
*/
@Override
void onStarted();

void onCompleted(@NonNull Drawable drawable, @NonNull ImageFrom imageFrom, @NonNull ImageAttrs imageAttrs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@
* 下载监听器
*/
public interface DownloadListener extends Listener {
/**
* 开始转入异步线程下载图片
*/
@Override
void onStarted();

void onCompleted(@NonNull DownloadResult result);
}
5 changes: 1 addition & 4 deletions sketch/src/main/java/me/xiaopan/sketch/request/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
* 请求监听器,可监听准备加载、失败、取消
*/
public interface Listener {
/**
* 准备转入异步线程加载图片
*/
void onReadyLoad();
void onStarted();

/**
* 失败
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@
import android.support.annotation.NonNull;

public interface LoadListener extends Listener {
/**
* 开始转入异步线程加载图片
*/
@Override
void onStarted();

void onCompleted(@NonNull LoadResult result);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ public DisplayListenerProxy(FunctionCallbackView view) {
}

@Override
public void onReadyLoad() {
public void onStarted() {
FunctionCallbackView view = viewWeakReference.get();
if (view == null) {
return;
}

boolean needInvokeInvalidate = view.getFunctions().onDisplayReadyLoad();
boolean needInvokeInvalidate = view.getFunctions().onDisplayStarted();
if (needInvokeInvalidate) {
view.invalidate();
}

if (view.wrappedDisplayListener != null) {
view.wrappedDisplayListener.onReadyLoad();
view.wrappedDisplayListener.onStarted();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public boolean onReadyDisplay(@Nullable UriModel uriModel) {
*
* @return 是否需要调用 invalidate() 刷新 ImageView
*/
public boolean onDisplayReadyLoad() {
public boolean onDisplayStarted() {
return false;
}

Expand Down
22 changes: 11 additions & 11 deletions sketch/src/main/java/me/xiaopan/sketch/viewfun/ViewFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,39 +342,39 @@ boolean onReadyDisplay(@Nullable UriModel uriModel) {
/**
* @return true:需要调用invalidate()刷新view
*/
boolean onDisplayReadyLoad() {
boolean onDisplayStarted() {
boolean needInvokeInvalidate = false;

if (showImageFromFunction != null) {
//noinspection ConstantConditions
needInvokeInvalidate |= showImageFromFunction.onDisplayReadyLoad();
needInvokeInvalidate |= showImageFromFunction.onDisplayStarted();
}
if (showDownloadProgressFunction != null) {
needInvokeInvalidate |= showDownloadProgressFunction.onDisplayReadyLoad();
needInvokeInvalidate |= showDownloadProgressFunction.onDisplayStarted();
}
if (showGifFlagFunction != null) {
needInvokeInvalidate |= showGifFlagFunction.onDisplayReadyLoad();
needInvokeInvalidate |= showGifFlagFunction.onDisplayStarted();
}
if (showPressedFunction != null) {
needInvokeInvalidate |= showPressedFunction.onDisplayReadyLoad();
needInvokeInvalidate |= showPressedFunction.onDisplayStarted();
}
if (clickRetryFunction != null) {
needInvokeInvalidate |= clickRetryFunction.onDisplayReadyLoad();
needInvokeInvalidate |= clickRetryFunction.onDisplayStarted();
}
if (requestFunction != null) {
needInvokeInvalidate |= requestFunction.onDisplayReadyLoad();
needInvokeInvalidate |= requestFunction.onDisplayStarted();
}
if (recyclerCompatFunction != null) {
needInvokeInvalidate |= recyclerCompatFunction.onDisplayReadyLoad();
needInvokeInvalidate |= recyclerCompatFunction.onDisplayStarted();
}
if (zoomFunction != null) {
needInvokeInvalidate |= zoomFunction.onDisplayReadyLoad();
needInvokeInvalidate |= zoomFunction.onDisplayStarted();
}
if (hugeImageFunction != null) {
needInvokeInvalidate |= hugeImageFunction.onDisplayReadyLoad();
needInvokeInvalidate |= hugeImageFunction.onDisplayStarted();
}
if (clickPlayGifFunction != null) {
needInvokeInvalidate |= clickPlayGifFunction.onDisplayReadyLoad();
needInvokeInvalidate |= clickPlayGifFunction.onDisplayStarted();
}

return needInvokeInvalidate;
Expand Down

0 comments on commit 341827d

Please sign in to comment.