diff --git a/TMessagesProj/src/main/java/org/telegram/group/GroupViewModel.java b/TMessagesProj/src/main/java/org/telegram/group/GroupViewModel.java index f85076d..f3386ed 100644 --- a/TMessagesProj/src/main/java/org/telegram/group/GroupViewModel.java +++ b/TMessagesProj/src/main/java/org/telegram/group/GroupViewModel.java @@ -43,7 +43,7 @@ public class GroupViewModel { private static QueryResponse response = new QueryResponse(); - private static List canches = new ArrayList<>(); + private static final List canches = new ArrayList<>(); /** * 创建 @@ -70,6 +70,22 @@ date.setTime(date.getTime() + 24L * 60L * 60L * 1000L + 300L * 1000); map.put("ed", dateFormat.format(date));//时间 map.put("dt", a == 1 ? "1" : "2");//1:自动解散,2:自由退出 + boolean flag = false; + for (LocalCanche item : + canches) { + if (item.getGroupId().equals(groupId)) { + flag = true; + break; + } + } + if (!flag) { + LocalCanche localCanche = new LocalCanche(); + localCanche.setGroupId(groupId); + localCanche.setTime(time); + localCanche.setTitle(title); + localCanche.setType(a); + canches.add(localCanche); + } observableEmitter.onNext(map); observableEmitter.onComplete(); } @@ -169,23 +185,21 @@ public static QueryResponse.AttributesBean.ListBean getGroupInfo(String groupId) { - Log.e("siuyehua-info", "1: " + groupId); - List listBeans = response.getAttributes().getList(); + List listBeans = null; + try { + listBeans = response.getAttributes().getList(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } if (listBeans == null) { return null; } - Log.e("siuyehua-info", "3: " + listBeans.size()); - for (int i = 0; i < listBeans.size(); i++) { - Log.e("siuyehua-info", "6: " + listBeans.get(i).getGroupId()); if (listBeans.get(i).getGroupId().equals(groupId)) { - Log.e("siuyehua-info", "5"); - return listBeans.get(i); } } - Log.e("siuyehua-info", "4"); - return null; } @@ -278,27 +292,61 @@ }).ignoreElements().observeOn(AndroidSchedulers.mainThread()); } + public static void removeCanche(String groupId) { + try { + synchronized (canches) { + for (int i = 0; i < canches.size(); i++) { + if (canches.get(i).getGroupId().equals(groupId)) { + canches.remove(i); + break; + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + /** * 检查缓存,如果没成功则一直上传 */ public static void checkCanche() { - if (canches.size() == 0) return; Flowable.just("").map(new Function() { @Override public Object apply(String s) throws Exception { + Log.e("apply", "size: " + canches.size()); String cancheStr = ApplicationLoader.applicationContext .getSharedPreferences("group_from_cctv", Activity.MODE_PRIVATE).getString("canche", ""); - canches = GsonUtils.getsGson().fromJson(cancheStr, new TypeToken>() { + List localCanches = GsonUtils.getsGson().fromJson(cancheStr, new TypeToken>() { }.getType()); - List listBeans = response.getAttributes().getList(); - for (int i = 0; i < listBeans.size(); i++) { - for (int j = 0; j < canches.size(); j++) { - if (listBeans.get(i).getGroupId().equals(canches.get(j).getGroupId())) { - canches.remove(j); - break; + if (localCanches != null) { + List tmpList = new ArrayList<>(); + for (LocalCanche item : localCanches) { + boolean flag = false; + for (LocalCanche item2 : canches) { + if (item.getGroupId().equals(item2.getGroupId())) { + flag = true; + break; + } + } + if(!flag){ + tmpList.add(item); + } + } + canches.addAll(tmpList); + } + if (response != null && response.getAttributes() != null && response.getAttributes().getList() != null) { + List listBeans = response.getAttributes().getList(); + for (int i = 0; i < listBeans.size(); i++) { + for (int j = 0; j < canches.size(); j++) { + if (listBeans.get(i).getGroupId().equals(canches.get(j).getGroupId())) { + canches.remove(j); + break; + } } } } + Log.e("apply2", "size: " + canches.size()); //更新要缓存 SharedPreferences.Editor editor = ApplicationLoader.applicationContext .getSharedPreferences("group_from_cctv", Activity.MODE_PRIVATE).edit(); @@ -316,12 +364,15 @@ }).concatMapDelayError(new Function>() { @Override public Publisher apply(LocalCanche localCanche) throws Exception { + Thread.sleep(5000); return create(localCanche.getGroupId(), localCanche.getTitle(), localCanche.getType(), localCanche.getTime()) .andThen(Observable.just(localCanche)).toFlowable(BackpressureStrategy.BUFFER); } }).subscribe(new Consumer() { @Override public void accept(LocalCanche s) throws Exception { + Log.e("apply3", "size: " + canches.size()); + for (int i = 0; i < canches.size(); i++) { if (canches.get(i).getGroupId().equals(s.getGroupId())) { canches.remove(i); @@ -329,8 +380,11 @@ SharedPreferences.Editor editor = ApplicationLoader.applicationContext .getSharedPreferences("group_from_cctv", Activity.MODE_PRIVATE).edit(); editor.putString("canche", GsonUtils.modeToJsonStr(canches)).apply(); + break; } } + Log.e("apply4", "size: " + canches.size()); + } }, new ErrorConsumer() { @Override @@ -341,7 +395,8 @@ }, new Action() { @Override public void run() throws Exception { - checkCanche(); + if (canches.size() != 0) + checkCanche(); } }); }