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

API Refactoring #536

Merged
merged 33 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5acafd3
Start to refactor the API buy generalizing all the API methods into o…
14slash12 Jan 16, 2023
1842c5d
Created for each API an new enum conforming to the new protocol API t…
14slash12 Jan 19, 2023
a7991e0
Refactored Calendar to work with the Asyn-Await-Pattern, the state en…
14slash12 Jan 20, 2023
7d108de
Refactored LecturesSearch to the new API and introduced the asnc-awai…
14slash12 Jan 20, 2023
b8b3bf1
Refactored the API for PersonSearch and added the new async-await pat…
14slash12 Jan 21, 2023
8f33d94
Completely rewritten the ProfileDetailView and its ViewModel to adopt…
14slash12 Jan 21, 2023
aff9f1f
Refactoring the complete Authentication procress to work with the new…
14slash12 Jan 21, 2023
048fa99
Refactored the ProfileViewModel, ProfileView, Model, to work with the…
14slash12 Jan 22, 2023
089e458
Adding the empty profile image placeholder and removing the old TUMOn…
14slash12 Jan 22, 2023
89e6130
Renamed the ProfileView2/ProfileViewModel2 -> to ProfileView/ProfileV…
14slash12 Jan 22, 2023
f3d81d7
Refactoring the News for the API including async-await-pattern, error…
14slash12 Jan 22, 2023
127735e
Renamed the new NewsViewModel, NewsView to the old names
14slash12 Jan 22, 2023
5c28de2
Extracted the new NewsScreen into a own file
14slash12 Jan 22, 2023
7fd7fdd
Refactored the MovieView for the new API, including error-handling, a…
14slash12 Jan 22, 2023
2f4ae88
Refactored the StudyRoomViewModel for fetching the RoomImageMapping. …
14slash12 Jan 22, 2023
8e7687e
Removed the old TUMCabeAPI
14slash12 Jan 22, 2023
f79937b
Refactoring the Cafeterias to work with the new API
14slash12 Jan 23, 2023
9a3343e
Refactored the MealPlanViewModel/View, to work with the new API, addi…
14slash12 Jan 23, 2023
96b9395
Refactored the DishLabels to work with the new API
14slash12 Jan 23, 2023
e1de88b
Refactored the StudyRoomService to work with the new API for the Stud…
14slash12 Jan 23, 2023
4ba8b61
Removed the old TUMDevAppAPI completely
14slash12 Jan 23, 2023
797b1e3
Fixed the links for the TUMSexyView, when using the built in WebView.…
14slash12 Jan 23, 2023
d6c2a0c
Removed the EntityImporter the old AuthenticationHanlder, the defualt…
14slash12 Jan 23, 2023
38f45fb
Refactored the Lectures to be a struct instead of the old RowSet-Enum…
14slash12 Jan 30, 2023
523470c
Refactored LectureDetails to work with the TUMOnlineAPI.Response inst…
14slash12 Jan 30, 2023
49fcb74
Fixing an issue for MealPlan. When next week's menu isn't ready the A…
14slash12 Feb 1, 2023
4587f1b
Refactoring each view from .onAppear{Task{}} to .task{} since this ca…
14slash12 Feb 1, 2023
0f0b08d
Deleted a print out and comment
14slash12 Feb 10, 2023
fc80ced
Renaming the APIs to its original naming scheme. And creating files f…
14slash12 Feb 10, 2023
ee812c8
Merge commit; Changing the error message for the studyroom details du…
14slash12 Mar 24, 2023
e142222
Merge commit: Make the new API working with NavigaTUM and refactor co…
14slash12 Apr 10, 2023
02e2d2d
Fix issue after logout via Profile sheet
14slash12 Apr 10, 2023
188a020
Readding old if clause to fix weird bevaiour; Add comments
14slash12 Apr 10, 2023
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
478 changes: 389 additions & 89 deletions Campus-iOS.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

28 changes: 27 additions & 1 deletion Campus-iOS/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import KVKCalendar
import Firebase

@main
@MainActor
struct CampusApp: App {
@StateObject var model: Model = Model()

Expand Down Expand Up @@ -46,13 +47,38 @@ struct CampusApp: App {
})
.environmentObject(model)
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.task {
if model.loginController.credentials == Credentials.noTumID {
model.isUserAuthenticated = false
} else {
await model.loginController.confirmToken() { result in
switch result {
case .success:
#if !targetEnvironment(macCatalyst)
Analytics.logEvent("token_confirmed", parameters: nil)
#endif
DispatchQueue.main.async {
model.isLoginSheetPresented = false
model.isUserAuthenticated = true
}

// model.loadProfile()
case .failure(_):
model.isUserAuthenticated = false
if !model.showProfile {
model.isLoginSheetPresented = true
}
}
}
}
}
}
}

func tabViewComponent() -> some View {
TabView(selection: $selectedTab) {
NavigationView {
CalendarContentView(
CalendarScreen(
model: model,
refresh: $model.isUserAuthenticated
)
Expand Down
17 changes: 16 additions & 1 deletion Campus-iOS/Base/Enums/Enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,22 @@ enum Gender: Decodable, Hashable {
}
}

enum ContactInfo {
enum ContactInfo: Identifiable {
var id: String {
switch self {
case .phone(let phone):
return phone
case .mobilePhone(let mobile):
return mobile
case .fax(let fax):
return fax
case .additionalInfo(let info):
return info
case .homepage(let homepage):
return homepage
}
}

case phone(String)
case mobilePhone(String)
case fax(String)
Expand Down
37 changes: 37 additions & 0 deletions Campus-iOS/Base/Networking/APIErrors/EatAPIError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// EatAPIError.swift
// Campus-iOS
//
// Created by David Lin on 10.02.23.
//

import Foundation

enum EatAPIError: APIError, LocalizedError {
case unknown(String)

enum CodingKeys: String, CodingKey {
case message
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let error = try container.decode(String.self, forKey: .message)

switch error {
default:
self = .unknown(error)
}
}

init(message: String) {
self = .unknown(message)
}

public var errorDescription: String? {
switch self {
case let .unknown(message):
return "\("Unkonw error".localized): \(message)"
}
}
}
37 changes: 37 additions & 0 deletions Campus-iOS/Base/Networking/APIErrors/MVGAPIError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// MVGAPIError.swift
// Campus-iOS
//
// Created by David Lin on 10.02.23.
//

import Foundation

enum MVGAPIError: APIError, LocalizedError {
case unknown(String)

enum CodingKeys: String, CodingKey {
case message
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let error = try container.decode(String.self, forKey: .message)

switch error {
default:
self = .unknown(error)
}
}

init(message: String) {
self = .unknown(message)
}

public var errorDescription: String? {
switch self {
case let .unknown(message):
return "\("Unkonw error".localized): \(message)"
}
}
}
37 changes: 37 additions & 0 deletions Campus-iOS/Base/Networking/APIErrors/TUMCabeAPIError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// TUMCabeAPIError.swift
// Campus-iOS
//
// Created by David Lin on 10.02.23.
//

import Foundation

enum TUMCabeAPIError: APIError, LocalizedError {
case unknown(String)

enum CodingKeys: String, CodingKey {
case message
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let error = try container.decode(String.self, forKey: .message)

switch error {
default:
self = .unknown(error)
}
}

init(message: String) {
self = .unknown(message)
}

public var errorDescription: String? {
switch self {
case let .unknown(message):
return "\("Unkonw error".localized): \(message)"
}
}
}
37 changes: 37 additions & 0 deletions Campus-iOS/Base/Networking/APIErrors/TUMDevAppAPIError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// TUMDevAppAPI.swift
// Campus-iOS
//
// Created by David Lin on 10.02.23.
//

import Foundation

enum TUMDevAppAPIError: APIError, LocalizedError {
case unknown(String)

enum CodingKeys: String, CodingKey {
case message
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let error = try container.decode(String.self, forKey: .message)

switch error {
default:
self = .unknown(error)
}
}

init(message: String) {
self = .unknown(message)
}

public var errorDescription: String? {
switch self {
case let .unknown(message):
return "\("Unkonw error".localized): \(message)"
}
}
}
66 changes: 66 additions & 0 deletions Campus-iOS/Base/Networking/APIErrors/TUMOnlineAPIError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// TUMOnlineAPIError.swift
// Campus-iOS
//
// Created by David Lin on 10.02.23.
//

import Foundation

enum TUMOnlineAPIError: APIError, LocalizedError {
case noPermission
case tokenNotConfirmed
case invalidToken
case unknown(String)

enum CodingKeys: String, CodingKey {
case message = "message"
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let error = try container.decode(String.self, forKey: .message)

switch error {
case let str where str.contains("Keine Rechte für Funktion"):
self = .noPermission
case "Token ist nicht bestätigt!":
self = .tokenNotConfirmed
case "Token ist ungültig!":
self = .invalidToken
default:
self = .unknown(error)
}
}

init(message: String) {
self = .unknown(message)
}

public var errorDescription: String? {
switch self {
case .noPermission:
return "No Permission".localized
case .tokenNotConfirmed:
return "Token not confirmed".localized
case .invalidToken:
return "Token invalid".localized
case let .unknown(message):
return "\("Unkonw error".localized): \(message)"

}
}

public var recoverySuggestion: String? {
switch self {
case .noPermission:
return "Make sure to enable the right permissions for your token.".localized
case .tokenNotConfirmed:
return "Go to TUMonline and confirm your token.".localized
case .invalidToken:
return "Try creating a new token.".localized
default:
return nil
}
}
}
37 changes: 37 additions & 0 deletions Campus-iOS/Base/Networking/APIErrors/TUMSexyAPIError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// TUMSexyAPIError.swift
// Campus-iOS
//
// Created by David Lin on 10.02.23.
//

import Foundation

enum TUMSexyAPIError: APIError, LocalizedError {
case unknown(String)

enum CodingKeys: String, CodingKey {
case message
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let error = try container.decode(String.self, forKey: .message)

switch error {
default:
self = .unknown(error)
}
}

init(message: String) {
self = .unknown(message)
}

public var errorDescription: String? {
switch self {
case let .unknown(message):
return "\("Unkonw error".localized): \(message)"
}
}
}
46 changes: 46 additions & 0 deletions Campus-iOS/Base/Networking/APIs/EatAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// EatAPI.swift
// Campus-iOS
//
// Created by David Lin on 10.02.23.
//

import Foundation
import Alamofire

enum EatAPI: API {
case canteens
case languages
case labels
case all
case all_ref
case menu(location: String, year: Int = Date().year, week: Int = Date().weekOfYear)

static var baseURL: String = "https://tum-dev.github.io/eat-api/"

static var baseHeaders: Alamofire.HTTPHeaders = []

static var error: APIError.Type = EatAPIError.self

var paths: String {
switch self {
case .canteens: return "enums/canteens.json"
case .languages: return "enums/languages.json"
case .labels: return "enums/labels.json"
case .all: return "all.json"
case .all_ref: return "all_ref.json"
case let .menu(location, year, week): return "\(location)/\(year)/\(String(format: "%02d", week)).json"
}
}

var parameters: [String : String] { [:] }

var needsAuth: Bool { false }

func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable {
let jsonDecoder = JSONDecoder()
jsonDecoder.dateDecodingStrategy = .formatted(DateFormatter.yyyyMMdd)

return try jsonDecoder.decode(type, from: data)
}
}
Loading