Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
SwiftUI redesign menu view (#412)
Browse files Browse the repository at this point in the history
* Remove unused function

* Redesign the menu view of the cafeteria and remove one layer of views

* Fix: don't add duplicate menu days
  • Loading branch information
Philipp000 authored Feb 10, 2022
1 parent 0fa1803 commit 4059d2d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Campus-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@
children = (
36108BD227A304B5007DC62D /* MealPlanView.swift */,
36108BD327A304B5007DC62D /* MealPlanViewModel.swift */,
36108BD427A304B5007DC62D /* MenuViewModel.swift */,
36108BD627A304B5007DC62D /* MenuView.swift */,
36108BD427A304B5007DC62D /* MenuViewModel.swift */,
);
path = Cafeterias;
sourceTree = "<group>";
Expand Down
56 changes: 43 additions & 13 deletions Campus-iOS/Map/Cafeterias/MealPlanView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,55 @@ import Alamofire

struct MealPlanView: View {
@ObservedObject var viewModel: MealPlanViewModel

let formatter = DateFormatter()

var body: some View {
VStack {
List {
ForEach(viewModel.menus) { menu in
let title = formatter.string(from: menu.date)
NavigationLink(destination: MenuView(viewModel: menu, title: title)) {
Text(formatter.string(from: menu.date))
HStack{
if viewModel.menus.count > 0{
VStack{
HStack{
ForEach(viewModel.menus.prefix(7), id: \.id){ menu in
Button(action: {
viewModel.selectedMenu = menu
}){
VStack{
Circle()
.fill(menu === viewModel.selectedMenu ? Color("tumBlue") : Color.clear)
.aspectRatio(contentMode: .fit)
.overlay(
Text(getFormattedDate(date: menu.date, format: "d"))
.fontWeight(.semibold).fixedSize()
.foregroundColor(menu === viewModel.selectedMenu ? Color.white : Color.black)
)
.frame(maxWidth: .infinity)

Text(getFormattedDate(date: menu.date, format: "EEE"))
.foregroundColor(Color.black)
}
}
}
}
.padding(.horizontal, 5.0)

if let menu = viewModel.selectedMenu {
MenuView(viewModel: menu)
}else{
Spacer()
}
}
}
.navigationTitle(viewModel.title)
.onAppear {
formatter.dateFormat = "EEEE, dd.MM.yyyy"
viewModel.fetch()
}else {
Text("Kein Menü")
}
}
.navigationTitle(viewModel.title)
.onAppear {
viewModel.fetch()
}
}

func getFormattedDate(date: Date, format: String) -> String{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.string(from: date)
}
}

Expand Down
67 changes: 32 additions & 35 deletions Campus-iOS/Map/Cafeterias/MealPlanViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class MealPlanViewModel: ObservableObject {

@Published private(set) var title: String
@Published private(set) var menus: [MenuViewModel] = []
@Published var selectedMenu: MenuViewModel?

init(cafeteria: Cafeteria) {
self.cafeteria = cafeteria
Expand All @@ -32,22 +33,10 @@ final class MealPlanViewModel: ObservableObject {

sessionManager.request(thisWeekEndpoint).responseDecodable(of: MealPlan.self, decoder: decoder) { [self] response in
guard let mealPlans = response.value else { return }
self.menus = mealPlans.days
.filter { !$0.dishes.isEmpty && ($0.date.isToday || $0.date.isLaterThanOrEqual(to: Date())) }
.sorted { $0.date < $1.date }
.map {
let categories = $0.dishes
.sorted { $0.dishType < $1.dishType }
.reduce(into: [:]) { (acc: inout [String: [Dish]], dish: Dish) -> () in
let type = dish.dishType.isEmpty ? "Sonstige" : dish.dishType
if acc[type] != nil {
acc[type]?.append(dish)
}
acc[type] = [dish]
}
.map { CategoryViewModel(name: $0.key, dishes: $0.value) }

return MenuViewModel(title: formatter.string(from: $0.date), date: $0.date, categories: categories) }
addMealPlans(mealPlans: mealPlans)
if self.menus.count > 0 {
selectedMenu = self.menus[0]
}
}

guard let nextWeek = Calendar.current.date(byAdding: .weekOfYear, value: 1, to: Date()) else { return }
Expand All @@ -56,28 +45,36 @@ final class MealPlanViewModel: ObservableObject {

sessionManager.request(nextWeekEndpoint).responseDecodable(of: MealPlan.self, decoder: decoder) { [self] response in
guard let mealPlans = response.value else { return }
self.menus.append(contentsOf: mealPlans.days
.filter { !$0.dishes.isEmpty && ($0.date.isToday || $0.date.isLaterThanOrEqual(to: Date())) }
.sorted { $0.date < $1.date }
.map {
let categories = $0.dishes
.sorted { $0.dishType < $1.dishType }
.reduce(into: [:]) { (acc: inout [String: [Dish]], dish: Dish) -> () in
let type = dish.dishType.isEmpty ? "Sonstige" : dish.dishType
if acc[type] != nil {
acc[type]?.append(dish)
}
acc[type] = [dish]
}
.map { CategoryViewModel(name: $0.key, dishes: $0.value) }

print("DATE: ", $0.date)

return MenuViewModel(title: formatter.string(from: $0.date), date: $0.date, categories: categories) }
)
addMealPlans(mealPlans: mealPlans)
}

// initiate loading of labels here, to prevent showing of placeholders
_ = MensaEnumService.shared.getLabels()
}

func addMealPlans(mealPlans: MealPlan){
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"

self.menus.append(contentsOf: mealPlans.days
.filter { !$0.dishes.isEmpty && ($0.date.isToday || $0.date.isLaterThanOrEqual(to: Date())) }
.sorted { $0.date < $1.date }
.map {
let categories = $0.dishes
.sorted { $0.dishType < $1.dishType }
.reduce(into: [:]) { (acc: inout [String: [Dish]], dish: Dish) -> () in
let type = dish.dishType.isEmpty ? "Sonstige" : dish.dishType
if acc[type] != nil {
acc[type]?.append(dish)
}
acc[type] = [dish]
}
.map { MenuCategory(name: $0.key, dishes: $0.value) }

return MenuViewModel(date: $0.date, categories: categories) }
.filter{ menu in !self.menus.contains(where: {$0.date == menu.date}) } // don't re-add already existent days
)

self.menus = self.menus.sorted { $0.date < $1.date }
}
}
2 changes: 0 additions & 2 deletions Campus-iOS/Map/Cafeterias/MenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import SwiftUI

struct MenuView: View {
@ObservedObject var viewModel: MenuViewModel
@State var title: String

var body: some View {
List {
Expand All @@ -21,7 +20,6 @@ struct MenuView: View {
}
}
}
.navigationTitle(title)
}
}

Expand Down
18 changes: 8 additions & 10 deletions Campus-iOS/Map/Cafeterias/MenuViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,22 @@ import SwiftUI

final class MenuViewModel: ObservableObject, Identifiable {
let id = UUID()
let title: String
let date: Date
@Published var categories: [CategoryViewModel]
@Published var categories: [MenuCategory]

init(title: String, date: Date, categories: [CategoryViewModel]) {
self.title = title
init(date: Date, categories: [MenuCategory]) {
self.date = date
self.categories = categories
}
}

struct CategoryViewModel: Identifiable {
var id = UUID()
var name: String
var dishes: [Dish]
struct MenuCategory: Identifiable {
let id = UUID()
let name: String
let dishes: [Dish]

init(name: String, dishes: [Dish]) {
self.name = name
self.dishes = dishes
}
}
}
11 changes: 0 additions & 11 deletions Campus-iOS/Map/MainView/PanelContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,6 @@ struct PanelContent: View {
}
}
}

private func fetch() {
sessionManager.request(endpoint).responseDecodable(of: [Cafeteria].self, decoder: JSONDecoder()) { [self] response in
var cafeterias: [Cafeteria] = response.value ?? []
if let currentLocation = self.locationManager.location {
cafeterias.sortByDistance(to: currentLocation)
}

self.canteens = cafeterias
}
}
}

struct SearchBar: View {
Expand Down

0 comments on commit 4059d2d

Please sign in to comment.