Commit 18305c8c authored by wangfugui's avatar wangfugui

完善卡组上传功能,重构代码,提高可读性

parent d26e8a1d
package cn.garymb.ygomobile.deck_square; package cn.garymb.ygomobile.deck_square;
import android.util.Log;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -15,6 +15,9 @@ import cn.garymb.ygomobile.AppsSettings; ...@@ -15,6 +15,9 @@ import cn.garymb.ygomobile.AppsSettings;
import cn.garymb.ygomobile.Constants; import cn.garymb.ygomobile.Constants;
import cn.garymb.ygomobile.utils.IOUtils; import cn.garymb.ygomobile.utils.IOUtils;
import cn.garymb.ygomobile.utils.LogUtil; import cn.garymb.ygomobile.utils.LogUtil;
import ocgcore.CardManager;
import ocgcore.DataManager;
import ocgcore.data.Card;
public class DeckSquareFileUtil { public class DeckSquareFileUtil {
// //
...@@ -38,7 +41,7 @@ public class DeckSquareFileUtil { ...@@ -38,7 +41,7 @@ public class DeckSquareFileUtil {
//读取file指定的ydk文件,返回其内包含的deckId。如果不包含deckId,返回null //读取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 = null;
FileInputStream inputStream = null; FileInputStream inputStream = null;
try { try {
inputStream = new FileInputStream(file); inputStream = new FileInputStream(file);
...@@ -49,22 +52,31 @@ public class DeckSquareFileUtil { ...@@ -49,22 +52,31 @@ 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("###")) {//注意,先判断###,后判断##。因为###会包括##的情况
line = line.replace("#", ""); try {
userId = Integer.parseInt(line); String data = line.replace("#", "");
// userId = Integer.parseInt(line.replaceAll("###", "")); if (!data.isEmpty()) {
userId = Integer.parseInt(data);
}
// userId = Integer.parseInt(line.replaceAll("###", ""));
} catch (NumberFormatException e) {
LogUtil.e(TAG, "integer" + line + "parse error" + e.toString());
}
} else if (line.startsWith("##")) { } else if (line.startsWith("##")) {
line = line.replace("#", ""); line = line.replace("#", "");
deckId = line; deckId = line;
} }
} }
} catch (Exception e) { } catch (IOException e) {
Log.e(TAG, "read 1", e);
LogUtil.e(TAG, "read 1", e);
} finally { } finally {
IOUtils.close(inputStream); IOUtils.close(inputStream);
} }
return deckId; return deckId;
...@@ -150,4 +162,48 @@ public class DeckSquareFileUtil { ...@@ -150,4 +162,48 @@ public class DeckSquareFileUtil {
return content; return content;
} }
public static boolean saveFileToPath(String path, String fileName, String content) {
try {
// Create file object
File file = new File(path, fileName);
// Create file output stream
FileOutputStream fos = new FileOutputStream(file);
// Write content
fos.write(content.getBytes());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static List<Card> convertTempDeckYdk(String deckYdk) {
String[] deckLine = deckYdk.split("\r\n|\r|\n");
List<Integer> tempDeck = new ArrayList<>();//存储当前卡组的基本内容
List<Card> cardList = new ArrayList<>();
CardManager mCardManager = DataManager.get().getCardManager();
for (String line : deckLine) {
if (!line.contains("#") && !line.contains("!")) {
//line.to
try {
Integer cardId = Integer.parseInt(line);
tempDeck.add(cardId);
Card card = mCardManager.getCard(cardId);
cardList.add(card);
} catch (NumberFormatException e) {
LogUtil.i(TAG, "cannot parse Interget" + line + e.getMessage());
}
}
}
return cardList;
}
} }
...@@ -11,21 +11,12 @@ import android.widget.EditText; ...@@ -11,21 +11,12 @@ import android.widget.EditText;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.Toast; import android.widget.Toast;
import com.google.gson.Gson;
import java.io.IOException;
import cn.garymb.ygomobile.deck_square.api_response.LoginRequest;
import cn.garymb.ygomobile.deck_square.api_response.LoginResponse; import cn.garymb.ygomobile.deck_square.api_response.LoginResponse;
import cn.garymb.ygomobile.lite.R; import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.ui.plus.VUiKit; import cn.garymb.ygomobile.ui.plus.VUiKit;
import cn.garymb.ygomobile.utils.LogUtil; import cn.garymb.ygomobile.utils.LogUtil;
import cn.garymb.ygomobile.utils.SharedPreferenceUtil; import cn.garymb.ygomobile.utils.SharedPreferenceUtil;
import okhttp3.MediaType; import cn.garymb.ygomobile.utils.YGOUtil;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class LoginDialog extends Dialog { public class LoginDialog extends Dialog {
...@@ -75,59 +66,10 @@ public class LoginDialog extends Dialog { ...@@ -75,59 +66,10 @@ public class LoginDialog extends Dialog {
btnLogin.setEnabled(false); btnLogin.setEnabled(false);
btnCancel.setEnabled(false); btnCancel.setEnabled(false);
// Simulate network call
// new Handler().postDelayed(() -> {
// if (listener != null) {
// listener.onLogin(username, password);
// }
// dismiss();
// }, 1500);
VUiKit.defer().when(() -> { VUiKit.defer().when(() -> {
LogUtil.d(TAG, "start fetch"); LogUtil.d(TAG, "start fetch");
LoginResponse result = null;
try {
String url = "https://sapi.moecube.com:444/accounts/signin";
String baseUrl = "https://sapi.moecube.com:444/accounts/signin";
// Create request body using Gson
Gson gson = new Gson();
LoginRequest loginRequest = new LoginRequest("1076306278@qq.com", "Qbz95qbz96");
String json = gson.toJson(loginRequest);//"{\"id\":1,\"name\":\"John\"}";
RequestBody body = RequestBody.create(
MediaType.parse("application/json"), json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
OkHttpClient okHttpClient = new OkHttpClient();
Response response = okHttpClient.newCall(request).execute();
// Read the response body
String responseBody = response.body().string();
LogUtil.i(TAG, "Response body: " + responseBody);
// Process the response
if (response.isSuccessful()) {
// Successful response (code 200-299)
// Parse the JSON response if needed
result = gson.fromJson(responseBody, LoginResponse.class);
LogUtil.i(TAG, "Login response: " + result);
} else {
// Error response
LogUtil.e(TAG, "Request failed: " + responseBody);
}
} catch (IOException e) {
Log.e(TAG, "Error occured when fetching data from pre-card server");
return null;
}
LoginResponse result = DeckSquareApiUtil.login("", "");
SharedPreferenceUtil.setServerToken(result.token); SharedPreferenceUtil.setServerToken(result.token);
SharedPreferenceUtil.setServerUserId(result.user.id); SharedPreferenceUtil.setServerUserId(result.user.id);
return result; return result;
...@@ -136,6 +78,7 @@ public class LoginDialog extends Dialog { ...@@ -136,6 +78,7 @@ public class LoginDialog extends Dialog {
Log.e(TAG, e + ""); Log.e(TAG, e + "");
listener.notifyResult(false, null); listener.notifyResult(false, null);
LogUtil.i(TAG, "login fail"); LogUtil.i(TAG, "login fail");
dismiss(); dismiss();
...@@ -143,9 +86,9 @@ public class LoginDialog extends Dialog { ...@@ -143,9 +86,9 @@ public class LoginDialog extends Dialog {
if (result != null) { if (result != null) {
LogUtil.i(TAG, "login done"); LogUtil.i(TAG, "login done");
listener.notifyResult(true, result); listener.notifyResult(true, result);
// getData().clear();
// addData(exCardDataList); YGOUtil.showTextToast("Login success!");
// notifyDataSetChanged();
} else { } else {
listener.notifyResult(false, null); listener.notifyResult(false, null);
} }
......
...@@ -3,46 +3,41 @@ package cn.garymb.ygomobile.deck_square; ...@@ -3,46 +3,41 @@ package cn.garymb.ygomobile.deck_square;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
import android.widget.Button; import android.widget.Button;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import java.io.File; import androidx.recyclerview.widget.LinearLayoutManager;
import java.io.FileOutputStream; import androidx.recyclerview.widget.RecyclerView;
import java.io.IOException;
import java.util.List;
import cn.garymb.ygomobile.AppsSettings; import cn.garymb.ygomobile.AppsSettings;
import cn.garymb.ygomobile.deck_square.api_response.DeckDetail;
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.PushDeckResponse;
import cn.garymb.ygomobile.lite.R; import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.ui.adapters.DeckPreviewListAdapter;
import cn.garymb.ygomobile.ui.plus.VUiKit; import cn.garymb.ygomobile.ui.plus.VUiKit;
import cn.garymb.ygomobile.utils.LogUtil; import cn.garymb.ygomobile.utils.LogUtil;
import cn.garymb.ygomobile.utils.SharedPreferenceUtil; import cn.garymb.ygomobile.utils.SharedPreferenceUtil;
import cn.garymb.ygomobile.utils.YGOUtil; import cn.garymb.ygomobile.utils.YGOUtil;
import ocgcore.data.Card;
public class MyDeckDetailDialog extends Dialog { public class MyDeckDetailDialog extends Dialog {
private static final String TAG = DeckSquareListAdapter.class.getSimpleName(); private static final String TAG = DeckSquareListAdapter.class.getSimpleName();
// private String deckId; DeckPreviewListAdapter mListAdapter;
// private String deckName; private RecyclerView mListView;
// private Integer userId; private DeckDetail mDeckDetail = null;
MyDeckItem item;
public interface ActionListener {
void onDownloadClicked();
void onLikeClicked(); MyDeckItem mItem = null;//存储触发本dialog的卡组的基本信息
}
private ActionListener listener;
public MyDeckDetailDialog(Context context, MyDeckItem item) { public MyDeckDetailDialog(Context context, MyDeckItem item) {
super(context); super(context);
// deckId = item.getDeckId();
// deckName = item.getDeckName(); this.mItem = item;
// userId = item.getUserId();
this.item = item;
} }
...@@ -55,103 +50,100 @@ public class MyDeckDetailDialog extends Dialog { ...@@ -55,103 +50,100 @@ public class MyDeckDetailDialog extends Dialog {
Button btnDownload = findViewById(R.id.dialog_my_deck_btn_download); Button btnDownload = findViewById(R.id.dialog_my_deck_btn_download);
Button btnPush = findViewById(R.id.dialog_my_deck_btn_push); Button btnPush = findViewById(R.id.dialog_my_deck_btn_push);
Button btnLike = findViewById(R.id.btnLike);
LinearLayout downloadLayout = findViewById(R.id.server_download_layout); LinearLayout downloadLayout = findViewById(R.id.server_download_layout);
LinearLayout uploadLayout = findViewById(R.id.server_upload_layout); LinearLayout uploadLayout = findViewById(R.id.server_upload_layout);
if (item.getDeckSouce() == 0) {//来自本地 if (mItem.getDeckSouce() == 0) {//来自本地
downloadLayout.setVisibility(View.GONE); downloadLayout.setVisibility(View.GONE);
uploadLayout.setVisibility(View.VISIBLE); uploadLayout.setVisibility(View.VISIBLE);
//btnDownload.setBackground(R.id.ic); //btnDownload.setBackground(R.id.ic);
} else if (item.getDeckSouce() == 1) {//来自服务器 } else if (mItem.getDeckSouce() == 1) {//来自服务器
downloadLayout.setVisibility(View.VISIBLE); downloadLayout.setVisibility(View.VISIBLE);
uploadLayout.setVisibility(View.GONE); uploadLayout.setVisibility(View.GONE);
} else if (item.getDeckSouce() == 2) {//本地、服务器均存在 previewDeckCard();
} else if (mItem.getDeckSouce() == 2) {//本地、服务器均存在
downloadLayout.setVisibility(View.VISIBLE); downloadLayout.setVisibility(View.VISIBLE);
uploadLayout.setVisibility(View.VISIBLE); uploadLayout.setVisibility(View.VISIBLE);
previewDeckCard();
} }
btnPush.setOnClickListener(v -> { mListAdapter = new DeckPreviewListAdapter(R.layout.item_square_deck_card_preview);
Integer userId = SharedPreferenceUtil.getServerUserId();
if (userId == null) {
YGOUtil.showTextToast("Please login first!");
return;
}
VUiKit.defer().when(() -> {
DeckSquareApiUtil.pushDeck(item.getDeckPath(),item.getDeckName(), userId);
});//.done();
});
mListView = findViewById(R.id.dialog_my_deck_detail_list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
mListView.setLayoutManager(linearLayoutManager);
mListView.setAdapter(mListAdapter);
btnDownload.setOnClickListener(v -> { //上传卡组
btnPush.setOnClickListener(v -> {
Integer userId = SharedPreferenceUtil.getServerUserId(); Integer userId = SharedPreferenceUtil.getServerUserId();
if (userId == null) { String serverToken = SharedPreferenceUtil.getServerToken();//todo serverToken要外部传入还是此处获取?考虑
if (userId == null || serverToken == null) {
YGOUtil.showTextToast("Please login first!"); YGOUtil.showTextToast("Please login first!");
return; return;
} }
VUiKit.defer().when(() -> {
DownloadDeckResponse response = DeckSquareApiUtil.getDeckById(item.getDeckId());
if (response != null) {
return response.getData(); VUiKit.defer().when(() -> {
PushDeckResponse result = DeckSquareApiUtil.pushDeck(mItem.getDeckPath(), mItem.getDeckName(), userId, serverToken);
return result;
}).fail(e -> {
LogUtil.i(TAG, "square deck detail fail" + e.getMessage());
}).done(data -> {
if (data.isData()) {
YGOUtil.showTextToast("push success!");
} else { } else {
return null;
}
}).fail((e) -> { YGOUtil.showTextToast(data.getMessage());
Log.e(TAG, e + "");
// if (dialog_read_ex.isShowing()) {//关闭异常
// try {
// dialog_read_ex.dismiss();
// } catch (Exception ex) {
//
// }
// }
LogUtil.i(TAG, "square deck detail fail");
}).done((deckData) -> {
if (deckData != null) {
String path = AppsSettings.get().getDeckDir();
saveFileToPath(path, item.getDeckName() + ".ydk", deckData.deckYdk);
LogUtil.i(TAG, "square deck detail done");
YGOUtil.showTextToast(R.string.down_complete);
} }
// if (dialog_read_ex.isShowing()) {
// try {
// dialog_read_ex.dismiss();
// } catch (Exception ex) {
// }
// }
}); });
dismiss(); //todo,改造pushDeck,传入回调,得到推送的http响应
}); });
btnLike.setOnClickListener(v -> { //下载用户在平台上的卡组
btnDownload.setOnClickListener(v -> {
dismiss(); if (mDeckDetail != null) {
String path = AppsSettings.get().getDeckDir();
DeckSquareFileUtil.saveFileToPath(path, mDeckDetail.getDeckName() + ".ydk", mDeckDetail.getDeckYdk());
}
}); });
} }
public void saveFileToPath(String path, String fileName, String content) { private void previewDeckCard() {
try { Integer userId = SharedPreferenceUtil.getServerUserId();
// Create file object if (userId == null) {
File file = new File(path, fileName); YGOUtil.showTextToast("Please login first!");
return;
}
VUiKit.defer().when(() -> {
DownloadDeckResponse response = DeckSquareApiUtil.getDeckById(mItem.getDeckId());
if (response != null) {
return response.getData();
} else {
return null;
}
// Create file output stream }).fail((e) -> {
FileOutputStream fos = new FileOutputStream(file);
LogUtil.i(TAG, "square deck detail fail" + e.getMessage());
}).done((deckData) -> {
if (deckData != null) {
mDeckDetail = deckData;
LogUtil.i(TAG, "square deck detail done");
List<Card> cardList = DeckSquareFileUtil.convertTempDeckYdk(deckData.getDeckYdk());
mListAdapter.updateData(cardList);
}
});
// Write content
fos.write(content.getBytes());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
} }
\ No newline at end of file
...@@ -3,50 +3,41 @@ package cn.garymb.ygomobile.deck_square; ...@@ -3,50 +3,41 @@ package cn.garymb.ygomobile.deck_square;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.Window; import android.view.Window;
import android.widget.Button; import android.widget.Button;
import com.google.gson.Gson; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.io.File; import java.util.List;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import cn.garymb.ygomobile.AppsSettings; import cn.garymb.ygomobile.AppsSettings;
import cn.garymb.ygomobile.deck_square.api_response.ApiDeckRecord; import cn.garymb.ygomobile.deck_square.api_response.ApiDeckRecord;
import cn.garymb.ygomobile.deck_square.api_response.BasicResponse;
import cn.garymb.ygomobile.deck_square.api_response.DeckDetail;
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.lite.R; import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.ui.adapters.DeckPreviewListAdapter;
import cn.garymb.ygomobile.ui.plus.VUiKit; import cn.garymb.ygomobile.ui.plus.VUiKit;
import cn.garymb.ygomobile.utils.LogUtil; import cn.garymb.ygomobile.utils.LogUtil;
import cn.garymb.ygomobile.utils.OkhttpUtil;
import cn.garymb.ygomobile.utils.SharedPreferenceUtil; import cn.garymb.ygomobile.utils.SharedPreferenceUtil;
import cn.garymb.ygomobile.utils.YGOUtil; import cn.garymb.ygomobile.utils.YGOUtil;
import okhttp3.Response; import ocgcore.data.Card;
public class SquareDeckDetailDialog extends Dialog { public class SquareDeckDetailDialog extends Dialog {
private static final String TAG = DeckSquareListAdapter.class.getSimpleName(); private static final String TAG = DeckSquareListAdapter.class.getSimpleName();
private String deckId;
private String deckName;
private Integer userId;
public interface ActionListener { DeckPreviewListAdapter mListAdapter;
void onDownloadClicked(); private RecyclerView mListView;
private DeckDetail mDeckDetail = null;
void onLikeClicked(); private ApiDeckRecord mItem = null;
}
private ActionListener listener;
public SquareDeckDetailDialog(Context context, ApiDeckRecord item) { public SquareDeckDetailDialog(Context context, ApiDeckRecord item) {
super(context); super(context);
deckId = item.getDeckId(); mItem = item;
deckName = item.getDeckName();
userId = item.getUserId();
} }
@Override @Override
...@@ -55,88 +46,83 @@ public class SquareDeckDetailDialog extends Dialog { ...@@ -55,88 +46,83 @@ public class SquareDeckDetailDialog extends Dialog {
requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_square_deck_detail); setContentView(R.layout.dialog_square_deck_detail);
Button btnDownload = findViewById(R.id.btnDownload); Button btnDownload = findViewById(R.id.dialog_square_deck_btn_download);
Button btnLike = findViewById(R.id.btnLike); Button btnLike = findViewById(R.id.btnLike);
btnDownload.setOnClickListener(v -> { previewDeckCard();
VUiKit.defer().when(() -> {
DownloadDeckResponse result = null;
String url = "http://rarnu.xyz:38383/api/mdpro3/deck/" + deckId;
Map<String, String> headers = new HashMap<>();
headers.put("ReqSource", "MDPro3"); mListAdapter = new DeckPreviewListAdapter(R.layout.item_square_deck_card_preview);
Response response = null; mListView = findViewById(R.id.dialog_square_deck_detail_list);
try { LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
response = OkhttpUtil.synchronousGet(url, null, headers); mListView.setLayoutManager(linearLayoutManager);
String responseBodyString = response.body().string(); mListView.setAdapter(mListAdapter);
//下载卡组广场的卡组
btnDownload.setOnClickListener(v -> {
if (mDeckDetail != null) {
String path = AppsSettings.get().getDeckDir();
boolean result = DeckSquareFileUtil.saveFileToPath(path, mDeckDetail.getDeckName() + ".ydk", mDeckDetail.getDeckYdk());
if (result) {
Gson gson = new Gson(); YGOUtil.showTextToast("Download deck success!");
// Convert JSON to Java object using Gson
result = gson.fromJson(responseBodyString, DownloadDeckResponse.class);
LogUtil.i(TAG, responseBodyString);
} catch (IOException e) {
throw new RuntimeException(e);
}
if (result == null) {
return null;
} else { } else {
return result.getData();
}
}).fail((e) -> { YGOUtil.showTextToast("Download deck fail!");
Log.e(TAG, e + "");
// if (dialog_read_ex.isShowing()) {//关闭异常
// try {
// dialog_read_ex.dismiss();
// } catch (Exception ex) {
//
// }
// }
LogUtil.i(TAG, "square deck detail fail");
}).done((deckData) -> {
if (deckData != null) {
String path = AppsSettings.get().getDeckDir();
saveFileToPath(path, deckName + ".ydk", deckData.deckYdk);
LogUtil.i(TAG, "square deck detail done");
YGOUtil.showTextToast(R.string.down_complete);
} }
// if (dialog_read_ex.isShowing()) { }
// try {
// dialog_read_ex.dismiss();
// } catch (Exception ex) {
// }
// }
});
dismiss();
}); });
//给卡组点赞
btnLike.setOnClickListener(v -> { btnLike.setOnClickListener(v -> {
VUiKit.defer().when(() -> {
dismiss(); BasicResponse result = DeckSquareApiUtil.likeDeck(mItem.getDeckId());
return result;
}).fail(e -> {
LogUtil.i(TAG, "Like deck fail" + e.getMessage());
}).done(data -> {
if (data.getMessage().equals("true")) {
YGOUtil.showTextToast("Like deck success!");
} else {
YGOUtil.showTextToast(data.getMessage());
}
});
}); });
} }
public void saveFileToPath(String path, String fileName, String content) { private void previewDeckCard() {
try { Integer userId = SharedPreferenceUtil.getServerUserId();
// Create file object if (userId == null) {
File file = new File(path, fileName); YGOUtil.showTextToast("Please login first!");
return;
}
VUiKit.defer().when(() -> {
// Create file output stream DownloadDeckResponse response = DeckSquareApiUtil.getDeckById(mItem.getDeckId());
FileOutputStream fos = new FileOutputStream(file); if (response != null) {
return response.getData();
} else {
return null;
}
}).fail((e) -> {
LogUtil.i(TAG, "square deck detail fail" + e.getMessage());
}).done((deckData) -> {
if (deckData != null) {
mDeckDetail = deckData;
LogUtil.i(TAG, "square deck detail done");
List<Card> cardList = DeckSquareFileUtil.convertTempDeckYdk(deckData.getDeckYdk());
mListAdapter.updateData(cardList);
}
});
// Write content
fos.write(content.getBytes());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
} }
\ No newline at end of file
package cn.garymb.ygomobile.deck_square.api_response;
public class BasicResponse {
private Integer code;
private String message;
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;
}
}
...@@ -8,23 +8,23 @@ import androidx.annotation.NonNull; ...@@ -8,23 +8,23 @@ import androidx.annotation.NonNull;
public class DeckDetail implements Parcelable { public class DeckDetail implements Parcelable {
public String deckId; private String deckId;
public String deckContributor; private String deckContributor;
public String deckName; private String deckName;
public String deckRank; private String deckRank;
public String deckLike; private String deckLike;
public String deckUploadDate; private String deckUploadDate;
public String deckUpdateDate; private String deckUpdateDate;
public int deckCoverCard1; private int deckCoverCard1;
public int deckCoverCard2; private int deckCoverCard2;
public int deckCoverCard3; private int deckCoverCard3;
public String deckCase; private String deckCase;
public String deckProtector; private String deckProtector;
public String deckMainSerial; private String deckMainSerial;
public String deckYdk; private String deckYdk;
public Integer userId; private Integer userId;
public String isPublic; private String isPublic;
public String isDelete; private String isDelete;
protected DeckDetail(Parcel in) { protected DeckDetail(Parcel in) {
......
package cn.garymb.ygomobile.deck_square.api_response; package cn.garymb.ygomobile.deck_square.api_response;
public class DownloadDeckResponse { public class DownloadDeckResponse {
public Integer code; private Integer code;
public String message; private String message;
public DeckDetail data; private DeckDetail data;
public Integer getCode() { public Integer getCode() {
return code; return code;
......
package cn.garymb.ygomobile.deck_square.api_response;
//将卡组上传后,返回的响应
//对应接口http://rarnu.xyz:38383/api/mdpro3/sync/single
public class PushDeckResponse {
private Integer code;
private String message;
private boolean data;
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 boolean isData() {
return data;
}
public void setData(boolean data) {
this.data = data;
}
}
package cn.garymb.ygomobile.ui.adapters;
import androidx.annotation.NonNull;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import java.util.List;
import cn.garymb.ygomobile.ex_card.ExCardListAdapter;
import cn.garymb.ygomobile.lite.R;
import ocgcore.data.Card;
//卡组预览的adapter
public class DeckPreviewListAdapter extends BaseQuickAdapter<Card, BaseViewHolder> {
private static final String TAG = ExCardListAdapter.class.getSimpleName();
public DeckPreviewListAdapter(int layoutResId) {
super(layoutResId);
}
public void updateData(List<Card> dataList) {
getData().clear();
addData(dataList);
notifyDataSetChanged();
}
@Override
protected void convert(@NonNull BaseViewHolder baseViewHolder, Card item) {
baseViewHolder.setText(R.id.preview_card_name, item.Name);
baseViewHolder.setText(R.id.preview_card_id, Integer.toString(item.Code));
}
}
...@@ -257,10 +257,8 @@ public class OkhttpUtil { ...@@ -257,10 +257,8 @@ public class OkhttpUtil {
public static void post(String url, String json, String cookie, Callback callback) { public static void post(String url, String json, String cookie, Callback callback) {
OkHttpClient okHttpClient = new OkHttpClient(); OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), json);
, json); Request.Builder request = new Request.Builder().url(url);//请求的url
Request.Builder request = new Request.Builder()
.url(url);//请求的url
if (TextUtils.isEmpty(json)) if (TextUtils.isEmpty(json))
request.post(okhttp3.internal.Util.EMPTY_REQUEST); request.post(okhttp3.internal.Util.EMPTY_REQUEST);
else else
...@@ -284,8 +282,13 @@ public class OkhttpUtil { ...@@ -284,8 +282,13 @@ public class OkhttpUtil {
} }
} }
/**
public static void postJson(String url, String json, Map<String, String> headers, int timeout, Callback callback) { * @param url
* @param json 可以传入null或空字符串,均代表不需要发送json
* @param headers 可以传入null
* @param timeout 可以为0,为0代表使用默认值
*/
public static Response postJson(String url, String json, Map<String, String> headers, int timeout) throws IOException {
okHttpClient = new OkHttpClient(); okHttpClient = new OkHttpClient();
if (timeout != 0) if (timeout != 0)
...@@ -294,15 +297,15 @@ public class OkhttpUtil { ...@@ -294,15 +297,15 @@ public class OkhttpUtil {
.build(); .build();
RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") Request.Builder request = new Request.Builder().url(url);//请求的url
, json); if (json == null || TextUtils.isEmpty(json)) {
Request.Builder request = new Request.Builder()
.url(url);//请求的url
if (TextUtils.isEmpty(json))
request.post(okhttp3.internal.Util.EMPTY_REQUEST); request.post(okhttp3.internal.Util.EMPTY_REQUEST);
else } else {
RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), json);
request.post(requestBody); request.post(requestBody);
}
if (headers != null) { if (headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) { for (Map.Entry<String, String> header : headers.entrySet()) {
request.addHeader(header.getKey(), header.getValue().toString()); request.addHeader(header.getKey(), header.getValue().toString());
...@@ -310,7 +313,8 @@ public class OkhttpUtil { ...@@ -310,7 +313,8 @@ public class OkhttpUtil {
} }
Log.e("OkhttpUtil", json + " 状态 " + request.build()); Log.e("OkhttpUtil", json + " 状态 " + request.build());
okHttpClient.newCall(request.build()).enqueue(callback); return okHttpClient.newCall(request.build()).execute();
//okHttpClient.newCall(request.build()).enqueue(callback);
} }
/** /**
......
...@@ -5,34 +5,50 @@ ...@@ -5,34 +5,50 @@
android:orientation="vertical" android:orientation="vertical"
android:padding="16dp"> android:padding="16dp">
<TextView <LinearLayout
android:id="@+id/tvTitle"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="16dp" android:orientation="horizontal">
android:gravity="center"
android:text="Choose Action" <ImageView
android:textSize="18sp" android:layout_width="60dp"
android:textStyle="bold" /> android:layout_height="60dp"
android:src="@drawable/ic_server_download" />
<Button
android:id="@+id/btnDownload" <Button
style="@style/Widget.AppCompat.Button.Borderless" android:id="@+id/dialog_square_deck_btn_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Download" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="8dp" android:orientation="horizontal">
android:drawableStart="@drawable/ic_download"
android:drawablePadding="8dp" <ImageView
android:text="Download" /> android:layout_width="60dp"
android:layout_height="60dp"
<Button android:src="@drawable/ic_like" />
android:id="@+id/btnLike"
style="@style/Widget.AppCompat.Button.Borderless" <Button
android:id="@+id/btnLike"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:text="Like" />
</LinearLayout>
<com.tubb.smrv.SwipeMenuRecyclerView
android:id="@+id/dialog_square_deck_detail_list"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:drawableStart="@drawable/ic_like" android:divider="@android:color/transparent"
android:drawablePadding="8dp" android:dividerHeight="4dp"
android:text="Like" /> android:padding="5dp"
android:scrollbars="vertical" />
</LinearLayout> </LinearLayout>
\ No newline at end of file
...@@ -5,16 +5,6 @@ ...@@ -5,16 +5,6 @@
android:orientation="vertical" android:orientation="vertical"
android:padding="16dp"> android:padding="16dp">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:gravity="center"
android:text="Choose Action"
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout <LinearLayout
android:id="@+id/server_download_layout" android:id="@+id/server_download_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
...@@ -54,14 +44,13 @@ ...@@ -54,14 +44,13 @@
</LinearLayout> </LinearLayout>
<Button
android:id="@+id/btnLike" <com.tubb.smrv.SwipeMenuRecyclerView
style="@style/Widget.AppCompat.Button.Borderless" android:id="@+id/dialog_my_deck_detail_list"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:drawableStart="@drawable/ic_like" android:divider="@android:color/transparent"
android:drawablePadding="8dp" android:dividerHeight="4dp"
android:text="Delete" /> android:padding="5dp"
android:scrollbars="vertical" />
</LinearLayout> </LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/preview_card_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="@drawable/veil"
android:gravity="top"
android:paddingLeft="8dp"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="12sp"
tools:text="name" />
<TextView
android:id="@+id/preview_card_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:textSize="12sp" />
</LinearLayout>
<!-- <ImageView-->
<!-- android:id="@+id/card_image"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_marginLeft="1px"-->
<!-- android:layout_marginRight="1px"-->
<!-- android:layout_marginBottom="1px"-->
<!-- tools:src="@drawable/unknown" />-->
<ImageView
android:id="@+id/right_top"
android:layout_width="@dimen/right_size2"
android:layout_height="@dimen/right_size2"
android:layout_alignParentRight="true"
android:visibility="visible"
app:srcCompat="@drawable/ic_close_black_24dp" />
</RelativeLayout>
\ No newline at end of file
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