切换CountDownLatch用Observable.merge

This commit is contained in:
huangzhuanghua
2016-12-13 15:30:02 +08:00
parent 8b0faa57aa
commit f05f95dc41
7 changed files with 176 additions and 261 deletions

View File

@ -37,6 +37,7 @@ import com.gh.common.util.PlatformUtils;
import com.gh.common.util.RandomUtils;
import com.gh.common.util.TokenUtils;
import com.gh.common.util.TrafficUtils;
import com.gh.common.util.Utils;
import com.gh.download.DataWatcher;
import com.gh.download.DownloadEntity;
import com.gh.download.DownloadManager;
@ -417,31 +418,24 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
EventBus.getDefault().post(new EBUISwitch("NewsFragment", -2)); // 没有关注
}
final CountDownLatch latch = ObservableUtil.latch(arrGameId.size(), new Action1<Object>() {
@Override
public void call(Object o) {
concernManager.addByList(concernDigest);
}
});
List<Observable<GameEntity>> list = new ArrayList<>();
for (String gameId : arrGameId) {
RetrofitManager.getApi().getGameDigest(gameId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<GameEntity>() {
@Override
public void onResponse(GameEntity response) {
concernDigest.add(response);
latch.countDown();
}
@Override
public void onFailure(Throwable e) {
latch.countDown();
}
});
list.add(RetrofitManager.getApi().getGameDigest(gameId));
}
Observable.merge(list)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<GameEntity>() {
@Override
public void onNext(GameEntity response) {
concernDigest.add(response);
}
@Override
public void onCompleted() {
concernManager.addByList(concernDigest);
}
});
}
private void initViews() {
@ -538,47 +532,37 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
if (concernIdList.isEmpty()) {
update();
} else {
final CountDownLatch latch = ObservableUtil.latch(concernIdList.size(), new Action1<Object>() {
@Override
public void call(Object o) {
update();
}
});
List<Observable<GameEntity>> sequences = new ArrayList<>();
for (String id : concernIdList) {
RetrofitManager.getApi().getGameDigest(id)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<GameEntity>() {
@Override
public void onResponse(GameEntity response) {
ConcernInfo concernInfo = concernManager.findConcernById(response.getId());
if (concernInfo != null && response.getApk() != null
&& response.getApk().size() != 0) {
HashMap<String, Boolean> packageNames = new HashMap<>();
String packageName;
for (int i = 0, size = response.getApk().size(); i < size; i++) {
packageName = response.getApk().get(i).getPackageName();
if (PackageManager.isInstalled(packageName)) {
packageNames.put(packageName, true);
} else {
packageNames.put(packageName, false);
}
}
concernInfo.setTime(System.currentTimeMillis());
concernInfo.setPackageNames(packageNames);
concernManager.updateByConcern(concernInfo);
}
latch.countDown();
}
@Override
public void onFailure(Throwable e) {
latch.countDown();
}
});
sequences.add(RetrofitManager.getApi().getGameDigest(id));
}
Observable.merge(sequences)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<GameEntity>() {
@Override
public void onNext(GameEntity response) {
ConcernInfo concernInfo = concernManager.findConcernById(response.getId());
if (concernInfo != null && response.getApk() != null && response.getApk().size() != 0) {
HashMap<String, Boolean> packageNames = new HashMap<>();
for (ApkEntity apkEntity : response.getApk()) {
if (PackageManager.isInstalled(apkEntity.getPackageName())) {
packageNames.put(apkEntity.getPackageName(), true);
} else {
packageNames.put(apkEntity.getPackageName(), false);
}
}
concernInfo.setTime(System.currentTimeMillis());
concernInfo.setPackageNames(packageNames);
concernManager.updateByConcern(concernInfo);
}
}
@Override
public void onCompleted() {
update();
}
});
}
}
@ -700,34 +684,25 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
}
private void checkGamePlugin() {
List<ConcernInfo> infos = concernManager.getInstalledGame();
final List<GameEntity> list = new ArrayList<>();
final CountDownLatch latch = ObservableUtil.latch(infos.size(), new Action1<Object>() {
@Override
public void call(Object o) {
processPluginData(list);
}
});
for (ConcernInfo info : infos) {
RetrofitManager.getApi().getGameDigest(info.getId())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<GameEntity>() {
@Override
public void onResponse(GameEntity response) {
list.add(response);
latch.countDown();
}
@Override
public void onFailure(Throwable e) {
latch.countDown();
}
});
List<Observable<GameEntity>> sequences = new ArrayList<>();
for (ConcernInfo info : concernManager.getInstalledGame()) {
sequences.add(RetrofitManager.getApi().getGameDigest(info.getId()));
}
Observable.merge(sequences)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<GameEntity>() {
@Override
public void onNext(GameEntity response) {
list.add(response);
}
@Override
public void onCompleted() {
processPluginData(list);
}
});
}
private void processPluginData(List<GameEntity> list) {