Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BER-78: Replace Home Drawer Section Table Cells With HomeSectionListRowView #338

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 41 additions & 15 deletions berkeley-mobile/Common/FilterTableView/HomeSectionListRowView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,42 @@

import SwiftUI

class HomeSectionListRowViewModel: ObservableObject {
@Published var title = ""
@Published var distance = 0.0
@Published var image = UIImage(imageLiteralResourceName: "DoeGlade")

func configureRow(with rowItem: SearchItem & HasLocation & HasImage) {
withAnimation {
title = rowItem.searchName
distance = rowItem.distanceToUser ?? 0.0
fetchImage(for: rowItem)
}
}

private func fetchImage(for itemWithImage: HasImage) {
if let itemImage = itemWithImage.image {
image = itemImage
} else if let url = itemWithImage.imageURL {
ImageLoader.shared.getImage(url: url) { result in
switch result {
case .success(let image):
self.image = image
default:
break
}
}
}
}
}

struct HomeSectionListRowView: View {
var title: String
var distance: Double
var image: UIImage
@EnvironmentObject var viewModel: HomeSectionListRowViewModel

var body: some View {
HStack {
VStack(alignment: .leading) {
Text(title)
Text(viewModel.title)
.foregroundStyle(Color(BMColor.blackText))
.font(Font(BMFont.bold(18)))
Spacer()
Expand All @@ -28,27 +55,22 @@ struct HomeSectionListRowView: View {

imageView
}
.padding(.horizontal, 16)
.frame(height: 100)
.background(Color(BMColor.modalBackground))
.clipShape(.rect(cornerRadius: 12))
.shadow(color: .black.opacity(0.25), radius: 5)
}

private var distanceLabelView: some View {
HStack {
Image(systemName: "figure.walk")
.foregroundStyle(Color(BMColor.blackText))
.font(.system(size: 16))
.font(.system(size: 14))

Text("\(distance, specifier: "%.1f") mi")
Text("\(viewModel.distance, specifier: "%.1f") mi")
.foregroundStyle(Color(BMColor.blackText))
.font(Font(BMFont.regular(16)))
.font(Font(BMFont.light(14)))
}
}

private var imageView: some View {
Image(uiImage: image)
Image(uiImage: viewModel.image)
.resizable()
.scaledToFill()
.frame(width: 80, height: 80)
Expand All @@ -57,7 +79,11 @@ struct HomeSectionListRowView: View {
}

#Preview {
let defaultImage = UIImage(named: "DoeGlade")!
HomeSectionListRowView(title: "Albany Bulb", distance: 8.4, image: defaultImage)
let viewModel = HomeSectionListRowViewModel()
let foothillDiningHall = DiningHall(name: "Foothill", address: nil, phoneNumber: nil, imageLink: "https://firebasestorage.googleapis.com/v0/b/berkeley-mobile.appspot.com/o/images%2FFoothill.jpg?alt=media&token=b645d675-6f51-45ea-99f7-9b36576e14b7", shifts: MealMap(), hours: nil, latitude: 37.87538, longitude: -122.25612109999999)
viewModel.configureRow(with: foothillDiningHall)

return HomeSectionListRowView()
.environmentObject(viewModel)
.padding(40)
}
20 changes: 11 additions & 9 deletions berkeley-mobile/Dining/DiningViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class DiningViewController: UIViewController, SearchDrawerViewDelegate {
object: nil
)

// fetch dining hall and cafe data, make sure occupancy data has been fetched after each one is complete
// Fetch dining hall and cafe data, make sure occupancy data has been fetched after each one is complete
DataManager.shared.fetch(source: DiningHallDataSource.self) { diningLocations in
self.diningLocations.append(contentsOf: diningLocations as? [DiningLocation] ?? [])
self.filterTableView.setData(data: self.diningLocations)
Expand All @@ -99,13 +99,17 @@ class DiningViewController: UIViewController, SearchDrawerViewDelegate {

extension DiningViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: FilterTableViewCell.kCellIdentifier, for: indexPath) as? FilterTableViewCell {
if let diningHall: DiningLocation = self.filterTableView.filteredData[safe: indexPath.row] {
cell.updateContents(item: diningHall)
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: FilterTableViewCell.kCellIdentifier, for: indexPath)
let diningHall = self.filterTableView.filteredData[indexPath.row]

let cellViewModel = HomeSectionListRowViewModel()
cellViewModel.configureRow(with: diningHall)

cell.contentConfiguration = UIHostingConfiguration {
HomeSectionListRowView()
.environmentObject(cellViewModel)
}
return UITableViewCell()
return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
Expand All @@ -122,7 +126,6 @@ extension DiningViewController: UITableViewDelegate, UITableViewDataSource {
// MARK: View

extension DiningViewController {
// General card page
func setupCardView() {
let card = CardView()
card.layoutMargins = UIEdgeInsets(top: 20, left: 16, bottom: 16, right: 16)
Expand Down Expand Up @@ -164,7 +167,6 @@ extension DiningViewController {
headerLabel = header
}

// Table of dining locations
func setupTableView() {
let functions: [TableFunction] = [
Sort<DiningLocation>(label: "Nearby", sort: DiningHall.locationComparator()),
Expand Down
17 changes: 11 additions & 6 deletions berkeley-mobile/Fitness/Fitness+Controllers/GymsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation
import UIKit
import SwiftUI

/**
Helper to handle Data Source protocols for collections with Gyms.
Expand All @@ -32,13 +33,17 @@ extension GymsController: UITableViewDataSource, UITableViewDelegate {
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: FilterTableViewCell.kCellIdentifier, for: indexPath) as? FilterTableViewCell {
if let gym: Gym = vc.filterTableView.filteredData[safe: indexPath.row] {
cell.updateContents(item: gym)
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: FilterTableViewCell.kCellIdentifier, for: indexPath)
let gym = vc.filterTableView.filteredData[indexPath.row]

let cellViewModel = HomeSectionListRowViewModel()
cellViewModel.configureRow(with: gym)

cell.contentConfiguration = UIHostingConfiguration {
HomeSectionListRowView()
.environmentObject(cellViewModel)
}
return UITableViewCell()
return cell
}

}
3 changes: 2 additions & 1 deletion berkeley-mobile/Fitness/FitnessViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class FitnessViewController: UIViewController, SearchDrawerViewDelegate {
var initialDrawerCenter = CGPoint()
var initialGestureTranslation: CGPoint = CGPoint()
var drawerStatePositions: [DrawerState : CGFloat] = [:]

// SearchDrawerViewDelegate property
var mainContainer: MainContainerViewController?
var pinDelegate: SearchResultsViewDelegate?
Expand Down Expand Up @@ -96,7 +97,7 @@ class FitnessViewController: UIViewController, SearchDrawerViewDelegate {

gymsController.vc = self

// fetch gyms and fetch occupancy data afterwards
// Fetch gyms and fetch occupancy data afterwards
DataManager.shared.fetch(source: GymDataSource.self) { gyms in
self.gyms = gyms as? [Gym] ?? []
self.filterTableView.setData(data: gyms as! [Gym])
Expand Down
16 changes: 10 additions & 6 deletions berkeley-mobile/Libraries/StudyViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,16 @@ class StudyViewController: UIViewController, UITableViewDataSource, UITableViewD
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: FilterTableViewCell.kCellIdentifier, for: indexPath) as? FilterTableViewCell {
if let lib: Library = self.filterTableView.filteredData[safe: indexPath.row] {
cell.updateContents(item: lib)
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: FilterTableViewCell.kCellIdentifier, for: indexPath)
let library = self.filterTableView.filteredData[indexPath.row]

let cellViewModel = HomeSectionListRowViewModel()
cellViewModel.configureRow(with: library)

cell.contentConfiguration = UIHostingConfiguration {
HomeSectionListRowView()
.environmentObject(cellViewModel)
}
return UITableViewCell()
return cell
}
}