Commit 0db705cded
Verified · cmc
Layout: unified · split
Sources/Orgstar/AgendaView.swift +35 −9
| @@ -229,11 +229,20 @@ struct AgendaView: View { | |||
| 229 | 229 | ||
| 230 | // MARK: - Lines | 230 | // MARK: - Lines |
| 231 | 231 | ||
| 232 | /// A selectable line: its selection id, the row, and the day it is on. | 232 | /// A selectable line: its selection id, the row, and the day it is on. `key`, the view's |
| 233 | /// identity, follows the row where `id` follows its place; nil uses `id`. | ||
| 233 | struct Line { | 234 | struct Line { |
| 234 | let id: String | 235 | let id: String |
| 235 | let row: AgendaRow | 236 | let row: AgendaRow |
| 236 | let day: Int | 237 | let day: Int |
| 238 | var key: String? | ||
| 239 | |||
| 240 | var viewID: String { key ?? id } | ||
| 241 | } | ||
| 242 | |||
| 243 | /// A day row's view identity. | ||
| 244 | static func key(_ row: AgendaRow, day: Int, prefix: String, overdue: Bool = false) -> String { | ||
| 245 | "\(prefix)\(day):\(overdue ? "o" : "")\(row.id)" | ||
| 237 | } | 246 | } |
| 238 | 247 | ||
| 239 | private var groups: [AgendaDayGroup] { | 248 | private var groups: [AgendaDayGroup] { |
| @@ -260,11 +269,19 @@ struct AgendaView: View { | |||
| 260 | 269 | ||
| 261 | private func dayLines(_ groups: [AgendaDayGroup], prefix: String) -> [Line] { | 270 | private func dayLines(_ groups: [AgendaDayGroup], prefix: String) -> [Line] { |
| 262 | groups.flatMap { group in | 271 | groups.flatMap { group in |
| 263 | (overdueShown ? group.overdue.enumerated().map { Line(id: "\(prefix)\(group.day):o\($0.offset)", row: $0.element, day: group.day) } : []) | 272 | (overdueShown ? group.overdue.enumerated().map { dayLine(group, $0.offset, prefix: prefix, overdue: true) } : []) |
| 264 | + group.rows.enumerated().map { Line(id: "\(prefix)\(group.day):\($0.offset)", row: $0.element, day: group.day) } | 273 | + group.rows.indices.map { dayLine(group, $0, prefix: prefix) } |
| 265 | } | 274 | } |
| 266 | } | 275 | } |
| 267 | 276 | ||
| 277 | private func dayLine(_ group: AgendaDayGroup, _ index: Int, prefix: String, overdue: Bool = false) -> Line { | ||
| 278 | let row = overdue ? group.overdue[index] : group.rows[index] | ||
| 279 | return Line( | ||
| 280 | id: "\(prefix)\(group.day):\(overdue ? "o" : "")\(index)", row: row, day: group.day, | ||
| 281 | key: Self.key(row, day: group.day, prefix: prefix, overdue: overdue) | ||
| 282 | ) | ||
| 283 | } | ||
| 284 | |||
| 268 | func line(_ id: String) -> Line? { lines(groups).first { $0.id == id } } | 285 | func line(_ id: String) -> Line? { lines(groups).first { $0.id == id } } |
| 269 | 286 | ||
| 270 | var body: some View { | 287 | var body: some View { |
| @@ -311,7 +328,7 @@ struct AgendaView: View { | |||
| 311 | .padding(.bottom, 12) | 328 | .padding(.bottom, 12) |
| 312 | } | 329 | } |
| 313 | .onChange(of: selection) { | 330 | .onChange(of: selection) { |
| 314 | if let selection { proxy.scrollTo(selection) } | 331 | if let selection { proxy.scrollTo(line(selection)?.viewID ?? selection) } |
| 315 | } | 332 | } |
| 316 | .onChange(of: agenda.days.first?.day) { | 333 | .onChange(of: agenda.days.first?.day) { |
| 317 | if groups.contains(where: \.isToday) { proxy.scrollTo("day\(agenda.today)", anchor: .top) } | 334 | if groups.contains(where: \.isToday) { proxy.scrollTo("day\(agenda.today)", anchor: .top) } |
| @@ -420,8 +437,8 @@ struct AgendaView: View { | |||
| 420 | if group.isToday, !group.overdue.isEmpty { | 437 | if group.isToday, !group.overdue.isEmpty { |
| 421 | overdueHeader(count: group.overdue.count) | 438 | overdueHeader(count: group.overdue.count) |
| 422 | if overdueShown { | 439 | if overdueShown { |
| 423 | ForEach(Array(group.overdue.enumerated()), id: \.element.id) { index, row in | 440 | ForEach(Array(group.overdue.enumerated()), id: \.element.id) { index, _ in |
| 424 | rowView(Line(id: "\(prefix)\(group.day):o\(index)", row: row, day: group.day)) | 441 | rowView(dayLine(group, index, prefix: prefix, overdue: true)) |
| 425 | } | 442 | } |
| 426 | Divider().padding(.leading, 16).padding(.vertical, 4) | 443 | Divider().padding(.leading, 16).padding(.vertical, 4) |
| 427 | } | 444 | } |
| @@ -433,9 +450,18 @@ struct AgendaView: View { | |||
| 433 | .padding(.horizontal, 16) | 450 | .padding(.horizontal, 16) |
| 434 | .padding(.vertical, 10) | 451 | .padding(.vertical, 10) |
| 435 | } | 452 | } |
| 436 | ForEach(Array(group.lines(now: Self.hhmm(now)).enumerated()), id: \.offset) { _, line in | 453 | // Identified by row, not place, so a refresh that moves rows doesn't reuse one row's view |
| 454 | // for another. | ||
| 455 | let lines = group.lines(now: Self.hhmm(now)).map { line in | ||
| 456 | switch line { | ||
| 457 | case .row(let index): (Self.key(group.rows[index], day: group.day, prefix: prefix), line) | ||
| 458 | case .grid(let time): ("grid\(time)", line) | ||
| 459 | case .now: ("now", line) | ||
| 460 | } | ||
| 461 | } | ||
| 462 | ForEach(lines, id: \.0) { _, line in | ||
| 437 | switch line { | 463 | switch line { |
| 438 | case .row(let index): rowView(Line(id: "\(prefix)\(group.day):\(index)", row: group.rows[index], day: group.day)) | 464 | case .row(let index): rowView(dayLine(group, index, prefix: prefix)) |
| 439 | case .grid(let time): gridLine(time) | 465 | case .grid(let time): gridLine(time) |
| 440 | case .now: nowLine | 466 | case .now: nowLine |
| 441 | } | 467 | } |
| @@ -540,7 +566,7 @@ struct AgendaView: View { | |||
| 540 | row: line.row, day: line.day, showsGutter: showsGutter, selected: selection == line.id, hovered: hovered == line.id, | 566 | row: line.row, day: line.day, showsGutter: showsGutter, selected: selection == line.id, hovered: hovered == line.id, |
| 541 | marked: line.row.item.flatMap(EntryRef.init).map(marked.contains) ?? false | 567 | marked: line.row.item.flatMap(EntryRef.init).map(marked.contains) ?? false |
| 542 | ) | 568 | ) |
| 543 | .id(line.id) | 569 | .id(line.viewID) |
| 544 | .contentShape(Rectangle()) | 570 | .contentShape(Rectangle()) |
| 545 | .onHover { inside in | 571 | .onHover { inside in |
| 546 | if inside { hovered = line.id } else if hovered == line.id { hovered = nil } | 572 | if inside { hovered = line.id } else if hovered == line.id { hovered = nil } |