Commit d26e8a1d authored by wangfugui's avatar wangfugui

完善卡组上传功能

parent 5436c3a1
...@@ -11,6 +11,7 @@ import java.util.Map; ...@@ -11,6 +11,7 @@ import java.util.Map;
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.DownloadDeckResponse; import cn.garymb.ygomobile.deck_square.api_response.DownloadDeckResponse;
import cn.garymb.ygomobile.deck_square.api_response.MyDeckResponse; import cn.garymb.ygomobile.deck_square.api_response.MyDeckResponse;
import cn.garymb.ygomobile.deck_square.api_response.PushCardJson;
import cn.garymb.ygomobile.utils.LogUtil; import cn.garymb.ygomobile.utils.LogUtil;
import cn.garymb.ygomobile.utils.OkhttpUtil; import cn.garymb.ygomobile.utils.OkhttpUtil;
import cn.garymb.ygomobile.utils.YGOUtil; import cn.garymb.ygomobile.utils.YGOUtil;
...@@ -85,7 +86,7 @@ public class DeckSquareApiUtil { ...@@ -85,7 +86,7 @@ public class DeckSquareApiUtil {
} }
//首先获取卡组id,之后将卡组id设置到ydk中,之后将其上传 //首先获取卡组id,之后将卡组id设置到ydk中,之后将其上传
public static void pushDeck(String deckPath, Integer userId) { public static void pushDeck(String deckPath, String deckName, Integer userId) {
String url = "http://rarnu.xyz:38383/api/mdpro3/sync/single"; String url = "http://rarnu.xyz:38383/api/mdpro3/sync/single";
String getDeckIdUrl = "http://rarnu.xyz:38383/api/mdpro3/deck/deckId"; String getDeckIdUrl = "http://rarnu.xyz:38383/api/mdpro3/deck/deckId";
...@@ -101,15 +102,30 @@ public class DeckSquareApiUtil { ...@@ -101,15 +102,30 @@ public class DeckSquareApiUtil {
Gson gson = new Gson(); Gson gson = new Gson();
// Convert JSON to Java object using Gson // Convert JSON to Java object using Gson
deckIdResult = gson.fromJson(responseBodyString, DeckIdResponse.class); deckIdResult = gson.fromJson(responseBodyString, DeckIdResponse.class);
LogUtil.i(TAG, "deck id result:" + deckIdResult.toString());
} }
String deckId = deckIdResult.getDeckId();//从服务器获取 String deckId = deckIdResult.getDeckId();//从服务器获取
DeckSquareFileUtil.setDeckId(deckPath, userId, deckId); String deckContent = DeckSquareFileUtil.setDeckId(deckPath, userId, deckId);
PushCardJson pushCardJson = new PushCardJson();
pushCardJson.setDeckContributor(userId.toString());
pushCardJson.setUserId(userId);
PushCardJson.DeckData deckData = new PushCardJson.DeckData();
deckData.setDeckId(deckId);
deckData.setDeckName(deckName);
deckData.setDelete(false);
deckData.setDeckYdk(deckContent);
pushCardJson.setDeck(deckData);
Gson gson = new Gson();
String json = gson.toJson(pushCardJson);
//todo 构造卡组的json //todo 构造卡组的json
OkhttpUtil.postJson(url, null, headers, 1000, new Callback() { OkhttpUtil.postJson(url, json, headers, 1000, new Callback() {
@Override @Override
public void onFailure(Call call, IOException e) { public void onFailure(Call call, IOException e) {
...@@ -124,8 +140,6 @@ public class DeckSquareApiUtil { ...@@ -124,8 +140,6 @@ public class DeckSquareApiUtil {
}); });
Gson gson = new Gson();
// Convert JSON to Java object using Gson
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
......
...@@ -34,6 +34,8 @@ public class DeckSquareFileUtil { ...@@ -34,6 +34,8 @@ public class DeckSquareFileUtil {
// 使用示例 // 使用示例
// Path logPath = Paths.get(context.getFilesDir().getAbsolutePath(), "log.txt"); // Path logPath = Paths.get(context.getFilesDir().getAbsolutePath(), "log.txt");
// List<String> lastTwo = readLastLinesWithNIO(logPath, 2); // List<String> lastTwo = readLastLinesWithNIO(logPath, 2);
//读取file指定的ydk文件,返回其内包含的deckId。如果不包含deckId,返回null
public static String getId(File file) { public static String getId(File file) {
String deckId = null; String deckId = null;
Integer userId; Integer userId;
...@@ -68,6 +70,7 @@ public class DeckSquareFileUtil { ...@@ -68,6 +70,7 @@ public class DeckSquareFileUtil {
return deckId; return deckId;
} }
//查询卡组目录下的所有ydk文件,返回File[]
public static File[] getAllYdk() { public static File[] getAllYdk() {
File dir = new File(AppsSettings.get().getResourcePath(), Constants.CORE_DECK_PATH); File dir = new File(AppsSettings.get().getResourcePath(), Constants.CORE_DECK_PATH);
File[] files = dir.listFiles((file, s) -> s.toLowerCase(Locale.US).endsWith(Constants.YDK_FILE_EX)); File[] files = dir.listFiles((file, s) -> s.toLowerCase(Locale.US).endsWith(Constants.YDK_FILE_EX));
...@@ -75,6 +78,7 @@ public class DeckSquareFileUtil { ...@@ -75,6 +78,7 @@ public class DeckSquareFileUtil {
return files; return files;
} }
//读取卡组目录下的所有ydk文件,解析ydk文件,生成List<MyDeckItem>解析结果
public static List<MyDeckItem> getMyDeckItem() { public static List<MyDeckItem> getMyDeckItem() {
List<MyDeckItem> result = new ArrayList<>(); List<MyDeckItem> result = new ArrayList<>();
File[] files = getAllYdk(); File[] files = getAllYdk();
...@@ -95,11 +99,14 @@ public class DeckSquareFileUtil { ...@@ -95,11 +99,14 @@ public class DeckSquareFileUtil {
return result; return result;
} }
//将卡组id、用户id设置到卡组文件上 // 读取deckPath对应的卡组文件到string,将卡组id、用户id设置到string中
//下载卡组后,保存之前将其原有id清除 //注意,不更改ydk文件内容
//上传卡组前,填入新的卡组id、用户id //下载卡组后,保存卡组之前
//上传卡组前,对于未存在卡组id的卡组,自动填入新的卡组id、用户id
public static String setDeckId(String deckPath, Integer userId, String deckId) { public static String setDeckId(String deckPath, Integer userId, String deckId) {
StringBuilder contentBuilder = new StringBuilder(); StringBuilder contentBuilder = new StringBuilder();
boolean userIdFlag = false;
boolean deckIdFlag = false;
FileInputStream inputStream = null; FileInputStream inputStream = null;
try { try {
...@@ -111,9 +118,17 @@ public class DeckSquareFileUtil { ...@@ -111,9 +118,17 @@ public class DeckSquareFileUtil {
String line = null; String line = null;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
/* 如果文件中已经存在deckId,则将其替换 */
if (line.contains("##") && !line.contains("###")) {
deckIdFlag = true;
line = "##" + deckId;
} else if (line.contains("###")) {//存在userId,将其替换
userIdFlag = true;
line = "###" + userId;
}
contentBuilder.append(line); contentBuilder.append(line);
contentBuilder.append('\n'); // Add line break
} }
String content = contentBuilder.toString();
} catch (Exception e) { } catch (Exception e) {
LogUtil.e(TAG, "read 1", e); LogUtil.e(TAG, "read 1", e);
...@@ -121,16 +136,17 @@ public class DeckSquareFileUtil { ...@@ -121,16 +136,17 @@ public class DeckSquareFileUtil {
IOUtils.close(inputStream); IOUtils.close(inputStream);
} }
String content = contentBuilder.toString();
//先替换XXX用户id if (!userIdFlag) {//原始ydk中不存在userId,添加userId行
//后替换XX卡组id contentBuilder.append("\n");
String original = "这是##测试1\r\n的内容,还有##测试2\r\n等其他部分"; contentBuilder.append("##" + userId);
String modified = original.replaceAll("##(.*?)\r\n", "##替换后的内容\r\n"); }
if (!deckIdFlag) {//原始ydk中不存在deckId,添加deckId行
contentBuilder.append("\n");
contentBuilder.append("###" + deckId);
}
System.out.println("修改前: " + original); String content = contentBuilder.toString();
System.out.println("修改后: " + modified);
return content; return content;
} }
......
...@@ -45,6 +45,7 @@ public class DeckSquareMyDeckFragment extends Fragment { ...@@ -45,6 +45,7 @@ public class DeckSquareMyDeckFragment extends Fragment {
}); });
//点击“我的卡组”中的某个卡组后,弹出dialog,dialog根据卡组的同步情况自动显示对应的下载/上传按钮
deckListAdapter.setOnItemClickListener( deckListAdapter.setOnItemClickListener(
(adapter, view, position) -> { (adapter, view, position) -> {
// Handle item click // Handle item click
......
...@@ -66,7 +66,7 @@ public class MyDeckDetailDialog extends Dialog { ...@@ -66,7 +66,7 @@ public class MyDeckDetailDialog extends Dialog {
} else if (item.getDeckSouce() == 1) {//来自服务器 } else if (item.getDeckSouce() == 1) {//来自服务器
downloadLayout.setVisibility(View.VISIBLE); downloadLayout.setVisibility(View.VISIBLE);
uploadLayout.setVisibility(View.GONE); uploadLayout.setVisibility(View.GONE);
} else if (item.getDeckSouce() == 2) {//来自服务器 } else if (item.getDeckSouce() == 2) {//本地、服务器均存在
downloadLayout.setVisibility(View.VISIBLE); downloadLayout.setVisibility(View.VISIBLE);
uploadLayout.setVisibility(View.VISIBLE); uploadLayout.setVisibility(View.VISIBLE);
} }
...@@ -75,13 +75,23 @@ public class MyDeckDetailDialog extends Dialog { ...@@ -75,13 +75,23 @@ public class MyDeckDetailDialog extends Dialog {
Integer userId = SharedPreferenceUtil.getServerUserId(); Integer userId = SharedPreferenceUtil.getServerUserId();
if (userId == null) {
YGOUtil.showTextToast("Please login first!");
return;
}
VUiKit.defer().when(() -> { VUiKit.defer().when(() -> {
DeckSquareApiUtil.pushDeck(item.getDeckPath(),userId); DeckSquareApiUtil.pushDeck(item.getDeckPath(),item.getDeckName(), userId);
});//.done(); });//.done();
}); });
btnDownload.setOnClickListener(v -> { btnDownload.setOnClickListener(v -> {
Integer userId = SharedPreferenceUtil.getServerUserId();
if (userId == null) {
YGOUtil.showTextToast("Please login first!");
return;
}
VUiKit.defer().when(() -> { VUiKit.defer().when(() -> {
DownloadDeckResponse response = DeckSquareApiUtil.getDeckById(item.getDeckId()); DownloadDeckResponse response = DeckSquareApiUtil.getDeckById(item.getDeckId());
......
...@@ -38,4 +38,13 @@ public class DeckIdResponse { ...@@ -38,4 +38,13 @@ public class DeckIdResponse {
public void setDeckId(String deckId) { public void setDeckId(String deckId) {
this.deckId = deckId; this.deckId = deckId;
} }
@Override
public String toString() {
return "DeckIdResponse{" +
"code=" + code +
", message='" + message + '\'' +
", deckId='" + deckId + '\'' +
'}';
}
} }
package cn.garymb.ygomobile.deck_square.api_response;
public class PushCardJson {
private String deckContributor;
private Integer userId;
private DeckData deck;
public String getDeckContributor() {
return deckContributor;
}
public void setDeckContributor(String deckContributor) {
this.deckContributor = deckContributor;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public DeckData getDeck() {
return deck;
}
public void setDeck(DeckData deck) {
this.deck = deck;
}
public static class DeckData {
private String deckId;
private String deckName;
private Integer deckCoverCard1 = 0;
private Integer deckCoverCard2 = 0;
private Integer deckCoverCard3 = 0;
private Integer deckCase = 0;
private Integer deckProtector = 0;
private String deckYdk;
private boolean isDelete = false;
public String getDeckId() {
return deckId;
}
public void setDeckId(String deckId) {
this.deckId = deckId;
}
public String getDeckName() {
return deckName;
}
public void setDeckName(String deckName) {
this.deckName = deckName;
}
public Integer getDeckCoverCard1() {
return deckCoverCard1;
}
public void setDeckCoverCard1(Integer deckCoverCard1) {
this.deckCoverCard1 = deckCoverCard1;
}
public Integer getDeckCoverCard2() {
return deckCoverCard2;
}
public void setDeckCoverCard2(Integer deckCoverCard2) {
this.deckCoverCard2 = deckCoverCard2;
}
public Integer getDeckCoverCard3() {
return deckCoverCard3;
}
public void setDeckCoverCard3(Integer deckCoverCard3) {
this.deckCoverCard3 = deckCoverCard3;
}
public Integer getDeckCase() {
return deckCase;
}
public void setDeckCase(Integer deckCase) {
this.deckCase = deckCase;
}
public Integer getDeckProtector() {
return deckProtector;
}
public void setDeckProtector(Integer deckProtector) {
this.deckProtector = deckProtector;
}
public String getDeckYdk() {
return deckYdk;
}
public void setDeckYdk(String deckYdk) {
this.deckYdk = deckYdk;
}
public boolean isDelete() {
return isDelete;
}
public void setDelete(boolean delete) {
isDelete = delete;
}
}
}
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