iT邦幫忙

2025 iThome 鐵人賽

DAY 15
0
Mobile Development

Xcode x Swift Vibe coding進階開發之旅系列 第 15

Xcode x Swift Vibe coding進階開發之旅 第十五天 點名系統(8) 增加時間限定

  • 分享至 

  • xImage
  •  

說明

增加時間限定可以減少重複簽到的問題以及減少記錄的資料數量/images/emoticon/emoticon08.gif

指令

我需要給人員資料庫增加一個欄位 紀錄最後一次簽到的時間 增加限制上次簽到時間與當前時間大於12小時才呢以簽到

心得

這次遇到少數需要修正的bug並且是在語法上的錯誤,如果是從未學過程式的人因該會無從入手我只能人工修正因為大小寫混用倒置的出錯. 且AI會使用階段進行的方式執行,如果AI做到你不想出現的東西時隨時可以暫停
https://ithelp.ithome.com.tw/upload/images/20250929/20168187ro2p3oH8uz.png

結果

https://ithelp.ithome.com.tw/upload/images/20250929/20168187Zfvux7jF5J.png

程式

//model.swift
 @objc dynamic var lastCheckInTime: Date? = nil
 // 執行簽到並寫入資料庫
    private func performCheckIn(for user: User, note: String?) {
        let realm = try! Realm()
        
        // 檢查上次簽到時間,判斷是否允許再次簽到
        if let lastCheckInTime = user.lastCheckInTime {
            // 計算上次簽到時間與當前時間的時間差(小時)
            let timeInterval = Date().timeIntervalSince(lastCheckInTime) / 3600
            
            // 如果時間差小於 12 小時,則不允許簽到
            if timeInterval < 12 {
                // 計算還需等待的時間
                let hoursLeft = 12 - timeInterval
                let minutesLeft = Int((hoursLeft - Double(Int(hoursLeft))) * 60)
                
                let message = String(format: "距離上次簽到時間不足 12 小時,還需等待 %d 小時 %d 分鐘", Int(hoursLeft), minutesLeft)
                showErrorAlert(message: message)
                return
            }
        }
        
        // 建立新的簽到記錄
        let checkInRecord = CheckInRecord(userId: user.userId, note: note)
        
        do {
            try realm.write {
                // 添加簽到記錄
                realm.add(checkInRecord)
                
                // 更新使用者的最後簽到時間
                let userToUpdate = realm.objects(User.self).filter("userId == %@", user.userId).first
                userToUpdate?.lastCheckInTime = checkInRecord.checkInTime
            }
            
            // 顯示簽到成功訊息
            showSuccessAlert(message: "\(user.Name) 簽到成功!時間:\(formatDate(checkInRecord.checkInTime))")
        } catch {
            showErrorAlert(message: "簽到失敗:\(error.localizedDescription)")
        }
    }

/images/emoticon/emoticon06.gif


上一篇
Xcode x Swift Vibe coding進階開發之旅 第十四天 點名系統(7) 記錄頁面與刪除功能
下一篇
Xcode x Swift Vibe coding進階開發之旅 第十六天 點名系統(9) 增加篩選查看簽到紀錄的人員
系列文
Xcode x Swift Vibe coding進階開發之旅16
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言