EditAlarmClockVC.swift 5.03 KB
//
//  EditAlarmClockVC.swift
//  Twear
//
//  Created by yangbin on 2021/12/24.
//

import UIKit
import SwiftDate

class EditAlarmClockVC: UIViewController {
    
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var collectionViewFlowLayout: UICollectionViewFlowLayout!
    @IBOutlet weak var datePicker: UIDatePicker!
    @IBOutlet weak var RemarkTF: UITextField!
    @IBOutlet weak var deleteBtn: UIButton!
    @IBOutlet weak var sureBtn: UIButton!
    
    private let cycleList = ["不重复", "周一", "周二", "周三", "周四", "周五", "周六", "周日"]
    
    var alarmClock = AlarmClockModel()
    
    
    var cycleArray: [Int] = []
    var index: Int = 0
    var isEdit: Bool = false
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = LocString("编辑闹钟")
        datePicker.locale = .current
        if !isEdit {
            deleteBtn.isHidden = true
            sureBtn.snp.updateConstraints { make in
                make.centerX.equalToSuperview()
            }
        } else {
            datePicker.date = alarmClock.date
        }
        RemarkTF.placeholder = LocString("在此输入标签")
        
        collectionViewFlowLayout.minimumLineSpacing = 0
        collectionViewFlowLayout.minimumInteritemSpacing = 0
        collectionViewFlowLayout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        collectionViewFlowLayout.itemSize = CGSize(width: (SCREEN_WIDTH-46)/4.0, height: 36)
        collectionView.bounces = false
        collectionView.showsVerticalScrollIndicator = false
        
        cycleArray = UInt8(alarmClock.cycle).toAlarmClockCycle()
    }
    

    @IBAction private func deleteAlarmColck(_ sender: Any) {
        self.showAlert(title: LocString("提示"), message: LocString("是否删除此闹钟?")) {[weak self] action in
            self?.delete()
        }
    }
    
    private func delete() {
        let device = CurDevice
        device.alarmClocks.remove(at: index)
        BluetoothManager.shared.setAlarmClock(device.alarmClocks) {[weak self] error in
            if error == nil {
                AdminHelper.shared.updateDevice(device)
                self?.navigationController?.popViewController(animated: true)
            }
        }
    }
    
    @IBAction private func saveAlarmColck(_ sender: Any) {
        let alarmClock = AlarmClockModel()
        alarmClock.isOn = true
        alarmClock.remark = RemarkTF.text ?? ""
        alarmClock.date = DateInRegion(year: 2022, month: 1, day: 1, hour: datePicker.date.hour, minute: datePicker.date.minute).date
        if cycleArray[0] == 1 {
            alarmClock.cycle = 0
        } else {
            var array = cycleArray
            array.remove(at: 0)
            alarmClock.cycle = bitsToInt(array.reversed())
        }
        let device = CurDevice
        if isEdit {
            device.alarmClocks[index] = alarmClock
        } else {
            device.alarmClocks.append(alarmClock)
        }
        BluetoothManager.shared.setAlarmClock(device.alarmClocks) {[weak self] error in
            if error == nil {
                AdminHelper.shared.updateDevice(device)
                self?.navigationController?.popViewController(animated: true)
            }
        }
    }
    
    func bitsToInt(_ bits: [Int]) -> Int {
        let decimalValue = bits.reduce(0) { v, byte in
            return v << 1 | Int(byte)
        }
        print(decimalValue)
        return Int(decimalValue)
    }

    deinit {
        print("deinit\(NSStringFromClass(type(of: self)))!!!!!!!")
    }
}




extension EditAlarmClockVC: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cycleList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AlarmClockCycleCell", for: indexPath) as! AlarmClockCycleCell
//        cell.se.image = UIImage(named: settingDic[settingArray[indexPath.row]]!)
        cell.label.text = LocString(cycleList[indexPath.row])
        cell.tick = cycleArray[indexPath.row] == 1
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as! AlarmClockCycleCell
        cell.tick = !cell.tick
        cycleArray[indexPath.row] = cell.tick ? 1 : 0
        print(cycleArray)
        if cycleArray == [0, 0, 0, 0, 0, 0, 0, 0] {
            cycleArray = [1, 0, 0, 0, 0, 0, 0, 0]
            collectionView.reloadData()
        } else if cell.tick {
            if indexPath.row == 0 {
                cycleArray = [1, 0, 0, 0, 0, 0, 0, 0]
            } else {
                cycleArray[0] = 0
            }
            collectionView.reloadData()
        }
        cell.selectImageView.image = UIImage(named: cell.tick ? "alarmclock_selected" : "alarmclock_no_selected")
    }
    
}