diff --git a/trivai/Networking/NetworkManager.swift b/trivai/Networking/NetworkManager.swift index dc6e4c1..1926897 100644 --- a/trivai/Networking/NetworkManager.swift +++ b/trivai/Networking/NetworkManager.swift @@ -7,7 +7,6 @@ // import Alamofire -import SwiftUI class NetworkManager { @@ -60,7 +59,7 @@ class NetworkManager { // Create multiple choice questions based on topic static func createTopicQuestion(topic: String, num_questions: Int, completion: @escaping ([Question]?, Bool, _ errorMsg: String?) -> Void) { - let endpoint = "\(host)/beta_topics_mc" + let endpoint = "\(host)/mcq/topic/" let params : Parameters = [ "topic": topic, @@ -84,11 +83,12 @@ class NetworkManager { } // Create true/false question - static func createTFQuestion(user_input: String, completion: @escaping ([Question]?, Bool, _ errorMsg: String?) -> Void) { - let endpoint = "\(host)/tf/" + static func createTFQuestion(topic: String, num_questions: Int, completion: @escaping ([Question]?, Bool, _ errorMsg: String?) -> Void) { + let endpoint = "\(host)/tf/topic/" let params : Parameters = [ - "user_input": user_input + "topic": topic, + "num_questions": num_questions ] AF.request(endpoint, method: .post, parameters: params, encoding: JSONEncoding.default).validate().responseData { @@ -107,20 +107,25 @@ class NetworkManager { } } - // Get topics - static func getTopics(completion: @escaping ([Question]?, Bool, _ errorMsg: String?) -> Void) { - let endpoint = "\(host)/beta_get_topics/" + // Registers a user + static func register(username: String, password: String, user_type: String, completion: @escaping (User, Bool, _ errorMsh: String?) -> Void) { + let endpoint = "\(host)/register/" + let params : Parameters = [ + "username": username, + "password": password, + "user_type": user_type + ] - AF.request(endpoint, method: .post, encoding: JSONEncoding.default).validate().responseData { + AF.request(endpoint, method: .post, parameters: params, encoding: JSONEncoding.default).validate().responseData { response in switch response.result { case .success(let data): let jsonDecoder = JSONDecoder() - if let userResponse = try? jsonDecoder.decode([Question].self, from: data) { + if let userResponse = try? jsonDecoder.decode(User.self, from: data) { completion(userResponse, true, nil) } else { - print("Failed to decode true false questions") + print("Failed to decode user") } case .failure(let error): print(error.localizedDescription) @@ -128,14 +133,13 @@ class NetworkManager { } } - // Registers a user - static func register(username: String, password: String, user_type: String, completion: @escaping (User, Bool, _ errorMsh: String?) -> Void) { - let endpoint = "\(host)/register/" + // Computer Vision Function + static func createQuestionsCV(topic: String, num_questions: Int, completion: @escaping ([Question]?, Bool, _ errorMsg: String?) -> Void) { + let endpoint = "\(host)/Testing1/get_mcq_passage" let params : Parameters = [ - "username": username, - "password": password, - "user_type": user_type + "topic": topic, + "num_questions": num_questions ] AF.request(endpoint, method: .post, parameters: params, encoding: JSONEncoding.default).validate().responseData { @@ -143,10 +147,10 @@ class NetworkManager { switch response.result { case .success(let data): let jsonDecoder = JSONDecoder() - if let userResponse = try? jsonDecoder.decode(User.self, from: data) { - completion(userResponse, true, nil) + if let userRespone = try? jsonDecoder.decode([Question].self, from: data) { + completion(userRespone, true, nil) } else { - print("Failed to decode user") + print("Failed to decode topic questions from computer vision") } case .failure(let error): print(error.localizedDescription) diff --git a/trivai/Networking/NetworkTesting.swift b/trivai/Networking/NetworkTesting.swift index 6342d79..65b5ff8 100644 --- a/trivai/Networking/NetworkTesting.swift +++ b/trivai/Networking/NetworkTesting.swift @@ -19,20 +19,34 @@ struct NetworkTesting: View { @State private var quizInfo: Info = Info(title: "Testing", peopleAttended: 100, rules: ["Answer the questions carefully"]) @State var numQuestions: Double = 3 @State var apiCall = false + @State var selectedOption: String = "MCQ" + let options = ["MCQ", "T/F"] func call_api() { self.quizInfo.title = user_input self.apiCall = true - NetworkManager.createTopicQuestion(topic: user_input, num_questions: Int(numQuestions+0.49)) { questions, success, error in - if (success) { - self.apiCall = false - DispatchQueue.main.async { - self.questionListWrapper = QuestionListWrapper(questions: questions ?? []) - } - } else { - print("Error decoding question") - } - } + if (selectedOption == "MCQ") { + NetworkManager.createTopicQuestion(topic: user_input, num_questions: Int(numQuestions+0.49)) { questions, success, error in + if (success) { + DispatchQueue.main.async { + self.questionListWrapper = QuestionListWrapper(questions: questions ?? []) + } + } else { + print("Error decoding question") + } + } + } else { + print("Performing true/false question generation") + NetworkManager.createTFQuestion(topic: user_input, num_questions: Int(numQuestions+0.49)) { questions, success, error in + if (success) { + DispatchQueue.main.async { + self.questionListWrapper = QuestionListWrapper(questions: questions ?? []) + } + } else { + print("Error decoding question") + } + } + } } @@ -52,37 +66,54 @@ struct NetworkTesting: View { .background(Color("background3")) .frame(maxWidth: .infinity) - - Spacer() + + Spacer() VStack { Text("\(numQuestions, specifier: "%.0f") QUESTIONS") - .font(.caption) - .fontWeight(.bold) - .foregroundColor(.gray) + .font(.caption) + .fontWeight(.bold) + .foregroundColor(.gray) Slider(value: $numQuestions, in: 1...10) + // Radio buttons to choose between T/F or MCQ questions + HStack { + VStack { + ForEach(options, id: \.self) { option in + HStack { + Text(option).padding(.trailing, 20).font(.system(size: 15)).foregroundColor(Color.cyan) + Spacer() + RadioCircle(selected: option == selectedOption) + } + .onTapGesture { + selectedOption = option + } + } + } + } + .padding(.horizontal, 100) + } .padding(.leading, 50) .padding(.trailing, 50) - TextField("ENTER TOPIC", text:$user_input) - .font(.system(size:25)) - .fontWeight(.bold) - .foregroundColor(.gray) - .textInputAutocapitalization(.never) - .disableAutocorrection(true) - .frame(width: 320.0, height: 40.0) - .padding(EdgeInsets(top: 8, leading: 8, bottom: 8, trailing: 8)) - .cornerRadius(10) - .overlay( - RoundedRectangle(cornerRadius: 5) - .stroke(lineWidth: 1.0) - .border(Color("background3")) - ).padding() - - - Spacer() + TextField("ENTER TOPIC", text:$user_input) + .font(.system(size:25)) + .fontWeight(.bold) + .foregroundColor(.gray) + .textInputAutocapitalization(.never) + .disableAutocorrection(true) + .frame(width: 320.0, height: 40.0) + .padding(EdgeInsets(top: 8, leading: 8, bottom: 8, trailing: 8)) + .cornerRadius(10) + .overlay( + RoundedRectangle(cornerRadius: 5) + .stroke(lineWidth: 1.0) + .border(Color("background3")) + ).padding() + + + Spacer() VStack { Button (action: { @@ -109,11 +140,30 @@ struct NetworkTesting: View { } .background(Color("background3")).padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10)) - - } + } + + } + +} + +struct RadioCircle: View { + var selected: Bool + + var body: some View { + ZStack { + Circle() + .stroke(selected ? Color.blue : Color.gray, lineWidth: 2) + .frame(width: 20, height: 20) + + if selected { + Circle() + .fill(Color.blue) + .frame(width: 12, height: 12) + } } + } } #if DEBUG diff --git a/trivai/main/LoadingView.swift b/trivai/main/LoadingView.swift index 5b20b97..551045c 100644 --- a/trivai/main/LoadingView.swift +++ b/trivai/main/LoadingView.swift @@ -39,4 +39,4 @@ struct LoadingView_Previews: PreviewProvider { static var previews: some View { LoadingView() } -} +} \ No newline at end of file