Skip to content

Commit e65198e

Browse files
committed
Use toggles for calendars selection
1 parent 3a6a431 commit e65198e

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

MeetingBar/Views/Preferences/CalendarsTab.swift

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,16 @@ struct CalendarsTab: View {
2929
Spacer()
3030
}
3131
VStack(spacing: 15) {
32-
Form {
33-
List {
34-
ForEach(Array(self.calendarsBySource.keys), id: \.self) { source in
35-
Section(header: Text(source)) {
36-
ForEach(self.calendarsBySource[source]!, id: \.ID) { calendar in
37-
CalendarRow(title: calendar.title, isSelected: self.selectedCalendarIDs.contains(calendar.ID), color: Color(calendar.color)) {
38-
if self.selectedCalendarIDs.contains(calendar.ID) {
39-
self.selectedCalendarIDs.removeAll { $0 == calendar.ID }
40-
} else {
41-
self.selectedCalendarIDs.append(calendar.ID)
42-
}
43-
}
44-
}
32+
List {
33+
ForEach(Array(self.calendarsBySource.keys), id: \.self) { source in
34+
Section(header: Text(source)) {
35+
ForEach(self.calendarsBySource[source]!, id: \.ID) { calendar in
36+
CalendarRow(calendar: calendar)
4537
}
4638
}
47-
}.listStyle(SidebarListStyle())
48-
}
49-
}.border(Color.gray)
39+
}
40+
}.listStyle(SidebarListStyle())
41+
}
5042
Divider()
5143

5244
VStack(alignment: .leading) {
@@ -75,9 +67,9 @@ struct CalendarsTab: View {
7567
}
7668
}.padding(.horizontal, 10)
7769
}
78-
}.onReceive(timer) { _ in loadCalendarList() }
70+
}.padding()
71+
.onReceive(timer) { _ in loadCalendarList() }
7972
.onDisappear { timer.upstream.connect().cancel() }
80-
.padding()
8173
}
8274

8375
func changeEventStoreProvider(_ provider: EventStoreProvider) {
@@ -121,24 +113,31 @@ struct AddAccountModal: View {
121113
}
122114

123115
struct CalendarRow: View {
124-
var title: String
125-
var isSelected: Bool
126-
var color: Color
127-
var action: () -> Void
116+
var calendar: MBCalendar
117+
@State var isSelected: Bool
118+
119+
init(calendar: MBCalendar) {
120+
self.calendar = calendar
121+
isSelected = Defaults[.selectedCalendarIDs].contains(calendar.ID)
122+
}
128123

129124
var body: some View {
130-
HStack {
131-
Button(action: self.action) {
132-
Section {
133-
if self.isSelected {
134-
Image(nsImage: NSImage(named: NSImage.menuOnStateTemplateName)!)
135-
} else {
136-
Image(nsImage: NSImage(named: NSImage.addTemplateName)!)
137-
}
138-
}.frame(width: 20, height: 17)
125+
Toggle(isOn: $isSelected) {
126+
HStack {
127+
Text("")
128+
Circle().fill(Color(calendar.color)).frame(width: 10, height: 10)
129+
Text(calendar.title)
139130
}
140-
Circle().fill(self.color).frame(width: 8, height: 8)
141-
Text(self.title)
142-
}.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
131+
}
132+
.onAppear {
133+
isSelected = Defaults[.selectedCalendarIDs].contains(calendar.ID)
134+
}
135+
.onReceive([self.isSelected].publisher.first()) { newValue in
136+
if newValue {
137+
Defaults[.selectedCalendarIDs].append(calendar.ID)
138+
} else {
139+
Defaults[.selectedCalendarIDs].removeAll { $0 == calendar.ID }
140+
}
141+
}
143142
}
144143
}

0 commit comments

Comments
 (0)