Custom username

This commit is contained in:
Pengwius 2021-03-10 10:54:34 +01:00
parent a04f4173b8
commit 7cf96b1575
7 changed files with 131 additions and 12 deletions

View file

@ -7,8 +7,8 @@
import Foundation
import SwiftUI
import KeychainAccess
import SwiftyJSON
import KeychainAccess
@available (iOS 14, macOS 11, watchOS 7, tvOS 14, *)

View file

@ -181,6 +181,7 @@ public class Sdk {
let saveAccount = """
{
"actualStudent": "0",
"customUsername": "null",
"privateKeyString": "\(privateKeyStringString)",
"fingerprint": "\(fingerprint)",
"deviceModel": "\(deviceModel)",

View file

@ -12,17 +12,23 @@ import SwiftyJSON
@available (iOS 14, macOS 11, watchOS 7, tvOS 14, *)
public func getLuckyNumber() -> Int {
let keychain = Keychain()
let actualStudent = keychain["actualStudent"] ?? "{}"
let data: Data = Data(actualStudent.utf8)
let actualStudentJSON = try! JSON(data: data)
let Id = "\(actualStudentJSON["ConstituentUnit"]["Id"])"
let actualStudentId = "\(keychain["actualStudentId"] ?? "0")"
let actualAccount = keychain[actualStudentId]
let dataAccount: Data = Data(actualAccount!.utf8)
let actualAccountJSON = try! JSON(data: dataAccount)
let studentId = Int("\(actualAccountJSON["actualStudent"])")
let actualStudentHebe = keychain["actualStudentHebe"]
let dataHebe: Data = Data(actualStudentHebe!.utf8)
let actualStudentHebeJSON = try! JSON(data: dataHebe)
let actualStudent = actualStudentHebeJSON["Envelope"][studentId!]
let Id = "\(actualStudent["ConstituentUnit"]["Id"])"
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let day = formatter.string(from: date)
let RestURL = "\(actualStudentJSON["Unit"]["RestURL"])/mobile/school/lucky?constituentId=\(Id)&day=\(day)"
let RestURL = "\(actualStudent["Unit"]["RestURL"])/mobile/school/lucky?constituentId=\(Id)&day=\(day)"
let apiResponseRequest = apiRequest(endpointURL: RestURL)

View file

@ -7,7 +7,7 @@
*/
//ONBOARDING
"onboardingTitle" = "Welcome in Wulkanowy!";
"onboardingTitle" = "Welcome to Wulkanowy!";
"notificationsOnboarding" = "Notifications";
"messagesOnboarding" = "Messages";
@ -15,7 +15,7 @@
"notificationsOnboardingContent" = "Forget about saving exams in calendar";
"messagesOnboardingContent" = "Simple contact with your favorite teachers";
"fastContent" = "Fast, like Sonic";
"fastContent" = "Fast as Sonic";
"continueButton" = "Let's go!";

View file

@ -43,7 +43,9 @@ struct AccountManagerView: View {
self.showLoginModal = true
}
private func openEditAccount() {
private func openEditAccount(id: String) {
let keychain = Keychain()
keychain["editAccountId"] = id
self.showEditAccountModal = true
}
@ -84,6 +86,14 @@ struct AccountManagerView: View {
accounts = getStudentsNames()
}
private func getUsername(student: JSON) -> String {
if("\(student["customUsername"])" == "null") {
return "\(student["account"]["UserName"])"
} else {
return "\(student["customUsername"])"
}
}
var body: some View {
VStack {
Form {
@ -96,7 +106,8 @@ struct AccountManagerView: View {
let studentString = "\(keychain[student] ?? "{}")"
let data: Data = Data(studentString.utf8)
let studentJSON = try! JSON(data: data)
let studentEmail = "\(studentJSON["account"]["UserName"])"
let studentEmail = getUsername(student: studentJSON)
Button("\(studentEmail)") { setActualAccount(id: student) }
.foregroundColor(Color("customControlColor"))
if("\(actualId)" == "\(student)") {
@ -105,7 +116,7 @@ struct AccountManagerView: View {
}
Spacer()
let image = Image(systemName: "pencil")
Button("\(image)") { openEditAccount() }
Button("\(image)") { openEditAccount(id: student) }
.sheet(isPresented: $showEditAccountModal, onDismiss: {
}) {
EditAccountView()

View file

@ -6,9 +6,95 @@
//
import SwiftUI
import KeychainAccess
import SwiftyJSON
struct EditAccountView: View {
@Environment(\.presentationMode) var presentation
@State private var account: JSON = []
@State private var username: String = ""
@State private var showAlert = false
let id: String
let keychain = Keychain()
private func saveNewUsername(newUsername: String) {
var accountJSON = self.account
accountJSON["customUsername"].stringValue = "\(newUsername)"
keychain[self.id] = "\(accountJSON)"
getAccount()
getUsername()
}
private func alert() {
let alert = UIAlertController(title: "Zmiana nazwy", message: "Tu wpisz swoją nową nazwę konta", preferredStyle: .alert)
alert.addTextField() { textField in
textField.placeholder = username
}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in })
alert.addAction(UIAlertAction(title: "Save", style: .default) { _ in let textField = alert.textFields![0]
let newUsername = "\(textField.text ?? "\(username)")"
saveNewUsername(newUsername: newUsername)})
showAlert(alert: alert)
}
func showAlert(alert: UIAlertController) {
if let controller = topMostViewController() {
controller.present(alert, animated: true)
}
}
private func keyWindow() -> UIWindow? {
return UIApplication.shared.connectedScenes
.filter {$0.activationState == .foregroundActive}
.compactMap {$0 as? UIWindowScene}
.first?.windows.filter {$0.isKeyWindow}.first
}
private func topMostViewController() -> UIViewController? {
guard let rootController = keyWindow()?.rootViewController else {
return nil
}
return topMostViewController(for: rootController)
}
private func topMostViewController(for controller: UIViewController) -> UIViewController {
if let presentedController = controller.presentedViewController {
return topMostViewController(for: presentedController)
} else if let navigationController = controller as? UINavigationController {
guard let topController = navigationController.topViewController else {
return navigationController
}
return topMostViewController(for: topController)
} else if let tabController = controller as? UITabBarController {
guard let topController = tabController.selectedViewController else {
return tabController
}
return topMostViewController(for: topController)
}
return controller
}
init() {
self.id = keychain["editAccountId"] ?? "0"
}
private func getAccount() {
let account = "\(keychain[self.id] ?? "{}")"
let data: Data = Data(account.utf8)
self.account = try! JSON(data: data)
}
private func getUsername() {
if("\(account["customUsername"])" == "null") {
username = "\(account["account"]["UserName"])"
} else {
username = "\(account["customUsername"])"
}
}
private func done() {
presentation.wrappedValue.dismiss()
@ -24,6 +110,11 @@ struct EditAccountView: View {
.frame(height: 92)
.foregroundColor(.accentColor)
.padding(.bottom)
HStack{
Text("\(username)")
let pencilSymbol = Image(systemName: "pencil")
Button("\(pencilSymbol)") { alert() }
}
Spacer()
Button("Done") {done()}
.font(.headline)
@ -34,6 +125,10 @@ struct EditAccountView: View {
.background(Color.accentColor.opacity(0.1))
.cornerRadius(12)
}.padding()
.onAppear {
getAccount()
getUsername()
}
}
}

View file

@ -56,7 +56,13 @@ struct ChooseStudentView: View {
let student = "\(json["Envelope"][i]["Login"]["DisplayName"])"
if(student == selectedStudent) {
//saving student
keychain["actualStudent"] = "\(json["Envelope"][i])"
let id = "\(keychain["actualStudentId"] ?? "0")"
let account = keychain[id]
let data: Data = Data(account!.utf8)
var accountJSON = try! JSON(data: data)
accountJSON["actualStudent"] = JSON(i)
print(accountJSON)
keychain[id] = "\(accountJSON)"
break
}
i += 1