Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions berkeley-mobile/Home/BMHomeSectionListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import SwiftUI
import CoreLocation

struct BMHomeSectionListView: View {
var sectionType: HomeDrawerViewType
Expand All @@ -15,12 +16,23 @@ struct BMHomeSectionListView: View {

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment

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)
Expand All @@ -46,16 +58,18 @@ struct BMHomeSectionListView: View {
.font(Font(BMFont.bold(20)))
}

// items -> sortedItems in both forEach calls
// displays all items as tappable rows
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment

@ViewBuilder
private var listView: some View {
if #available(iOS 26.0, *) {
List {
Section {
ForEach(items, id: \.name) { item in
Button(action: {
ForEach(sortedItems, id: \.name) { item in
Button {
mapViewController.choosePlacemark(item: item)
selectionHandler?(item)
}) {
} label: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for buttons, we typically do something like this:

Button (action: {
}) {

}

What you have is valid, but since label is the last parameter and it is a closure, we can use the trailing closure syntax: https://www.hackingwithswift.com/sixty/6/5/trailing-closure-syntax

HomeSectionListRowView(rowItem: item)
.frame(width: 290)
}
Expand All @@ -70,11 +84,11 @@ struct BMHomeSectionListView: View {
.scrollContentBackground(.hidden)
.clipShape(RoundedRectangle(cornerRadius: 10))
} else {
List(items, id: \.name) { item in
Button(action: {
List(sortedItems, id: \.name) { item in
Button {
mapViewController.choosePlacemark(item: item)
selectionHandler?(item)
}) {
} label: {
HomeSectionListRowView(rowItem: item)
.frame(width: 290)
}
Expand All @@ -89,8 +103,17 @@ struct BMHomeSectionListView: View {
#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: nil, longitude: nil)
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())
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline