iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 15
0
Software Development

利用Swift 4開發iOS App,Daily Work List系列 第 15

Day 15. Develop Day Page View Controller 2

  • 分享至 

  • xImage
  •  

這篇Part 2.我們來處理上面那15的按鈕和資料的讀取寫入吧

UIButton Collections Setting & Update by SQLite

因為這是五五五個一組,分為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的時候初始化他們

  • 預設的被選取都是false
  • 設定被選取後的圖片
  • 加入action觸及事件,但因三種按鈕的觸及事件相同,所以都指向clickButtonList
  • 利用tag屬性紀錄其對應欄位,在clickButtonList的時候可以區分來源
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 {
    }
}

這樣就完成了喔

Query Data to Show on View

再來,我們有異動了今天的心情,再切換至別天、或切換到今天時,因為心情欄位的值不同,就應該有不同的顯示內容,包含其他水、運動、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?
}

https://ithelp.ithome.com.tw/upload/images/20181015/20111916W3wiBazWLC.png

於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的資料


上一篇
Day 14. Develop Day Page View Controller 1
下一篇
Day 16. Develop Day Page View Controller 3
系列文
利用Swift 4開發iOS App,Daily Work List31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言