Commit 2c7d8c68 authored by wangfugui's avatar wangfugui

修复bug

parent e36c905e
...@@ -20,6 +20,7 @@ import cn.garymb.ygomobile.bean.DeckType; ...@@ -20,6 +20,7 @@ import cn.garymb.ygomobile.bean.DeckType;
import cn.garymb.ygomobile.bean.events.DeckFile; import cn.garymb.ygomobile.bean.events.DeckFile;
import cn.garymb.ygomobile.deck_square.api_response.BasicResponse; import cn.garymb.ygomobile.deck_square.api_response.BasicResponse;
import cn.garymb.ygomobile.deck_square.api_response.DeckIdResponse; import cn.garymb.ygomobile.deck_square.api_response.DeckIdResponse;
import cn.garymb.ygomobile.deck_square.api_response.DeckMultiIdResponse;
import cn.garymb.ygomobile.deck_square.api_response.DownloadDeckResponse; import cn.garymb.ygomobile.deck_square.api_response.DownloadDeckResponse;
import cn.garymb.ygomobile.deck_square.api_response.GetSquareDeckCondition; import cn.garymb.ygomobile.deck_square.api_response.GetSquareDeckCondition;
import cn.garymb.ygomobile.deck_square.api_response.LoginRequest; import cn.garymb.ygomobile.deck_square.api_response.LoginRequest;
...@@ -32,7 +33,6 @@ import cn.garymb.ygomobile.deck_square.api_response.PushDeckResponse; ...@@ -32,7 +33,6 @@ import cn.garymb.ygomobile.deck_square.api_response.PushDeckResponse;
import cn.garymb.ygomobile.deck_square.api_response.PushMultiDeck; import cn.garymb.ygomobile.deck_square.api_response.PushMultiDeck;
import cn.garymb.ygomobile.deck_square.api_response.PushSingleDeck; import cn.garymb.ygomobile.deck_square.api_response.PushSingleDeck;
import cn.garymb.ygomobile.deck_square.api_response.SquareDeckResponse; import cn.garymb.ygomobile.deck_square.api_response.SquareDeckResponse;
import cn.garymb.ygomobile.deck_square.api_response.SyncDecksResponse;
import cn.garymb.ygomobile.deck_square.bo.MyDeckItem; import cn.garymb.ygomobile.deck_square.bo.MyDeckItem;
import cn.garymb.ygomobile.deck_square.bo.SyncMutliDeckResult; import cn.garymb.ygomobile.deck_square.bo.SyncMutliDeckResult;
import cn.garymb.ygomobile.utils.LogUtil; import cn.garymb.ygomobile.utils.LogUtil;
...@@ -172,51 +172,8 @@ public class DeckSquareApiUtil { ...@@ -172,51 +172,8 @@ public class DeckSquareApiUtil {
} }
/**
* 阻塞方法,推送新卡组的内容时使用。首先从服务器请求一个新的卡组id,之后将卡组上传到服务器
* 首先调用服务端api获取卡组id,之后将卡组id设置到ydk中,之后调用服务器api将卡组上传
*
* @param deckFile
* @param loginToken
*/
public static PushDeckResponse requestIdAndPushDeck(DeckFile deckFile, LoginToken loginToken) throws IOException {
if (loginToken == null) {
return null;
}
String getDeckIdUrl = "http://rarnu.xyz:38383/api/mdpro3/deck/deckId";
Map<String, String> headers = new HashMap<>();
headers.put("ReqSource", "MDPro3");
headers.put("token", loginToken.getServerToken());
Gson gson = new Gson();
DeckIdResponse deckIdResult = null;
Response response = OkhttpUtil.synchronousGet(getDeckIdUrl, null, headers);
String responseBodyString = response.body().string();
// Convert JSON to Java object using Gson
deckIdResult = gson.fromJson(responseBodyString, DeckIdResponse.class);
LogUtil.i(TAG, "deck id result:" + deckIdResult.toString());
if (deckIdResult == null) {
return null;
}
String deckId = deckIdResult.getDeckId();//从服务器获取
if (deckId == null) {
return null;
}
return pushDeck(deckFile, loginToken, deckId);
}
/** /**
* 阻塞方法,将对应于deckId、deckName的卡组内容json推送到服务器。 * 阻塞方法,将对应于deckId、deckName的卡组内容json推送到服务器。
* 如果在服务器上不存在deckId、deckName对应的记录,则创建新卡组
* 如果在服务器存在deckId相同的记录,则更新卡组,deckName会覆盖服务器上的卡组名 * 如果在服务器存在deckId相同的记录,则更新卡组,deckName会覆盖服务器上的卡组名
* 如果在服务器存在deckName相同、deckId不同的记录,则更新失败 * 如果在服务器存在deckName相同、deckId不同的记录,则更新失败
* *
...@@ -259,55 +216,132 @@ public class DeckSquareApiUtil { ...@@ -259,55 +216,132 @@ public class DeckSquareApiUtil {
return result; return result;
} }
/** /**
* 批量同步卡组到云,入参为DeckData * 阻塞方法,将对应于deckDataList、deckIdList的卡组内容json推送到服务器。
* 如果在服务器存在deckId相同的记录,则更新卡组,deckName会覆盖服务器上的卡组名
* 如果在服务器存在deckName相同、deckId不同的记录,则更新失败
* *
* @param deckDataList * @param deckDataList
* @param loginToken * @param loginToken
* @param deckIdList
* @return * @return
* @throws IOException * @throws IOException
*/ */
public static SyncDecksResponse syncDecks(List<PushMultiDeck.DeckData> deckDataList, LoginToken loginToken) throws IOException { private static PushDeckResponse pushDecks(List<MyDeckItem> deckDataList, LoginToken loginToken, List<String> deckIdList) throws IOException {
SyncDecksResponse result = null; List<PushMultiDeck.DeckData> decks = new ArrayList<>();
String url = "http://rarnu.xyz:38383/api/mdpro3/sync/multi"; List<String> deckContents = new ArrayList<>();
if (deckDataList.size() != deckIdList.size()) {
return null;
}
for (int i = 0; i < deckDataList.size(); i++) {
MyDeckItem myDeckItem = deckDataList.get(i);
String deckContent = DeckSquareFileUtil.setDeckId(myDeckItem.getDeckPath(), loginToken.getUserId(), deckIdList.get(i));
PushMultiDeck.DeckData data = new PushMultiDeck.DeckData();
data.setDeckYdk(deckContent);
data.setDeckName(myDeckItem.getDeckName());
data.setDeckId(deckIdList.get(i));
decks.add(data);
}
return pushMultiDecks(decks, loginToken);
}
/**
* 阻塞方法,推送新卡组的内容时使用。首先从服务器请求一个新的卡组id,之后将卡组上传到服务器
* 首先调用服务端api获取卡组id,之后将卡组id设置到ydk中,之后调用服务器api将卡组上传
*
* @param deckFile
* @param loginToken
*/
public static PushDeckResponse requestIdAndPushNewDeck(DeckFile deckFile, LoginToken loginToken) throws IOException {
if (loginToken == null) {
return null;
}
String getDeckIdUrl = "http://rarnu.xyz:38383/api/mdpro3/deck/deckId";
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
headers.put("ReqSource", "MDPro3"); headers.put("ReqSource", "MDPro3");
headers.put("token", loginToken.getServerToken()); headers.put("token", loginToken.getServerToken());
Gson gson = new Gson(); Gson gson = new Gson();
PushMultiDeck pushMultiDeck = new PushMultiDeck();
pushMultiDeck.setDeckContributor(loginToken.getUserId().toString());
pushMultiDeck.setUserId(loginToken.getUserId());
pushMultiDeck.setDecks(deckDataList);
String json = gson.toJson(pushMultiDeck); DeckIdResponse deckIdResult = null;
Response response = OkhttpUtil.postJson(url, json, headers);
Response response = OkhttpUtil.synchronousGet(getDeckIdUrl, null, headers);
String responseBodyString = response.body().string(); String responseBodyString = response.body().string();
// Convert JSON to Java object using Gson
deckIdResult = gson.fromJson(responseBodyString, DeckIdResponse.class);
LogUtil.i(TAG, "deck id result:" + deckIdResult.toString());
result = gson.fromJson(responseBodyString, SyncDecksResponse.class);
LogUtil.i(TAG, "push deck response:" + responseBodyString);
return result; if (deckIdResult == null) {
return null;
}
String deckId = deckIdResult.getDeckId();//从服务器获取
if (deckId == null) {
return null;
}
return pushDeck(deckFile, loginToken, deckId);
} }
/** /**
* 批量同步卡组到云,入参为MyDeckItem * 阻塞方法,推送新卡组的内容时使用。首先从服务器请求一个新的卡组id,之后将卡组上传到服务器
* 首先调用服务端api获取卡组id,之后将卡组id设置到ydk中,之后调用服务器api将卡组上传
* 首先获取卡组id,之后上传新卡组
* *
* @param deckDataList * @param deckDataList
* @param loginToken * @param loginToken
* @return * @return
* @throws IOException * @throws IOException
*/ */
public static SyncDecksResponse syncMyDecks(List<MyDeckItem> deckDataList, LoginToken loginToken) throws IOException { public static PushDeckResponse requestIdAndPushNewDecks(List<MyDeckItem> deckDataList, LoginToken loginToken) throws IOException {
SyncDecksResponse result = null; if (loginToken == null) {
String url = "http://rarnu.xyz:38383/api/mdpro3/sync/multi"; return null;
}
Gson gson = new Gson();
String getDeckIdUrl = "http://rarnu.xyz:38383/api/mdpro3/deck/deckIds";
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
headers.put("ReqSource", "MDPro3"); headers.put("ReqSource", "MDPro3");
headers.put("token", loginToken.getServerToken()); headers.put("token", loginToken.getServerToken());
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("count", deckDataList.size());
Response response = OkhttpUtil.synchronousGet(getDeckIdUrl, paramMap, headers);
DeckMultiIdResponse deckIdResult = gson.fromJson(response.body().string(), DeckMultiIdResponse.class);
if (deckIdResult == null) {
return null;
}
List<String> deckId = deckIdResult.getDeckId();//从服务器获取
if (deckId == null) {
return null;
}
return pushDecks(deckDataList, loginToken, deckId);
}
/**
* 批量上传已经在云上存在的卡组
*
* @param deckItems
* @param loginToken
* @return
* @throws IOException
*/
public static PushDeckResponse syncMyDecks(List<MyDeckItem> deckItems, LoginToken loginToken) throws IOException {
/* 构造json */ /* 构造json */
List<PushMultiDeck.DeckData> dataList = new ArrayList<>(); List<PushMultiDeck.DeckData> dataList = new ArrayList<>();
for (MyDeckItem item : deckDataList) { for (MyDeckItem item : deckItems) {
PushMultiDeck.DeckData data = new PushMultiDeck.DeckData(); PushMultiDeck.DeckData data = new PushMultiDeck.DeckData();
data.setDeckId(item.getDeckId()); data.setDeckId(item.getDeckId());
data.setDeckName(item.getDeckName()); data.setDeckName(item.getDeckName());
...@@ -318,6 +352,18 @@ public class DeckSquareApiUtil { ...@@ -318,6 +352,18 @@ public class DeckSquareApiUtil {
data.setDeckYdk(deckContent); data.setDeckYdk(deckContent);
dataList.add(data); dataList.add(data);
} }
return pushMultiDecks(dataList, loginToken);
}
public static PushDeckResponse pushMultiDecks
(List<PushMultiDeck.DeckData> dataList, LoginToken loginToken) throws IOException {
String url = "http://rarnu.xyz:38383/api/mdpro3/sync/multi";
Map<String, String> headers = new HashMap<>();
headers.put("ReqSource", "MDPro3");
headers.put("token", loginToken.getServerToken());
PushDeckResponse result = null;
Gson gson = new Gson(); Gson gson = new Gson();
PushMultiDeck pushMultiDeck = new PushMultiDeck(); PushMultiDeck pushMultiDeck = new PushMultiDeck();
...@@ -329,7 +375,7 @@ public class DeckSquareApiUtil { ...@@ -329,7 +375,7 @@ public class DeckSquareApiUtil {
Response response = OkhttpUtil.postJson(url, json, headers); Response response = OkhttpUtil.postJson(url, json, headers);
String responseBodyString = response.body().string(); String responseBodyString = response.body().string();
result = gson.fromJson(responseBodyString, SyncDecksResponse.class); result = gson.fromJson(responseBodyString, PushDeckResponse.class);
LogUtil.i(TAG, "push deck response:" + responseBodyString); LogUtil.i(TAG, "push deck response:" + responseBodyString);
return result; return result;
...@@ -364,7 +410,8 @@ public class DeckSquareApiUtil { ...@@ -364,7 +410,8 @@ public class DeckSquareApiUtil {
* *
* @param deckId * @param deckId
*/ */
public static BasicResponse setDeckPublic(String deckId, LoginToken loginToken, boolean publicState) throws IOException { public static BasicResponse setDeckPublic(String deckId, LoginToken loginToken,
boolean publicState) throws IOException {
BasicResponse result = null; BasicResponse result = null;
...@@ -436,7 +483,8 @@ public class DeckSquareApiUtil { ...@@ -436,7 +483,8 @@ public class DeckSquareApiUtil {
} }
public static PushDeckResponse deleteDeck(String deckId, LoginToken loginToken) throws IOException { public static PushDeckResponse deleteDeck(String deckId, LoginToken loginToken) throws
IOException {
PushDeckResponse result = null; PushDeckResponse result = null;
String url = "http://rarnu.xyz:38383/api/mdpro3/sync/single"; String url = "http://rarnu.xyz:38383/api/mdpro3/sync/single";
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
...@@ -478,13 +526,13 @@ public class DeckSquareApiUtil { ...@@ -478,13 +526,13 @@ public class DeckSquareApiUtil {
public static SyncMutliDeckResult synchronizeDecksV2() throws IOException, ParseException { public static SyncMutliDeckResult synchronizeDecksV2() throws IOException, ParseException {
SyncMutliDeckResult result = new SyncMutliDeckResult(); SyncMutliDeckResult autoSyncResult = new SyncMutliDeckResult();
// 检查用户是否登录 // 检查用户是否登录
LoginToken loginToken = DeckSquareApiUtil.getLoginData(); LoginToken loginToken = DeckSquareApiUtil.getLoginData();
if (loginToken == null) { if (loginToken == null) {
result.setFlag(false); autoSyncResult.setFlag(false);
result.setInfo("need login"); autoSyncResult.setInfo("need login");
return result; return autoSyncResult;
} }
// 获取本地卡组列表 // 获取本地卡组列表
...@@ -492,9 +540,9 @@ public class DeckSquareApiUtil { ...@@ -492,9 +540,9 @@ public class DeckSquareApiUtil {
// 获取在线卡组列表 // 获取在线卡组列表
MyDeckResponse onlineDecksResponse = DeckSquareApiUtil.getUserDecks(loginToken); MyDeckResponse onlineDecksResponse = DeckSquareApiUtil.getUserDecks(loginToken);
if (onlineDecksResponse == null || onlineDecksResponse.getData() == null) { if (onlineDecksResponse == null || onlineDecksResponse.getData() == null) {
result.setFlag(false); autoSyncResult.setFlag(false);
result.setInfo("no online decks"); autoSyncResult.setInfo("no online decks");
return result; return autoSyncResult;
} }
List<MyOnlineDeckDetail> onlineDecks = onlineDecksResponse.getData(); List<MyOnlineDeckDetail> onlineDecks = onlineDecksResponse.getData();
...@@ -504,18 +552,28 @@ public class DeckSquareApiUtil { ...@@ -504,18 +552,28 @@ public class DeckSquareApiUtil {
for (MyOnlineDeckDetail onlineDeck : onlineDecks) { for (MyOnlineDeckDetail onlineDeck : onlineDecks) {
onlineDeckProcessed.put(onlineDeck.getDeckName(), false); onlineDeckProcessed.put(onlineDeck.getDeckName(), false);
} }
// 用于标记本地卡组是否在在线有对应
List<MyDeckItem> toUploadDecks = new ArrayList<>(); /* Map<String, Boolean> localDeckProcessed = new HashMap<>();
for (MyDeckItem localDeck : localDecks) {
String deckName = localDeck.getDeckName().replace(Constants.YDK_FILE_EX, "");
localDeckProcessed.put(deckName, false);
}*/
List<MyDeckItem> syncUploadDecks = new ArrayList<>();
List<MyDeckItem> newPushDecks = new ArrayList<>();
// 遍历本地卡组,处理同名卡组的情况 // 遍历本地卡组,处理同名卡组的情况
for (MyDeckItem localDeck : localDecks) { for (MyDeckItem localDeck : localDecks) {
boolean foundOnlineDeck = false;
String localDeckName = localDeck.getDeckName(); String localDeckName = localDeck.getDeckName();
// localDeckName = localDeckName.replace(".ydk", "");
for (MyOnlineDeckDetail onlineDeck : onlineDecks) { for (MyOnlineDeckDetail onlineDeck : onlineDecks) {
if (localDeckName.equals(onlineDeck.getDeckName())) { if (localDeckName.equals(onlineDeck.getDeckName())) {
// 标记该在线卡组已处理 // 标记该在线卡组已处理
onlineDeckProcessed.put(onlineDeck.getDeckName(), true); onlineDeckProcessed.put(onlineDeck.getDeckName(), true);
// 标记该本地卡组已处理
foundOnlineDeck = true;
// 比对更新时间 // 比对更新时间
long localUpdateDate = localDeck.getUpdateTimestamp(); long localUpdateDate = localDeck.getUpdateTimestamp();
...@@ -526,6 +584,7 @@ public class DeckSquareApiUtil { ...@@ -526,6 +584,7 @@ public class DeckSquareApiUtil {
// 在线卡组更新时间更晚,下载在线卡组覆盖本地卡组 // 在线卡组更新时间更晚,下载在线卡组覆盖本地卡组
LogUtil.w(TAG, "sync-download deck: " + localDeck.getDeckName()); LogUtil.w(TAG, "sync-download deck: " + localDeck.getDeckName());
autoSyncResult.syncDownload.add(localDeck);
downloadOnlineDeck(onlineDeck, localDeck.getDeckPath(), onlineUpdateDate); downloadOnlineDeck(onlineDeck, localDeck.getDeckPath(), onlineUpdateDate);
} else if (onlineUpdateDate == localUpdateDate) { } else if (onlineUpdateDate == localUpdateDate) {
...@@ -537,28 +596,40 @@ public class DeckSquareApiUtil { ...@@ -537,28 +596,40 @@ public class DeckSquareApiUtil {
LogUtil.w(TAG, "sync-upload deck: " + localDeck.getDeckName()); LogUtil.w(TAG, "sync-upload deck: " + localDeck.getDeckName());
localDeck.setDeckId(onlineDeck.getDeckId()); localDeck.setDeckId(onlineDeck.getDeckId());
toUploadDecks.add(localDeck); syncUploadDecks.add(localDeck);
result.toUpload.add(localDeck); autoSyncResult.syncUpload.add(localDeck);
} }
break; break;
} }
} }
// 本地卡组在在线列表中不存在,则需要获取新的deckid来直接上传
if (!foundOnlineDeck) {
newPushDecks.add(localDeck);
autoSyncResult.newUpload.add(localDeck);
}
} }
SyncDecksResponse response = syncMyDecks(toUploadDecks, loginToken); PushDeckResponse response = syncMyDecks(syncUploadDecks, loginToken);
result.pushResponse = response; autoSyncResult.pushResponse = response;
if (!newPushDecks.isEmpty()) {
PushDeckResponse pushNewRes = requestIdAndPushNewDecks(newPushDecks, loginToken);
}
// 处理只存在于在线的卡组(即本地没有同名卡组) // 处理只存在于在线的卡组(即本地没有同名卡组)
for (MyOnlineDeckDetail onlineDeck : onlineDecks) { for (MyOnlineDeckDetail onlineDeck : onlineDecks) {
if (!onlineDeckProcessed.get(onlineDeck.getDeckName())) { if (!onlineDeckProcessed.get(onlineDeck.getDeckName())) {
result.download.add(onlineDeck); autoSyncResult.newDownload.add(onlineDeck);
LogUtil.w(TAG, "sync-download new deck: " + onlineDeck.getDeckName()); LogUtil.w(TAG, "sync-download new deck: " + onlineDeck.getDeckName());
SyncMutliDeckResult.DownloadResult downloadResult = downloadMissingDeckToLocal(onlineDeck, convertToUnixTimestamp(onlineDeck.getDeckUpdateDate())); SyncMutliDeckResult.DownloadResult downloadResult = downloadMissingDeckToLocal(onlineDeck, convertToUnixTimestamp(onlineDeck.getDeckUpdateDate()));
result.downloadResponse.add(downloadResult); autoSyncResult.downloadResponse.add(downloadResult);
} }
} }
return result; return autoSyncResult;
} }
...@@ -568,7 +639,8 @@ public class DeckSquareApiUtil { ...@@ -568,7 +639,8 @@ public class DeckSquareApiUtil {
List<MyOnlineDeckDetail> onlineDecks; List<MyOnlineDeckDetail> onlineDecks;
} }
private static SyncMutliDeckResult.DownloadResult downloadMissingDeckToLocal(MyOnlineDeckDetail onlineDeck, Long onlineUpdateDate) { private static SyncMutliDeckResult.DownloadResult downloadMissingDeckToLocal
(MyOnlineDeckDetail onlineDeck, Long onlineUpdateDate) {
try { try {
// 根据卡组ID查询在线卡组详情 // 根据卡组ID查询在线卡组详情
DownloadDeckResponse deckResponse = DeckSquareApiUtil.getDeckById(onlineDeck.getDeckId()); DownloadDeckResponse deckResponse = DeckSquareApiUtil.getDeckById(onlineDeck.getDeckId());
...@@ -577,8 +649,6 @@ public class DeckSquareApiUtil { ...@@ -577,8 +649,6 @@ public class DeckSquareApiUtil {
return new SyncMutliDeckResult.DownloadResult(false, onlineDeck.getDeckId(), "Failed to get deck details for: " + onlineDeck.getDeckName()); return new SyncMutliDeckResult.DownloadResult(false, onlineDeck.getDeckId(), "Failed to get deck details for: " + onlineDeck.getDeckName());
} }
MyOnlineDeckDetail deckDetail = deckResponse.getData();
String deckContent = deckDetail.getDeckYdk();
// 构建本地文件路径 // 构建本地文件路径
String deckDirectory = AppsSettings.get().getDeckDir(); String deckDirectory = AppsSettings.get().getDeckDir();
...@@ -597,21 +667,21 @@ public class DeckSquareApiUtil { ...@@ -597,21 +667,21 @@ public class DeckSquareApiUtil {
fileName += Constants.YDK_FILE_EX; fileName += Constants.YDK_FILE_EX;
} }
String filePath = deckDirectory + "/" + fileName; String fileFullPath = deckDirectory + "/" + fileName;
// 保存在线卡组到本地 // 保存在线卡组到本地
boolean saved = DeckSquareFileUtil.saveFileToPath(deckDirectory, fileName, deckResponse.getData().getDeckYdk(), onlineUpdateDate); boolean saved = DeckSquareFileUtil.saveFileToPath(fileFullPath, deckResponse.getData().getDeckYdk(), onlineUpdateDate);
if (!saved) { if (!saved) {
LogUtil.e(TAG, "Failed to save deck file: " + filePath); LogUtil.e(TAG, "Failed to save deck file: " + fileFullPath);
return new SyncMutliDeckResult.DownloadResult(false, onlineDeck.getDeckId(), "Failed to save deck file: " + filePath); return new SyncMutliDeckResult.DownloadResult(false, onlineDeck.getDeckId(), "Failed to save deck file: " + fileFullPath);
} }
LogUtil.i(TAG, "Deck saved to: " + filePath); LogUtil.i(TAG, "Deck saved to: " + fileFullPath);
// 更新本地卡组列表 // 更新本地卡组列表
MyDeckItem newLocalDeck = new MyDeckItem(); MyDeckItem newLocalDeck = new MyDeckItem();
newLocalDeck.setDeckName(fileName); newLocalDeck.setDeckName(fileName);
newLocalDeck.setDeckPath(filePath); newLocalDeck.setDeckPath(fileFullPath);
newLocalDeck.setDeckId(onlineDeck.getDeckId()); newLocalDeck.setDeckId(onlineDeck.getDeckId());
newLocalDeck.setIdUploaded(2); // 已上传状态 newLocalDeck.setIdUploaded(2); // 已上传状态
newLocalDeck.setUpdateDate(onlineDeck.getDeckUpdateDate()); newLocalDeck.setUpdateDate(onlineDeck.getDeckUpdateDate());
...@@ -624,7 +694,8 @@ public class DeckSquareApiUtil { ...@@ -624,7 +694,8 @@ public class DeckSquareApiUtil {
} }
} }
private static boolean downloadOnlineDeck(MyOnlineDeckDetail onlineDeck, String localPath, Long onlineUpdateDate) { private static boolean downloadOnlineDeck(MyOnlineDeckDetail onlineDeck, String
fileFullPath, Long onlineUpdateDate) {
try { try {
// 根据卡组ID查询在线卡组详情 // 根据卡组ID查询在线卡组详情
DownloadDeckResponse deckResponse = DeckSquareApiUtil.getDeckById(onlineDeck.getDeckId()); DownloadDeckResponse deckResponse = DeckSquareApiUtil.getDeckById(onlineDeck.getDeckId());
...@@ -636,15 +707,13 @@ public class DeckSquareApiUtil { ...@@ -636,15 +707,13 @@ public class DeckSquareApiUtil {
MyOnlineDeckDetail deckDetail = deckResponse.getData(); MyOnlineDeckDetail deckDetail = deckResponse.getData();
String deckContent = deckDetail.getDeckYdk(); String deckContent = deckDetail.getDeckYdk();
// 保存在线卡组到本地 // 保存在线卡组到本地
String fileName = onlineDeck.getDeckName() + Constants.YDK_FILE_EX; boolean saved = DeckSquareFileUtil.saveFileToPath(fileFullPath, deckContent, onlineUpdateDate);
// 保存在线卡组到本地
boolean saved = DeckSquareFileUtil.saveFileToPath(localPath, onlineDeck.getDeckName(), deckContent, onlineUpdateDate);
if (!saved) { if (!saved) {
LogUtil.e(TAG, "Failed to save deck file: " + localPath); LogUtil.e(TAG, "Failed to save deck file: " + fileFullPath);
return false; return false;
} }
LogUtil.i(TAG, "Deck updated: " + localPath); LogUtil.i(TAG, "Deck updated: " + fileFullPath);
return true; return true;
} catch (Exception e) { } catch (Exception e) {
LogUtil.e(TAG, "Error downloading deck: " + e.getMessage()); LogUtil.e(TAG, "Error downloading deck: " + e.getMessage());
...@@ -661,7 +730,8 @@ public class DeckSquareApiUtil { ...@@ -661,7 +730,8 @@ public class DeckSquareApiUtil {
* @param loginToken * @param loginToken
* @return * @return
*/ */
private static boolean uploadLocalDeck(MyDeckItem localDeck, String onlineDeckId, LoginToken loginToken) { private static boolean uploadLocalDeck(MyDeckItem localDeck, String
onlineDeckId, LoginToken loginToken) {
try { try {
DeckFile deckFile = new DeckFile(localDeck.getDeckPath(), DeckType.ServerType.MY_SQUARE); DeckFile deckFile = new DeckFile(localDeck.getDeckPath(), DeckType.ServerType.MY_SQUARE);
deckFile.setName(localDeck.getDeckName()); deckFile.setName(localDeck.getDeckName());
......
...@@ -26,7 +26,8 @@ import ocgcore.data.Card; ...@@ -26,7 +26,8 @@ import ocgcore.data.Card;
public class DeckSquareFileUtil { public class DeckSquareFileUtil {
// //
private static final String TAG = DeckSquareListAdapter.class.getSimpleName(); private static final String TAG = "seesee decksquareApiUtil";
//private static final String TAG = DeckSquareListAdapter.class.getSimpleName();
// public static List<String> readLastLinesWithNIO(File file, int numLines) { // public static List<String> readLastLinesWithNIO(File file, int numLines) {
// try { // try {
...@@ -57,7 +58,7 @@ public class DeckSquareFileUtil { ...@@ -57,7 +58,7 @@ public class DeckSquareFileUtil {
String line = null; String line = null;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
LogUtil.i(TAG, line); // LogUtil.i(TAG, line);
if (line.startsWith("###")) {//注意,先判断###,后判断##。因为###会包括##的情况 if (line.startsWith("###")) {//注意,先判断###,后判断##。因为###会包括##的情况
try { try {
String data = line.replace("#", ""); String data = line.replace("#", "");
...@@ -207,20 +208,10 @@ public class DeckSquareFileUtil { ...@@ -207,20 +208,10 @@ public class DeckSquareFileUtil {
return content; return content;
} }
/** public static boolean saveFile(File file, String content, long modificationTime) {
* 保存文件到指定路径,并设置指定的最后修改时间
*
* @param path 保存路径
* @param fileName 文件名
* @param content 文件内容
* @param modificationTime 期望的最后修改时间(毫秒时间戳)
* @return 保存是否成功
*/
public static boolean saveFileToPath(String path, String fileName, String content, long modificationTime) {
FileOutputStream fos = null; FileOutputStream fos = null;
try { try {
// 创建文件对象 // 创建文件对象
File file = new File(path, fileName);
fos = new FileOutputStream(file); fos = new FileOutputStream(file);
// 创建文件输出流 // 创建文件输出流
// 写入内容 // 写入内容
...@@ -251,6 +242,33 @@ public class DeckSquareFileUtil { ...@@ -251,6 +242,33 @@ public class DeckSquareFileUtil {
return true; return true;
} }
/**
*
* @param fileFullPath 文件的完整路径
* @param content
* @param modificationTime
* @return
*/
public static boolean saveFileToPath(String fileFullPath, String content, long modificationTime) {
File file = new File(fileFullPath);
return saveFile(file, content, modificationTime);
}
/**
* 保存文件到指定路径,并设置指定的最后修改时间
*
* @param fileParentPath 保存文件的父目录路径
* @param fileName 文件名
* @param content 文件内容
* @param modificationTime 最后修改时间(毫秒时间戳)
* @return 保存是否成功
*/
public static boolean saveFileToPath(String fileParentPath, String fileName, String content, long modificationTime) {
File file = new File(fileParentPath, fileName);
return saveFile(file, content, modificationTime);
}
public static List<Card> convertTempDeckYdk(String deckYdk) { public static List<Card> convertTempDeckYdk(String deckYdk) {
String[] deckLine = deckYdk.split("\r\n|\r|\n"); String[] deckLine = deckYdk.split("\r\n|\r|\n");
...@@ -277,13 +295,17 @@ public class DeckSquareFileUtil { ...@@ -277,13 +295,17 @@ public class DeckSquareFileUtil {
} }
public static long convertToUnixTimestamp(String dateTimeStr) throws ParseException { public static long convertToUnixTimestamp(String dateTimeStr) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
sdf.setLenient(false); sdf.setLenient(false);
Date date = sdf.parse(dateTimeStr); Date date = sdf.parse(dateTimeStr);
return date.getTime(); return date.getTime();
}catch(ParseException e){
e.printStackTrace();
}
return 0;
} }
} }
...@@ -128,7 +128,7 @@ public class DeckSquareMyDeckFragment extends Fragment { ...@@ -128,7 +128,7 @@ public class DeckSquareMyDeckFragment extends Fragment {
LogUtil.i(TAG, "Sync decks fail" + e.getMessage()); LogUtil.i(TAG, "Sync decks fail" + e.getMessage());
}).done((result) -> { }).done((result) -> {
String info = "sync decks: upload " + result.toUpload.size() + ", download " + result.download.size(); String info = "sync decks: upload " + result.syncUpload.size() + ", download " + result.newDownload.size();
YGOUtil.showTextToast(info, Toast.LENGTH_LONG); YGOUtil.showTextToast(info, Toast.LENGTH_LONG);
}); });
...@@ -216,7 +216,7 @@ public class DeckSquareMyDeckFragment extends Fragment { ...@@ -216,7 +216,7 @@ public class DeckSquareMyDeckFragment extends Fragment {
YGOUtil.showTextToast("Sync decks fail", Toast.LENGTH_LONG); YGOUtil.showTextToast("Sync decks fail", Toast.LENGTH_LONG);
LogUtil.i(TAG, "Sync decks fail" + e.getMessage()); LogUtil.i(TAG, "Sync decks fail" + e.getMessage());
}).done((result) -> { }).done((result) -> {
String info = "sync decks: upload " + result.toUpload.size() + ", download " + result.download.size(); String info = "sync decks: upload " + result.syncUpload.size() + ", download " + result.newDownload.size();
YGOUtil.showTextToast(info, Toast.LENGTH_LONG); YGOUtil.showTextToast(info, Toast.LENGTH_LONG);
}); });
......
package cn.garymb.ygomobile.deck_square.api_response;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class DeckMultiIdResponse {
@Expose
public int code;
@Expose
public String message;
@Expose
@SerializedName("data")
public List<String> deckId;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<String> getDeckId() {
return deckId;
}
public void setDeckId(List<String> deckId) {
this.deckId = deckId;
}
@Override
public String toString() {
return "DeckMultiIdResponse{" +
"code=" + code +
", message='" + message + '\'' +
", deckId=" + deckId +
'}';
}
}
package cn.garymb.ygomobile.deck_square.api_response;
/* 同步卡组响应体类*/
public class SyncDecksResponse {
private Integer code;
private String message;
private Integer data;
public SyncDecksResponse(Integer code, String message, Integer data) {
this.code = code;
this.message = message;
this.data = data;
}
// getters and setters
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Integer getData() {
return data;
}
public void setData(Integer data) {
this.data = data;
}
}
package cn.garymb.ygomobile.deck_square.bo; package cn.garymb.ygomobile.deck_square.bo;
import org.minidns.record.A;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import cn.garymb.ygomobile.deck_square.api_response.MyOnlineDeckDetail; import cn.garymb.ygomobile.deck_square.api_response.MyOnlineDeckDetail;
import cn.garymb.ygomobile.deck_square.api_response.SyncDecksResponse; import cn.garymb.ygomobile.deck_square.api_response.PushDeckResponse;
public class SyncMutliDeckResult { public class SyncMutliDeckResult {
boolean flag = false; boolean flag = false;
String info = null; String info = null;
public SyncDecksResponse pushResponse; public PushDeckResponse pushResponse;
public List<MyDeckItem> toUpload;//用于记录已推送的卡组 public List<MyDeckItem> syncUpload;//用于记录已推送的卡组
public List<MyOnlineDeckDetail> download; public List<MyDeckItem> newUpload;//用于记录第一次推送到云的卡组
public List<MyDeckItem> syncDownload;//用于记录已推送的卡组
public List<MyOnlineDeckDetail> newDownload;
public List<DownloadResult> downloadResponse; public List<DownloadResult> downloadResponse;
public static class DownloadResult { public static class DownloadResult {
...@@ -37,16 +41,20 @@ public class SyncMutliDeckResult { ...@@ -37,16 +41,20 @@ public class SyncMutliDeckResult {
public SyncMutliDeckResult() { public SyncMutliDeckResult() {
flag = true; flag = true;
downloadResponse = new ArrayList<>(); downloadResponse = new ArrayList<>();
download = new ArrayList<>(); newDownload = new ArrayList<>();
toUpload = new ArrayList<>(); syncUpload = new ArrayList<>();
newUpload = new ArrayList<>();
syncDownload = new ArrayList<>();
} }
public SyncMutliDeckResult(boolean flag, String info) { public SyncMutliDeckResult(boolean flag, String info) {
this.flag = flag; this.flag = flag;
this.info = info; this.info = info;
downloadResponse = new ArrayList<>(); downloadResponse = new ArrayList<>();
download = new ArrayList<>(); newDownload = new ArrayList<>();
toUpload = new ArrayList<>(); syncUpload = new ArrayList<>();
newUpload = new ArrayList<>();
syncDownload = new ArrayList<>();
} }
public boolean isFlag() { public boolean isFlag() {
...@@ -64,4 +72,10 @@ public class SyncMutliDeckResult { ...@@ -64,4 +72,10 @@ public class SyncMutliDeckResult {
public void setInfo(String info) { public void setInfo(String info) {
this.info = info; this.info = info;
} }
public String getMessage() {
String info = "sync decks: " + syncUpload.size() + ", push new:" + newUpload.size() + ", download " + newDownload.size();
return info;
}
} }
...@@ -4,6 +4,7 @@ import static android.content.Context.CLIPBOARD_SERVICE; ...@@ -4,6 +4,7 @@ import static android.content.Context.CLIPBOARD_SERVICE;
import static cn.garymb.ygomobile.Constants.ORI_DECK; import static cn.garymb.ygomobile.Constants.ORI_DECK;
import static cn.garymb.ygomobile.Constants.YDK_FILE_EX; import static cn.garymb.ygomobile.Constants.YDK_FILE_EX;
import static cn.garymb.ygomobile.core.IrrlichtBridge.ACTION_SHARE_FILE; import static cn.garymb.ygomobile.core.IrrlichtBridge.ACTION_SHARE_FILE;
import static cn.garymb.ygomobile.deck_square.DeckSquareFileUtil.convertToUnixTimestamp;
import android.content.ClipData; import android.content.ClipData;
import android.content.ClipboardManager; import android.content.ClipboardManager;
...@@ -1351,7 +1352,7 @@ public class DeckManagerFragment extends BaseFragemnt implements RecyclerViewIte ...@@ -1351,7 +1352,7 @@ public class DeckManagerFragment extends BaseFragemnt implements RecyclerViewIte
String fileFullName = deckData.getDeckName() + ".ydk"; String fileFullName = deckData.getDeckName() + ".ydk";
File dir = new File(getActivity().getApplicationInfo().dataDir, "cache"); File dir = new File(getActivity().getApplicationInfo().dataDir, "cache");
//将卡组存到cache缓存目录中 //将卡组存到cache缓存目录中
boolean result = DeckSquareFileUtil.saveFileToPath(dir.getPath(), fileFullName, deckData.getDeckYdk(), Long.valueOf(deckData.getDeckUpdateDate())); boolean result = DeckSquareFileUtil.saveFileToPath(dir.getPath(), fileFullName, deckData.getDeckYdk(), convertToUnixTimestamp(deckData.getDeckUpdateDate()));
if (result) {//存储成功,使用预加载功能 if (result) {//存储成功,使用预加载功能
LogUtil.i(TAG, "square deck detail done"); LogUtil.i(TAG, "square deck detail done");
//File file = new File(dir, fileFullName); //File file = new File(dir, fileFullName);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment