Skip to content

Commit

Permalink
Merge pull request #479 from pennlabs/polls-vc
Browse files Browse the repository at this point in the history
Add Poll History page
  • Loading branch information
JHawk0224 authored Oct 9, 2023
2 parents f8df9da + f04fcf6 commit d1fa566
Show file tree
Hide file tree
Showing 25 changed files with 316 additions and 94 deletions.
8 changes: 8 additions & 0 deletions PennMobile.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@
423B7EF52ACC67B900D30504 /* MeterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 423B7EF42ACC67B900D30504 /* MeterView.swift */; };
426A0B51299034910066C7B7 /* DiningAnalyticsGraph.swift in Sources */ = {isa = PBXBuildFile; fileRef = 426A0B4F299034910066C7B7 /* DiningAnalyticsGraph.swift */; };
426A0B52299034910066C7B7 /* DiningAnalyticsGraphBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = 426A0B50299034910066C7B7 /* DiningAnalyticsGraphBox.swift */; };
42CC49D02AAE38C6008C41EE /* PollsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42CC49CF2AAE38C6008C41EE /* PollsViewController.swift */; };
42CC49D22AAE3904008C41EE /* PollsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42CC49D12AAE3904008C41EE /* PollsView.swift */; };
42D9237129E0C69200E9E18E /* FitnessModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D9237029E0C69200E9E18E /* FitnessModel.swift */; };
42D9237329E0C9AF00E9E18E /* FitnessViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D9237229E0C9AF00E9E18E /* FitnessViewController.swift */; };
42D9237529E0CCFB00E9E18E /* FitnessView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D9237429E0CCFB00E9E18E /* FitnessView.swift */; };
Expand Down Expand Up @@ -514,6 +516,8 @@
425E281A28FF5EE200218F50 /* DiningPlan.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiningPlan.swift; sourceTree = "<group>"; };
426A0B4F299034910066C7B7 /* DiningAnalyticsGraph.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiningAnalyticsGraph.swift; sourceTree = "<group>"; };
426A0B50299034910066C7B7 /* DiningAnalyticsGraphBox.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiningAnalyticsGraphBox.swift; sourceTree = "<group>"; };
42CC49CF2AAE38C6008C41EE /* PollsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PollsViewController.swift; sourceTree = "<group>"; };
42CC49D12AAE3904008C41EE /* PollsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PollsView.swift; sourceTree = "<group>"; };
42D9237029E0C69200E9E18E /* FitnessModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FitnessModel.swift; sourceTree = "<group>"; };
42D9237229E0C9AF00E9E18E /* FitnessViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FitnessViewController.swift; sourceTree = "<group>"; };
42D9237429E0CCFB00E9E18E /* FitnessView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FitnessView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1333,6 +1337,8 @@
children = (
6CAA4B4827A7639600473CC6 /* Networking */,
6CAA4B4A27A7639600473CC6 /* Model */,
42CC49CF2AAE38C6008C41EE /* PollsViewController.swift */,
42CC49D12AAE3904008C41EE /* PollsView.swift */,
);
path = Polls;
sourceTree = "<group>";
Expand Down Expand Up @@ -2435,6 +2441,7 @@
6CC88D6027B1BF51006896F6 /* CardHeaderView.swift in Sources */,
EF6329A22409D2CE00E7ED36 /* GSRGroupConfirmBookingController.swift in Sources */,
6CC88D7627B1BF51006896F6 /* DiningLoginNavigationView.swift in Sources */,
42CC49D22AAE3904008C41EE /* PollsView.swift in Sources */,
6C23AF9526E57903002F60F0 /* (null) in Sources */,
F2D416C528E9F8C800F50CAB /* NotificationViewModel.swift in Sources */,
B658393B1FABD295009486FC /* LaundryAPIService.swift in Sources */,
Expand Down Expand Up @@ -2462,6 +2469,7 @@
C1D90F1D2220A25700DAB8EE /* NoReservationsCell.swift in Sources */,
217A7834204D2DF5004F1227 /* HomeDiningCellItem.swift in Sources */,
C11DFA31219F90E5000FC573 /* CalendarEvent.swift in Sources */,
42CC49D02AAE38C6008C41EE /* PollsViewController.swift in Sources */,
6CC88D6B27B1BF51006896F6 /* DiningVenueView.swift in Sources */,
42D9237529E0CCFB00E9E18E /* FitnessView.swift in Sources */,
6C11C08026F842CF00407C04 /* HomeUpdateCellItem.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "Polls.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Polls 1.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Polls 2.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "Polls_Blue.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Polls_Blue 1.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Polls_Blue 2.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "Polls_Grey.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Polls_Grey 1.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Polls_Grey 2.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions PennMobile/Auth/OAuth2NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ extension OAuth2NetworkManager {
}
}
}

func getAccessTokenAsync() async -> AccessToken? {
return await withCheckedContinuation { continuation in
getAccessToken { accessToken in
continuation.resume(returning: accessToken)
}
}
}

func saveAccessToken(accessToken: AccessToken) {
OAuth2NetworkManager.authQueue.sync {
Expand Down
3 changes: 0 additions & 3 deletions PennMobile/GSR-Booking/Networking/GSRNetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ class GSRNetworkManager: NSObject, Requestable {
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

let deviceID = getDeviceID()
request.setValue(deviceID, forHTTPHeaderField: "X-Device-ID")

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXXXX"
Expand Down
4 changes: 0 additions & 4 deletions PennMobile/General/Networking + Analytics/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ extension Requestable {
let request = NSMutableURLRequest(url: url)
request.cachePolicy = .reloadIgnoringCacheData

let deviceID = getDeviceID()
request.setValue(deviceID, forHTTPHeaderField: "X-Device-ID")
if let accountID = UserDefaults.standard.getAccountID() {
request.setValue(accountID, forHTTPHeaderField: "X-Account-ID")
}
Expand Down Expand Up @@ -136,8 +134,6 @@ extension Requestable {
internal func getAnalyticsRequest(url: String) -> NSMutableURLRequest {
let url = URL(string: url)!
let request = NSMutableURLRequest(url: url)
let deviceID = getDeviceID()
request.setValue(deviceID, forHTTPHeaderField: "X-Device-ID")
if let accountID = UserDefaults.standard.getAccountID() {
request.setValue(accountID, forHTTPHeaderField: "X-Account-ID")
}
Expand Down
21 changes: 0 additions & 21 deletions PennMobile/General/Networking + Analytics/UserDBManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ import UIKit
import SwiftyJSON
import PennMobileShared

func getDeviceID() -> String {
let deviceID = UIDevice.current.identifierForVendor!.uuidString
#if DEBUG
return "test"
#else
return deviceID
#endif
}

class UserDBManager: NSObject, Requestable, SHA256Hashable {
static let shared = UserDBManager()
fileprivate let baseUrl = "https://api.pennlabs.org"
Expand Down Expand Up @@ -153,9 +144,6 @@ extension UserDBManager {
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSON(params).rawData()

let deviceID = getDeviceID()
request.setValue(deviceID, forHTTPHeaderField: "X-Device-ID")

let task = URLSession.shared.dataTask(with: request)
task.resume()
}
Expand All @@ -167,9 +155,6 @@ extension UserDBManager {
let url = URL(string: url)!
var request = token != nil ? URLRequest(url: url, accessToken: token!) : URLRequest(url: url)

let deviceID = getDeviceID()
request.setValue(deviceID, forHTTPHeaderField: "X-Device-ID")

let task = URLSession.shared.dataTask(with: request) { (data, _, _) in
if let data = data, let rooms = JSON(data)["rooms"].arrayObject {
callback(rooms.compactMap { $0 as? Int })
Expand Down Expand Up @@ -600,9 +585,6 @@ extension UserDBManager {
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSON(params).rawData()

let deviceID = getDeviceID()
request.setValue(deviceID, forHTTPHeaderField: "X-Device-ID")

let task = URLSession.shared.dataTask(with: request)
task.resume()
}
Expand All @@ -614,9 +596,6 @@ extension UserDBManager {
let url = URL(string: url)!
var request = token != nil ? URLRequest(url: url, accessToken: token!) : URLRequest(url: url)

let deviceID = getDeviceID()
request.setValue(deviceID, forHTTPHeaderField: "X-Device-ID")

let task = URLSession.shared.dataTask(with: request) { (data, _, _) in
if let data = data, let rooms = JSON(data)["rooms"].arrayObject {
callback(rooms.compactMap { $0 as? Int })
Expand Down
21 changes: 16 additions & 5 deletions PennMobile/Home/Cells/Polls/HomePollsCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ final class HomePollsCell: UITableViewCell, HomeCellConformable {

return CGFloat(totalHeight)
}

static func getPollHeight(for pollQuestion: PollQuestion) -> CGFloat {
var totalHeight: CGFloat = 63 + HomePollsCellFooter.height + 28 + 8

for i in 0..<pollQuestion.options.count {
totalHeight += HomePollsCell.height(forOption: pollQuestion.options[i])
}

return totalHeight + HomeViewController.cellSpacing
}

var pollQuestion: PollQuestion! {
didSet {
Expand Down Expand Up @@ -153,25 +163,26 @@ extension HomePollsCell: UITableViewDelegate {
tableView.deselectRow(at: indexPath, animated: false)
tableView.isUserInteractionEnabled = false
let pollOptionId = self.pollQuestion.options[indexPath.row].id
PollsNetworkManager.instance.answerPoll(withId: PollsNetworkManager.id, response: pollOptionId) { success in
Task {
let success = await PollsNetworkManager.instance.answerPoll(withId: PollsNetworkManager.id, response: pollOptionId)
if success {
DispatchQueue.main.async {
self.pollQuestion.options[indexPath.row].voteCount += 1
// Change selected cell to chosen
// Change selected cell to chosen
let chosenCell = (tableView.cellForRow(at: indexPath) as! PollOptionCell)
chosenCell.pollOption.voteCount += 1
chosenCell.chosen = true

// Update cells to reflect question answered
for cell in tableView.visibleCells as! [PollOptionCell] {
cell.totalResponses += 1
cell.answered = true
}

// Update model
self.pollQuestion.optionChosenId = self.pollQuestion.options[indexPath.row].id
}

// TODO: Send Network Request to reflect changes
} else {
print("not changing chosen")
Expand Down
15 changes: 10 additions & 5 deletions PennMobile/Home/Cells/Polls/HomePollsCellItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ import SwiftyJSON

final class HomePollsCellItem: HomeCellItem {
static func getHomeCellItem(_ completion: @escaping (([HomeCellItem]) -> Void)) {

PollsNetworkManager.instance.getActivePolls { polls in
if let polls = polls, polls.count > 0 {
completion([HomePollsCellItem(pollQuestion: polls[0])])
} else {
Task {
let pollsResult = await PollsNetworkManager.instance.getActivePolls()
switch pollsResult {
case .failure:
completion([])
case .success(let polls):
if polls.count > 0 {
completion([HomePollsCellItem(pollQuestion: polls[0])])
} else {
completion([])
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions PennMobile/Polls/Model/PollQuestion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct PollQuestion: Codable {
let id: Int
let question: String
let clubCode: String
let createdDate: Date
let startDate: Date
let expireDate: Date
let multiselect: Bool
Expand All @@ -35,3 +36,11 @@ struct PollQuestion: Codable {
}
var optionChosenId: Int?
}

struct PollPost: Codable {
let id: Int
let idHash: String
let poll: PollQuestion
let pollOptions: [PollOption]
//let createdDate: Date
}
Loading

0 comments on commit d1fa566

Please sign in to comment.