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

Commit

Permalink
API Refactoring (#536)
Browse files Browse the repository at this point in the history
* Start to refactor the API buy generalizing all the API methods into one MainAPI enum independent of the API (TUMCabe, TUMOnline, CampusOnline, etc.), the respective data type which (i.e. Grades, Movie, StudyRoom, etc.) and independent of the decoder (XML or JSON).

* Created for each API an new enum conforming to the new protocol API to be used with the newly created method makeRequest<T,S> in the enum MainAPI

* Refactored Calendar to work with the Asyn-Await-Pattern, the state enum and the new API. This includes, that Calendar now shows the error screen, if the fetching failed

* Refactored LecturesSearch to the new API and introduced the asnc-await-pattern with the state enum

* Refactored the API for PersonSearch and added the new async-await pattern and error handling for this view.

* Completely rewritten the ProfileDetailView and its ViewModel to adopt to the async-await-pattern, and the state-enum and handling errors properly

* Refactoring the complete Authentication procress to work with the new API and to work with asnc-await

* Refactored the ProfileViewModel, ProfileView, Model, to work with the new API, including async-await pattern and many many adjustments alongside.

* Adding the empty profile image placeholder and removing the old TUMOnlineAPI/CampusOnlineAPI completely.

* Renamed the ProfileView2/ProfileViewModel2 -> to ProfileView/ProfileViewModel

* Refactoring the News for the API including async-await-pattern, error-handling, state-enum

* Renamed the new NewsViewModel, NewsView to the old names

* Extracted the new NewsScreen into a own file

* Refactored the MovieView for the new API, including error-handling, async-await, and state-enum

* Refactored the StudyRoomViewModel for fetching the RoomImageMapping. Adapted for the async-await-patter and enum-state

* Removed the old TUMCabeAPI

* Refactoring the Cafeterias to work with the new API

* Refactored the MealPlanViewModel/View, to work with the new API, adding async-await pattern and error-handling

* Refactored the DishLabels to work with the new API

* Refactored the StudyRoomService to work with the new API for the StudyRoomApiResponse

* Removed the old TUMDevAppAPI completely

* Fixed the links for the TUMSexyView, when using the built in WebView. Refactored to the new API, added async-await-pattern and error-handling

* Removed the EntityImporter the old AuthenticationHanlder, the defualt Session and the Entity-Protocol

* Refactored the Lectures to be a struct instead of the old RowSet-Enum-Construct. This is obsolet since the TUMOnlineAPI response enum can be used for decoding

* Refactored LectureDetails to work with the TUMOnlineAPI.Response instead of its own old LectureDetailsComponents

* Fixing an issue for MealPlan. When next week's menu isn't ready the API throws a 404 error, but we can fetch meals for the current week. Thus, we need to be patient, i.e. no error needs to be thrown. And the this weeks menu can be shown without any error

* Refactoring each view from .onAppear{Task{}} to .task{} since this can be cancled by the view (see https://stackoverflow.com/questions/68114509/what-is-the-difference-between-onappear-and-task-in-swiftui-3)

* Deleted a print out and comment

* Renaming the APIs to its original naming scheme. And creating files for each API and rename all temporarily created ViewModels to their original naming scheme. Fixing the problem with Cafeterias, which do not have any menus and the API is returning a 404 error. Now just a plain 'No Menus available' text shows up instead of an error message. Fixing a problem with the PersonSearch where the pIdentNr for the API was the wrong number, so the response was an error, now the API gets called with the obfuscated_id and the right results are retrieved.

* Fix issue after logout via Profile sheet

* Readding old if clause to fix weird bevaiour; Add comments
  • Loading branch information
14slash12 authored Apr 10, 2023
1 parent 2587c8e commit f27f704
Show file tree
Hide file tree
Showing 150 changed files with 5,198 additions and 2,613 deletions.
698 changes: 495 additions & 203 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/NavigaTUMAPIError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// NAvigaTUMAPIError.swift
// Campus-iOS
//
// Created by David Lin on 10.04.23.
//

import Foundation

enum NavigaTUMAPIError: 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)"
}
}
}
Loading

0 comments on commit f27f704

Please sign in to comment.