Skip to content

Commit

Permalink
feat: 修改设置信息出错则展示详细出错信息
Browse files Browse the repository at this point in the history
  • Loading branch information
jixiaoyong committed Jun 28, 2024
1 parent 02a12fa commit 30456e9
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import ApkSigner
import Logger
import io.github.jixiaoyong.base.BaseViewModel
import io.github.jixiaoyong.data.SettingPreferencesRepository
import io.github.jixiaoyong.utils.showToast
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import org.jetbrains.skiko.OS.*
Expand Down Expand Up @@ -105,12 +104,12 @@ class SettingInfoViewModel(private val repository: SettingPreferencesRepository)
}
}

fun setupBuildToolsConfig(buildToolDir: String) {
fun setupBuildToolsConfig(buildToolDir: String): String? {
val result = ApkSigner.init(buildToolDir)
saveApkSigner(ApkSigner.apkSignerPath)
saveZipAlign(ApkSigner.zipAlignPath)
saveAapt(ApkSigner.aaptPath)
showToast(result ?: "修改成功")
return result
}

fun saveApkSigner(apkSigner: String?) {
Expand Down
75 changes: 69 additions & 6 deletions src/main/kotlin/io/github/jixiaoyong/pages/settings/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ClipboardManager
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
Expand Down Expand Up @@ -60,6 +63,7 @@ fun PageSettingInfo() {
val i18nString = strings
val lyricist = LocalLyricist.current
val repository = LocalDatastore.current
val clipboard = LocalClipboardManager.current

val viewModel = viewModel { SettingInfoViewModel(repository) }
val uiState by viewModel.uiState.collectAsState()
Expand Down Expand Up @@ -169,12 +173,30 @@ fun PageSettingInfo() {
if (chooseFileName.isNullOrBlank()) {
showToast(i18nString.chooseBuildTools, ToastConfig.DURATION.Long)
} else {
viewModel.setupBuildToolsConfig(chooseFileName)
val result = viewModel.setupBuildToolsConfig(chooseFileName)
showErrorMsg(
scaffoldState,
result,
i18nString.changeSuccess,
i18nString.copyErrorMsg,
clipboard
)
}
}
},
component = JPanel(),
onFileDrop = { scope.launch { viewModel.setupBuildToolsConfig(it.first()) } }) {
onFileDrop = {
scope.launch {
val result = viewModel.setupBuildToolsConfig(it.first())
showErrorMsg(
scaffoldState,
result,
i18nString.changeSuccess,
i18nString.copyErrorMsg,
clipboard
)
}
}) {
Text(
text = i18nString.chooseBuildToolsTips,
color = MaterialTheme.colors.onSurface,
Expand All @@ -195,7 +217,13 @@ fun PageSettingInfo() {
} else {
val result = ApkSigner.setupApkSigner(chooseFileName)
viewModel.saveApkSigner(ApkSigner.apkSignerPath)
showToast(result ?: i18nString.changeSuccess)
showErrorMsg(
scaffoldState,
result,
i18nString.changeSuccess,
i18nString.copyErrorMsg,
clipboard
)
}
}

Expand All @@ -212,7 +240,13 @@ fun PageSettingInfo() {
} else {
val result = ApkSigner.setupZipAlign(chooseFileName)
viewModel.saveZipAlign(ApkSigner.zipAlignPath)
showToast(result ?: i18nString.changeSuccess)
showErrorMsg(
scaffoldState,
result,
i18nString.changeSuccess,
i18nString.copyErrorMsg,
clipboard
)
}
}
})
Expand Down Expand Up @@ -259,7 +293,13 @@ fun PageSettingInfo() {
} else {
val result = ApkSigner.setupAapt(chooseFileName)
viewModel.saveAapt(ApkSigner.aaptPath)
showToast(result ?: i18nString.changeSuccess)
showErrorMsg(
scaffoldState,
result,
i18nString.changeSuccess,
i18nString.copyErrorMsg,
clipboard
)
}
}
})
Expand Down Expand Up @@ -292,7 +332,7 @@ fun PageSettingInfo() {
val result = viewModel.openLogDirectory()
if (!result) {
showToast(i18nString.openLogDirectoryFailed)
}else{
} else {
showToast(i18nString.openLogDirectorySucceed)
}
}) {}
Expand Down Expand Up @@ -339,3 +379,26 @@ fun PageSettingInfo() {
}

}

private suspend inline fun showErrorMsg(
scaffoldState: ScaffoldState,
msg: String?,
successMsg: String,
actionLabel: String,
clipboard: ClipboardManager
) {
if (msg.isNullOrBlank()) {
showToast(successMsg, ToastConfig.DURATION.Long)
return
}

val result = scaffoldState.snackbarHostState.showSnackbar(
msg,
actionLabel = actionLabel,
duration = SnackbarDuration.Long
)

if (SnackbarResult.ActionPerformed == result) {
clipboard.setText(AnnotatedString(msg))
}
}
12 changes: 6 additions & 6 deletions src/main/kotlin/io/github/jixiaoyong/utils/ApkSigner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ object ApkSigner {
val buildTools =
if (androidBuildToolsDir.isNullOrBlank()) return "" else File(androidBuildToolsDir)
if (!buildTools.exists() || !buildTools.isDirectory) {
return "指定的Android SDK中build-tools目录无效或不存在,请重新选择。\n该目录一般为${ANDROID_BUILD_TOOLS_DIR_EXAMPLE}"
return "指定的Android SDK中build-tools目录无效或不存在($androidBuildToolsDir),请重新选择。\n该目录一般为${ANDROID_BUILD_TOOLS_DIR_EXAMPLE}"
}

val apkSignerPath = "$androidBuildToolsDir${File.separator}apksigner"
Expand Down Expand Up @@ -89,15 +89,15 @@ object ApkSigner {
// 校验文件是否为AOSP提供的apk signer,而非本APP避免误操作导致无限循环启动
val apkSignerFile = File(apkSignerPath)
if (!apkSignerFile.exists()) {
return "apkSigner命令不存在,请重新选择。"
return "apkSigner命令(${apkSignerPath})不存在,请重新选择。"
} else if (!apkSignerFile.readText().contains(AOSP_NAME)) {
return "apkSigner命令不是${AOSP_NAME}官方提供的,请重新选择。"
return "apkSigner命令(${apkSignerPath})不是${AOSP_NAME}官方提供的,请重新选择。"
}

val result = RunCommandUtil.runCommand("$apkSignerPath version", "apk signer")

return if (result != null) {
"apkSigner命令检查失败,请重试(${result.message}"
"apkSigner命令(${apkSignerPath})检查失败,请重试(${result.message}"
} else {
apkSignerCmdPath = apkSignerPath
null
Expand All @@ -114,7 +114,7 @@ object ApkSigner {
}

return if (null != result) {
"zipAlign命令检查失败,请重试(${result.message}"
"zipAlign命令(${zipAlignPath})检查失败,请重试(${result.message}"
} else {
zipAlignCmdPath = zipAlignPath
null
Expand All @@ -131,7 +131,7 @@ object ApkSigner {
}

return if (null != result) {
"aapt命令检查失败,请重试(${result.message}"
"aapt命令(${aaptPath})检查失败,请重试(${result.message}"
} else {
aaptCmdPath = aaptPath
null
Expand Down

0 comments on commit 30456e9

Please sign in to comment.