Compare commits
6 Commits
2339e5b980
...
7933452ab5
Author | SHA1 | Date | |
---|---|---|---|
7933452ab5 | |||
026df11933 | |||
316176bf6a | |||
bdf01f6bbe | |||
439080499b | |||
a86898011d |
8
.claude/settings.local.json
Normal file
8
.claude/settings.local.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"permissions": {
|
||||
"allow": [
|
||||
"Bash(./gradlew:*)"
|
||||
],
|
||||
"deny": []
|
||||
}
|
||||
}
|
6
.idea/AndroidProjectSystem.xml
generated
Normal file
6
.idea/AndroidProjectSystem.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="AndroidProjectSystem">
|
||||
<option name="providerId" value="com.android.tools.idea.GradleProjectSystem" />
|
||||
</component>
|
||||
</project>
|
3
.idea/inspectionProfiles/Project_Default.xml
generated
3
.idea/inspectionProfiles/Project_Default.xml
generated
@@ -49,6 +49,9 @@
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewParameterProviderOnFirstParameter" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewPickerAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
|
117
CLAUDE.md
Normal file
117
CLAUDE.md
Normal file
@@ -0,0 +1,117 @@
|
||||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## 项目概述
|
||||
|
||||
轻记账是一个使用 Kotlin 和 Jetpack Compose 开发的 Android 记账应用,采用 MVVM 架构。项目强调隐私保护,完全离线运行。
|
||||
|
||||
## 常用命令
|
||||
|
||||
### 构建和运行
|
||||
```bash
|
||||
./gradlew build # 构建项目
|
||||
./gradlew assembleDebug # 构建调试 APK
|
||||
./gradlew assembleRelease # 构建发布 APK
|
||||
./gradlew installDebug # 安装调试版本到设备
|
||||
./gradlew clean # 清理构建产物
|
||||
```
|
||||
|
||||
### 测试相关
|
||||
```bash
|
||||
./gradlew test # 运行单元测试
|
||||
./gradlew connectedAndroidTest # 运行设备测试
|
||||
```
|
||||
|
||||
## 架构概览
|
||||
|
||||
### MVVM 架构分层
|
||||
- **Model 层**: Room 数据库实体 (`model/` 目录下的 BookkeepingRecord, Category, Member, Settings)
|
||||
- **Data 层**: DAO 接口和 Repository 模式实现数据访问
|
||||
- **ViewModel 层**: 每个功能模块独立的 ViewModel (HomeViewModel, AnalysisViewModel 等)
|
||||
- **View 层**: Jetpack Compose UI,包含 screens、components 和 dialogs
|
||||
|
||||
### 核心数据流
|
||||
1. UI 层通过 ViewModel 发起数据请求
|
||||
2. ViewModel 调用 Repository 获取数据
|
||||
3. Repository 通过 DAO 访问 Room 数据库
|
||||
4. 数据通过 Flow/StateFlow 响应式传递回 UI
|
||||
|
||||
### 数据库架构
|
||||
- **主数据库**: BookkeepingDatabase (当前版本 5)
|
||||
- **核心表**: bookkeeping_records, categories, members, settings
|
||||
- **关键关系**: records 通过 memberId 关联 members 表
|
||||
- **迁移策略**: 使用 Room 的 Migration 机制处理版本升级
|
||||
|
||||
### 导航架构
|
||||
使用 Jetpack Navigation Compose,主要页面:
|
||||
- HomeScreen: 主页记账列表
|
||||
- AnalysisScreen: 数据分析图表
|
||||
- AddRecordScreen: 添加/编辑记录
|
||||
- CategoryManagementScreen: 分类管理
|
||||
- MemberManagementScreen: 成员管理
|
||||
- SettingsScreen: 设置页面
|
||||
|
||||
## 关键技术决策
|
||||
|
||||
### UI 框架
|
||||
- 完全使用 Jetpack Compose 构建 UI
|
||||
- Material 3 设计系统,支持深色/浅色主题
|
||||
- 自定义主题色功能通过 ColorThemeDialog 实现
|
||||
|
||||
### 数据可视化
|
||||
- 使用 MPAndroidChart 实现图表功能
|
||||
- ChartManager 统一管理图表样式和行为
|
||||
- 支持饼图和折线图两种展示方式
|
||||
|
||||
### 文件导入导出
|
||||
- Apache POI 处理 Excel 文件
|
||||
- OpenCSV 处理 CSV 文件
|
||||
- FileUtils 提供统一的文件操作接口
|
||||
|
||||
### 图标系统
|
||||
- 所有图标使用 Material Icons 和自定义矢量图标
|
||||
- CategoryIcon 和 MemberIcon 枚举管理图标映射
|
||||
- 支持动态图标选择和预览
|
||||
|
||||
## 开发注意事项
|
||||
|
||||
### 分支策略
|
||||
- master: 稳定主分支
|
||||
- develop: 开发分支
|
||||
- feature/*: 功能开发
|
||||
- release/*: 版本发布
|
||||
- hotfix/*: 紧急修复
|
||||
|
||||
### 提交规范
|
||||
使用约定式提交: `<type>: <description>`
|
||||
- feat: 新功能
|
||||
- fix: 修复 bug
|
||||
- docs: 文档更新
|
||||
- style: 代码格式
|
||||
- refactor: 代码重构
|
||||
- perf: 性能优化
|
||||
- test: 测试相关
|
||||
- build: 构建相关
|
||||
|
||||
### 重要功能模块
|
||||
|
||||
#### 月度记账开始日期 (Settings 中的 monthStartDay)
|
||||
- 支持自定义每月记账的开始日期 (1-31)
|
||||
- 影响月度统计和分析的日期范围计算
|
||||
- 默认值为 1 (每月 1 日开始)
|
||||
|
||||
#### 加密功能 (EncryptionUtils)
|
||||
- 预留的备份加密功能接口
|
||||
- 使用 AES 加密算法
|
||||
- 目前尚未集成到备份功能中
|
||||
|
||||
#### 默认数据初始化
|
||||
- 首次启动时自动创建默认分类
|
||||
- 包含常用的收入和支出分类
|
||||
- 可通过 insertDefaultCategories() 查看默认分类列表
|
||||
|
||||
### 性能考虑
|
||||
- 使用 Room 的 Flow 实现数据的响应式更新
|
||||
- 图表数据计算在 ViewModel 中异步处理
|
||||
- 大量数据导入时使用批量插入优化性能
|
@@ -10,6 +10,7 @@ import androidx.room.migration.Migration
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import com.yovinchen.bookkeeping.R
|
||||
import com.yovinchen.bookkeeping.model.BookkeepingRecord
|
||||
import com.yovinchen.bookkeeping.model.Budget
|
||||
import com.yovinchen.bookkeeping.model.Category
|
||||
import com.yovinchen.bookkeeping.model.Converters
|
||||
import com.yovinchen.bookkeeping.model.Member
|
||||
@@ -20,8 +21,8 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Database(
|
||||
entities = [BookkeepingRecord::class, Category::class, Member::class, Settings::class],
|
||||
version = 5,
|
||||
entities = [BookkeepingRecord::class, Category::class, Member::class, Settings::class, Budget::class],
|
||||
version = 6,
|
||||
exportSchema = false
|
||||
)
|
||||
@TypeConverters(Converters::class)
|
||||
@@ -30,6 +31,7 @@ abstract class BookkeepingDatabase : RoomDatabase() {
|
||||
abstract fun categoryDao(): CategoryDao
|
||||
abstract fun memberDao(): MemberDao
|
||||
abstract fun settingsDao(): SettingsDao
|
||||
abstract fun budgetDao(): BudgetDao
|
||||
|
||||
companion object {
|
||||
private const val TAG = "BookkeepingDatabase"
|
||||
@@ -147,6 +149,30 @@ abstract class BookkeepingDatabase : RoomDatabase() {
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
private val MIGRATION_5_6 = object : Migration(5, 6) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
// 创建预算表
|
||||
db.execSQL("""
|
||||
CREATE TABLE IF NOT EXISTS budgets (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
categoryName TEXT,
|
||||
memberId INTEGER,
|
||||
amount REAL NOT NULL,
|
||||
startDate INTEGER NOT NULL,
|
||||
endDate INTEGER NOT NULL,
|
||||
isEnabled INTEGER NOT NULL DEFAULT 1,
|
||||
alertThreshold REAL NOT NULL DEFAULT 0.8,
|
||||
createdAt INTEGER NOT NULL,
|
||||
updatedAt INTEGER NOT NULL
|
||||
)
|
||||
""")
|
||||
|
||||
// 在 settings 表中添加 encryptBackup 列
|
||||
db.execSQL("ALTER TABLE settings ADD COLUMN encryptBackup INTEGER NOT NULL DEFAULT 1")
|
||||
}
|
||||
}
|
||||
|
||||
@Volatile
|
||||
private var INSTANCE: BookkeepingDatabase? = null
|
||||
@@ -158,7 +184,7 @@ abstract class BookkeepingDatabase : RoomDatabase() {
|
||||
BookkeepingDatabase::class.java,
|
||||
"bookkeeping_database"
|
||||
)
|
||||
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5)
|
||||
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6)
|
||||
.addCallback(object : Callback() {
|
||||
override fun onCreate(db: SupportSQLiteDatabase) {
|
||||
super.onCreate(db)
|
||||
|
105
app/src/main/java/com/yovinchen/bookkeeping/data/BudgetDao.kt
Normal file
105
app/src/main/java/com/yovinchen/bookkeeping/data/BudgetDao.kt
Normal file
@@ -0,0 +1,105 @@
|
||||
package com.yovinchen.bookkeeping.data
|
||||
|
||||
import androidx.room.*
|
||||
import com.yovinchen.bookkeeping.model.Budget
|
||||
import com.yovinchen.bookkeeping.model.BudgetType
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import java.util.Date
|
||||
|
||||
/**
|
||||
* 预算数据访问对象
|
||||
* 提供预算相关的数据库操作接口
|
||||
*/
|
||||
@Dao
|
||||
interface BudgetDao {
|
||||
|
||||
/**
|
||||
* 插入新预算
|
||||
*/
|
||||
@Insert
|
||||
suspend fun insertBudget(budget: Budget): Long
|
||||
|
||||
/**
|
||||
* 更新预算
|
||||
*/
|
||||
@Update
|
||||
suspend fun updateBudget(budget: Budget)
|
||||
|
||||
/**
|
||||
* 删除预算
|
||||
*/
|
||||
@Delete
|
||||
suspend fun deleteBudget(budget: Budget)
|
||||
|
||||
/**
|
||||
* 根据ID获取预算
|
||||
*/
|
||||
@Query("SELECT * FROM budgets WHERE id = :budgetId")
|
||||
suspend fun getBudgetById(budgetId: Int): Budget?
|
||||
|
||||
/**
|
||||
* 获取所有启用的预算
|
||||
*/
|
||||
@Query("SELECT * FROM budgets WHERE isEnabled = 1 ORDER BY type, amount DESC")
|
||||
fun getAllEnabledBudgets(): Flow<List<Budget>>
|
||||
|
||||
/**
|
||||
* 获取所有预算(包括禁用的)
|
||||
*/
|
||||
@Query("SELECT * FROM budgets ORDER BY type, amount DESC")
|
||||
fun getAllBudgets(): Flow<List<Budget>>
|
||||
|
||||
/**
|
||||
* 根据类型获取预算
|
||||
*/
|
||||
@Query("SELECT * FROM budgets WHERE type = :type AND isEnabled = 1")
|
||||
fun getBudgetsByType(type: BudgetType): Flow<List<Budget>>
|
||||
|
||||
/**
|
||||
* 获取总预算
|
||||
*/
|
||||
@Query("SELECT * FROM budgets WHERE type = 'TOTAL' AND isEnabled = 1 AND :date BETWEEN startDate AND endDate LIMIT 1")
|
||||
suspend fun getTotalBudget(date: Date): Budget?
|
||||
|
||||
/**
|
||||
* 获取指定分类的预算
|
||||
*/
|
||||
@Query("SELECT * FROM budgets WHERE type = 'CATEGORY' AND categoryName = :categoryName AND isEnabled = 1 AND :date BETWEEN startDate AND endDate LIMIT 1")
|
||||
suspend fun getCategoryBudget(categoryName: String, date: Date): Budget?
|
||||
|
||||
/**
|
||||
* 获取指定成员的预算
|
||||
*/
|
||||
@Query("SELECT * FROM budgets WHERE type = 'MEMBER' AND memberId = :memberId AND isEnabled = 1 AND :date BETWEEN startDate AND endDate LIMIT 1")
|
||||
suspend fun getMemberBudget(memberId: Int, date: Date): Budget?
|
||||
|
||||
/**
|
||||
* 获取当前有效的所有预算
|
||||
*/
|
||||
@Query("SELECT * FROM budgets WHERE isEnabled = 1 AND :date BETWEEN startDate AND endDate ORDER BY type, amount DESC")
|
||||
fun getActiveBudgets(date: Date): Flow<List<Budget>>
|
||||
|
||||
/**
|
||||
* 更新预算的启用状态
|
||||
*/
|
||||
@Query("UPDATE budgets SET isEnabled = :enabled, updatedAt = :updatedAt WHERE id = :budgetId")
|
||||
suspend fun updateBudgetEnabled(budgetId: Int, enabled: Boolean, updatedAt: Date = Date())
|
||||
|
||||
/**
|
||||
* 删除所有过期的预算(可选)
|
||||
*/
|
||||
@Query("DELETE FROM budgets WHERE endDate < :date AND isEnabled = 0")
|
||||
suspend fun deleteExpiredBudgets(date: Date)
|
||||
|
||||
/**
|
||||
* 获取指定日期范围内的分类预算
|
||||
*/
|
||||
@Query("SELECT * FROM budgets WHERE type = 'CATEGORY' AND isEnabled = 1 AND :date BETWEEN startDate AND endDate")
|
||||
fun getCategoryBudgetsForDate(date: Date): Flow<List<Budget>>
|
||||
|
||||
/**
|
||||
* 获取指定日期范围内的成员预算
|
||||
*/
|
||||
@Query("SELECT * FROM budgets WHERE type = 'MEMBER' AND isEnabled = 1 AND :date BETWEEN startDate AND endDate")
|
||||
fun getMemberBudgetsForDate(date: Date): Flow<List<Budget>>
|
||||
}
|
@@ -0,0 +1,215 @@
|
||||
package com.yovinchen.bookkeeping.data
|
||||
|
||||
import com.yovinchen.bookkeeping.model.*
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
import java.util.Date
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* 预算仓库类
|
||||
* 提供预算相关的业务逻辑和数据访问
|
||||
*/
|
||||
class BudgetRepository(
|
||||
private val budgetDao: BudgetDao,
|
||||
private val bookkeepingDao: BookkeepingDao,
|
||||
private val memberDao: MemberDao
|
||||
) {
|
||||
|
||||
/**
|
||||
* 创建新预算
|
||||
*/
|
||||
suspend fun createBudget(budget: Budget): Long {
|
||||
return budgetDao.insertBudget(budget)
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新预算
|
||||
*/
|
||||
suspend fun updateBudget(budget: Budget) {
|
||||
budgetDao.updateBudget(budget.copy(updatedAt = Date()))
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除预算
|
||||
*/
|
||||
suspend fun deleteBudget(budget: Budget) {
|
||||
budgetDao.deleteBudget(budget)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有预算
|
||||
*/
|
||||
fun getAllBudgets(): Flow<List<Budget>> {
|
||||
return budgetDao.getAllBudgets()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有启用的预算
|
||||
*/
|
||||
fun getAllEnabledBudgets(): Flow<List<Budget>> {
|
||||
return budgetDao.getAllEnabledBudgets()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前有效的预算状态
|
||||
*/
|
||||
fun getActiveBudgetStatuses(date: Date = Date()): Flow<List<BudgetStatus>> {
|
||||
return combine(
|
||||
budgetDao.getActiveBudgets(date),
|
||||
bookkeepingDao.getAllRecords(),
|
||||
memberDao.getAllMembers()
|
||||
) { budgets, records, members ->
|
||||
budgets.map { budget ->
|
||||
val spent = calculateSpent(budget, records, members, date)
|
||||
val remaining = budget.amount - spent
|
||||
val percentage = if (budget.amount > 0) spent / budget.amount else 0.0
|
||||
|
||||
BudgetStatus(
|
||||
budget = budget,
|
||||
spent = spent,
|
||||
remaining = remaining,
|
||||
percentage = percentage,
|
||||
isOverBudget = spent > budget.amount,
|
||||
isNearLimit = percentage >= budget.alertThreshold
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算预算已花费金额
|
||||
*/
|
||||
private fun calculateSpent(
|
||||
budget: Budget,
|
||||
records: List<BookkeepingRecord>,
|
||||
members: List<Member>,
|
||||
currentDate: Date
|
||||
): Double {
|
||||
// 只计算支出类型的记录
|
||||
val expenseRecords = records.filter {
|
||||
it.type == TransactionType.EXPENSE &&
|
||||
it.date >= budget.startDate &&
|
||||
it.date <= budget.endDate &&
|
||||
it.date <= currentDate
|
||||
}
|
||||
|
||||
return when (budget.type) {
|
||||
BudgetType.TOTAL -> {
|
||||
// 总预算:计算所有支出
|
||||
expenseRecords.sumOf { it.amount }
|
||||
}
|
||||
BudgetType.CATEGORY -> {
|
||||
// 分类预算:计算指定分类的支出
|
||||
expenseRecords
|
||||
.filter { it.category == budget.categoryName }
|
||||
.sumOf { it.amount }
|
||||
}
|
||||
BudgetType.MEMBER -> {
|
||||
// 成员预算:计算指定成员的支出
|
||||
expenseRecords
|
||||
.filter { it.memberId == budget.memberId }
|
||||
.sumOf { it.amount }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取总预算状态
|
||||
*/
|
||||
suspend fun getTotalBudgetStatus(date: Date = Date()): BudgetStatus? {
|
||||
val budget = budgetDao.getTotalBudget(date) ?: return null
|
||||
val records = bookkeepingDao.getAllRecords().first()
|
||||
val members = memberDao.getAllMembers().first()
|
||||
|
||||
val spent = calculateSpent(budget, records, members, date)
|
||||
val remaining = budget.amount - spent
|
||||
val percentage = if (budget.amount > 0) spent / budget.amount else 0.0
|
||||
|
||||
return BudgetStatus(
|
||||
budget = budget,
|
||||
spent = spent,
|
||||
remaining = remaining,
|
||||
percentage = percentage,
|
||||
isOverBudget = spent > budget.amount,
|
||||
isNearLimit = percentage >= budget.alertThreshold
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分类预算状态
|
||||
*/
|
||||
fun getCategoryBudgetStatuses(date: Date = Date()): Flow<List<BudgetStatus>> {
|
||||
return combine(
|
||||
budgetDao.getCategoryBudgetsForDate(date),
|
||||
bookkeepingDao.getAllRecords(),
|
||||
memberDao.getAllMembers()
|
||||
) { budgets, records, members ->
|
||||
budgets.map { budget ->
|
||||
val spent = calculateSpent(budget, records, members, date)
|
||||
val remaining = budget.amount - spent
|
||||
val percentage = if (budget.amount > 0) spent / budget.amount else 0.0
|
||||
|
||||
BudgetStatus(
|
||||
budget = budget,
|
||||
spent = spent,
|
||||
remaining = remaining,
|
||||
percentage = percentage,
|
||||
isOverBudget = spent > budget.amount,
|
||||
isNearLimit = percentage >= budget.alertThreshold
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取成员预算状态
|
||||
*/
|
||||
fun getMemberBudgetStatuses(date: Date = Date()): Flow<List<BudgetStatus>> {
|
||||
return combine(
|
||||
budgetDao.getMemberBudgetsForDate(date),
|
||||
bookkeepingDao.getAllRecords(),
|
||||
memberDao.getAllMembers()
|
||||
) { budgets, records, members ->
|
||||
budgets.map { budget ->
|
||||
val spent = calculateSpent(budget, records, members, date)
|
||||
val remaining = budget.amount - spent
|
||||
val percentage = if (budget.amount > 0) spent / budget.amount else 0.0
|
||||
|
||||
BudgetStatus(
|
||||
budget = budget,
|
||||
spent = spent,
|
||||
remaining = remaining,
|
||||
percentage = percentage,
|
||||
isOverBudget = spent > budget.amount,
|
||||
isNearLimit = percentage >= budget.alertThreshold
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有预算超支或接近限制
|
||||
*/
|
||||
suspend fun checkBudgetAlerts(date: Date = Date()): List<BudgetStatus> {
|
||||
val allStatuses = getActiveBudgetStatuses(date).first()
|
||||
return allStatuses.filter { it.isOverBudget || it.isNearLimit }
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新预算启用状态
|
||||
*/
|
||||
suspend fun updateBudgetEnabled(budgetId: Int, enabled: Boolean) {
|
||||
budgetDao.updateBudgetEnabled(budgetId, enabled)
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理过期的禁用预算
|
||||
*/
|
||||
suspend fun cleanupExpiredBudgets() {
|
||||
budgetDao.deleteExpiredBudgets(Date())
|
||||
}
|
||||
}
|
@@ -1,6 +1,7 @@
|
||||
package com.yovinchen.bookkeeping.data
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
import com.yovinchen.bookkeeping.model.BudgetType
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.*
|
||||
@@ -29,4 +30,14 @@ class Converters {
|
||||
fun toDate(timestamp: String?): Date? {
|
||||
return timestamp?.let { Date(it.toLong()) }
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun fromBudgetType(budgetType: BudgetType?): String? {
|
||||
return budgetType?.name
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun toBudgetType(budgetType: String?): BudgetType? {
|
||||
return budgetType?.let { BudgetType.valueOf(it) }
|
||||
}
|
||||
}
|
||||
|
@@ -29,4 +29,7 @@ interface SettingsDao {
|
||||
|
||||
@Query("UPDATE settings SET lastBackupTime = :time WHERE id = 1")
|
||||
suspend fun updateLastBackupTime(time: Long)
|
||||
|
||||
@Query("UPDATE settings SET encryptBackup = :encrypt WHERE id = 1")
|
||||
suspend fun updateEncryptBackup(encrypt: Boolean)
|
||||
}
|
86
app/src/main/java/com/yovinchen/bookkeeping/model/Budget.kt
Normal file
86
app/src/main/java/com/yovinchen/bookkeeping/model/Budget.kt
Normal file
@@ -0,0 +1,86 @@
|
||||
package com.yovinchen.bookkeeping.model
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import java.util.Date
|
||||
|
||||
/**
|
||||
* 预算实体类
|
||||
* 用于设置和跟踪月度预算、分类预算和成员预算
|
||||
*/
|
||||
@Entity(tableName = "budgets")
|
||||
data class Budget(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val id: Int = 0,
|
||||
|
||||
/**
|
||||
* 预算类型:TOTAL(总预算), CATEGORY(分类预算), MEMBER(成员预算)
|
||||
*/
|
||||
val type: BudgetType,
|
||||
|
||||
/**
|
||||
* 预算关联的分类名称(仅在 type 为 CATEGORY 时使用)
|
||||
*/
|
||||
val categoryName: String? = null,
|
||||
|
||||
/**
|
||||
* 预算关联的成员ID(仅在 type 为 MEMBER 时使用)
|
||||
*/
|
||||
val memberId: Int? = null,
|
||||
|
||||
/**
|
||||
* 预算金额
|
||||
*/
|
||||
val amount: Double,
|
||||
|
||||
/**
|
||||
* 预算生效开始日期
|
||||
*/
|
||||
val startDate: Date,
|
||||
|
||||
/**
|
||||
* 预算生效结束日期
|
||||
*/
|
||||
val endDate: Date,
|
||||
|
||||
/**
|
||||
* 是否启用此预算
|
||||
*/
|
||||
val isEnabled: Boolean = true,
|
||||
|
||||
/**
|
||||
* 提醒阈值(百分比,如 0.8 表示达到 80% 时提醒)
|
||||
*/
|
||||
val alertThreshold: Double = 0.8,
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
val createdAt: Date = Date(),
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
val updatedAt: Date = Date()
|
||||
)
|
||||
|
||||
/**
|
||||
* 预算类型枚举
|
||||
*/
|
||||
enum class BudgetType {
|
||||
TOTAL, // 总预算
|
||||
CATEGORY, // 分类预算
|
||||
MEMBER // 成员预算
|
||||
}
|
||||
|
||||
/**
|
||||
* 预算状态数据类,用于展示预算使用情况
|
||||
*/
|
||||
data class BudgetStatus(
|
||||
val budget: Budget,
|
||||
val spent: Double, // 已花费金额
|
||||
val remaining: Double, // 剩余金额
|
||||
val percentage: Double, // 使用百分比
|
||||
val isOverBudget: Boolean, // 是否超预算
|
||||
val isNearLimit: Boolean // 是否接近预算限制
|
||||
)
|
@@ -10,5 +10,6 @@ data class Settings(
|
||||
val themeMode: String = "FOLLOW_SYSTEM", // 主题模式:FOLLOW_SYSTEM, LIGHT, DARK
|
||||
val autoBackupEnabled: Boolean = false, // 自动备份开关
|
||||
val autoBackupInterval: Int = 7, // 自动备份间隔(天)
|
||||
val lastBackupTime: Long = 0L // 上次备份时间
|
||||
val lastBackupTime: Long = 0L, // 上次备份时间
|
||||
val encryptBackup: Boolean = true // 备份加密开关,默认开启
|
||||
)
|
@@ -19,6 +19,7 @@ import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.yovinchen.bookkeeping.model.Settings
|
||||
import com.yovinchen.bookkeeping.model.ThemeMode
|
||||
import com.yovinchen.bookkeeping.ui.components.*
|
||||
import com.yovinchen.bookkeeping.ui.dialog.*
|
||||
@@ -43,6 +44,7 @@ fun SettingsScreen(
|
||||
val selectedType by viewModel.selectedCategoryType.collectAsState()
|
||||
val members by memberViewModel.allMembers.collectAsState(initial = emptyList())
|
||||
val monthStartDay by viewModel.monthStartDay.collectAsState()
|
||||
val settings by viewModel.settings.collectAsState()
|
||||
val context = LocalContext.current
|
||||
|
||||
var showMonthStartDayDialog by remember { mutableStateOf(false) }
|
||||
@@ -277,6 +279,29 @@ fun SettingsScreen(
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
}
|
||||
|
||||
HorizontalDivider(modifier = Modifier.padding(vertical = 8.dp))
|
||||
|
||||
// 备份加密开关
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Text("备份加密", modifier = Modifier.weight(1f))
|
||||
Switch(
|
||||
checked = settings?.encryptBackup ?: true,
|
||||
onCheckedChange = { enabled ->
|
||||
viewModel.updateSettings(
|
||||
settings?.copy(encryptBackup = enabled) ?: Settings(encryptBackup = enabled)
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
Text(
|
||||
"开启后,导出的备份文件将被加密保护",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
|
@@ -0,0 +1,136 @@
|
||||
package com.yovinchen.bookkeeping.utils
|
||||
|
||||
import android.security.keystore.KeyGenParameterSpec
|
||||
import android.security.keystore.KeyProperties
|
||||
import android.util.Base64
|
||||
import java.security.KeyStore
|
||||
import javax.crypto.Cipher
|
||||
import javax.crypto.KeyGenerator
|
||||
import javax.crypto.SecretKey
|
||||
import javax.crypto.spec.GCMParameterSpec
|
||||
|
||||
/**
|
||||
* 加密工具类,使用 Android Keystore 系统安全地存储加密密钥
|
||||
*/
|
||||
object EncryptionUtils {
|
||||
private const val ANDROID_KEYSTORE = "AndroidKeyStore"
|
||||
private const val TRANSFORMATION = "AES/GCM/NoPadding"
|
||||
private const val KEY_ALIAS = "BookkeepingBackupKey"
|
||||
private const val GCM_TAG_LENGTH = 128
|
||||
|
||||
init {
|
||||
generateKey()
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成或获取加密密钥
|
||||
*/
|
||||
private fun generateKey() {
|
||||
val keyStore = KeyStore.getInstance(ANDROID_KEYSTORE)
|
||||
keyStore.load(null)
|
||||
|
||||
// 如果密钥已存在,则不需要重新生成
|
||||
if (keyStore.containsAlias(KEY_ALIAS)) {
|
||||
return
|
||||
}
|
||||
|
||||
val keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEYSTORE)
|
||||
val keyGenParameterSpec = KeyGenParameterSpec.Builder(
|
||||
KEY_ALIAS,
|
||||
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT
|
||||
)
|
||||
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
|
||||
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
|
||||
.setRandomizedEncryptionRequired(true)
|
||||
.build()
|
||||
|
||||
keyGenerator.init(keyGenParameterSpec)
|
||||
keyGenerator.generateKey()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取密钥
|
||||
*/
|
||||
private fun getKey(): SecretKey {
|
||||
val keyStore = KeyStore.getInstance(ANDROID_KEYSTORE)
|
||||
keyStore.load(null)
|
||||
return keyStore.getKey(KEY_ALIAS, null) as SecretKey
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密字符串
|
||||
* @param plainText 要加密的明文
|
||||
* @return Base64编码的加密数据(包含IV)
|
||||
*/
|
||||
fun encrypt(plainText: String): String {
|
||||
val cipher = Cipher.getInstance(TRANSFORMATION)
|
||||
cipher.init(Cipher.ENCRYPT_MODE, getKey())
|
||||
|
||||
val iv = cipher.iv
|
||||
val encryptedBytes = cipher.doFinal(plainText.toByteArray(Charsets.UTF_8))
|
||||
|
||||
// 将IV和加密数据组合
|
||||
val combined = ByteArray(iv.size + encryptedBytes.size)
|
||||
System.arraycopy(iv, 0, combined, 0, iv.size)
|
||||
System.arraycopy(encryptedBytes, 0, combined, iv.size, encryptedBytes.size)
|
||||
|
||||
return Base64.encodeToString(combined, Base64.DEFAULT)
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密字符串
|
||||
* @param encryptedData Base64编码的加密数据
|
||||
* @return 解密后的明文
|
||||
*/
|
||||
fun decrypt(encryptedData: String): String {
|
||||
val combined = Base64.decode(encryptedData, Base64.DEFAULT)
|
||||
|
||||
// 提取IV(前12字节)
|
||||
val iv = combined.sliceArray(0..11)
|
||||
val encryptedBytes = combined.sliceArray(12 until combined.size)
|
||||
|
||||
val cipher = Cipher.getInstance(TRANSFORMATION)
|
||||
val spec = GCMParameterSpec(GCM_TAG_LENGTH, iv)
|
||||
cipher.init(Cipher.DECRYPT_MODE, getKey(), spec)
|
||||
|
||||
val decryptedBytes = cipher.doFinal(encryptedBytes)
|
||||
return String(decryptedBytes, Charsets.UTF_8)
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密字节数组
|
||||
* @param data 要加密的数据
|
||||
* @return 加密后的数据(包含IV)
|
||||
*/
|
||||
fun encryptBytes(data: ByteArray): ByteArray {
|
||||
val cipher = Cipher.getInstance(TRANSFORMATION)
|
||||
cipher.init(Cipher.ENCRYPT_MODE, getKey())
|
||||
|
||||
val iv = cipher.iv
|
||||
val encryptedBytes = cipher.doFinal(data)
|
||||
|
||||
// 将IV和加密数据组合
|
||||
val combined = ByteArray(iv.size + encryptedBytes.size)
|
||||
System.arraycopy(iv, 0, combined, 0, iv.size)
|
||||
System.arraycopy(encryptedBytes, 0, combined, iv.size, encryptedBytes.size)
|
||||
|
||||
return combined
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密字节数组
|
||||
* @param encryptedData 加密的数据
|
||||
* @return 解密后的数据
|
||||
*/
|
||||
fun decryptBytes(encryptedData: ByteArray): ByteArray {
|
||||
// 提取IV(前12字节)
|
||||
val iv = encryptedData.sliceArray(0..11)
|
||||
val encryptedBytes = encryptedData.sliceArray(12 until encryptedData.size)
|
||||
|
||||
val cipher = Cipher.getInstance(TRANSFORMATION)
|
||||
val spec = GCMParameterSpec(GCM_TAG_LENGTH, iv)
|
||||
cipher.init(Cipher.DECRYPT_MODE, getKey(), spec)
|
||||
|
||||
return cipher.doFinal(encryptedBytes)
|
||||
}
|
||||
}
|
@@ -107,11 +107,16 @@ object FilePickerUtil {
|
||||
private fun isValidFileType(fileName: String, mimeType: String?): Boolean {
|
||||
val fileExtension = fileName.lowercase()
|
||||
return fileExtension.endsWith(".csv") ||
|
||||
fileExtension.endsWith(".csv.enc") ||
|
||||
fileExtension.endsWith(".xlsx") ||
|
||||
fileExtension.endsWith(".xlsx.enc") ||
|
||||
fileExtension.endsWith(".xls") ||
|
||||
fileExtension.endsWith(".xls.enc") ||
|
||||
fileExtension.endsWith(".enc") ||
|
||||
mimeType == "text/csv" ||
|
||||
mimeType == "application/vnd.ms-excel" ||
|
||||
mimeType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||
mimeType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
|
||||
mimeType == "application/octet-stream" // 加密文件可能被识别为二进制流
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -14,6 +14,7 @@ import com.yovinchen.bookkeeping.model.BookkeepingRecord
|
||||
import com.yovinchen.bookkeeping.model.Category
|
||||
import com.yovinchen.bookkeeping.model.Settings
|
||||
import com.yovinchen.bookkeeping.model.TransactionType
|
||||
import com.yovinchen.bookkeeping.utils.EncryptionUtils
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.delay
|
||||
@@ -30,6 +31,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook
|
||||
import java.io.File
|
||||
import java.io.FileReader
|
||||
import java.io.FileWriter
|
||||
import java.io.StringWriter
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
@@ -157,15 +159,24 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
|
||||
fun exportToCSV(context: Context, customDir: File? = null) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
val currentSettings = settings.value ?: Settings()
|
||||
val timestamp =
|
||||
SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
|
||||
val fileName = "bookkeeping_backup_$timestamp.csv"
|
||||
val shouldEncrypt = currentSettings.encryptBackup
|
||||
val fileName = if (shouldEncrypt) {
|
||||
"bookkeeping_backup_$timestamp.csv.enc"
|
||||
} else {
|
||||
"bookkeeping_backup_$timestamp.csv"
|
||||
}
|
||||
val downloadsDir = customDir ?: Environment.getExternalStoragePublicDirectory(
|
||||
Environment.DIRECTORY_DOWNLOADS
|
||||
)
|
||||
val file = File(downloadsDir, fileName)
|
||||
|
||||
CSVWriter(FileWriter(file)).use { writer ->
|
||||
// 先创建CSV内容到字符串
|
||||
val csvContent = StringWriter().use { stringWriter ->
|
||||
val writer = CSVWriter(stringWriter)
|
||||
|
||||
// 写入头部
|
||||
writer.writeNext(arrayOf("日期", "类型", "金额", "类别", "备注", "成员"))
|
||||
|
||||
@@ -189,11 +200,25 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
|
||||
)
|
||||
)
|
||||
}
|
||||
writer.close()
|
||||
stringWriter.toString()
|
||||
}
|
||||
|
||||
// 根据设置决定是否加密
|
||||
if (shouldEncrypt) {
|
||||
val encryptedContent = EncryptionUtils.encrypt(csvContent)
|
||||
file.writeText(encryptedContent)
|
||||
} else {
|
||||
file.writeText(csvContent)
|
||||
}
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(context, "CSV导出成功: ${file.absolutePath}", Toast.LENGTH_LONG)
|
||||
.show()
|
||||
val message = if (shouldEncrypt) {
|
||||
"CSV导出成功(已加密): ${file.absolutePath}"
|
||||
} else {
|
||||
"CSV导出成功: ${file.absolutePath}"
|
||||
}
|
||||
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
@@ -207,6 +232,9 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
|
||||
fun exportToExcel(context: Context) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
val currentSettings = settings.value ?: Settings()
|
||||
val shouldEncrypt = currentSettings.encryptBackup
|
||||
|
||||
val workbook = XSSFWorkbook()
|
||||
val sheet = workbook.createSheet("账目记录")
|
||||
|
||||
@@ -239,18 +267,39 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
|
||||
|
||||
val timestamp =
|
||||
SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
|
||||
val fileName = "bookkeeping_backup_$timestamp.xlsx"
|
||||
val fileName = if (shouldEncrypt) {
|
||||
"bookkeeping_backup_$timestamp.xlsx.enc"
|
||||
} else {
|
||||
"bookkeeping_backup_$timestamp.xlsx"
|
||||
}
|
||||
val downloadsDir =
|
||||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
|
||||
val file = File(downloadsDir, fileName)
|
||||
|
||||
workbook.write(file.outputStream())
|
||||
if (shouldEncrypt) {
|
||||
// 将 workbook 写入字节数组
|
||||
val byteArrayOutputStream = java.io.ByteArrayOutputStream()
|
||||
workbook.write(byteArrayOutputStream)
|
||||
val excelBytes = byteArrayOutputStream.toByteArray()
|
||||
|
||||
// 加密字节数组
|
||||
val encryptedBytes = EncryptionUtils.encryptBytes(excelBytes)
|
||||
|
||||
// 写入加密文件
|
||||
file.writeBytes(encryptedBytes)
|
||||
} else {
|
||||
workbook.write(file.outputStream())
|
||||
}
|
||||
|
||||
workbook.close()
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(
|
||||
context, "Excel导出成功: ${file.absolutePath}", Toast.LENGTH_LONG
|
||||
).show()
|
||||
val message = if (shouldEncrypt) {
|
||||
"Excel导出成功(已加密): ${file.absolutePath}"
|
||||
} else {
|
||||
"Excel导出成功: ${file.absolutePath}"
|
||||
}
|
||||
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
@@ -265,10 +314,30 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
when {
|
||||
backupFile.name.endsWith(".csv.enc", ignoreCase = true) -> {
|
||||
// 解密CSV文件
|
||||
val encryptedContent = backupFile.readText()
|
||||
val decryptedContent = EncryptionUtils.decrypt(encryptedContent)
|
||||
val tempFile = File(context.cacheDir, "temp_decrypted.csv")
|
||||
tempFile.writeText(decryptedContent)
|
||||
restoreFromCSV(tempFile)
|
||||
tempFile.delete()
|
||||
}
|
||||
|
||||
backupFile.name.endsWith(".csv", ignoreCase = true) -> {
|
||||
restoreFromCSV(backupFile)
|
||||
}
|
||||
|
||||
backupFile.name.endsWith(".xlsx.enc", ignoreCase = true) -> {
|
||||
// 解密Excel文件
|
||||
val encryptedBytes = backupFile.readBytes()
|
||||
val decryptedBytes = EncryptionUtils.decryptBytes(encryptedBytes)
|
||||
val tempFile = File(context.cacheDir, "temp_decrypted.xlsx")
|
||||
tempFile.writeBytes(decryptedBytes)
|
||||
restoreFromExcel(tempFile)
|
||||
tempFile.delete()
|
||||
}
|
||||
|
||||
backupFile.name.endsWith(".xlsx", ignoreCase = true) -> {
|
||||
restoreFromExcel(backupFile)
|
||||
}
|
||||
@@ -287,7 +356,11 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(context, "数据恢复失败: ${e.message}", Toast.LENGTH_LONG).show()
|
||||
val errorMessage = when {
|
||||
e.message?.contains("decrypt") == true -> "解密失败,请确认文件未损坏"
|
||||
else -> "数据恢复失败: ${e.message}"
|
||||
}
|
||||
Toast.makeText(context, errorMessage, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -343,4 +416,10 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
|
||||
private suspend fun findMemberIdByName(name: String): Int? {
|
||||
return memberDao.getAllMembers().first().find { member -> member.name == name }?.id
|
||||
}
|
||||
|
||||
fun updateSettings(settings: Settings) {
|
||||
viewModelScope.launch {
|
||||
settingsRepository.updateSettings(settings)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user