diff --git a/ProjectClock/AccountViewController.swift b/ProjectClock/AccountViewController.swift
index 6040634..51a9bc8 100644
--- a/ProjectClock/AccountViewController.swift
+++ b/ProjectClock/AccountViewController.swift
@@ -7,6 +7,9 @@
import UIKit
+/**
+ Account view controller controlling the two separate view controllers
+ */
class AccountViewController: UIViewController
{
@IBOutlet var vLogin: UIView!
@@ -80,29 +83,28 @@ class LoginVC: UIViewController
return
}
+ // Error messages
+ let errors = ["409 - [\"A0111\"]": "Account already exists, please login instead.",
+ "401 -": "Incorrect username/password",
+ "404 -": "Username does not exist in the database",
+ ]
+
// Send register request
- let a = alert(login ? "Logging in..." : "Registering...", "Please Wait")
- send(login ? APIs.login : APIs.register, ["username": name, "password": pass.sha256])
+ sendReq(login ? APIs.login : APIs.register,
+ title: login ? "Logging in..." : "Registering...", errors: errors,
+ params: ["username": name, "password": pass.sha256])
{
// Store username and password
localStorage["name"] = name
localStorage["pass"] = pass.sha256
localStorage["id"] = $0
- a.dismiss
- {
- // Send feedback
- if login { self.msg("Login success!", "Now you can use account features, yay!") }
- else { self.msg("Registration success!", "Now you have an account, yay!") }
-
- // Hide registration and show account detail view
- ui { AccountViewController.this.login() }
- }
- }
- err:
- {
- print($0)
- a.dismiss { self.msg("An error occurred", "Maybe the server is on fire, just wait a few hours.") }
+ // Send feedback
+ if login { self.msg("Login success!", "Now you can use account features, yay!") }
+ else { self.msg("Registration success!", "Now you have an account, yay!") }
+
+ // Hide registration and show account detail view
+ AccountViewController.this.login()
}
}
@@ -123,6 +125,9 @@ class LoginVC: UIViewController
}
}
+/**
+ Account manage view controller
+ */
class ManageVC: UIViewController
{
static var this: ManageVC!
@@ -157,4 +162,17 @@ class ManageVC: UIViewController
{
AccountViewController.this.logout()
}
+
+ /**
+ Called when the user clicks the delete account button
+ */
+ @IBAction func deleteAccount(_ sender: Any)
+ {
+ sendReq(APIs.delete, title: "Deleting...")
+ {
+ print("Deleted! \($0)")
+ self.msg("Deleted!", "You are erased from our database, you no longer exist.")
+ self.logout(sender)
+ }
+ }
}
diff --git a/ProjectClock/Base.lproj/Main.storyboard b/ProjectClock/Base.lproj/Main.storyboard
index 5eaf6d5..d87ddd9 100644
--- a/ProjectClock/Base.lproj/Main.storyboard
+++ b/ProjectClock/Base.lproj/Main.storyboard
@@ -638,7 +638,7 @@
-
+
@@ -690,13 +690,13 @@
-
+
-
-
+
+
@@ -728,44 +728,23 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -778,6 +757,10 @@
+
+
+
+
@@ -789,6 +772,7 @@
+
diff --git a/ProjectClock/Utils.swift b/ProjectClock/Utils.swift
index 8cee80d..5edb7d6 100644
--- a/ProjectClock/Utils.swift
+++ b/ProjectClock/Utils.swift
@@ -139,7 +139,25 @@ extension UIViewController
func msg(_ title: String, _ message: String) -> UIAlertController { alert(title, message, okayable: true) }
/// More convenient dismiss function
- func dismiss(_ completion: (() -> Void)? = nil) { dismiss(animated: false, completion: completion) }
+ func dismiss(_ completion: (() -> Void)? = nil) { ui { self.dismiss(animated: false, completion: completion) } }
+
+ /**
+ Send a http request even more conveniently
+ */
+ func sendReq(_ api: API, title: String, errors: [String: String] = [:], params: [String: String]? = [:], _ success: @escaping (T) -> Void, err: @escaping (String) -> Void = {it in})
+ {
+ // Send request
+ let a = alert(title, "Please Wait")
+ send(api, params) { it in a.dismiss { success(it) } }
+ err:
+ {
+ // Display error message
+ print("===== Error: \($0) =====")
+ let message = errors[$0.trimmingCharacters(in: .whitespaces)]
+ ?? "Maybe the server is on fire, just wait a few hours."
+ a.dismiss { self.msg("An error occurred", message) }
+ }
+ }
}