Skip to content

Commit

Permalink
Add Support for Custom URL
Browse files Browse the repository at this point in the history
  • Loading branch information
KuhlTime committed May 6, 2021
1 parent eeb063d commit 3adb48f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/Alamofire/Alamofire",
"state": {
"branch": null,
"revision": "eaf6e622dd41b07b251d8f01752eab31bc811493",
"version": "5.4.1"
"revision": "f96b619bcb2383b43d898402283924b80e2c4bae",
"version": "5.4.3"
}
},
{
Expand All @@ -24,8 +24,8 @@
"repositoryURL": "https://github.com/kishikawakatsumi/KeychainAccess",
"state": {
"branch": null,
"revision": "654d52d30f3dd4592e944c3e0bccb53178c992f6",
"version": "4.2.1"
"revision": "84e546727d66f1adc5439debad16270d0fdd04e7",
"version": "4.2.2"
}
},
{
Expand All @@ -42,8 +42,8 @@
"repositoryURL": "https://github.com/siteline/SwiftUI-Introspect.git",
"state": {
"branch": null,
"revision": "36ecf80429d00a4cd1e81fbfe4655b1d99ebd651",
"version": "0.1.2"
"revision": "2e09be8af614401bc9f87d40093ec19ce56ccaf2",
"version": "0.1.3"
}
},
{
Expand Down
33 changes: 23 additions & 10 deletions iOSSC/Model/APIManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class APIManager: ObservableObject {
private var username: String?
private var password: String?

/**
When set a custom server url will be used
*/
var customUrl: String?

/**
The KeychainAccess reference object
*/
Expand Down Expand Up @@ -80,6 +85,10 @@ class APIManager: ObservableObject {
try keychain.set(username, key: "username")
try keychain.set(password, key: "password")

if let url = customUrl {
try keychain.set(url, key: "customUrl")
}

print("Saved credentials")
} catch {
print("Error while setting keychain: \(error.localizedDescription)")
Expand All @@ -94,6 +103,7 @@ class APIManager: ObservableObject {
do {
try keychain.remove("username")
try keychain.remove("password")
try keychain.remove("customUrl")

print("Removed credentials")
} catch {
Expand All @@ -110,6 +120,7 @@ class APIManager: ObservableObject {
do {
username = try keychain.get("username")
password = try keychain.get("password")
customUrl = try keychain.get("customUrl")

if (username != nil && password != nil) {
print("Restored credentials")
Expand Down Expand Up @@ -138,7 +149,7 @@ class APIManager: ObservableObject {
return
}

AF.request(environment.url, method: .get, headers: getHeaders(username, password))
AF.request(url, method: .get, headers: getHeaders(username, password))
.responseDecodable { (response: DataResponse<APIResponse<ResponseData>, AFError>) in
if let error = response.error {
print(error.localizedDescription)
Expand Down Expand Up @@ -184,6 +195,17 @@ class APIManager: ObservableObject {
return ["Authorization": "Basic \(base64Date.base64EncodedString())"]
}

var baseUrl: String {
return customUrl ?? "https://ossc.api.kuhlti.me"
}

var url: String {
switch (environment) {
case .development: return baseUrl + "/test"
case .production: return baseUrl
}
}

/**
Sets the environment the application should run in
*/
Expand All @@ -197,15 +219,6 @@ class APIManager: ObservableObject {
In production mode the user recieves its original data from the ossc's servers
*/
case production

var url: String {
switch self {
case .development:
return "https://ossc.api.kuhlti.me/test"
case .production:
return "https://ossc.api.kuhlti.me"
}
}
}

enum LoginState {
Expand Down
35 changes: 34 additions & 1 deletion iOSSC/View/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import SwiftUI
import Haptica
import SFSafeSymbols

struct LoginView: View {
@EnvironmentObject var manager: APIManager
Expand All @@ -15,10 +16,20 @@ struct LoginView: View {
@State private var password = ""
@State private var saveCredentials = false

@State private var editUrl: Bool = false
@State private var customUrl: String = ""

var body: some View {
ZStack {
BackgroundView()

HStack(alignment: .top) {
Spacer()
infoButton
}
.frame(maxHeight: .infinity, alignment: .topTrailing)
.padding()

VStack(alignment: .leading) {
header
inputs
Expand All @@ -36,11 +47,24 @@ struct LoginView: View {
guard !username.isEmpty && !password.isEmpty else { return }

Haptic.impact(.medium).generate()
manager.customUrl = customUrl.isEmpty ? nil : customUrl
manager.login(username, password)
}

private var infoButton: some View {
Button(action: {
editUrl.toggle()
}) {
Image(systemName: "gearshape.fill")
.resizable()
.frame(width: 24, height: 24)
.foregroundColor(.white)
}
}

private var header: some View {
VStack(alignment: .leading) {

Text("IOSSC")
.font(.custom("HSD Sans", size: 38))
.foregroundColor(Color(hex: 0xE60028))
Expand All @@ -55,9 +79,18 @@ struct LoginView: View {

private var inputs: some View {
VStack {
if (editUrl) {
VStack {
Text("Nur HTTPS erlaubt!")
.foregroundColor(.white)
InputField(manager.baseUrl, text: $customUrl)
}
.padding(.bottom, 8)
}

InputField("Benutzername", text: $username)
.padding(.bottom, 8)
InputField("Password", text: $password, isSecure: true)
.padding(.top, 8)
}
}

Expand Down

0 comments on commit 3adb48f

Please sign in to comment.