Skip to content

Commit 74c0b16

Browse files
committed
v2.5.6
新增 1. 导入/导出设置 优化 1. 错误显示
1 parent cd68a50 commit 74c0b16

27 files changed

+596
-224
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
minSdkVersion 23
1313
//noinspection OldTargetApi
1414
targetSdkVersion 34
15-
versionCode 78
16-
versionName '2.5.5'
15+
versionCode 79
16+
versionName '2.5.6'
1717
resourceConfigurations += ['zh', 'zh-rCN']
1818

1919
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

app/src/main/java/top/fumiama/copymanga/LoginActivity.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class LoginActivity : AppCompatActivity() {
3535
).show()
3636
return@launch
3737
}
38-
val pwd = altpwd.text?.toString()
39-
if (pwd.isNullOrEmpty()) {
38+
val pwd = altpwd.text?.toString()?:""
39+
if (!isLogout && pwd.isEmpty()) {
4040
Toast.makeText(this@LoginActivity, R.string.login_null_pwd, Toast.LENGTH_SHORT)
4141
.show()
4242
return@launch
@@ -66,7 +66,7 @@ class LoginActivity : AppCompatActivity() {
6666
Toast.makeText(
6767
this@LoginActivity,
6868
"${e::class.simpleName} ${e.message}",
69-
Toast.LENGTH_SHORT
69+
Toast.LENGTH_LONG
7070
).show()
7171
}
7272
}

app/src/main/java/top/fumiama/copymanga/MainActivity.kt

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ import top.fumiama.copymanga.api.update.Update
6363
import top.fumiama.copymanga.api.user.Member
6464
import top.fumiama.copymanga.lib.Comancry
6565
import top.fumiama.copymanga.lib.Comandy
66+
import top.fumiama.copymanga.storage.DataLoader
67+
import top.fumiama.copymanga.strings.Base16384
6668
import top.fumiama.dmzj.copymanga.BuildConfig
6769
import top.fumiama.dmzj.copymanga.R
6870
import java.io.File
@@ -158,7 +160,7 @@ class MainActivity : AppCompatActivity() {
158160
Log.d("MyMain", "start menu waiting")
159161
lifecycleScope.launch {
160162
withContext(Dispatchers.IO) {
161-
Config.myHostApiUrl.init()
163+
Config.api.init()
162164
}
163165
withContext(Dispatchers.IO) {
164166
delay(1000)
@@ -206,6 +208,7 @@ class MainActivity : AppCompatActivity() {
206208
return true
207209
}
208210

211+
@SuppressLint("CheckResult")
209212
@OptIn(ExperimentalStdlibApi::class)
210213
override fun onOptionsItemSelected(item: MenuItem): Boolean {
211214
return when (item.itemId) {
@@ -214,10 +217,39 @@ class MainActivity : AppCompatActivity() {
214217
true
215218
}
216219
R.id.action_download -> {
217-
if (NewDownloadFragment.wn != null) {
218-
//TODO: fill it
219-
} else {
220-
bookHandler.get()?.sendEmptyMessage(BookHandler.NAVIGATE_TO_DOWNLOAD)
220+
when (navController?.currentDestination?.id) {
221+
R.id.nav_new_download -> {
222+
//TODO: fill it
223+
}
224+
R.id.nav_settings -> {
225+
toolsBox.buildInfo("备份管理", "可选择导出或导入base16384格式配置项",
226+
"导出", "导入", "取消", { // ok
227+
MaterialDialog(this).show {
228+
input(prefill = Base16384.encode(DataLoader().toByteArray()))
229+
positiveButton(android.R.string.ok)
230+
title(null, "请复制配置文本并保存")
231+
}
232+
}, { // neutral
233+
MaterialDialog(this).show {
234+
input { _, c ->
235+
try {
236+
DataLoader(Base16384.decode(c.toString())).settings.export()
237+
navController?.apply {
238+
currentDestination?.id?.let {
239+
popBackStack()
240+
navigate(it)
241+
}
242+
}
243+
} catch (e: Exception) {
244+
Toast.makeText(this@MainActivity, e.message?:e::class.simpleName, Toast.LENGTH_SHORT).show()
245+
}
246+
}
247+
positiveButton(android.R.string.ok)
248+
title(null, "请粘贴配置文本")
249+
}
250+
})
251+
}
252+
else -> bookHandler.get()?.sendEmptyMessage(BookHandler.NAVIGATE_TO_DOWNLOAD)
221253
}
222254
true
223255
}
@@ -338,6 +370,13 @@ class MainActivity : AppCompatActivity() {
338370
menuMain?.findItem(R.id.action_sort)?.isVisible = false
339371
menuMain?.findItem(R.id.action_del)?.isVisible = true
340372
}
373+
R.id.nav_settings -> {
374+
Log.d("MyMain", "enter settings")
375+
menuMain?.findItem(R.id.action_info)?.isVisible = false
376+
menuMain?.findItem(R.id.action_download)?.isVisible = true
377+
menuMain?.findItem(R.id.action_sort)?.isVisible = false
378+
menuMain?.findItem(R.id.action_del)?.isVisible = false
379+
}
341380
else -> {
342381
Log.d("MyMain", "enter others")
343382
menuMain?.findItem(R.id.action_info)?.isVisible = false
@@ -433,7 +472,7 @@ class MainActivity : AppCompatActivity() {
433472
dl.setMessage("${getString(R.string.app_description)}\n" +
434473
"\n$comandy\n" +
435474
"$comancry\n\n"+ File("/proc/self/cmdline").readText() + "\n" +
436-
"当前API: ${Config.myHostApiUrl.getApis().joinToString(", ")}")
475+
"当前API: ${Config.api.getApis().joinToString(", ")}")
437476
dl.setTitle("${getString(R.string.action_info)} ${BuildConfig.VERSION_NAME}")
438477
dl.setIcon(R.mipmap.ic_launcher)
439478
dl.setPositiveButton(android.R.string.ok) { _, _ -> }

app/src/main/java/top/fumiama/copymanga/api/Config.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import top.fumiama.dmzj.copymanga.R
1414
import java.io.File
1515

1616
object Config {
17+
val api = Api()
18+
1719
var imageProxy: Proxy? = null
1820
get() {
1921
if (!net_use_img_proxy.value) return null
@@ -55,30 +57,30 @@ object Config {
5557
}
5658

5759
val proxyUrl = MainActivity.mainWeakReference?.get()?.getString(R.string.proxyUrl)!!
58-
val reverseProxyUrl = PreferenceString(R.string.reverseProxyKeyID)
59-
val networkApiUrl = PreferenceString("settings_cat_net_et_api_url", R.string.hostUrl)
60-
val myHostApiUrl = Api()
61-
val navTextInfo = UserPreferenceString("navTextInfo", R.string.navTextInfo)
62-
val proxy_key = PreferenceString(R.string.imgProxyCodeKeyID)
63-
val app_ver = PreferenceString("settings_cat_general_et_app_version", R.string.app_ver)
64-
val platform = PreferenceString("settings_cat_general_et_platform", R.string.platform)
65-
val token = UserPreferenceString("token", "", null)
6660
val pc_ua get() = MainActivity.mainWeakReference?.get()?.getString(R.string.pc_ua)?.format(app_ver.value)?:""
6761
val referer get() = MainActivity.mainWeakReference?.get()?.getString(R.string.referer)?.format(app_ver.value)?:""
62+
63+
val navTextInfo = UserPreferenceString("navTextInfo", R.string.navTextInfo)
64+
val token = UserPreferenceString("token", "", null)
6865
val comandy_version = UserPreferenceInt("comandy_version", 0)
6966
val comancry_version = UserPreferenceInt("comancry_version", 0)
7067
val user_id = UserPreferenceString("user_id")
7168
val username = UserPreferenceString("username")
7269
val nickname = UserPreferenceString("nickname")
7370
val avatar = UserPreferenceString("avatar")
7471

72+
val app_ver = PreferenceString("settings_cat_general_et_app_version", R.string.app_ver)
73+
val platform = PreferenceString("settings_cat_general_et_platform", R.string.platform)
7574
val general_enable_transparent_system_bar = PreferenceBoolean("settings_cat_general_sw_enable_transparent_systembar", false)
7675
val general_disable_kanban_animation = PreferenceBoolean("settings_cat_general_sw_disable_kanban_animation", false)
7776
val general_card_per_row = PreferenceInt("settings_cat_general_sb_card_per_row", 0)
7877

7978
val manga_dl_max_batch = PreferenceInt("settings_cat_md_sb_max_batch", 16)
8079
val manga_dl_show_0m_manga = PreferenceBoolean("settings_cat_md_sw_show_0m_manga", false)
8180

81+
val reverseProxyUrl = PreferenceString(R.string.reverseProxyKeyID)
82+
val networkApiUrl = PreferenceString("settings_cat_net_et_api_url", R.string.hostUrl)
83+
val proxy_key = PreferenceString(R.string.imgProxyCodeKeyID)
8284
val net_use_gzip = PreferenceBoolean("settings_cat_net_sw_use_gzip", false)
8385
val net_use_json = PreferenceBoolean("settings_cat_net_sw_use_json", false)
8486
val net_platform = PreferenceBoolean("settings_cat_net_sw_platform", false)
@@ -88,7 +90,7 @@ object Config {
8890
val net_no_webp = PreferenceBoolean("settings_cat_net_no_webp", false)
8991
val net_use_comandy = PreferenceBoolean("settings_cat_net_sw_use_comandy", false)
9092
val net_use_foreign = PreferenceBoolean("settings_cat_net_sw_use_foreign", false)
91-
private val net_use_img_proxy = PreferenceBoolean("settings_cat_net_sw_use_img_proxy", false)
93+
val net_use_img_proxy = PreferenceBoolean("settings_cat_net_sw_use_img_proxy", false)
9294
val net_use_api_proxy = PreferenceBoolean("settings_cat_net_sw_use_api_proxy", false)
9395
val net_img_resolution = PreferenceString(R.string.imgResolutionKeyID)
9496
val net_umstring = PreferenceString("settings_cat_net_et_umstring")

app/src/main/java/top/fumiama/copymanga/api/manga/Book.kt

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package top.fumiama.copymanga.api.manga
22

33
import android.util.Log
44
import android.widget.Toast
5+
import com.google.android.material.snackbar.Snackbar
56
import com.google.gson.Gson
7+
import kotlinx.android.synthetic.main.app_bar_main.*
68
import kotlinx.android.synthetic.main.card_book.*
79
import kotlinx.android.synthetic.main.line_booktandb.*
810
import kotlinx.coroutines.Dispatchers
@@ -56,38 +58,29 @@ class Book(val path: String, private val getString: (Int) -> String, private val
5658
* 更新云端最新图书信息并缓存到本地
5759
*/
5860
suspend fun updateInfo() = withContext(Dispatchers.IO) {
59-
try {
60-
var isDownload = false
61-
val data: String = if (loadCache) {
62-
name?.let { loadInfo(it) } ?: run {
63-
isDownload = true
64-
Config.myHostApiUrl.get(mBookApiUrl)
65-
}
66-
} else {
61+
var isDownload = false
62+
val data: String = if (loadCache) {
63+
name?.let { loadInfo(it) } ?: run {
6764
isDownload = true
68-
Config.myHostApiUrl.get(mBookApiUrl)
69-
}
70-
mBook = Gson().fromJson(data, BookInfoStructure::class.java)
71-
if (isDownload) saveInfo(data)
72-
mGroupPathWords = arrayOf()
73-
mKeys = arrayOf()
74-
mCounts = intArrayOf()
75-
mBook?.results?.groups?.values?.forEach {
76-
mKeys += it.name
77-
mGroupPathWords += it.path_word
78-
if (it.count == 0) {
79-
it.count = 1
80-
}
81-
mCounts += it.count
82-
Log.d("MyB", "Add caption: ${it.name} @ ${it.path_word} of ${it.count}")
65+
Config.api.get(mBookApiUrl)
8366
}
84-
} catch (e: Exception) {
85-
e.printStackTrace()
86-
MainActivity.mainWeakReference?.get()?.apply {
87-
withContext(Dispatchers.Main) {
88-
runOnUiThread { Toast.makeText(this@apply, "${e::class.simpleName} ${e.message}", Toast.LENGTH_SHORT).show() }
89-
}
67+
} else {
68+
isDownload = true
69+
Config.api.get(mBookApiUrl)
70+
}
71+
mBook = Gson().fromJson(data, BookInfoStructure::class.java)
72+
if (isDownload) saveInfo(data)
73+
mGroupPathWords = arrayOf()
74+
mKeys = arrayOf()
75+
mCounts = intArrayOf()
76+
mBook?.results?.groups?.values?.forEach {
77+
mKeys += it.name
78+
mGroupPathWords += it.path_word
79+
if (it.count == 0) {
80+
it.count = 1
9081
}
82+
mCounts += it.count
83+
Log.d("MyB", "Add caption: ${it.name} @ ${it.path_word} of ${it.count}")
9184
}
9285
}
9386

app/src/main/java/top/fumiama/copymanga/api/manga/Shelf.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Shelf(private val getString: (Int) -> String) {
2424
append("")
2525
append(Config.token.value)
2626
}
27-
val re = Config.myHostApiUrl.request(
27+
val re = Config.api.request(
2828
addApiUrl, body.encodeToByteArray(), "POST",
2929
"application/x-www-form-urlencoded;charset=utf-8")
3030
Gson().fromJson(re, ReturnBase::class.java).message
@@ -43,15 +43,15 @@ class Shelf(private val getString: (Int) -> String) {
4343
append("authorization=Token+")
4444
append(Config.token.value)
4545
}
46-
val re = Config.myHostApiUrl.request(
46+
val re = Config.api.request(
4747
delApiUrl, body.encodeToByteArray(),
4848
"DELETE", "application/x-www-form-urlencoded;charset=utf-8")
4949
Gson().fromJson(re, ReturnBase::class.java).message
5050
}
5151

5252
suspend fun query(pathWord: String): BookQueryStructure? = withContext(Dispatchers.IO) {
5353
val queryUrl = queryApiUrlTemplate.format(pathWord, Config.platform.value)
54-
Config.myHostApiUrl.get(queryUrl).let {
54+
Config.api.get(queryUrl).let {
5555
Gson().fromJson(it, BookQueryStructure::class.java)
5656
}
5757
}

app/src/main/java/top/fumiama/copymanga/api/network/Api.kt

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,14 @@ class Api {
5050
e.printStackTrace()
5151
mHostApiUrls = mutableListOf(networkApiUrl.value)
5252
withContext(Dispatchers.Main) {
53-
runOnUiThread {
54-
Toast.makeText(this@apply, "${e::class.simpleName} ${e.message}", Toast.LENGTH_SHORT).show()
55-
}
53+
Toast.makeText(this@apply, "${e::class.simpleName} ${e.message}", Toast.LENGTH_LONG).show()
5654
}
5755
}
5856
if (mHostApiUrls.isEmpty()) {
5957
mHostApiUrls = mutableListOf(networkApiUrl.value)
6058
Log.d("MyApi", "myHostApiUrl set default ${mHostApiUrls[0]}")
6159
withContext(Dispatchers.Main) {
62-
runOnUiThread {
63-
Toast.makeText(this@apply, "无法获取API列表", Toast.LENGTH_SHORT).show()
64-
}
60+
Toast.makeText(this@apply, "无法获取API列表", Toast.LENGTH_SHORT).show()
6561
}
6662
}
6763
}
@@ -77,22 +73,12 @@ class Api {
7773
var r: ReturnBase? = null
7874
apis.forEachIndexed { i, api ->
7975
val u = "https://$api$path"
76+
var ret = ""
8077
try {
81-
val ret = (apiProxy?.comancry(u) {
78+
ret = (apiProxy?.comancry(u) {
8279
DownloadTools.getApiContent(it)
8380
}?: DownloadTools.getApiContent(u)).decodeToString()
8481
r = Gson().fromJson(ret, ReturnBase::class.java)
85-
if (r!!.code != 200) {
86-
withContext(Dispatchers.Main) {
87-
MainActivity.mainWeakReference?.get()?.apply {
88-
runOnUiThread {
89-
Toast.makeText(this, "错误码${r?.code?:-1}, 信息: ${r?.message?:""}", Toast.LENGTH_SHORT).show()
90-
}
91-
}
92-
}
93-
} else {
94-
return ret
95-
}
9682
} catch (e: Exception) {
9783
mu.withLock {
9884
if (mHostApiUrls.size <= 1) return@withLock
@@ -102,34 +88,32 @@ class Api {
10288
throw e
10389
}
10490
}
91+
r?.let {
92+
if (it.code != 200) {
93+
throw IllegalArgumentException("错误码${it.code}, 信息: ${it.message?:""}")
94+
} else {
95+
return ret
96+
}
97+
}
10598
}
106-
throw IllegalStateException("错误码${r?.code?:-1}, 信息: ${r?.message?:""}")
99+
throw NoSuchElementException("无可用API")
107100
}
101+
108102
// request throw error on non-json or non-200 or empty apis, path: /api/v3/xxx, return json string
109-
suspend fun request(path: String, body: ByteArray, method: String, contentType: String, forceApi: String? = null): String {
103+
suspend fun request(path: String, body: ByteArray, method: String, contentType: String, forceApi: String? = null): String {
110104
val apis = if (forceApi == null) mu.withLock { mHostApiUrls } else mutableListOf(forceApi)
111105
if (apis.isEmpty()) {
112106
throw NoSuchElementException("API列表为空")
113107
}
114108
var r: ReturnBase? = null
115109
apis.forEachIndexed { i, api ->
116110
val u = "https://$api$path"
111+
var ret = ""
117112
try {
118-
val ret = (apiProxy?.comancry(u) {
113+
ret = (apiProxy?.comancry(u) {
119114
DownloadTools.requestApiWithBody(u, method, body, contentType)
120115
}?: DownloadTools.requestApiWithBody(u, method, body, contentType)).decodeToString()
121116
r = Gson().fromJson(ret, ReturnBase::class.java)
122-
if (r!!.code != 200) {
123-
withContext(Dispatchers.Main) {
124-
MainActivity.mainWeakReference?.get()?.apply {
125-
runOnUiThread {
126-
Toast.makeText(this, "错误码${r?.code?:-1}, 信息: ${r?.message?:""}", Toast.LENGTH_SHORT).show()
127-
}
128-
}
129-
}
130-
} else {
131-
return ret
132-
}
133117
} catch (e: Exception) {
134118
e.printStackTrace()
135119
mu.withLock {
@@ -140,7 +124,14 @@ class Api {
140124
throw e
141125
}
142126
}
127+
r?.let {
128+
if (it.code != 200) {
129+
throw IllegalArgumentException("错误码${it.code}, 信息: ${it.message?:""}")
130+
} else {
131+
return ret
132+
}
133+
}
143134
}
144-
throw IllegalStateException("错误码${r?.code?:-1}, 信息: ${r?.message?:""}")
135+
throw NoSuchElementException("无可用API")
145136
}
146137
}

0 commit comments

Comments
 (0)