Commit 5e136e20 authored by wangfugui's avatar wangfugui

修改卡组编辑功能,支持在线卡组

parent 7e3a7f78
package cn.garymb.ygomobile.bean;
public class DeckType extends TextSelect {
public enum ServerType {
LOCAL,
SQUARE,
MY_SQUARE,
}
private String name;
private String path;
private ServerType onServer;//true代表云端卡组,false代表本地卡组
public DeckType(String name, String path) {
this.name = name;
this.path = path;
onServer = ServerType.LOCAL;
super.setName(name);
setObject(this);
}
public DeckType(String name, String path, ServerType onServer) {
this.name = name;
this.path = path;
this.onServer = onServer;
setObject(this);
}
public String getName() {
return name;
}
......@@ -27,4 +42,16 @@ public class DeckType extends TextSelect {
this.path = path;
}
public ServerType getOnServer() {
return onServer;
}
public void setOnServer(ServerType onServer) {
this.onServer = onServer;
}
//true代表卡组位于本地
public boolean isLocal() {
return (this.onServer == ServerType.LOCAL);
}
}
......@@ -2,16 +2,35 @@ package cn.garymb.ygomobile.bean.events;
import java.io.File;
import cn.garymb.ygomobile.bean.DeckType;
import cn.garymb.ygomobile.bean.TextSelect;
import cn.garymb.ygomobile.utils.DeckUtil;
public class DeckFile extends TextSelect {
private final File path;
private final String fullName;
private File path;
private String fileFullName;//本地文件对应的名称,仅在onServer为false时有效
// private final File path;
// private final String fullName;
//分类
private String typeName;
private String typeName;//可取值包括cacheDeck,Pack
private int firstCode;
private DeckType.ServerType onServer;//true代表云端卡组,false代表本地卡组
private String deckId;//如果onServer为true,代表该卡组存储在云上,deckId是唯一id
// public DeckFile(boolean onServer) {
// this.onServer = onServer;
// }
public DeckFile(String name, String typeName, DeckType.ServerType onServer, String deckId) {
this.typeName = typeName;
this.onServer = onServer;
this.deckId = deckId;
this.setName(name);
this.fileFullName = null;
this.path = null;
this.firstCode = -1;
setObject(this);
}
public DeckFile(String path) {
this(new File(path));
......@@ -19,10 +38,10 @@ public class DeckFile extends TextSelect {
public DeckFile(File file) {
path = file;
fullName = file.getName();
String name = fullName;
fileFullName = file.getName();
String name = fileFullName;
int index = name.lastIndexOf(".");
if(index > 0) {
if (index > 0) {
name = name.substring(0, index);
}
typeName = DeckUtil.getDeckTypeName(path.getAbsolutePath());
......@@ -49,7 +68,7 @@ public class DeckFile extends TextSelect {
}
public String getFileName() {
return fullName;
return fileFullName;
}
public File getPathFile() {
......@@ -64,4 +83,26 @@ public class DeckFile extends TextSelect {
return path.lastModified();
}
public DeckType.ServerType getOnServer() {
return onServer;
}
public void setOnServer(DeckType.ServerType onServer) {
this.onServer = onServer;
}
//true代表卡组位于本地
public boolean isLocal() {
return (this.onServer == DeckType.ServerType.LOCAL);
}
public String getDeckId() {
return deckId;
}
public void setDeckId(String deckId) {
this.deckId = deckId;
}
}
......@@ -16,6 +16,7 @@ import cn.garymb.ygomobile.deck_square.api_response.LoginResponse;
import cn.garymb.ygomobile.deck_square.api_response.MyDeckResponse;
import cn.garymb.ygomobile.deck_square.api_response.PushCardJson;
import cn.garymb.ygomobile.deck_square.api_response.PushDeckResponse;
import cn.garymb.ygomobile.deck_square.api_response.SquareDeckResponse;
import cn.garymb.ygomobile.utils.LogUtil;
import cn.garymb.ygomobile.utils.OkhttpUtil;
import cn.garymb.ygomobile.utils.YGOUtil;
......@@ -29,9 +30,25 @@ public class DeckSquareApiUtil {
private static final String TAG = DeckSquareListAdapter.class.getSimpleName();
public static SquareDeckResponse getSquareDecks() throws IOException {
SquareDeckResponse result = null;
String url = "http://rarnu.xyz:38383/api/mdpro3/deck/list";
Map<String, String> headers = new HashMap<>();
headers.put("ReqSource", "MDPro3");
Response response = OkhttpUtil.synchronousGet(url, null, headers);
String responseBodyString = response.body().string();
// Type listType = new TypeToken<List<DeckInfo>>() {
// }.getType();
Gson gson = new Gson();
// Convert JSON to Java object using Gson
result = gson.fromJson(responseBodyString, SquareDeckResponse.class);
return result;
}
/**
* 阻塞方法
* 获取指定用户的卡组列表
* 获取指定用户的卡组列表(只能用于获取登录用户本人的卡组)
*
* @param serverUserId
* @param serverToken
......@@ -221,7 +238,6 @@ public class DeckSquareApiUtil {
}
return result;
}
......
......@@ -6,25 +6,19 @@ import android.widget.ImageView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.garymb.ygomobile.deck_square.api_response.ApiDeckRecord;
import cn.garymb.ygomobile.deck_square.api_response.ApiResponse;
import cn.garymb.ygomobile.deck_square.api_response.OnlineDeckDetail;
import cn.garymb.ygomobile.deck_square.api_response.SquareDeckResponse;
import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.loader.ImageLoader;
import cn.garymb.ygomobile.ui.plus.DialogPlus;
import cn.garymb.ygomobile.ui.plus.VUiKit;
import cn.garymb.ygomobile.utils.LogUtil;
import cn.garymb.ygomobile.utils.OkhttpUtil;
import okhttp3.Response;
//提供recyclerview的数据
public class DeckSquareListAdapter extends BaseQuickAdapter<ApiDeckRecord, BaseViewHolder> {
public class DeckSquareListAdapter extends BaseQuickAdapter<OnlineDeckDetail, BaseViewHolder> {
private static final String TAG = DeckSquareListAdapter.class.getSimpleName();
private ImageLoader imageLoader;
......@@ -37,27 +31,7 @@ public class DeckSquareListAdapter extends BaseQuickAdapter<ApiDeckRecord, BaseV
public void loadData() {
final DialogPlus dialog_read_ex = DialogPlus.show(getContext(), null, getContext().getString(R.string.fetch_ex_card));
VUiKit.defer().when(() -> {
LogUtil.d(TAG, "start fetch");
ApiResponse result = null;
try {
String url = "http://rarnu.xyz:38383/api/mdpro3/deck/list";
Map<String, String> headers = new HashMap<>();
headers.put("ReqSource", "MDPro3");
Response response = OkhttpUtil.synchronousGet(url, null, headers);
String responseBodyString = response.body().string();
// Type listType = new TypeToken<List<DeckInfo>>() {
// }.getType();
Gson gson = new Gson();
// Convert JSON to Java object using Gson
result = gson.fromJson(responseBodyString, ApiResponse.class);
LogUtil.i(TAG, responseBodyString);
int a = 0;
} catch (IOException e) {
Log.e(TAG, "Error occured when fetching data from pre-card server");
return null;
}
SquareDeckResponse result = DeckSquareApiUtil.getSquareDecks();
if (result == null) {
return null;
} else {
......@@ -73,11 +47,11 @@ public class DeckSquareListAdapter extends BaseQuickAdapter<ApiDeckRecord, BaseV
}
}
LogUtil.i(TAG, "webCrawler fail");
LogUtil.i(TAG, "Get square deck fail");
}).done((exCardDataList) -> {
if (exCardDataList != null) {
LogUtil.i(TAG, "webCrawler done");
LogUtil.i(TAG, "Get square deck success");
getData().clear();
addData(exCardDataList);
notifyDataSetChanged();
......@@ -102,7 +76,7 @@ public class DeckSquareListAdapter extends BaseQuickAdapter<ApiDeckRecord, BaseV
}
@Override
protected void convert(BaseViewHolder helper, ApiDeckRecord item) {
protected void convert(BaseViewHolder helper, OnlineDeckDetail item) {
helper.setText(R.id.deck_info_name, item.getDeckName());
helper.setText(R.id.deck_contributor, item.getDeckContributor());
helper.setText(R.id.deck_last_date, item.getLastDate());
......
......@@ -37,7 +37,7 @@ public class TextSelectAdapter<T extends TextSelect> extends BaseQuickAdapter<T,
selectPosition = position;
notifyDataSetChanged();
if (onItemSelectListener != null)
onItemSelectListener.onItemSelect(position, data.get(position).getObject());
onItemSelectListener.onItemSelect(position, data.get(position));
}
});
}
......@@ -59,7 +59,7 @@ public class TextSelectAdapter<T extends TextSelect> extends BaseQuickAdapter<T,
} else {
helper.setBackgroundResource(R.id.ll_layout, Color.TRANSPARENT);
}
}else {
} else {
helper.setBackgroundResource(R.id.ll_layout, Color.TRANSPARENT);
}
......
......@@ -76,6 +76,9 @@ import cn.garymb.ygomobile.bean.events.CardInfoEvent;
import cn.garymb.ygomobile.bean.events.DeckFile;
import cn.garymb.ygomobile.core.IrrlichtBridge;
import cn.garymb.ygomobile.deck_square.DeckSquareActivity;
import cn.garymb.ygomobile.deck_square.DeckSquareApiUtil;
import cn.garymb.ygomobile.deck_square.DeckSquareFileUtil;
import cn.garymb.ygomobile.deck_square.api_response.DownloadDeckResponse;
import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.loader.CardLoader;
import cn.garymb.ygomobile.loader.CardSearchInfo;
......@@ -99,6 +102,7 @@ import cn.garymb.ygomobile.utils.BitmapUtil;
import cn.garymb.ygomobile.utils.DeckUtil;
import cn.garymb.ygomobile.utils.FileUtils;
import cn.garymb.ygomobile.utils.IOUtils;
import cn.garymb.ygomobile.utils.LogUtil;
import cn.garymb.ygomobile.utils.ShareUtil;
import cn.garymb.ygomobile.utils.YGODialogUtil;
import cn.garymb.ygomobile.utils.YGOUtil;
......@@ -207,6 +211,7 @@ public class DeckManagerFragment extends BaseFragemnt implements RecyclerViewIte
mContext = (BaseActivity) getActivity();
}
//通过本文件,外部调用fragment时,如果通过setArguments(mBundle)方法设置了ydk文件路径,则直接打开它
public void preLoadFile() {
String preLoadFile = "";
if (getArguments() != null) {
......@@ -382,6 +387,7 @@ public class DeckManagerFragment extends BaseFragemnt implements RecyclerViewIte
}
}
//region load deck
private void loadDeckFromFile(File file) {
if (!mCardLoader.isOpen() || file == null || !file.exists()) {
......@@ -397,7 +403,8 @@ public class DeckManagerFragment extends BaseFragemnt implements RecyclerViewIte
}
}).done((rs) -> {
dlg.dismiss();
setCurDeck(rs, file.getParent().equals(mSettings.getPackDeckDir()) || file.getParent().equals(mSettings.getCacheDeckDir()));
//setCurDeck(rs, file.getParent().equals(mSettings.getPackDeckDir()) || file.getParent().equals(mSettings.getCacheDeckDir()));
setCurDeck(rs, file.getParent().equals(mSettings.getPackDeckDir()));
});
}
......@@ -445,7 +452,7 @@ public class DeckManagerFragment extends BaseFragemnt implements RecyclerViewIte
}
/**
* 设置当前卡组
* 用户选中某个卡组后,更新当前界面,显示已选中的卡组。包括更新界面显示(tv_deck)、更新AppsSettings、通知DeckAdapter
*/
private void setCurDeck(DeckInfo deckInfo, boolean isPack) {
if (deckInfo == null) {
......@@ -1231,7 +1238,42 @@ public class DeckManagerFragment extends BaseFragemnt implements RecyclerViewIte
@Override
public void onDeckSelect(DeckFile deckFile) {
loadDeckFromFile(deckFile.getPathFile());
if (!deckFile.isLocal()) {//不在本地,在云上(卡组广场中或用户的云上)
VUiKit.defer().when(() -> {
DownloadDeckResponse response = DeckSquareApiUtil.getDeckById(deckFile.getDeckId());
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) {
deckData.getDeckYdk();
String fileFullName = deckData.getDeckName() + ".ydk";
String path = AppsSettings.get().getCacheDeckDir();
File dir = new File(getActivity().getApplicationInfo().dataDir, "cache");
boolean result = DeckSquareFileUtil.saveFileToPath(dir.getPath(), fileFullName, deckData.getDeckYdk());
if (result) {
LogUtil.i(TAG, "square deck detail done");
File file = new File(dir, fileFullName);
loadDeckFromFile(file);
}
}
});
} else {
loadDeckFromFile(deckFile.getPathFile());
}
}
@Override
......
......@@ -68,11 +68,19 @@ public class DeckUtil {
}
};
/**
* 生成卡组类型的list
*
* @param context
* @return
*/
public static List<DeckType> getDeckTypeList(Context context) {
List<DeckType> deckTypeList = new ArrayList<>();
deckTypeList.add(new DeckType(YGOUtil.s(R.string.category_pack), AppsSettings.get().getPackDeckDir()));
deckTypeList.add(new DeckType(YGOUtil.s(R.string.category_windbot_deck), AppsSettings.get().getAiDeckDir()));
deckTypeList.add(new DeckType(YGOUtil.s(R.string.category_Uncategorized), AppsSettings.get().getDeckDir()));
deckTypeList.add(new DeckType(YGOUtil.s(R.string.category_pack), AppsSettings.get().getPackDeckDir(), DeckType.ServerType.LOCAL));
deckTypeList.add(new DeckType(YGOUtil.s(R.string.category_windbot_deck), AppsSettings.get().getAiDeckDir(), DeckType.ServerType.LOCAL));
deckTypeList.add(new DeckType(YGOUtil.s(R.string.category_Uncategorized), AppsSettings.get().getDeckDir(), DeckType.ServerType.LOCAL));
deckTypeList.add(new DeckType(YGOUtil.s(R.string.category_online_deck), "", DeckType.ServerType.SQUARE));
deckTypeList.add(new DeckType(YGOUtil.s(R.string.category_my_online_deck), "", DeckType.ServerType.MY_SQUARE));
File[] files = new File(AppsSettings.get().getDeckDir()).listFiles();
if (files != null) {
......@@ -138,6 +146,7 @@ public class DeckUtil {
/**
* 根据卡组绝对路径获取卡组分类名称
*
* @param deckPath
* @return
*/
......@@ -149,19 +158,20 @@ public class DeckUtil {
if (name.equals("pack") || name.equals("cacheDeck")) {
//卡包
return YGOUtil.s(R.string.category_pack);
} else if (name.equals("Decks")&&lastName.equals(Constants.WINDBOT_PATH)) {
} else if (name.equals("Decks") && lastName.equals(Constants.WINDBOT_PATH)) {
//ai卡组
return YGOUtil.s(R.string.category_windbot_deck);
} else if (name.equals("deck") && lastName.equals(Constants.PREF_DEF_GAME_DIR)) {
//如果是deck并且上一个目录是ygocore的话,保证不会把名字为deck的卡包识别为未分类
return YGOUtil.s(R.string.category_Uncategorized);
} else {
return name;
return name;
}
}
return null;
}
//获取扩展卡的列表
public static List<DeckFile> getExpansionsDeckList() throws IOException {
AppsSettings appsSettings = AppsSettings.get();
List<DeckFile> deckList = new ArrayList<>();
......
......@@ -34,12 +34,19 @@
<cn.garymb.ygomobile.ui.widget.AlwaysMarqueeTextView
android:id="@+id/deck_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/white"
tools:text="Deck Name" />
<TextView
android:id="@+id/onlie_deck_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/holo_blue_bright"
android:textSize="10sp"
tools:text="40" />
<LinearLayout
android:id="@+id/deck_info"
......@@ -53,16 +60,16 @@
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginRight="5dp"
android:visibility="visible"
android:background="@drawable/ic_expansions"/>
android:background="@drawable/ic_expansions"
android:visibility="visible" />
<ImageView
android:id="@+id/banned_mark"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginRight="@dimen/dp_10"
android:visibility="visible"
android:background="@drawable/ic_banned"/>
android:background="@drawable/ic_banned"
android:visibility="visible" />
<TextView
android:id="@+id/count_main"
......@@ -71,45 +78,46 @@
android:textColor="@color/holo_blue_bright"
android:textSize="10sp"
tools:text="40" />
<LinearLayout
android:id="@+id/ll_extra_n_side"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/TextExtra"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:textColor="@android:color/holo_green_light"
android:textSize="10sp" />
<TextView
android:id="@+id/TextExtra"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:textColor="@android:color/holo_green_light"
android:textSize="10sp" />
<TextView
android:id="@+id/count_ex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/holo_blue_bright"
android:textSize="10sp"
tools:text="15" />
<TextView
android:id="@+id/count_ex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/holo_blue_bright"
android:textSize="10sp"
tools:text="15" />
<TextView
android:id="@+id/TextSide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:textColor="@android:color/holo_green_light"
android:textSize="10sp" />
<TextView
android:id="@+id/TextSide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:textColor="@android:color/holo_green_light"
android:textSize="10sp" />
<TextView
android:id="@+id/count_side"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/holo_blue_bright"
android:textSize="10sp"
tools:text="15" />
<TextView
android:id="@+id/count_side"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/holo_blue_bright"
android:textSize="10sp"
tools:text="15" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
......
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