Commit 6f75eaf1 authored by fallenstardust's avatar fallenstardust

getDeckFirstCode

parent 44ce1006
...@@ -3,11 +3,15 @@ package cn.garymb.ygomobile.bean.events; ...@@ -3,11 +3,15 @@ package cn.garymb.ygomobile.bean.events;
import java.io.File; import java.io.File;
import cn.garymb.ygomobile.bean.TextSelect; import cn.garymb.ygomobile.bean.TextSelect;
import cn.garymb.ygomobile.utils.DeckUtil;
public class DeckFile extends TextSelect { public class DeckFile extends TextSelect {
private final File path; private final File path;
private final String fullName; private final String fullName;
//
private String typeName;
private int firstCode;
public DeckFile(String path) { public DeckFile(String path) {
this(new File(path)); this(new File(path));
...@@ -21,10 +25,29 @@ public class DeckFile extends TextSelect { ...@@ -21,10 +25,29 @@ public class DeckFile extends TextSelect {
if(index > 0) { if(index > 0) {
name = name.substring(0, index); name = name.substring(0, index);
} }
typeName= DeckUtil.getDeckTypeName(path.getAbsolutePath());
firstCode=DeckUtil.getFirstCardCode(path.getAbsolutePath());
super.setName(name); super.setName(name);
setObject(this); setObject(this);
} }
public int getFirstCode() {
return firstCode;
}
public void setFirstCode(int firstCode) {
this.firstCode = firstCode;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public String getFileName() { public String getFileName() {
return fullName; return fullName;
} }
......
package cn.garymb.ygomobile.utils; package cn.garymb.ygomobile.utils;
import android.content.Context; import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
...@@ -18,9 +23,36 @@ import cn.garymb.ygomobile.Constants; ...@@ -18,9 +23,36 @@ import cn.garymb.ygomobile.Constants;
import cn.garymb.ygomobile.bean.DeckType; import cn.garymb.ygomobile.bean.DeckType;
import cn.garymb.ygomobile.bean.events.DeckFile; import cn.garymb.ygomobile.bean.events.DeckFile;
import cn.garymb.ygomobile.lite.R; import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.ui.cards.deck.DeckItemType;
public class DeckUtil { public class DeckUtil {
private final static Comparator<DeckFile> nameCom = new Comparator<DeckFile>() {
@Override
public int compare(DeckFile ydk1, DeckFile ydk2) {
if (!ydk1.getTypeName().equals(YGOUtil.s(R.string.category_Uncategorized))
&& ydk2.getTypeName().equals(YGOUtil.s(R.string.category_Uncategorized)))
return 1;
else if (ydk1.getTypeName().equals(YGOUtil.s(R.string.category_Uncategorized))
&& !ydk2.getTypeName().equals(YGOUtil.s(R.string.category_Uncategorized)))
return -1;
int id = ydk1.getTypeName().compareTo(ydk2.getTypeName());
if (id == 0)
return ydk1.getName().compareTo(ydk2.getName());
else
return id;
}
};
private final static Comparator<DeckFile> dateCom = new Comparator<DeckFile>() {
@Override
public int compare(DeckFile ydk1, DeckFile ydk2) {
return ydk2.getDate().compareTo(ydk1.getDate());
}
};
public static List<DeckType> getDeckTypeList(Context context) { public static List<DeckType> getDeckTypeList(Context context) {
List<DeckType> deckTypeList = new ArrayList<>(); 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_pack), AppsSettings.get().getPackDeckDir()));
...@@ -56,6 +88,39 @@ public class DeckUtil { ...@@ -56,6 +88,39 @@ public class DeckUtil {
return deckList; return deckList;
} }
public static List<DeckFile> getDeckAllList() {
return getDeckAllList(AppsSettings.get().getDeckDir());
}
public static List<DeckFile> getDeckAllList(String path) {
return getDeckAllList(path, false);
}
public static List<DeckFile> getDeckAllList(String path, boolean isDir) {
List<DeckFile> deckList = new ArrayList<>();
File[] files = new File(path).listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
deckList.addAll(getDeckAllList(file.getAbsolutePath(), true));
}
if (file.isFile() && file.getName().endsWith(".ydk")) {
deckList.add(new DeckFile(file));
}
}
}
if (!isDir) {
Log.e("DeckUtil", "路径 " + path);
Log.e("DeckUtil", "路径1 " + AppsSettings.get().getPackDeckDir());
if (path.equals(AppsSettings.get().getPackDeckDir())) {
Collections.sort(deckList, dateCom);
} else {
Collections.sort(deckList, nameCom);
}
}
return deckList;
}
/** /**
* 根据卡组绝对路径获取卡组分类名称 * 根据卡组绝对路径获取卡组分类名称
* @param deckPath * @param deckPath
...@@ -120,18 +185,42 @@ public class DeckUtil { ...@@ -120,18 +185,42 @@ public class DeckUtil {
return deckList; return deckList;
} }
private final static Comparator<DeckFile> nameCom = new Comparator<DeckFile>() { public static int getFirstCardCode(String deckPath) {
@Override InputStreamReader in = null;
public int compare(DeckFile ydk1, DeckFile ydk2) { try {
return ydk1.getName().compareTo(ydk2.getName()); in = new InputStreamReader(new FileInputStream(new File(deckPath)), "utf-8");
BufferedReader reader = new BufferedReader(in);
String line = null;
DeckItemType type = DeckItemType.Space;
while ((line = reader.readLine()) != null) {
if (line.startsWith("!side")) {
type = DeckItemType.SideCard;
continue;
}
if (line.startsWith("#")) {
if (line.startsWith("#main")) {
type = DeckItemType.MainCard;
} else if (line.startsWith("#extra")) {
type = DeckItemType.ExtraCard;
}
continue;
}
line = line.trim();
if (line.length() == 0 || !TextUtils.isDigitsOnly(line)) {
if (Constants.DEBUG)
Log.w("kk", "read not number " + line);
continue;
}
Integer id = Integer.parseInt(line);
return id;
}
} catch (IOException e) {
Log.e("deckreader", "read 2", e);
} finally {
IOUtils.close(in);
} }
};
private final static Comparator<DeckFile> dateCom = new Comparator<DeckFile>() { return -1;
@Override
public int compare(DeckFile ydk1, DeckFile ydk2) {
return ydk2.getDate().compareTo(ydk1.getDate());
} }
};
} }
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