Skip to content

Commit

Permalink
自定义文件命名格式最终版
Browse files Browse the repository at this point in the history
  • Loading branch information
CeuiLiSA committed Jul 10, 2020
1 parent e801bf0 commit be4cb71
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 146 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "ceui.lisa.pixiv"
minSdkVersion 21
targetSdkVersion 28
versionCode 103
versionName "2.0.3 beta"
versionCode 104
versionName "2.0.4 beta"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/ceui/lisa/adapters/FileNameAdapter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ceui.lisa.adapters;

import android.content.Context;
import android.view.View;
import android.widget.CompoundButton;

import androidx.annotation.Nullable;
Expand Down Expand Up @@ -38,6 +39,12 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
bindView.baseBind.title.setChecked(target.isChecked());
bindView.baseBind.title.setText(target.getTitle());
bindView.baseBind.description.setText(target.getDesc());
bindView.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bindView.baseBind.title.performClick();
}
});
}

public void unCheckAll() {
Expand Down
163 changes: 150 additions & 13 deletions app/src/main/java/ceui/lisa/download/FileCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import ceui.lisa.activities.Shaft;
import ceui.lisa.model.CustomFileNameCell;
import ceui.lisa.models.IllustsBean;
import ceui.lisa.utils.Common;
import ceui.lisa.utils.Settings;
Expand Down Expand Up @@ -75,17 +80,7 @@ public static File createIllustFile(IllustsBean illustsBean, int index) {
return null;
}

String title = illustsBean.getTitle();
int id = illustsBean.getId();

//只有1P,不带P数
return illustsBean.getPage_count() == 1 ?
new File(Shaft.sSettings.getIllustPath(),
deleteSpecialWords(title + DASH + id + ".png")
) :
new File(Shaft.sSettings.getIllustPath(),
deleteSpecialWords(title + DASH + id + DASH + "p" + index + ".png")
);
return new File(Shaft.sSettings.getIllustPath(), customFileName(illustsBean, index));
}

private static String deleteSpecialWords(String before) {
Expand Down Expand Up @@ -115,10 +110,152 @@ public static File createLogFile(String name) {
return new File(parent, deleteSpecialWords(name));
}

public static final int ILLUST_ID = 1;
public static final int ILLUST_TITLE = 2;
public static final int ILLUST_TITLE = 1;
public static final int ILLUST_ID = 2;
public static final int P_SIZE = 3;
public static final int USER_ID = 4;
public static final int USER_NAME = 5;
public static final int ILLUST_SIZE = 6;

public static String customFileName(IllustsBean illustsBean, int index) {
List<CustomFileNameCell> result = new ArrayList<>();
if (TextUtils.isEmpty(Shaft.sSettings.getFileNameJson())) {
result.add(new CustomFileNameCell("作品标题", "作品标题,可选项", 1, true));
result.add(new CustomFileNameCell("作品ID", "不选的话可能两个文件名重复,导致下载失败,必选项", 2, true));
result.add(new CustomFileNameCell("作品P数", "显示当前图片是作品的第几P,如果只有1P则隐藏,必选项", 3, true));
result.add(new CustomFileNameCell("画师ID", "画师ID,可选项", 4, false));
result.add(new CustomFileNameCell("画师昵称", "画师昵称,可选项", 5, false));
result.add(new CustomFileNameCell("作品尺寸", "显示当前图片的尺寸信息,可选项", 6, false));
} else {
result.addAll(Shaft.sGson.fromJson(Shaft.sSettings.getFileNameJson(),
new TypeToken<List<CustomFileNameCell>>() {
}.getType()));
}

String fileName = "";

for (int i = 0; i < result.size(); i++) {
CustomFileNameCell cell = result.get(i);
if (cell.isChecked()) {
switch (cell.getCode()) {
case ILLUST_ID:
if (!TextUtils.isEmpty(fileName)) {
fileName = fileName + "_" + illustsBean.getId();
} else {
fileName = String.valueOf(illustsBean.getId());
}
break;
case ILLUST_TITLE:
if (!TextUtils.isEmpty(fileName)) {
fileName = fileName + "_" + illustsBean.getTitle();
} else {
fileName = illustsBean.getTitle();
}
break;
case P_SIZE:
if (illustsBean.getPage_count() != 1) {
if (!TextUtils.isEmpty(fileName)) {
fileName = fileName + "_p" + (index + 1);
} else {
fileName = "p" + (index + 1);
}
}
break;
case USER_ID:
if (!TextUtils.isEmpty(fileName)) {
fileName = fileName + "_" + illustsBean.getUser().getId();
} else {
fileName = String.valueOf(illustsBean.getUser().getId());
}
break;
case USER_NAME:
if (!TextUtils.isEmpty(fileName)) {
fileName = fileName + "_" + illustsBean.getUser().getName();
} else {
fileName = illustsBean.getUser().getName();
}
break;
case ILLUST_SIZE:
if (!TextUtils.isEmpty(fileName)) {
fileName = fileName + "_" + illustsBean.getWidth() + "px*" + illustsBean.getHeight() + "px";
} else {
fileName = illustsBean.getWidth() + "px*" + illustsBean.getHeight() + "px";
}
break;
default:
break;
}
}
}

return deleteSpecialWords(fileName + "." + Shaft.sSettings.getFileLastType());
}

public static String customFileNameForPreview(IllustsBean illustsBean,
List<CustomFileNameCell> cells, int index) {
List<CustomFileNameCell> result;
if (cells != null && cells.size() != 0) {
result = new ArrayList<>(cells);
} else {
result = new ArrayList<>();
}

String fileName = "";

for (int i = 0; i < result.size(); i++) {
CustomFileNameCell cell = result.get(i);
if (cell.isChecked()) {
switch (cell.getCode()) {
case ILLUST_ID:
if (!TextUtils.isEmpty(fileName)) {
fileName = fileName + "_" + illustsBean.getId();
} else {
fileName = String.valueOf(illustsBean.getId());
}
break;
case ILLUST_TITLE:
if (!TextUtils.isEmpty(fileName)) {
fileName = fileName + "_" + illustsBean.getTitle();
} else {
fileName = illustsBean.getTitle();
}
break;
case P_SIZE:
if (illustsBean.getPage_count() != 1) {
if (!TextUtils.isEmpty(fileName)) {
fileName = fileName + "_p" + (index + 1);
} else {
fileName = "p" + (index + 1);
}
}
break;
case USER_ID:
if (!TextUtils.isEmpty(fileName)) {
fileName = fileName + "_" + illustsBean.getUser().getId();
} else {
fileName = String.valueOf(illustsBean.getUser().getId());
}
break;
case USER_NAME:
if (!TextUtils.isEmpty(fileName)) {
fileName = fileName + "_" + illustsBean.getUser().getName();
} else {
fileName = illustsBean.getUser().getName();
}
break;
case ILLUST_SIZE:
if (!TextUtils.isEmpty(fileName)) {
fileName = fileName + "_" + illustsBean.getWidth() + "px*" + illustsBean.getHeight() + "px";
} else {
fileName = illustsBean.getWidth() + "px*" + illustsBean.getHeight() + "px";
}
break;
default:
break;
}
}
}

return deleteSpecialWords(fileName + "." + Shaft.sSettings.getFileLastType());
}
}
107 changes: 39 additions & 68 deletions app/src/main/java/ceui/lisa/fragments/FragmentFileName.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import ceui.lisa.adapters.FileNameAdapter;
import ceui.lisa.base.SwipeFragment;
import ceui.lisa.databinding.FragmentFileNameBinding;
import ceui.lisa.download.FileCreator;
import ceui.lisa.model.CustomFileNameCell;
import ceui.lisa.models.IllustsBean;
import ceui.lisa.utils.Common;
Expand Down Expand Up @@ -54,15 +55,13 @@ protected void initView() {
baseBind.showNow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showResult();
showPreview();
}
});
baseBind.saveNow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String json = Shaft.sGson.toJson(allItems);
Shaft.sSettings.setFileNameJson(json);
Local.setSettings(Shaft.sSettings);
saveSettings();
}
});
baseBind.removeAll.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -107,8 +106,8 @@ public void onClick(DialogInterface dialog, int which) {
protected void initData() {
allItems.clear();
if (TextUtils.isEmpty(Shaft.sSettings.getFileNameJson())) {
allItems.add(new CustomFileNameCell("作品ID", "不选的话可能两个文件名重复,导致下载失败,必选项", 1, true));
allItems.add(new CustomFileNameCell("作品标题", "作品标题,可选项", 2, false));
allItems.add(new CustomFileNameCell("作品标题", "作品标题,可选项", 1, true));
allItems.add(new CustomFileNameCell("作品ID", "不选的话可能两个文件名重复,导致下载失败,必选项", 2, true));
allItems.add(new CustomFileNameCell("作品P数", "显示当前图片是作品的第几P,如果只有1P则隐藏,必选项", 3, true));
allItems.add(new CustomFileNameCell("画师ID", "画师ID,可选项", 4, false));
allItems.add(new CustomFileNameCell("画师昵称", "画师昵称,可选项", 5, false));
Expand Down Expand Up @@ -150,72 +149,44 @@ public void onMoved(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.Vi
.getAdapterPosition());
}
}).attachToRecyclerView(baseBind.recyclerView);

showPreview();
}


private void showResult() {
for (int i = 0; i < allItems.size(); i++) {
Common.showLog(allItems.get(i).getTitle());
private void showPreview() {
for (CustomFileNameCell allItem : allItems) {
if (allItem.getCode() == FileCreator.ILLUST_ID && !allItem.isChecked()) {
Common.showToast("作品ID为必选项,请选择作品ID");
return;
}

if (allItem.getCode() == FileCreator.P_SIZE && !allItem.isChecked()) {
Common.showToast("作品P数为必选项,请选择作品P数");
return;
}
}
// if (!baseBind.illustId.isChecked()) {
// Common.showToast("作品ID为必选项,请选择作品ID");
// return;
// }
//
// if (!baseBind.pSize.isChecked()) {
// Common.showToast("作品P数为必选项,请选择作品P数");
// return;
// }
//
//
// String illustID = String.valueOf(illust.getId());
//
// String illustTitle = "";
// if (baseBind.illustTitle.isChecked()) {
// illustTitle = illust.getTitle();
// }
//
//
// String userId = "";
// if (baseBind.userId.isChecked()) {
// userId = String.valueOf(illust.getUser().getId());
// }
//
//
// String userName = "";
// if (baseBind.userName.isChecked()) {
// userName = illust.getUser().getName();
// }
//
// String pSize = "p2";
//
// String illustSize = "";
// if (baseBind.illustSize.isChecked()) {
// illustSize = illust.getWidth() + "px*" + illust.getHeight() + "px";
// }
//
//
// String result = "";
// if (!TextUtils.isEmpty(illustID)) {
// result = result + illustID;
// }
// if (!TextUtils.isEmpty(illustTitle)) {
// result = result + "_" + illustTitle;
// }
// if (!TextUtils.isEmpty(userId)) {
// result = result + "_" + userId;
// }
// if (!TextUtils.isEmpty(userName)) {
// result = result + "_" + userName;
// }
// if (!TextUtils.isEmpty(pSize)) {
// result = result + "_" + pSize;
// }
// if (!TextUtils.isEmpty(illustSize)) {
// result = result + "_" + illustSize;
// }
// result = result + "." + Shaft.sSettings.getFileLastType();
// baseBind.fileName.setText(result);
String name = FileCreator.customFileNameForPreview(illust, allItems, 1);
baseBind.fileName.setText(name);
}

private void saveSettings() {
for (CustomFileNameCell allItem : allItems) {
if (allItem.getCode() == FileCreator.ILLUST_ID && !allItem.isChecked()) {
Common.showToast("作品ID为必选项,请选择作品ID");
return;
}

if (allItem.getCode() == FileCreator.P_SIZE && !allItem.isChecked()) {
Common.showToast("作品P数为必选项,请选择作品P数");
return;
}
}

String json = Shaft.sGson.toJson(allItems);
Shaft.sSettings.setFileNameJson(json);
Local.setSettings(Shaft.sSettings);
Common.showToast("保存成功!");
}

@Override
Expand Down
Loading

0 comments on commit be4cb71

Please sign in to comment.