Skip to content

Commit 648a32c

Browse files
author
DevelopLab
committed
1. Add 'Export all records to CSV'
1 parent 10853e7 commit 648a32c

File tree

7 files changed

+82
-9
lines changed

7 files changed

+82
-9
lines changed

BatteryInfo.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@
355355
CLANG_ENABLE_MODULES = YES;
356356
CODE_SIGN_IDENTITY = "Apple Development";
357357
CODE_SIGN_STYLE = Automatic;
358-
CURRENT_PROJECT_VERSION = 11;
358+
CURRENT_PROJECT_VERSION = 12;
359359
DEVELOPMENT_TEAM = X2WABLB38D;
360360
FRAMEWORK_SEARCH_PATHS = (
361361
/System/Library/Frameworks,
@@ -378,7 +378,7 @@
378378
"$(inherited)",
379379
"$(PROJECT_DIR)/BatteryInfo",
380380
);
381-
MARKETING_VERSION = 1.1.5;
381+
MARKETING_VERSION = 1.1.6;
382382
OTHER_CFLAGS = "-Wno-error=unguarded-availability-new";
383383
OTHER_LDFLAGS = "";
384384
PRODUCT_BUNDLE_IDENTIFIER = com.developlab.BatteryInfo;
@@ -404,7 +404,7 @@
404404
CLANG_ENABLE_MODULES = YES;
405405
CODE_SIGN_IDENTITY = "Apple Development";
406406
CODE_SIGN_STYLE = Automatic;
407-
CURRENT_PROJECT_VERSION = 11;
407+
CURRENT_PROJECT_VERSION = 12;
408408
DEVELOPMENT_TEAM = X2WABLB38D;
409409
FRAMEWORK_SEARCH_PATHS = (
410410
/System/Library/Frameworks,
@@ -427,7 +427,7 @@
427427
"$(inherited)",
428428
"$(PROJECT_DIR)/BatteryInfo",
429429
);
430-
MARKETING_VERSION = 1.1.5;
430+
MARKETING_VERSION = 1.1.6;
431431
OTHER_CFLAGS = "-Wno-error=unguarded-availability-new";
432432
OTHER_LDFLAGS = "";
433433
PRODUCT_BUNDLE_IDENTIFIER = com.developlab.BatteryInfo;

BatteryInfo/Controller/BatteryRecordDatabaseManager.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,38 @@ class BatteryRecordDatabaseManager {
191191
return latestRecord
192192
}
193193

194+
// 导出全部记录为CSV
195+
func exportToCSV() -> URL? {
196+
let records = fetchAllRecords()
197+
guard !records.isEmpty else {
198+
NSLog("No records found to export.")
199+
return nil
200+
}
201+
202+
let fileName = NSLocalizedString("BatteryDataRecordsCSVName", comment: "BatteryDataRecords").appending(".csv")
203+
let fileURL = FileManager.default.temporaryDirectory.appendingPathComponent(fileName)
204+
205+
var csvText = "ID,CreateDate,CycleCount,NominalChargeCapacity,DesignCapacity,MaximumCapacity\n"
206+
207+
let dateFormatter = DateFormatter()
208+
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
209+
210+
for record in records {
211+
let createDateStr = dateFormatter.string(from: Date(timeIntervalSince1970: TimeInterval(record.createDate)))
212+
let line = "\(record.id),\(createDateStr),\(record.cycleCount),\(record.nominalChargeCapacity ?? 0),\(record.designCapacity ?? 0),\(record.maximumCapacity ?? "N/A")\n"
213+
csvText.append(line)
214+
}
215+
216+
do {
217+
try csvText.write(to: fileURL, atomically: true, encoding: .utf8)
218+
NSLog("CSV file created at: \(fileURL.path)")
219+
return fileURL
220+
} catch {
221+
NSLog("Failed to write CSV file: \(error.localizedDescription)")
222+
return nil
223+
}
224+
}
225+
194226
// 删除全部数据
195227
func deleteAllRecords() {
196228
let deleteQuery = "DELETE FROM \(recordTableName);"

BatteryInfo/Localizable.xcstrings

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@
113113
}
114114
}
115115
},
116+
"BatteryDataRecordsCSVName" : {
117+
"localizations" : {
118+
"en" : {
119+
"stringUnit" : {
120+
"state" : "translated",
121+
"value" : "BatteryDataRecords"
122+
}
123+
},
124+
"zh-Hans" : {
125+
"stringUnit" : {
126+
"state" : "translated",
127+
"value" : "电池历史数据记录"
128+
}
129+
}
130+
}
131+
},
116132
"BatteryDataSourceMessage" : {
117133
"localizations" : {
118134
"en" : {
@@ -889,6 +905,22 @@
889905
}
890906
}
891907
},
908+
"ExportAllRecordsToCSV" : {
909+
"localizations" : {
910+
"en" : {
911+
"stringUnit" : {
912+
"state" : "translated",
913+
"value" : "Export all records to CSV"
914+
}
915+
},
916+
"zh-Hans" : {
917+
"stringUnit" : {
918+
"state" : "translated",
919+
"value" : "将所有记录导出到CSV"
920+
}
921+
}
922+
}
923+
},
892924
"Floor" : {
893925
"localizations" : {
894926
"en" : {

BatteryInfo/ViewController/DataRecordSettingsViewController.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class DataRecordSettingsViewController: UIViewController, UITableViewDelegate, U
77

88
private let settingsUtils = SettingsUtils.instance
99

10-
private let tableTitleList = [nil, NSLocalizedString("RecordFrequencySettings", comment: "记录频率设置"), nil]
10+
private let tableTitleList = [nil, NSLocalizedString("RecordFrequencySettings", comment: "记录频率设置"), nil, nil]
1111

12-
private let tableCellList = [[NSLocalizedString("Enable", comment: "启用"), NSLocalizedString("HistoryRecordViewInHomeView", comment: "在主界面显示历史记录界面")], [NSLocalizedString("Automatic", comment: ""), NSLocalizedString("DataChanged", comment: ""), NSLocalizedString("EveryDay", comment: ""), NSLocalizedString("Manual", comment: "")], [NSLocalizedString("DeleteAllRecords", comment: "")]]
12+
private let tableCellList = [[NSLocalizedString("Enable", comment: "启用"), NSLocalizedString("HistoryRecordViewInHomeView", comment: "在主界面显示历史记录界面")], [NSLocalizedString("Automatic", comment: ""), NSLocalizedString("DataChanged", comment: ""), NSLocalizedString("EveryDay", comment: ""), NSLocalizedString("Manual", comment: "")], [NSLocalizedString("ExportAllRecordsToCSV", comment: "")], [NSLocalizedString("DeleteAllRecords", comment: "")]]
1313

1414
private var reloadMainTabBar = false
1515

@@ -94,6 +94,9 @@ class DataRecordSettingsViewController: UIViewController, UITableViewDelegate, U
9494
cell.accessoryType = .none
9595
}
9696
} else if indexPath.section == 2 {
97+
cell.textLabel?.textAlignment = .center
98+
cell.textLabel?.textColor = .systemBlue
99+
} else if indexPath.section == 3 {
97100
cell.textLabel?.textAlignment = .center
98101
cell.textLabel?.textColor = .systemRed
99102
}
@@ -113,6 +116,12 @@ class DataRecordSettingsViewController: UIViewController, UITableViewDelegate, U
113116
// 设置当前的cell选中状态
114117
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
115118
} else if indexPath.section == 2 {
119+
// 导出记录为CSV
120+
if let csvFileURL = BatteryRecordDatabaseManager.shared.exportToCSV() {
121+
let activityVC = UIActivityViewController(activityItems: [csvFileURL], applicationActivities: nil)
122+
present(activityVC, animated: true, completion: nil)
123+
}
124+
} else if indexPath.section == 3 {
116125
// 删除全部数据的按钮
117126
let alert = UIAlertController(
118127
title: NSLocalizedString("DeleteAllRecordsTitle", comment: "确定要删除所有数据吗?"),

BatteryInfo/ViewController/SettingsViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import UIKit
33

44
class SettingsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
55

6-
let versionCode = "1.1.5"
6+
let versionCode = "1.1.6"
77

88
private var tableView = UITableView()
99

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ include $(THEOS)/makefiles/common.mk
55

66
# 使用 Xcode 项目构建
77
XCODEPROJ_NAME = BatteryInfo
8-
BUILD_VERSION = "1.1.5"
8+
BUILD_VERSION = "1.1.6"
99

1010
# 指定 Theos 使用 xcodeproj 规则
1111
include $(THEOS_MAKE_PATH)/xcodeproj.mk

control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: com.developlab.batteryinfo
22
Name: Battery Info
3-
Version: 1.1.5
3+
Version: 1.1.6
44
Architecture: iphoneos-arm
55
Description: Battery information
66
Maintainer: developlab

0 commit comments

Comments
 (0)