Display registered accounts

This commit is contained in:
Pengwius 2021-03-05 12:49:37 +01:00
parent 0a0ce1c55d
commit 322a0b4cb4
7 changed files with 149 additions and 58 deletions

View file

@ -212,7 +212,7 @@ public class Sdk {
"Timestamp": now.millisecondsSince1970,
"TimestampFormatted": "\(timestampFormatted) GMT"
]
request.httpBody = try? JSONSerialization.data(withJSONObject: body)
request.allHTTPHeaderFields = [
@ -262,9 +262,8 @@ public class Sdk {
// Handle HTTP request response
let responseBody = String(data: data, encoding: String.Encoding.utf8)
let keychain = Keychain()
keychain["students"] = responseBody
keychain["students"] = responseBody!
} else {
// Handle unexpected error

View file

@ -176,6 +176,7 @@
5C1B849F25E1B7A30074F29D /* Login */ = {
isa = PBXGroup;
children = (
5CF095AC25F053B10068F2C6 /* accountManager.swift */,
5CF6695E25EEE2FD00AC0A86 /* chooseStudent.swift */,
F4C6D9092544E17400F8903A /* LoginView.swift */,
);
@ -217,7 +218,6 @@
5C1794BB25E8FE19007AD91A /* settings.swift */,
5C1794BF25E8FE27007AD91A /* about.swift */,
5C89C8F425EA6AA4000B5816 /* licenses.swift */,
5CF095AC25F053B10068F2C6 /* accountManager.swift */,
5C5162D725F0DCDE00EF0777 /* more.swift */,
5C5162DB25F0DD9200EF0777 /* attendance.swift */,
);

View file

@ -19,6 +19,10 @@
"invalidData" = "Wrong token, symbol or pin";
"success" = "Success";
//CHOOSE ACCOUNT
"registerButton" = "Register";
"selectStudent" = "Select student";
//NAVIGATION
"dashboardButton" = "Dashboard";
"gradesButton" = "Grades";
@ -45,3 +49,7 @@
//LICENCES
"noLicence" = "No licence";
//ACCOUNT MANAGER
"addAccount" = "Add account";
"chooseAccount" = "Choose account";

View file

@ -19,6 +19,10 @@
"invalidData" = "Zły token, symbol lub pin";
"success" = "Sukces";
//CHOOSE ACCOUNT
"registerButton" = "Zarejestruj";
"selectStudent" = "Wybierz ucznia";
//NAVIGATION
"dashboardButton" = "Start";
"gradesButton" = "Oceny";
@ -45,3 +49,7 @@
//LICENCES
"noLicence" = "Brak licencji";
//ACCOUNT MANAGER
"addAccount" = "Dodaj konto";
"chooseAccount" = "Wybierz konto";

View file

@ -1,34 +0,0 @@
//
// accountManager.swift
// wulkanowy
//
// Created by Tomasz on 04/03/2021.
//
import SwiftUI
struct AccountManagerView: View {
@State private var showModal = false
@AppStorage("isLogged") private var isLogged: Bool = false
var body: some View {
if(isLogged == false){
VStack {
Text("No accounts added")
}.padding()
} else {
Text("Here is account manager (in my imagination)")
}
}
}
struct AccountManagerView_Previews: PreviewProvider {
static var previews: some View {
Group {
AccountManagerView()
}
.preferredColorScheme(.dark)
}
}

View file

@ -0,0 +1,88 @@
//
// accountManager.swift
// wulkanowy
//
// Created by Tomasz on 04/03/2021.
//
import SwiftUI
import KeychainAccess
import SwiftyJSON
struct AccountManagerView: View {
@State private var showModal = false
@AppStorage("isLogged") private var isLogged: Bool = false
private func getStudentsNames() -> [String] {
//getting allStudentsKeys
let keychain = Keychain()
let allStudentsKeys: String! = keychain["allStudentsKeys"] ?? "[]"
//parsing allStudentsKeys to array
var allStudents: [String] = []
let data = Data(allStudentsKeys.utf8)
do {
let keys = try JSONSerialization.jsonObject(with: data) as! [String]
for key in keys {
let student = keychain["student-\(key)"]
let data = Data(student!.utf8)
let studentParsed = try! JSON(data: data)
allStudents.append("\(studentParsed["Login"]["DisplayName"])")
}
} catch {
print(error)
}
return allStudents
}
private func addAccount() {
self.showModal = true
}
private func getJsonFromString(body: String) -> JSON {
let data = Data(body.utf8)
let json = try! JSON(data: data)
return json
}
var body: some View {
VStack {
Form {
Section(header: Text("chooseAccount")
.font(.title)) {
ForEach(getStudentsNames(), id: \.self) { student in
Text(student)
}
}
}.padding(.bottom)
Spacer()
Button("addAccount") {addAccount()}
.font(.headline)
.multilineTextAlignment(.center)
.padding(.horizontal)
.frame(height: 55)
.frame(maxWidth: .infinity)
.background(Color.accentColor.opacity(0.1))
.cornerRadius(12)
.padding()
.sheet(isPresented: $showModal, onDismiss: {
print(self.showModal)
}) {
LoginView()
}
}
}
}
struct AccountManagerView_Previews: PreviewProvider {
static var previews: some View {
Group {
AccountManagerView()
}
.preferredColorScheme(.dark)
}
}

View file

@ -19,9 +19,17 @@ struct ChooseStudentView: View {
@State private var selectedStudent: String = ""
init() {
let responseBody = keychain["students"]
let json = getJsonFromString(body: responseBody!)
var responseBody = keychain["students"]
while responseBody == nil {
responseBody = keychain["students"]
}
let data = Data(responseBody!.utf8)
let json = try! JSON(data: data)
selectedStudent = "\(json["Envelope"][0]["Login"]["DisplayName"])"
var i: Int = 0
while true {
if (String(describing: json["Envelope"][i]) == "null") {
@ -29,28 +37,45 @@ struct ChooseStudentView: View {
}
else {
displayStudents.append(String(describing: json["Envelope"][i]["Login"]["DisplayName"]))
displayStudents.append("dupa")
i += 1
}
}
selectedStudent = displayStudents[0]
}
private func getJsonFromString(body: String) -> JSON {
let data = Data(body.utf8)
let json = try! JSON(data: data)
return json
}
private func saveStudent() {
isLogged = true
let responseBody = keychain["students"]
let json = getJsonFromString(body: responseBody!)
let data = Data(responseBody!.utf8)
let json = try! JSON(data: data)
var i: Int = 0
if(selectedStudent == "") {
selectedStudent = "\(json["Envelope"][i]["Login"]["DisplayName"])"
}
while true {
if (String(describing: json["Envelope"][i]["Login"]["DisplayName"]) == selectedStudent) {
keychain["acctualStudent"] = "\(json["Envelope"][i])"
let student = "\(json["Envelope"][i]["Login"]["DisplayName"])"
if(student == selectedStudent) {
//adding student key to allStudentsKeys
let keyFingerprint: String! = keychain["keyFingerprint"]
let allStudentsKeys: String! = keychain["allStudentsKeys"] ?? ""
//parsing allStudentsKeys to array
var allStudents: [String] = []
let data = Data(allStudentsKeys!.utf8)
do {
let array = try JSONSerialization.jsonObject(with: data) as! [String]
allStudents = array
} catch {
print(error)
}
allStudents.append(keyFingerprint!)
keychain["allStudentsKeys"] = "\(allStudents)"
//saving student
keychain["student-\(String(describing: keyFingerprint!))"] = "\(json["Envelope"][i])"
break
}
i += 1
@ -62,18 +87,16 @@ struct ChooseStudentView: View {
var body: some View {
VStack {
Spacer()
Text(selectedStudent)
Text("Wybierz ucznia")
Text("selectStudent")
.font(.title)
.padding(.top)
Picker(selection: $selectedStudent, label: Text("Wybierz ucznia:")) {
Picker(selection: $selectedStudent, label: Text("selectStudent")) {
ForEach(displayStudents, id: \.self) { student in
Text(student)
}
}
Spacer()
//NavigationLink(destination: NavigationBarView()){
Button("Zarejestruj") {saveStudent()}
Button("registerButton") {saveStudent()}
.font(.headline)
.multilineTextAlignment(.center)
.padding(.horizontal)
@ -81,7 +104,6 @@ struct ChooseStudentView: View {
.frame(maxWidth: .infinity)
.background(Color.accentColor.opacity(0.1))
.cornerRadius(12)
//}
}.padding()
}