Skip to content

Commit eac8015

Browse files
committed
stats can be written by manual and at end, even if statsInterval == 0
1 parent 2b2f163 commit eac8015

3 files changed

Lines changed: 19 additions & 15 deletions

File tree

MacTcode/AppDelegate.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
6969

7070
func applicationWillTerminate(_ notification: Notification) {
7171
Log.i("★AppDelegate terminating self=\(ObjectIdentifier(self))")
72-
InputStats.i.writeStatsToFile()
72+
InputStats.i.writeStatsToFile(force: true)
7373
}
7474

7575
private func setupSigintHandler() {
@@ -128,11 +128,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
128128
// シグナルの種類によって処理を分岐
129129
if buf == SIGNAL_SIGINT {
130130
Log.i("★SIGINT received, syncing data...")
131-
InputStats.i.writeStatsToFile()
131+
InputStats.i.writeStatsToFile(force: true)
132132
Log.i("Data sync completed")
133133
} else if buf == SIGNAL_SIGTERM {
134134
Log.i("★SIGTERM received, syncing data and exiting...")
135-
InputStats.i.writeStatsToFile()
135+
InputStats.i.writeStatsToFile(force: true)
136136
Log.i("Data sync completed, exiting")
137137
_exit(0)
138138
}

MacTcode/InputStats.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Foundation
33
/// 入力統計情報を管理するシングルトンクラス
44
class InputStats {
55
static let i = InputStats()
6-
6+
77
private var lastSyncDate = Date()
88

99
private var basicCount = 0
@@ -210,7 +210,7 @@ class InputStats {
210210
}
211211
}
212212
/// 統計情報をファイルに書き出す
213-
public func writeStatsToFile() {
213+
public func writeStatsToFile(force: Bool = false) {
214214
queue.sync {
215215
// 学習データも同じタイミングで保存
216216
MazegakiDict.i.saveMruData()
@@ -220,10 +220,13 @@ class InputStats {
220220
lastSyncDate = Date()
221221
return
222222
}
223-
guard UserConfigs.i.system.syncStatsInterval > 0 else {
224-
return
223+
// If not forced, obey syncStatsInterval; if forced (manual/menu/signal), allow write even when interval == 0
224+
if !force {
225+
guard UserConfigs.i.system.syncStatsInterval > 0 else {
226+
return
227+
}
225228
}
226-
229+
227230
let fileManager = FileManager.default
228231
let fileURL = UserConfigs.i.configFileURL("tc-record.txt")
229232

@@ -241,11 +244,11 @@ class InputStats {
241244

242245
// 統計行を作成
243246
let statsLine = String(format: "%@ 文字: %4d 部首: %3d(%d%%) 交ぜ書き: %3d(%d%%) 機能: %3d(%d%%)\n",
244-
dateString,
245-
basicCount,
246-
bushuCount, bushuPercent,
247-
mazegakiCount, mazegakiPercent,
248-
functionCount, functionPercent)
247+
dateString,
248+
basicCount,
249+
bushuCount, bushuPercent,
250+
mazegakiCount, mazegakiPercent,
251+
functionCount, functionPercent)
249252

250253
// ファイルに追記
251254
if let data = statsLine.data(using: .utf8) {

MacTcode/Tcode/TcodeInputController.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class TcodeInputController: IMKInputController, Controller {
4646

4747
@objc
4848
func writeStatsToFile() {
49-
InputStats.i.writeStatsToFile()
49+
InputStats.i.writeStatsToFile(force: true)
5050
}
5151

5252
@objc
@@ -68,7 +68,8 @@ class TcodeInputController: IMKInputController, Controller {
6868
}
6969
// continuity break
7070
InputStats.i.recordNonStrokeEvent()
71-
InputStats.i.writeStatsToFileMaybe()
71+
// Force a write on deactivate (user action / IME switch)
72+
InputStats.i.writeStatsToFile(force: true)
7273
super.deactivateServer(sender)
7374
}
7475

0 commit comments

Comments
 (0)