This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* MC * Draft some kind of viewmodel (#396) Co-authored-by: Tim Gymnich <tgymnich@icloud.com> * Bug fix for next weeks menu * Menu Prices * Toolbar * - added MenuBtn to Toolbar - added clearBtn to search field * directions functionality in maps/toolbar * - Mealplan List built - fitted to lastest EatAPI update * - Mealplan List built - fitted to lastest EatAPI update * Full MealPlan & MenuViews * MC * Draft some kind of viewmodel (#396) Co-authored-by: Tim Gymnich <tgymnich@icloud.com> * Bug fix for next weeks menu * Menu Prices * Toolbar * resolved merge conflicts map-grades Co-authored-by: Tim Gymnich <tgymnich@icloud.com>
- Loading branch information
Showing
18 changed files
with
792 additions
and
459 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// File.swift | ||
// Campus-iOS | ||
// | ||
// Created by Tim Gymnich on 19.01.22. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
import Alamofire | ||
|
||
final class MealPlanViewModel: ObservableObject { | ||
private let cafeteria: Cafeteria | ||
private let endpoint = EatAPI.canteens | ||
private let sessionManager = Session.defaultSession | ||
|
||
@Published private(set) var title: String | ||
@Published private(set) var menus: [MenuViewModel] = [] | ||
|
||
init(cafeteria: Cafeteria) { | ||
self.cafeteria = cafeteria | ||
self.title = cafeteria.name | ||
} | ||
|
||
func fetch() { | ||
let decoder = JSONDecoder() | ||
let formatter = DateFormatter() | ||
formatter.dateFormat = "yyyy-MM-dd" | ||
decoder.dateDecodingStrategy = .formatted(formatter) | ||
|
||
let thisWeekEndpoint = EatAPI.menu(location: cafeteria.id, year: Date().year, week: Date().weekOfYear) | ||
|
||
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) } | ||
} | ||
|
||
guard let nextWeek = Calendar.current.date(byAdding: .weekOfYear, value: 1, to: Date()) else { return } | ||
|
||
let nextWeekEndpoint = EatAPI.menu(location: cafeteria.id, year: nextWeek.year, week: nextWeek.weekOfYear) | ||
|
||
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) } | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// MealPlanViewModel.swift | ||
// Campus-iOS | ||
// | ||
// Created by Tim Gymnich on 19.01.22. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
|
||
final class MenuViewModel: ObservableObject, Identifiable { | ||
let id = UUID() | ||
let title: String | ||
let date: Date | ||
@Published var categories: [CategoryViewModel] | ||
|
||
init(title: String, date: Date, categories: [CategoryViewModel]) { | ||
self.title = title | ||
self.date = date | ||
self.categories = categories | ||
} | ||
} | ||
|
||
struct CategoryViewModel: Identifiable { | ||
var id = UUID() | ||
var name: String | ||
var dishes: [Dish] | ||
var isExpanded: Bool = false | ||
|
||
init(name: String, dishes: [Dish]) { | ||
self.name = name | ||
self.dishes = dishes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// MealPlanViewModel.swift | ||
// Campus-iOS | ||
// | ||
// Created by Tim Gymnich on 19.01.22. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
|
||
final class MenuViewModel: ObservableObject, Identifiable { | ||
let id = UUID() | ||
let title: String | ||
let date: Date | ||
@Published var categories: [CategoryViewModel] | ||
|
||
init(title: String, date: Date, categories: [CategoryViewModel]) { | ||
self.title = title | ||
self.date = date | ||
self.categories = categories | ||
} | ||
} | ||
|
||
struct CategoryViewModel: Identifiable { | ||
var id = UUID() | ||
var name: String | ||
var dishes: [Dish] | ||
var isExpanded: Bool = false | ||
|
||
init(name: String, dishes: [Dish]) { | ||
self.name = name | ||
self.dishes = dishes | ||
} | ||
} |
Oops, something went wrong.