Skip to content

Commit 310f20f

Browse files
authored
Merge pull request #440 from pennlabs/widget/dining-analytics
Add Dining Analytics widget
2 parents 2c04d5a + b89e4cf commit 310f20f

32 files changed

Lines changed: 1151 additions & 255 deletions

PennMobile.xcodeproj/project.pbxproj

Lines changed: 112 additions & 42 deletions
Large diffs are not rendered by default.

PennMobile/Dining/SwiftUI/DiningAnalyticsView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ struct DiningAnalyticsView: View {
8181
showMissingDiningTokenAlert = true
8282
return
8383
}
84-
await diningAnalyticsViewModel.refresh()
84+
await diningAnalyticsViewModel.refresh(refreshWidgets: true)
8585
}
8686
.alert(isPresented: $showMissingDiningTokenAlert) {
8787
showCorrectAlert()
@@ -97,7 +97,7 @@ extension DiningAnalyticsView {
9797
func getSmoothedData(from trans: [DiningAnalyticsBalance]) -> [PredictionsGraphView.YXDataPoint] {
9898
let sos = Date.startOfSemester
9999
let eos = Date.endOfSemester
100-
100+
101101
let totalLength = eos.distance(to: sos)
102102
let maxDollarValue = trans.max(by: { $0.balance < $1.balance })?.balance ?? 1.0
103103
let yxPoints: [PredictionsGraphView.YXDataPoint] = trans.map { (t) -> PredictionsGraphView.YXDataPoint in

PennMobile/Dining/SwiftUI/DiningLoginViewSwiftUI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct DiningLoginViewSwiftUI: UIViewControllerRepresentable {
3232

3333
func dismissDiningLoginController() {
3434
parent.presentationMode.wrappedValue.dismiss()
35-
Task.init() {
35+
Task.init {
3636
await DiningViewModelSwiftUI.instance.refreshBalance()
3737
await parent.diningAnalyticsViewModel.refresh()
3838
}

PennMobile/Dining/SwiftUI/DiningViewModelSwiftUI.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class DiningViewModelSwiftUI: ObservableObject {
1818
@Published var diningVenuesIsLoading = false
1919
@Published var alertType: NetworkingError?
2020

21-
@Published var diningBalance = UserDefaults.standard.getDiningBalance() ?? DiningBalance(date: Date.dayOfMonthFormatter.string(from: Date()), diningDollars: "0.0", regularVisits: 0, guestVisits: 0, addOnVisits: 0)
21+
@Published var diningBalance = (try? Storage.retrieveThrowing(DiningBalance.directory, from: .groupCaches, as: DiningBalance.self)) ?? DiningBalance(date: Date.dayOfMonthFormatter.string(from: Date()), diningDollars: "0.0", regularVisits: 0, guestVisits: 0, addOnVisits: 0)
2222
// MARK: - Venue Methods
2323
let ordering: [VenueType] = [.dining, .retail]
2424

@@ -80,7 +80,7 @@ class DiningViewModelSwiftUI: ObservableObject {
8080
let result = await DiningAPI.instance.getDiningBalance(diningToken: diningToken)
8181
switch result {
8282
case .success(let balance):
83-
UserDefaults.standard.setdiningBalance(balance)
83+
Storage.store(balance, to: .groupCaches, as: DiningBalance.directory)
8484
self.diningBalance = balance
8585
case .failure:
8686
return

PennMobile/Dining/SwiftUI/Views/Venue/DiningVenueView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ struct CustomHeader: View {
7272
showMissingDiningTokenAlert = true
7373
return
7474
}
75-
Task.init() {
75+
Task.init {
7676
await DiningViewModelSwiftUI.instance.refreshBalance()
7777
}
7878
}, label: {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// KeychainAccessibleExtensions.swift
3+
// PennMobile
4+
//
5+
// Created by Anthony Li on 11/5/22.
6+
// Copyright © 2022 PennLabs. All rights reserved.
7+
//
8+
9+
extension KeychainAccessible {
10+
11+
func savePennKey(_ pennkey: String) {
12+
let secureStore = getWebLoginSecureStore()
13+
try? secureStore.setValue(pennkey, for: pennkeyKeychainKey)
14+
}
15+
16+
func removePennKey() {
17+
let secureStore = getWebLoginSecureStore()
18+
try? secureStore.removeValue(for: pennkeyKeychainKey)
19+
}
20+
21+
func savePassword(_ password: String) {
22+
let secureStore = getWebLoginSecureStore()
23+
try? secureStore.setValue(password, for: passwordKeychainKey)
24+
}
25+
26+
func removePassword() {
27+
let secureStore = getWebLoginSecureStore()
28+
try? secureStore.removeValue(for: passwordKeychainKey)
29+
}
30+
31+
func savePacCode(_ pacCode: String) {
32+
let secureStore = getWebLoginSecureStore()
33+
try? secureStore.setValue(pacCode, for: pacCodeKeychainKey)
34+
}
35+
36+
func removePacCode() {
37+
let secureStore = getWebLoginSecureStore()
38+
try? secureStore.removeValue(for: pacCodeKeychainKey)
39+
}
40+
41+
func saveDiningToken(_ diningToken: String) {
42+
let secureStore = getWebLoginSecureStore()
43+
try? secureStore.setValue(diningToken, for: diningTokenKeychainKey)
44+
}
45+
46+
func removeDiningToken() {
47+
let secureStore = getWebLoginSecureStore()
48+
try? secureStore.removeValue(for: diningTokenKeychainKey)
49+
UserDefaults.standard.clearDiningBalance()
50+
}
51+
52+
}

PennMobile/General/KeychainAccessible.swift

Lines changed: 0 additions & 114 deletions
This file was deleted.

PennMobile/General/Networking + Analytics/Networking.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ public enum Method {
1919
case other(method: String)
2020
}
2121

22-
enum NetworkingError: String, Error {
23-
case noInternet
24-
case parsingError
25-
case serverError
26-
case jsonError = "JSON error"
27-
case authenticationError = "Unable to authenticate"
28-
case other
29-
var localizedDescription: String { self.rawValue }
30-
}
31-
3222
extension Method {
3323
public init(_ rawValue: String) {
3424
let method = rawValue.uppercased()

PennMobile/General/Networking + Analytics/UserDBManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class UserDBManager: NSObject, Requestable, SHA256Hashable {
4646
task.resume()
4747
}
4848
}
49-
49+
5050
/**
5151
Returns a URLRequest configured for making anonymous requests. The server matches either the pennkey-password hash or the private UUID in the DB to find the anonymous account ID, updating the identifiers if the password of device changes.
5252

PennMobile/General/UserDefaults + Helpers.swift

100755100644
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -542,13 +542,15 @@ extension UserDefaults {
542542

543543
// MARK: - Current Dining Balance Object
544544
extension UserDefaults {
545+
@available(*, deprecated)
545546
func setdiningBalance(_ diningBalance: DiningBalance) {
546547
let encoder = JSONEncoder()
547548
if let encoded = try? encoder.encode(diningBalance) {
548549
UserDefaults.standard.set(encoded, forKey: UserDefaultsKeys.diningBalance.rawValue)
549550
}
550551
synchronize()
551552
}
553+
@available(*, deprecated)
552554
func getDiningBalance() -> DiningBalance? {
553555
let decoder = JSONDecoder()
554556
if let decodedData = UserDefaults.standard.data(forKey: UserDefaultsKeys.diningBalance.rawValue) {
@@ -561,23 +563,6 @@ extension UserDefaults {
561563
}
562564
}
563565

564-
// MARK: - Last dining analytics end date called
565-
extension UserDefaults {
566-
func setNextAnalyticsStartDate(_ date: String) {
567-
set(date, forKey: UserDefaultsKeys.nextAnalyticsStartDate.rawValue)
568-
synchronize()
569-
}
570-
571-
func getNextAnalyticsStartDate() -> String? {
572-
return string(forKey: UserDefaultsKeys.nextAnalyticsStartDate.rawValue)
573-
}
574-
575-
func clearNextAnalyticsStartDateDate() {
576-
removeObject(forKey: UserDefaultsKeys.nextAnalyticsStartDate.rawValue)
577-
}
578-
579-
}
580-
581566
extension UserDefaults {
582567
func setFirstDollarsBalance(_ balance: Double) {
583568
set(balance, forKey: UserDefaultsKeys.firstDollarsBalance.rawValue)

0 commit comments

Comments
 (0)