Skip to content

Commit 3b2a7f1

Browse files
committed
修复一段时间后无法连接到数据库的问题、增加‘备注’字段
1 parent 51f3660 commit 3b2a7f1

File tree

5 files changed

+137
-80
lines changed

5 files changed

+137
-80
lines changed

Diff for: src/main/kotlin/Main.kt

+58-56
Original file line numberDiff line numberDiff line change
@@ -58,77 +58,79 @@ val FontLXGWNeoXiHeiScreenFamily = FontFamily(FontLXGWNeoXiHeiScreen())
5858

5959
private val logger = LoggerFactory.getLogger("MAIN")
6060

61-
fun main() = application {
61+
fun main() {
6262
Thread.setDefaultUncaughtExceptionHandler { t, e ->
6363
logger.error("UncaughtExceptionHandler on Thread[{}]", t, e)
6464
}
6565

66-
val scope = rememberCoroutineScope { Dispatchers.Default }
67-
val materialThemeState = rememberMaterialThemeState()
66+
val databaseOp = connectDatabaseOperator(dataDir = storeAppPath(), schemaName = "bonus")
6867

69-
val winSize = kotlin.runCatching {
70-
with(Toolkit.getDefaultToolkit().screenSize) {
71-
DpSize((width * 0.8f).dp, (height * 0.8f).dp)
72-
}
73-
}.getOrElse {
74-
DpSize(1024.dp, 768.dp)
75-
}
68+
application {
69+
val scope = rememberCoroutineScope { Dispatchers.Default }
70+
val materialThemeState = rememberMaterialThemeState()
7671

77-
val winState = rememberWindowState(size = winSize)
78-
val databaseOp = remember { connectDatabaseOperator(dataDir = storeAppPath(), schemaName = "bonus") }
72+
val winSize = kotlin.runCatching {
73+
with(Toolkit.getDefaultToolkit().screenSize) {
74+
DpSize((width * 0.8f).dp, (height * 0.8f).dp)
75+
}
76+
}.getOrElse {
77+
DpSize(1024.dp, 768.dp)
78+
}
7979

80-
val trayState = rememberTrayState()
80+
val winState = rememberWindowState(size = winSize)
81+
val trayState = rememberTrayState()
8182

82-
if (winState.isMinimized) {
83-
Tray(
84-
icon = Logo(),
85-
tooltip = "别奖励了😡",
86-
state = trayState,
87-
onAction = {
88-
winState.isMinimized = false
89-
winState.position = WindowPosition.PlatformDefault
90-
},
91-
menu = {
92-
Item("Open") {
83+
if (winState.isMinimized) {
84+
Tray(
85+
icon = Logo(),
86+
tooltip = "别奖励了😡",
87+
state = trayState,
88+
onAction = {
9389
winState.isMinimized = false
9490
winState.position = WindowPosition.PlatformDefault
91+
},
92+
menu = {
93+
Item("Open") {
94+
winState.isMinimized = false
95+
winState.position = WindowPosition.PlatformDefault
96+
}
97+
Separator()
98+
Item("Exit") {
99+
exitApplication()
100+
}
95101
}
96-
Separator()
97-
Item("Exit") {
98-
exitApplication()
99-
}
100-
}
101-
)
102-
}
103-
104-
Window(
105-
icon = Logo(),
106-
state = winState,
107-
title = "别奖励了!",
108-
visible = !winState.isMinimized,
109-
enabled = !winState.isMinimized,
110-
onCloseRequest = {
111-
exitApplication()
102+
)
112103
}
113-
) {
114-
MaterialTheme(
115-
colors = materialThemeState.colors,
116-
typography = materialThemeState.typography,
117-
shapes = materialThemeState.shapes
104+
105+
Window(
106+
icon = Logo(),
107+
state = winState,
108+
title = "别奖励了!",
109+
visible = !winState.isMinimized,
110+
enabled = !winState.isMinimized,
111+
onCloseRequest = {
112+
exitApplication()
113+
}
118114
) {
119-
App(
120-
remember {
121-
AppState(
122-
winState,
123-
materialThemeState,
124-
scope,
125-
databaseOp
126-
)
127-
}
128-
)
115+
MaterialTheme(
116+
colors = materialThemeState.colors,
117+
typography = materialThemeState.typography,
118+
shapes = materialThemeState.shapes
119+
) {
120+
App(
121+
remember {
122+
AppState(
123+
winState,
124+
materialThemeState,
125+
scope,
126+
databaseOp
127+
)
128+
}
129+
)
130+
}
129131
}
130-
}
131132

133+
}
132134
}
133135

134136
@Suppress("MemberVisibilityCanBePrivate", "unused")

Diff for: src/main/kotlin/database/DatabaseConnector.kt

+21-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import kotlin.coroutines.CoroutineContext
1212
import kotlin.io.path.Path
1313
import kotlin.io.path.div
1414
import kotlin.io.path.pathString
15-
import kotlin.math.max
1615

1716
class DatabaseOperator(
1817
val schema: Schema,
@@ -52,11 +51,30 @@ private const val DATA_FILE_NAME = "bonus.d"
5251
fun connectDatabaseOperator(dataDir: Path = DEFAULT_DATA_DIR, schemaName: String): DatabaseOperator {
5352
val schema = Schema(schemaName)
5453

54+
val jdbcUrl = "jdbc:h2:file:${(dataDir / DATA_FILE_NAME).pathString};" +
55+
"DB_CLOSE_DELAY=-1;" +
56+
"DB_CLOSE_ON_EXIT=FALSE;" +
57+
"TRACE_LEVEL_FILE=3;" +
58+
"AUTO_RECONNECT=TRUE;"
59+
60+
// val database = Database.connect(
61+
// url = jdbcUrl,
62+
// driver = "org.h2.Driver",
63+
// databaseConfig = DatabaseConfig {
64+
// // set other parameters here
65+
// defaultFetchSize = 100
66+
// keepLoadedReferencesOutOfTransaction = true
67+
// defaultMaxRepetitionDelay = 6000
68+
// }
69+
// )
70+
5571
val config = hikariConfig {
56-
jdbcUrl = "jdbc:h2:file:${(dataDir / DATA_FILE_NAME).pathString}"
72+
this.jdbcUrl = jdbcUrl
5773
driverClassName = "org.h2.Driver"
5874
poolName = "BonusDBPool"
59-
minimumIdle = max(1, Runtime.getRuntime().availableProcessors() / 2)
75+
minimumIdle = 1
76+
maximumPoolSize = 1
77+
6078
}
6179

6280
val dataSource = HikariDataSource(config)

Diff for: src/main/kotlin/database/entity/BonusRecord.kt

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ object BonusRecords : BaseIntIdTable() {
3333
* 给本次打分。0~10分。
3434
*/
3535
val score = uinteger("score").default(0u)
36+
37+
/**
38+
* 备注
39+
*/
40+
val remark = varchar("remark", 500).clientDefault { "" }
3641
}
3742

3843
class BonusRecord(id: EntityID<Int>) : BaseIntEntity(id, BonusRecords) {
@@ -43,6 +48,7 @@ class BonusRecord(id: EntityID<Int>) : BaseIntEntity(id, BonusRecords) {
4348
var endTime by BonusRecords.endTime
4449
var duration by BonusRecords.duration
4550
var score by BonusRecords.score
51+
var remark by BonusRecords.remark
4652

4753
var weapons by Weapon via BonusRecordWeapons
4854
}
@@ -72,6 +78,7 @@ data class BonusRecordView(
7278
val endTime: Instant,
7379
val duration: Duration,
7480
val score: UInt,
81+
val remark: String,
7582
val weapons: List<WeaponView>
7683
) : BaseIntEntityView
7784

@@ -80,12 +87,14 @@ fun BonusRecord.toView(
8087
endTime: Instant = this.endTime,
8188
duration: Duration = this.duration,
8289
score: UInt = this.score,
90+
remark: String = this.remark,
8391
weapons: List<WeaponView> = this.weapons.map { it.toView() }
8492
): BonusRecordView = BonusRecordView(
8593
entityID = id,
8694
startTime = startTime,
8795
endTime = endTime,
8896
duration = duration,
8997
score = score,
98+
remark = remark,
9099
weapons = weapons,
91100
)

Diff for: src/main/kotlin/view/account/home/AccountHomeView.kt

+20-14
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,18 @@ private fun AccountHome(state: PageViewState) {
259259
ScoreSelector(score)
260260
}
261261

262-
// AnimatedVisibility(duration != null) {
263-
// Text(
264-
// text = duration?.toString() ?: "",
265-
// fontFamily = FontLXGWNeoXiHeiScreenFamily,
266-
// modifier = Modifier
267-
// .align(Alignment.CenterHorizontally)
268-
// )
269-
// }
262+
var remarkValue by remember { mutableStateOf("") }
263+
264+
AnimatedVisibility(duration != null) {
265+
// 备注
266+
OutlinedTextField(
267+
value = remarkValue,
268+
onValueChange = { remarkValue = if (it.length <= 500) it else it.substring(0, 500) },
269+
label = { Text("备注") },
270+
placeholder = { Text("备注") },
271+
supportingText = { Text("500字内") },
272+
)
273+
}
270274

271275
var recording by remember { mutableStateOf(false) }
272276

@@ -291,9 +295,11 @@ private fun AccountHome(state: PageViewState) {
291295
this.endTime =
292296
ZonedDateTime.of(selectedEndDateTime, ZoneId.systemDefault()).toInstant()
293297
this.score = score.value.toUInt()
298+
this.remark = remarkValue
294299
weapon?.also { w ->
295-
this.weapons = SizedCollection(listOf(Weapon.findById(w.id)!!))
296-
// TODO null?
300+
Weapon.findById(w.id)?.also {
301+
this.weapons = SizedCollection(listOf(it))
302+
}
297303
}
298304
}
299305
}
@@ -361,12 +367,12 @@ private inline fun WeaponSelector(
361367
) {
362368
val weapons = remember { mutableStateListOf<WeaponView>() }
363369
LaunchedEffect(state) {
364-
state.accountState.inAccountTransaction { account ->
365-
val all = Weapon.find { Weapons.account eq account.id }
370+
val all = state.accountState.inAccountTransaction { account ->
371+
Weapon.find { Weapons.account eq account.id }
366372
.notForUpdate().map { it.toView() }
367-
368-
weapons.addAll(all)
369373
}
374+
375+
weapons.addAll(all)
370376
}
371377

372378
var expanded by remember { mutableStateOf(false) }

Diff for: src/main/kotlin/view/account/record/AccountBonusRecordView.kt

+29-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package view.account.record
22

33
import FontBTTFamily
4+
import FontLXGWNeoXiHeiScreenFamily
45
import androidx.compose.animation.AnimatedVisibility
56
import androidx.compose.foundation.clickable
67
import androidx.compose.foundation.hoverable
78
import androidx.compose.foundation.interaction.MutableInteractionSource
89
import androidx.compose.foundation.interaction.collectIsHoveredAsState
910
import androidx.compose.foundation.layout.Arrangement
11+
import androidx.compose.foundation.layout.Column
1012
import androidx.compose.foundation.layout.Row
1113
import androidx.compose.foundation.lazy.LazyColumn
1214
import androidx.compose.foundation.lazy.items
@@ -21,6 +23,7 @@ import androidx.compose.ui.Alignment
2123
import androidx.compose.ui.Modifier
2224
import androidx.compose.ui.draw.clip
2325
import androidx.compose.ui.graphics.Color
26+
import androidx.compose.ui.text.font.FontWeight
2427
import androidx.compose.ui.unit.dp
2528
import database.entity.BonusRecord
2629
import database.entity.BonusRecordView
@@ -170,10 +173,10 @@ private fun ListItemRecord(
170173
modifier = Modifier.hoverable(interactionSource),
171174
headlineContent = {
172175
if (record.weapons.isEmpty()) {
173-
Text("手艺活")
176+
Text("手艺活", fontFamily = FontLXGWNeoXiHeiScreenFamily)
174177
} else {
175178
val weaponsString = record.weapons.joinToString("", prefix = "", postfix = "") { it.name }
176-
Text("使用 $weaponsString")
179+
Text("使用 $weaponsString", fontFamily = FontLXGWNeoXiHeiScreenFamily)
177180
}
178181
},
179182
supportingContent = {
@@ -183,12 +186,31 @@ private fun ListItemRecord(
183186
val endDate = end.toLocalDate()
184187
val startTime = start.toLocalTime()
185188
val endTime = end.toLocalTime()
189+
val remark = record.remark
190+
val score = record.score
191+
192+
Column {
193+
if (startDate == endDate) {
194+
Text("$startDate$startTime 开始, 直到 $endTime", fontFamily = FontLXGWNeoXiHeiScreenFamily)
195+
} else {
196+
Text(
197+
"$startDate$startTime 开始, 直到 $endDate$endTime",
198+
fontFamily = FontLXGWNeoXiHeiScreenFamily
199+
)
200+
}
201+
// 评分
202+
Row {
203+
Text("评分: ", fontWeight = FontWeight.Bold, fontFamily = FontLXGWNeoXiHeiScreenFamily)
204+
Text(score.toString(), fontFamily = FontLXGWNeoXiHeiScreenFamily)
205+
}
186206

187-
if (startDate == endDate) {
188-
Text("$startDate$startTime 开始, 直到 $endTime")
189-
} else {
190-
Text("$startDate$startTime 开始, 直到 $endDate$endTime")
207+
// 备注
208+
Row {
209+
Text("备注: ", fontWeight = FontWeight.Bold, fontFamily = FontLXGWNeoXiHeiScreenFamily)
210+
Text(remark.ifBlank { "" }, fontFamily = FontLXGWNeoXiHeiScreenFamily)
211+
}
191212
}
213+
192214
},
193215
trailingContent = {
194216
Row(
@@ -206,7 +228,7 @@ private fun ListItemRecord(
206228
}
207229
)
208230
}
209-
Text("持续:" + record.duration.format())
231+
Text("持续:" + record.duration.format(), fontFamily = FontLXGWNeoXiHeiScreenFamily)
210232
}
211233
}
212234
)

0 commit comments

Comments
 (0)