-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBMHomeSectionListView.swift
More file actions
119 lines (108 loc) · 3.9 KB
/
BMHomeSectionListView.swift
File metadata and controls
119 lines (108 loc) · 3.9 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
//
// BMHomeSectionListView.swift
// berkeley-mobile
//
// Created by Justin Wong on 6/6/25.
// Copyright © 2025 ASUC OCTO. All rights reserved.
//
import SwiftUI
import CoreLocation
struct BMHomeSectionListView: View {
var sectionType: HomeDrawerViewType
var items: [SearchItem & HasLocation & HasImage]
var mapViewController: MapViewController
var selectionHandler: ((SearchItem & HasLocation & HasImage) -> Void)?
// Sort items by proximity to user (ascending)
// utilizes the CLLocation.distanceFromUser() helper
// items without lat/lon sent to the bottom
private var sortedItems: [SearchItem & HasLocation & HasImage] {
items.sorted { a, b in
let d1 = a.distanceToUser ?? .greatestFiniteMagnitude
let d2 = b.distanceToUser ?? .greatestFiniteMagnitude
return d1 < d2
}
}
var body: some View {
VStack {
if #unavailable(iOS 26.0) {
sectionHeaderView
}
if #available(iOS 17.0, *) {
listView
.contentMargins(.top, 0)
.contentMargins([.leading, .trailing], 5)
} else {
listView
}
}
.padding()
.background(Color(BMColor.cardBackground))
.clipShape(RoundedRectangle(cornerRadius: 10))
}
private var sectionHeaderView: some View {
HStack(spacing: 10) {
Image(systemName: sectionType.getSectionInfo().systemName)
Text(sectionType.getSectionInfo().title)
Spacer()
Text("\(items.count)")
.font(Font(BMFont.bold(15)))
.addBadgeStyle(widthAndHeight: 30, isInteractive: false)
}
.font(Font(BMFont.bold(20)))
}
// items -> sortedItems in both forEach calls
// displays all items as tappable rows
@ViewBuilder
private var listView: some View {
if #available(iOS 26.0, *) {
List {
Section {
ForEach(sortedItems, id: \.name) { item in
Button {
mapViewController.choosePlacemark(item: item)
selectionHandler?(item)
} label: {
HomeSectionListRowView(rowItem: item)
.frame(width: 290)
}
.listRowSeparator(.hidden)
.listRowBackground(Color(BMColor.cardBackground))
}
} header: {
sectionHeaderView
}
}
.listStyle(.plain)
.scrollContentBackground(.hidden)
.clipShape(RoundedRectangle(cornerRadius: 10))
} else {
List(sortedItems, id: \.name) { item in
Button {
mapViewController.choosePlacemark(item: item)
selectionHandler?(item)
} label: {
HomeSectionListRowView(rowItem: item)
.frame(width: 290)
}
.listRowSeparator(.hidden)
.listRowBackground(Color(BMColor.cardBackground))
}
.scrollContentBackground(.hidden)
}
}
}
#Preview {
let homeViewModel = HomeViewModel()
let diningHalls = [
BMDiningHall(
name: "Cafe 3",
address: "2436 Durant Ave, Berkeley, CA 94704",
phoneNumber: nil,
imageLink: "https://firebasestorage.googleapis.com/v0/b/berkeley-mobile.appspot.com/o/images%2FCafe3.jpg?alt=media&token=f1062476-2cb0-4ce9-9ac1-6109bf588aaa",
hours: [],
latitude: 37.8688,
longitude: -122.2590
)
]
BMHomeSectionListView(sectionType: .dining, items: diningHalls, mapViewController: MapViewController())
}