EditAlarmClockVC.swift
5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// 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 user = UserInfo
user.alarmClocks.remove(at: index)
BluetoothManager.shared.setAlarmClock(user.alarmClocks) {[weak self] error in
if error == nil {
AdminHelper.shared.updateUser(user)
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 user = UserInfo
if isEdit {
user.alarmClocks[index] = alarmClock
} else {
user.alarmClocks.append(alarmClock)
}
BluetoothManager.shared.setAlarmClock(user.alarmClocks) {[weak self] error in
if error == nil {
AdminHelper.shared.updateUser(user)
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")
}
}