zhipeng пре 3 година
родитељ
комит
0a540513f2

+ 39 - 2
src/main/java/com/atmob/task/AppTaskView.java

@@ -41,6 +41,8 @@ public class AppTaskView extends TwinklingRecyclerView implements AppTaskItemAda
 
     private OnAppTaskRewardRequestListener onAppTaskRewardRequestListener;
 
+    private OnAppTaskLoadListener onAppTaskLoadListener;
+
     private boolean loadingFlag;
 
     private Handler handler;
@@ -88,13 +90,25 @@ public class AppTaskView extends TwinklingRecyclerView implements AppTaskItemAda
                     @Override
                     public void onSuccess(AppTaskDataResponse data) {
                         loadingFlag = false;
-                        if (data == null) {
+                        if (data == null || data.getPtasks() == null || data.getPtasks().size() == 0) {
+                            if (onAppTaskLoadListener != null) {
+                                onAppTaskLoadListener.onLoaded(0);
+                            }
                             return;
                         }
+                        int activeCount = 0;
+                        for (AppTaskBean ptask : data.getPtasks()) {
+                            if (ptask.getTaskStatus() != AppTaskBean.AppTaskStatus.Finish) {
+                                activeCount++;
+                            }
+                        }
                         AppTaskBean.CDN = data.getUrl();
                         if (data.getPtasks() != null) {
                             appTaskItemAdapter.update(data.getPtasks());
                         }
+                        if (onAppTaskLoadListener != null) {
+                            onAppTaskLoadListener.onLoaded(activeCount);
+                        }
                         AppTaskEvent.report(AppTaskEvent.ATE_LOAD_DATA_SUCCESS);
                     }
 
@@ -137,6 +151,11 @@ public class AppTaskView extends TwinklingRecyclerView implements AppTaskItemAda
         return this;
     }
 
+    public AppTaskView setOnAppTaskEmptyListener(OnAppTaskLoadListener onAppTaskLoadListener) {
+        this.onAppTaskLoadListener = onAppTaskLoadListener;
+        return this;
+    }
+
     /**
      * 触发积分墙列表第一个可触发的积分墙任务
      */
@@ -160,7 +179,13 @@ public class AppTaskView extends TwinklingRecyclerView implements AppTaskItemAda
      * 刷新积分墙任务
      */
     public void refreshTaskList() {
-        loadData();
+        if (getVisibility() == VISIBLE) {
+            loadData();
+        }
+    }
+
+    public void setMaxLength(int length) {
+        appTaskItemAdapter.setMaxLength(length);
     }
 
     @Override
@@ -174,6 +199,14 @@ public class AppTaskView extends TwinklingRecyclerView implements AppTaskItemAda
     }
 
     @Override
+    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
+        super.onVisibilityChanged(changedView, visibility);
+        if (visibility == VISIBLE) {
+            loadData();
+        }
+    }
+
+    @Override
     public void onVisibilityAggregated(boolean isVisible) {
         super.onVisibilityAggregated(isVisible);
         onVisibilityAggregatedInternal(isVisible);
@@ -230,6 +263,10 @@ public class AppTaskView extends TwinklingRecyclerView implements AppTaskItemAda
         void onClick(@AppTaskBean.AppTaskStatus int appTaskStatus);
     }
 
+    public interface OnAppTaskLoadListener {
+        void onLoaded(int size);
+    }
+
     public abstract static class AppTaskViewHolder extends RecyclerView.ViewHolder {
 
         public AppTaskViewHolder(@NonNull View itemView) {

+ 50 - 32
src/main/java/com/atmob/task/adapter/AppTaskItemAdapter.java

@@ -41,10 +41,12 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import atmob.io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
 import atmob.io.reactivex.rxjava3.core.Completable;
 import atmob.io.reactivex.rxjava3.core.CompletableObserver;
 import atmob.io.reactivex.rxjava3.core.Observable;
 import atmob.io.reactivex.rxjava3.core.Observer;
+import atmob.io.reactivex.rxjava3.core.SingleObserver;
 import atmob.io.reactivex.rxjava3.disposables.CompositeDisposable;
 import atmob.io.reactivex.rxjava3.disposables.Disposable;
 import atmob.io.reactivex.rxjava3.schedulers.Schedulers;
@@ -72,6 +74,10 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
 
     private AppTaskAdapter<T> appTaskAdapter;
 
+    private int maxLength = Integer.MAX_VALUE;
+
+    private List<AppTaskDownloadWrapper> dataCache = Collections.EMPTY_LIST;
+
     public AppTaskItemAdapter(RecyclerView recyclerView, AppTaskAdapter<T> appTaskAdapter, CompositeDisposable compositeDisposable) {
         this.compositeDisposable = compositeDisposable;
         this.appTaskAdapter = appTaskAdapter;
@@ -229,7 +235,7 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
             ATLog.d(TAG, "toOpen: app exist, open it.");
         } else {
             AppTaskEvent.report(AppTaskEvent.ATE_DOWNLOAD);
-            toDownload(appTaskDownloadWrapper);
+            toInstall(appTaskDownloadWrapper);
             ATLog.d(TAG, "toOpen: app not exist, try to download.");
         }
     }
@@ -358,41 +364,34 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
 
     public void update(ArrayList<AppTaskBean> appTaskBeans) {
         ATLog.d(TAG, "update() called with: appTaskBeans = [" + appTaskBeans + "]");
-        ArrayList<AppTaskDownloadWrapper> appTaskDownloadWrappers = new ArrayList<>();
         Observable.fromIterable(appTaskBeans)
                 .filter(appTaskBean -> appTaskBean.getTaskStatus() != AppTaskBean.AppTaskStatus.Finish)
                 .map(AppTaskDownloadWrapper::new)
-                .compose(RxSchedulersUtils.observableIO2Main())
-                .subscribe(new Observer<AppTaskDownloadWrapper>() {
+                .doOnNext(appTaskDownloadWrapper -> {
+                    if (RetrofitDownloader.isActiveTaskExist(appTaskDownloadWrapper.getAppLink())) {
+                        toDownload(appTaskDownloadWrapper);
+                    }
+                })
+                .toList()
+                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
+                .subscribe(new SingleObserver<List<AppTaskDownloadWrapper>>() {
                     @Override
-                    public void onSubscribe(@atmob.io.reactivex.rxjava3.annotations.NonNull Disposable d) {
+                    public void onSubscribe(Disposable d) {
                         compositeDisposable.add(d);
                         ATLog.d(TAG, "onSubscribe() called with: d = [" + d + "]");
                     }
 
                     @Override
-                    public void onNext(@atmob.io.reactivex.rxjava3.annotations.NonNull AppTaskDownloadWrapper appTaskDownloadWrapper) {
-                        ATLog.d(TAG, "onNext() called with: appTaskDownloadWrapper = [" + appTaskDownloadWrapper + "]");
-                        if (RetrofitDownloader.isActiveTaskExist(appTaskDownloadWrapper.getAppLink())) {
-                            toDownload(appTaskDownloadWrapper);
-                        }
-                        appTaskDownloadWrappers.add(appTaskDownloadWrapper);
+                    public void onSuccess(List<AppTaskDownloadWrapper> list) {
+                        ATLog.d(TAG, "onComplete: app task list update success, total length ==> " + list.size());
+                        Collections.reverse(list);
+                        submit(list);
                     }
 
                     @Override
-                    public void onError(@atmob.io.reactivex.rxjava3.annotations.NonNull Throwable e) {
+                    public void onError(Throwable e) {
                         ATLog.e(TAG, "onError: AppTaskBean transform to AppTaskDownloadWrapper error", e);
                     }
-
-                    @Override
-                    public void onComplete() {
-                        ATLog.d(TAG, "onComplete: app task list update success, total length ==> " + appTaskDownloadWrappers.size());
-                        Collections.reverse(appTaskDownloadWrappers);
-                        if (appTaskDownloadWrappers.size() != asyncListDiffer.getCurrentList().size()) {
-                            recyclerView.setHasFixedSize(false);
-                        }
-                        asyncListDiffer.submitList(appTaskDownloadWrappers);
-                    }
                 });
     }
 
@@ -464,8 +463,6 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
 
     public void release() {
         RetrofitDownloader.clearAllObserver();
-        recyclerView = null;
-        appTaskAdapter = null;
         ATLog.d(TAG, "release: clear all observer.");
     }
 
@@ -497,16 +494,14 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
                     @Override
                     public void onSuccess(AppTaskUpdateResponse data) {
                         List<AppTaskDownloadWrapper> currentList = asyncListDiffer.getCurrentList();
-                        Iterator<AppTaskDownloadWrapper> iterator = currentList.iterator();
-                        while (iterator.hasNext()) {
-                            AppTaskDownloadWrapper next = iterator.next();
-                            if (next == null) {
+                        for (AppTaskDownloadWrapper wrapper : currentList) {
+                            if (wrapper == null) {
                                 continue;
                             }
-                            if (next.getId() == id) {
-                                int i = currentList.indexOf(next);
-                                iterator.remove();
-                                notifyItemRemoved(i);
+                            if (wrapper.getId() == id) {
+                                int i = currentList.indexOf(wrapper);
+                                wrapper.setTaskStatus(targetStatus);
+                                notifyItemChanged(i);
                                 return;
                             }
                         }
@@ -521,6 +516,29 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
                 });
     }
 
+    public void setMaxLength(int length) {
+        maxLength = length < 0 ? Integer.MAX_VALUE : length;
+        if (getItemCount() == 0) {
+            return;
+        }
+        if (dataCache.size() == getItemCount() && getItemCount() < maxLength) {
+            return;
+        }
+        if (getItemCount() == maxLength) {
+            return;
+        }
+        submit(dataCache);
+    }
+
+    private void submit(List<AppTaskDownloadWrapper> list) {
+        dataCache = list;
+        List<AppTaskDownloadWrapper> subList = dataCache.subList(0, Math.min(maxLength, dataCache.size()));
+        if (subList.size() != asyncListDiffer.getCurrentList().size()) {
+            recyclerView.setHasFixedSize(false);
+        }
+        asyncListDiffer.submitList(subList);
+    }
+
     private static class DiffCallback extends DiffUtil.ItemCallback<AppTaskDownloadWrapper> {
 
         @Override

+ 1 - 3
src/main/java/com/atmob/task/utils/ATLog.java

@@ -2,10 +2,8 @@ package com.atmob.task.utils;
 
 import android.util.Log;
 
-import com.atmob.task.BuildConfig;
-
 public class ATLog {
-    private static final boolean LogEnable = BuildConfig.DEBUG;
+    private static final boolean LogEnable = true;
 
     public static void v(String TAG, String format, Object... args) {
         if (LogEnable) {