Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
leavesCZY committed Sep 29, 2024
1 parent 66c0032 commit 134653d
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 78 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ on:
- "**/workflows-trigger.properties"

env:
PACKAGE_VERSION: 1.0.1
V_PACKAGE_VERSION: v1.0.1
PACKAGE_VERSION: 1.0.2
V_PACKAGE_VERSION: v1.0.2

jobs:

Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# compose-multiplatform-xlog-decode

一个跨平台的 xlog 日志解析工具,支持 Windows、MacOS、Linux
一个跨平台的 xlog 日志解析工具,支持 Windows、MacOS、Linux 等多个系统,包含两个功能点:

- 解压和解密 xlog 日志文件
- 生成加密 xlog 日志文件所需要的公钥和私钥

相关联的文章

- [Compose Multiplatform 写一个跨平台的 xlog 解析工具](https://juejin.cn/post/7378452954221084682)

Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ tasks {
"META-INF/*.LIST",
"META-INF/*.kotlin_module",
"META-INF/LICENSE",
"META-INF/LICENSE.txt",
"META-INF/LICENSE.txt"
)
}
}
Expand Down Expand Up @@ -83,7 +83,7 @@ compose.desktop {
}
}
packageName = mPackageName
packageVersion = "1.0.1"
packageVersion = "1.0.2"
description = "compose multiplatform xlog decode"
copyright = "© 2024 leavesCZY. All rights reserved."
vendor = "leavesCZY"
Expand Down
14 changes: 7 additions & 7 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[versions]
jetbrains-kotlin-plugin = "2.0.0"
jetbrains-compose-plugin = "1.6.11"
jetbrains-kotlin = "2.0.20"
jetbrains-compose = "1.7.0-beta02"

androidx-datastore = "1.1.1"
jetbrains-lifecycle-viewmodel-compose = "2.8.0"
luben-zstd-jni = "1.5.6-3"
jetbrains-lifecycle-viewmodel-compose = "2.8.2"
luben-zstd-jni = "1.5.6-6"
bouncycastle-bcprov-jdk18on = "1.78.1"

[plugins]
jetbrains-kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "jetbrains-kotlin-plugin" }
jetbrains-compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "jetbrains-kotlin-plugin" }
jetbrains-compose = { id = "org.jetbrains.compose", version.ref = "jetbrains-compose-plugin" }
jetbrains-kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "jetbrains-kotlin" }
jetbrains-compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "jetbrains-kotlin" }
jetbrains-compose = { id = "org.jetbrains.compose", version.ref = "jetbrains-compose" }

[libraries]
androidx-datastore-preferences-core = { module = "androidx.datastore:datastore-preferences-core", version.ref = "androidx-datastore" }
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import java.awt.Desktop
import java.io.File
import java.io.PrintWriter
import java.io.StringWriter
import java.nio.file.Path
import java.text.SimpleDateFormat
import java.util.*
import kotlin.io.path.pathString

/**
* @Author: leavesCZY
Expand Down Expand Up @@ -102,8 +104,8 @@ class LogDecodeViewModel : ViewModel(viewModelScope = CoroutineScope(SupervisorJ
}
}

private fun onInputLogFilePath(logPath: String) {
mainPageViewState = mainPageViewState.copy(logPath = logPath)
private fun onInputLogFilePath(logPath: Path) {
mainPageViewState = mainPageViewState.copy(logPath = logPath.pathString)
}

private suspend fun openFileDialog() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/github/leavesczy/xlog/decode/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private fun preferredWindowSize(): DpSize {
val screenSize = Toolkit.getDefaultToolkit().screenSize
val screenWidth = screenSize.width
val screenHeight = screenSize.height
val preferredWidth = screenWidth * 0.50f
val preferredHeight = screenHeight * 0.60f
val preferredWidth = screenWidth * 0.60f
val preferredHeight = screenHeight * 0.75f
return DpSize(preferredWidth.dp, preferredHeight.dp)
}
117 changes: 62 additions & 55 deletions src/main/kotlin/github/leavesczy/xlog/decode/ui/MainPage.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package github.leavesczy.xlog.decode.ui

import androidx.compose.foundation.*
import androidx.compose.foundation.draganddrop.dragAndDropTarget
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draganddrop.DragAndDropEvent
import androidx.compose.ui.draganddrop.DragAndDropTarget
import androidx.compose.ui.draganddrop.DragData
import androidx.compose.ui.draganddrop.dragData
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.geometry.CornerRadius
Expand All @@ -32,50 +41,39 @@ import kotlin.io.path.toPath
*/
private const val xLogFileExtension = "xlog"

private fun Path.isXLogFile(): Boolean {
val file = File(pathString)
return file.exists() && file.isFile && file.extension == xLogFileExtension
}

@Composable
fun FrameWindowScope.MainPage(
pageViewState: MainPageViewState,
snackBarHostState: SnackbarHostState
) {
val coroutineScope = rememberCoroutineScope()
fun confirmLogFilePath(paths: List<Path>) {
val filePath = paths.firstNotNullOfOrNull {
val pathString = it.pathString
val file = File(pathString)
if (file.exists() && file.isFile && file.extension == xLogFileExtension) {
pathString
} else {
null
}
}
if (filePath != null) {
pageViewState.onInputLogFilePath(filePath)
} else {
coroutineScope.launch {
snackBarHostState.showSnackbar(message = "请选择 $xLogFileExtension 文件")
}
}
}
Column(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 18.dp, vertical = 18.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(space = 10.dp)
) {
PrivateKey(
privateKey = pageViewState.privateKey,
onInputPrivateKey = pageViewState.onInputPrivateKey
)
LogFilePath(
logPath = pageViewState.logPath,
confirmLogFilePath = ::confirmLogFilePath,
confirmLogFilePath = {
pageViewState.onInputLogFilePath(it)
},
openFileDialog = {
coroutineScope.launch {
pageViewState.openFileDialog()
}
}
)
PrivateKey(
privateKey = pageViewState.privateKey,
onInputPrivateKey = pageViewState.onInputPrivateKey
)
Button(
modifier = Modifier
.fillMaxWidth(fraction = 0.3f),
Expand Down Expand Up @@ -121,13 +119,20 @@ fun FrameWindowScope.MainPage(
)
}
if (pageViewState.openDialog.isAwaiting) {
val fileExtension = xLogFileExtension
FileLoadDialog(
title = "请选择 $xLogFileExtension 文件",
title = "请选择 $fileExtension 文件",
isMultipleMode = false,
fileExtension = xLogFileExtension,
fileExtension = fileExtension,
onResult = {
if (it != null) {
confirmLogFilePath(paths = listOf(element = it))
if (it.isXLogFile()) {
pageViewState.onInputLogFilePath(it)
} else {
coroutineScope.launch {
snackBarHostState.showSnackbar(message = "请选择 $fileExtension 文件")
}
}
}
pageViewState.openDialog.onResult(result = it)
}
Expand Down Expand Up @@ -158,45 +163,47 @@ private fun PrivateKey(
@Composable
private fun LogFilePath(
logPath: String,
confirmLogFilePath: (List<Path>) -> Unit,
confirmLogFilePath: (Path) -> Unit,
openFileDialog: () -> Unit
) {
Box(
modifier = Modifier
.fillMaxWidth()
) {
var isDragging by remember {
mutableStateOf(value = false)
}
OutlinedTextField(
modifier = Modifier
.fillMaxWidth()
.dashedBorder(
color = if (isDragging) {
MaterialTheme.colorScheme.primary
} else {
Color.Transparent
},
strokeWidth = 2.dp,
radius = 16.dp
)
.onExternalDrag(
onDragStart = {
isDragging = true
},
onDragExit = {
isDragging = false
},
onDrop = { value ->
isDragging = false
val dragData = value.dragData
.dragAndDropTarget(
shouldStartDragAndDrop = { startEvent ->
val dragData = startEvent.dragData()
if (dragData is DragData.FilesList) {
val paths = dragData.readFiles().map {
URI(it).toPath()
val files = dragData.readFiles()
files.any {
URI(it).toPath().isXLogFile()
}
confirmLogFilePath(paths)
} else {
false
}
}
},
target = object : DragAndDropTarget {
override fun onDrop(event: DragAndDropEvent): Boolean {
val dragData = event.dragData() as DragData.FilesList
val files = dragData.readFiles()
val paths = files.mapNotNull {
val path = URI(it).toPath()
if (path.isXLogFile()) {
path
} else {
null
}
}
val path = paths.firstOrNull()
if (path != null) {
confirmLogFilePath(path)
}
return true
}
},
),
value = logPath,
readOnly = true,
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/github/leavesczy/xlog/decode/ui/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ data class MainPageViewState(
val logScrollState: ScrollState,
val onInputPrivateKey: (String) -> Unit,
val openFileDialog: suspend () -> Unit,
val onInputLogFilePath: (String) -> Unit,
val onInputLogFilePath: (Path) -> Unit,
val decodeLog: suspend () -> File?,
val openFile: suspend (File) -> Unit,
val switchPage: (Page) -> Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ val surfaceContainerLight = Color(0xFFEDEDF4)
val surfaceContainerHighLight = Color(0xFFE7E8EE)
val surfaceContainerHighestLight = Color(0xFFE2E2E9)

val primaryDark = Color(0xFFAAC7FF)
val onPrimaryDark = Color(0xFF0A305F)
val primaryDark = Color(0xFF3E6EBD)
val onPrimaryDark = Color(0xFFFFFFFF)
val primaryContainerDark = Color(0xFF284777)
val onPrimaryContainerDark = Color(0xFFD6E3FF)
val secondaryDark = Color(0xFFBEC6DC)
Expand Down
11 changes: 9 additions & 2 deletions src/main/kotlin/github/leavesczy/xlog/decode/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ import androidx.compose.ui.unit.sp
import github.leavesczy.xlog.decode.ui.Theme

private val Typography = Typography(
bodyLarge = TextStyle(
labelLarge = TextStyle(
fontFamily = FontFamily.SansSerif,
fontWeight = FontWeight.Normal,
fontSize = 16.sp,
lineHeight = 24.sp,
lineHeight = 22.sp,
letterSpacing = 0.5.sp
),
bodyLarge = TextStyle(
fontFamily = FontFamily.SansSerif,
fontWeight = FontWeight.Normal,
fontSize = 17.sp,
lineHeight = 22.sp,
letterSpacing = 0.5.sp
)
)
Expand Down
2 changes: 1 addition & 1 deletion workflows-trigger.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
trigger=4
trigger=5

0 comments on commit 134653d

Please sign in to comment.