Fix sync bug

release
FongMi 4 weeks ago
parent 1f00054da8
commit b5713c86ca
  1. 41
      app/src/main/java/com/fongmi/android/tv/server/process/Action.java
  2. 11
      app/src/mobile/java/com/fongmi/android/tv/ui/dialog/SyncDialog.java

@ -110,18 +110,25 @@ public class Action implements Process {
}
private void onSync(Map<String, String> params) {
boolean keep = Objects.equals(params.get("type"), "keep");
String type = params.get("type");
boolean force = Objects.equals(params.get("force"), "true");
boolean history = Objects.equals(params.get("type"), "history");
String mode = Objects.requireNonNullElse(params.get("mode"), "0");
if (params.get("device") != null && (mode.equals("0") || mode.equals("2"))) {
Device device = Device.objectFrom(params.get("device"));
if (history) sendHistory(device, params);
else if (keep) sendKeep(device);
if ("history".equals(type)) sendHistory(device, params);
else if ("keep".equals(type)) sendKeep(device);
}
if (mode.equals("0") || mode.equals("1")) {
if (history) syncHistory(params, force);
else if (keep) syncKeep(params, force);
if ("history".equals(type)) syncHistory(params, force);
else if ("keep".equals(type)) syncKeep(params, force);
}
}
private void post(Device device, String type, FormBody.Builder body) {
try {
OkHttp.newCall(OkHttp.client(Constant.TIMEOUT_SYNC), device.getIp().concat("/action?do=sync&mode=0&type=" + type), body.build()).execute();
} catch (Exception e) {
App.post(() -> Notify.show(e.getMessage()));
}
}
@ -132,7 +139,7 @@ public class Action implements Process {
FormBody.Builder body = new FormBody.Builder();
body.add("config", config.toString());
body.add("targets", App.gson().toJson(History.get(config.getId())));
OkHttp.newCall(OkHttp.client(Constant.TIMEOUT_SYNC), device.getIp().concat("/action?do=sync&mode=0&type=history"), body.build()).execute();
post(device, "history", body);
} catch (Exception e) {
App.post(() -> Notify.show(e.getMessage()));
}
@ -143,7 +150,7 @@ public class Action implements Process {
FormBody.Builder body = new FormBody.Builder();
body.add("targets", App.gson().toJson(Keep.getVod()));
body.add("configs", App.gson().toJson(Config.findUrls()));
OkHttp.newCall(OkHttp.client(Constant.TIMEOUT_SYNC), device.getIp().concat("/action?do=sync&mode=0&type=keep"), body.build()).execute();
post(device, "keep", body);
} catch (Exception e) {
App.post(() -> Notify.show(e.getMessage()));
}
@ -158,18 +165,19 @@ public class Action implements Process {
History.sync(targets);
RefreshEvent.history();
} else {
VodConfig.load(config, getCallback(targets));
VodConfig.load(config, getCallback(targets, force, config.getId()));
}
}
private Callback getCallback(List<History> targets) {
private Callback getCallback(List<History> targets, boolean force, int cid) {
return new Callback() {
@Override
public void success() {
RefreshEvent.config();
RefreshEvent.video();
if (force) History.delete(cid);
History.sync(targets);
RefreshEvent.history();
RefreshEvent.config();
RefreshEvent.video();
}
@Override
@ -183,7 +191,7 @@ public class Action implements Process {
List<Keep> targets = Keep.arrayFrom(params.get("targets"));
List<Config> configs = Config.arrayFrom(params.get("configs"));
if (TextUtils.isEmpty(VodConfig.getUrl()) && !configs.isEmpty()) {
VodConfig.load(Config.find(configs.get(0)), getCallback(configs, targets));
VodConfig.load(Config.find(configs.get(0)), getCallback(configs, targets, force));
} else {
if (force) Keep.deleteAll();
Keep.sync(configs, targets);
@ -191,14 +199,15 @@ public class Action implements Process {
}
}
private Callback getCallback(List<Config> configs, List<Keep> targets) {
private Callback getCallback(List<Config> configs, List<Keep> targets, boolean force) {
return new Callback() {
@Override
public void success() {
RefreshEvent.config();
RefreshEvent.video();
if (force) Keep.deleteAll();
Keep.sync(configs, targets);
RefreshEvent.history();
RefreshEvent.config();
RefreshEvent.video();
RefreshEvent.keep();
}

@ -168,12 +168,17 @@ public class SyncDialog extends BaseDialog implements DeviceAdapter.OnClickListe
public boolean onLongClick(Device item) {
String mode = binding.mode.getTag().toString();
if (mode.equals("0")) return false;
if (mode.equals("2") && type.equals("keep")) Keep.deleteAll();
if (mode.equals("2") && type.equals("history")) History.delete(VodConfig.getCid());
OkHttp.newCall(client, String.format(Locale.getDefault(), "%s/action?do=sync&mode=%s&type=%s&force=true", item.getIp(), binding.mode.getTag().toString(), type), body.build()).enqueue(getCallback());
if (mode.equals("2")) deleteLocal();
String force = mode.equals("1") ? "&force=true" : "";
OkHttp.newCall(client, String.format(Locale.getDefault(), "%s/action?do=sync&mode=%s&type=%s%s", item.getIp(), mode, type, force), body.build()).enqueue(getCallback());
return true;
}
private void deleteLocal() {
if (type.equals("keep")) Keep.deleteAll();
if (type.equals("history")) History.delete(VodConfig.getCid());
}
private Callback getCallback() {
return new Callback() {
@Override

Loading…
Cancel
Save