From 8849dc44b0153fd5885c3eeb64f97a7e79881858 Mon Sep 17 00:00:00 2001 From: Kenneth Chiem Date: Fri, 14 Apr 2023 16:22:28 -0400 Subject: [PATCH 1/2] create toggle buttons for questions --- trivai/Networking/NetworkManager.swift | 34 ++----- trivai/Networking/NetworkTesting.swift | 118 ++++++++++++++++++------- trivai/main/LoadingView.swift | 40 +++------ 3 files changed, 102 insertions(+), 90 deletions(-) diff --git a/trivai/Networking/NetworkManager.swift b/trivai/Networking/NetworkManager.swift index dc6e4c1..8b53872 100644 --- a/trivai/Networking/NetworkManager.swift +++ b/trivai/Networking/NetworkManager.swift @@ -7,11 +7,11 @@ // import Alamofire -import SwiftUI class NetworkManager { - static let host = "" + static let host = "http://100.26.175.163:5000" + static let local_host = "http://10.48.21.136:3000" // Testing static func testQuestions(completion: @escaping ([Question]?, Bool, _ errorMsg: String?) -> Void) { @@ -60,7 +60,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 = "\(local_host)/mcq/topic/" let params : Parameters = [ "topic": topic, @@ -84,11 +84,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,27 +108,6 @@ class NetworkManager { } } - // Get topics - static func getTopics(completion: @escaping ([Question]?, Bool, _ errorMsg: String?) -> Void) { - let endpoint = "\(host)/beta_get_topics/" - - - AF.request(endpoint, method: .post, 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) { - completion(userResponse, true, nil) - } else { - print("Failed to decode true false questions") - } - case .failure(let error): - print(error.localizedDescription) - } - } - } - // Registers a user static func register(username: String, password: String, user_type: String, completion: @escaping (User, Bool, _ errorMsh: String?) -> Void) { let endpoint = "\(host)/register/" 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 25de05e..aa4145a 100644 --- a/trivai/main/LoadingView.swift +++ b/trivai/main/LoadingView.swift @@ -2,41 +2,23 @@ // LoadingView.swift // trivai // -// Created by Mason Bulling on 4/2/23. +// Created by Ken Chiem on 4/14/23. // Copyright © 2023 Mithun. All rights reserved. // -import Foundation -import SwiftUI +import UIKit -struct LoadingView: View { - @State private var isAnimating = false - - var body: some View { - ZStack { - Circle() - .stroke(Color.gray, lineWidth: 8) - .frame(width: 100, height: 100) - - Circle() - .trim(from: 0, to: 0.8) - .stroke(Color("background3"), lineWidth: 8) - .frame(width: 100, height: 100) - .rotationEffect(.degrees(isAnimating ? 360 : 0)) - .animation(Animation.linear(duration: 1).repeatForever(autoreverses: false)) - } - .onAppear { - self.isAnimating = true - } +class LoadingView: UITableViewCell { + + override func awakeFromNib() { + super.awakeFromNib() + // Initialization code } -} + override func setSelected(_ selected: Bool, animated: Bool) { + super.setSelected(selected, animated: animated) -struct LoadingView_Preview: View { - var body: some View { - VStack { - LoadingView() - } + // Configure the view for the selected state } -} +} From b8808288c9199a745e58b08950e73f290d4a2813 Mon Sep 17 00:00:00 2001 From: Kenneth Chiem Date: Wed, 19 Apr 2023 14:59:53 -0400 Subject: [PATCH 2/2] Create CV API call function --- trivai/Networking/NetworkManager.swift | 30 +++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/trivai/Networking/NetworkManager.swift b/trivai/Networking/NetworkManager.swift index 8b53872..1926897 100644 --- a/trivai/Networking/NetworkManager.swift +++ b/trivai/Networking/NetworkManager.swift @@ -10,8 +10,7 @@ import Alamofire class NetworkManager { - static let host = "http://100.26.175.163:5000" - static let local_host = "http://10.48.21.136:3000" + static let host = "" // Testing static func testQuestions(completion: @escaping ([Question]?, Bool, _ errorMsg: String?) -> Void) { @@ -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 = "\(local_host)/mcq/topic/" + let endpoint = "\(host)/mcq/topic/" let params : Parameters = [ "topic": topic, @@ -133,4 +132,29 @@ class NetworkManager { } } } + + // 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 = [ + "topic": topic, + "num_questions": num_questions + ] + + 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 userRespone = try? jsonDecoder.decode([Question].self, from: data) { + completion(userRespone, true, nil) + } else { + print("Failed to decode topic questions from computer vision") + } + case .failure(let error): + print(error.localizedDescription) + } + } + } }