這篇Part 2.我們來處理上面那15的按鈕和資料的讀取寫入吧
因為這是五五五個一組,分為Mood、Water、Exercise,三組不同的欄位,所以我們還是拆成三個Outlet Collections
@IBOutlet var moodList: [UIButton]!
@IBOutlet var waterList: [UIButton]!
@IBOutlet var exerciseList: [UIButton]!
不過因為他們其實長得很像,功用上也大同小異,所以我還是新建一個enum來給後面使用好了
enum DayStatus: Int {
case Mood = 1
case Water = 2
case Exercise = 3
}
在viewDidLoad的時候初始化他們
for mood in moodList {
mood.tag = DayStatus.Mood.rawValue
mood.isSelected = false
mood.setImage(UIImage(named: "mood_activate"), for: .selected)
mood.addTarget(self, action: #selector(clickButtonList), for: .touchUpInside)
}
for water in waterList {
water.tag = DayStatus.Water.rawValue
water.isSelected = false
water.setImage(UIImage(named: "water_activate"), for: .selected)
water.addTarget(self, action: #selector(clickButtonList), for: .touchUpInside)
}
for exercise in exerciseList {
exercise.tag = DayStatus.Exercise.rawValue
exercise.isSelected = false
exercise.setImage(UIImage(named: "exercise_activate"), for: .selected)
exercise.addTarget(self, action: #selector(clickButtonList), for: .touchUpInside)
}
加入clickButtonList功能
@objc func clickButtonList(_ sender: UIButton) {
// 這次改變得值,如果是選取則+1、反之取消選取則-1
var changeValue: Int = 0
if sender.isSelected {
sender.isSelected = false
changeValue = -1
} else {
sender.isSelected = true
changeValue = 1
}
// 判斷是哪個群組的按鈕異動,修正該資料庫欄位值
switch sender.tag {
case DayStatus.Mood.rawValue:
sqlManager.updateDayMoodById(id: dateFormatter.string(from: datePicker.date), val: changeValue)
case DayStatus.Water.rawValue:
sqlManager.updateDayWaterById(id: dateFormatter.string(from: datePicker.date), val: changeValue)
case DayStatus.Exercise.rawValue:
sqlManager.updateDayExerciseById(id: dateFormatter.string(from: datePicker.date), val: changeValue)
default:
break
}
}
SQLiteManager.swift內容加入對應功能
func updateDayMoodById(id: String, val: Int) {
do {
let item = TB_DAY.filter(TB_DAY_WORK_ID == id)
if try database.run(item.update(TB_DAY_MOOD <- TB_DAY_MOOD + val)) > 0 {
print("update day")
}
} catch {
}
}
func updateDayWaterById(id: String, val: Int) {
do {
let item = TB_DAY.filter(TB_DAY_WORK_ID == id)
if try database.run(item.update(TB_DAY_WATER <- TB_DAY_WATER + val)) > 0 {
print("update day")
}
} catch {
}
}
func updateDayExerciseById(id: String, val: Int) {
do {
let item = TB_DAY.filter(TB_DAY_WORK_ID == id)
if try database.run(item.update(TB_DAY_EXERCISE <- TB_DAY_EXERCISE + val)) > 0 {
print("update day")
}
} catch {
}
}
這樣就完成了喔
再來,我們有異動了今天的心情,再切換至別天、或切換到今天時,因為心情欄位的值不同,就應該有不同的顯示內容,包含其他水、運動、Note等欄位,所以我們要來調整昨天的changeDate(),在此之前,我們先加入一個swift 檔案,建立Day的Struct,方便資料庫查出的資料轉型(擔心之後檔案越來越多會亂,所以放在Model的Group內)
struct Day {
var work_id: String
var mood: Int
var water: Int
var exercise: Int
var note: String?
}
於SQLiteManager.swift加入查詢功能
func queryDayById(id: String) -> Array<Day> {
var dayList:[Day] = [Day]()
do {
for result in Array(try database.prepare(TB_DAY.filter(TB_DAY_WORK_ID == id))) {
// 查詢出的資料轉型成Day Struct後存入陣列中
dayList.append(Day(work_id: result[TB_DAY_WORK_ID], mood: result[TB_DAY_MOOD], water: result[TB_DAY_WATER], exercise: result[TB_DAY_EXERCISE], note: result[TB_DAY_NOTE] ?? ""))
}
} catch {
}
return dayList
}
修改changeDate()
@objc func changeDate() {
dateText.text = showDateFormatter.string(from: datePicker.date)
// 以輸入日期查出該天資料
let dayArray = sqlManager.queryDayById(id: dateFormatter.string(from: datePicker.date))
if dayArray.count > 0 {
// 如果有該天的資料,將該天的資料帶出顯示
let day = dayArray[0]
for i in 0...moodList.count - 1 {
if day.mood > i {
moodList[i].isSelected = true
} else {
moodList[i].isSelected = false
}
}
for i in 0...waterList.count - 1 {
if day.water > i {
waterList[i].isSelected = true
} else {
waterList[i].isSelected = false
}
}
for i in 0...exerciseList.count - 1 {
if day.exercise > i {
exerciseList[i].isSelected = true
} else {
exerciseList[i].isSelected = false
}
}
// 顯示該天Note資料
noteText.text = day.note
} else {
// 如果沒有,就新增一筆
sqlManager.insertDay(work_id: dateFormatter.string(from: datePicker.date))
// 預設按鈕皆為未選取
for mood in moodList {
mood.isSelected = false
}
for water in waterList {
water.isSelected = false
}
for exercise in exerciseList {
exercise.isSelected = false
}
// 預設筆記為空
noteText.text = ""
}
view.endEditing(true)
}
接著測試看看~沒問題就可以啦~
明天來處理中間UITableView的部分,也就是我們TB_EVENT的資料