Skip to content

Commit

Permalink
Fix account VM attempting registration for exams before token is not …
Browse files Browse the repository at this point in the history
…null
  • Loading branch information
adisve committed Oct 26, 2023
1 parent c7f81b7 commit e6fdfa1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Tumble/Data/DataSource/Kronox/KronoxManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ enum KronoxManagerError: LocalizedError {
class KronoxManager {
private let urlRequestUtils = NetworkUtilities.shared
private let session = URLSession.shared
private let decoder = JSONDecoder()

func get<NetworkResponse: Decodable>(
_ endpoint: Endpoint,
Expand Down Expand Up @@ -83,7 +84,6 @@ class KronoxManager {

if statusCode == 200 {
do {
let decoder = JSONDecoder()
let decodedData = try decoder.decode(NetworkResponse.self, from: data)
return decodedData
} catch {
Expand Down
21 changes: 12 additions & 9 deletions Tumble/Observables/ViewModels/AccountViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ final class AccountViewModel: ObservableObject {
private var resourceSectionDataTask: URLSessionDataTask? = nil
private var eventSectionDataTask: URLSessionDataTask? = nil
private let popupFactory: PopupFactory = PopupFactory.shared
private var registeredForExams: Bool = false


var userDisplayName: String? {
Expand All @@ -59,22 +60,23 @@ final class AccountViewModel: ObservableObject {

init() {
setupPublishers()
Task {
if self.userController.autoSignup {
await self.registerAutoSignup()
}
}
}

private func setupPublishers() {
let authStatusPublisher = userController.$authStatus.receive(on: RunLoop.main)
let authSchoolIdPublisher = preferenceService.$authSchoolId.receive(on: RunLoop.main)
Publishers.CombineLatest(authStatusPublisher, authSchoolIdPublisher)
.sink { [weak self] authStatus, authSchoolId in
DispatchQueue.main.async {
self?.authStatus = authStatus
self?.authSchoolId = authSchoolId
}
guard let self else { return }
DispatchQueue.main.async {
self.authStatus = authStatus
self.authSchoolId = authSchoolId
}
if authStatus == .authorized && !self.registeredForExams {
Task.detached(priority: .userInitiated) {
await self.registerAutoSignup()
}
}
}
.store(in: &cancellables)
}
Expand Down Expand Up @@ -292,6 +294,7 @@ final class AccountViewModel: ObservableObject {
}
let _: Response.KronoxEventRegistration?
= try await kronoxManager.put(request, refreshToken: refreshToken.value, body: Request.Empty())
self.registeredForExams = true
} catch {
AppLogger.shared.error("Failed to sign up for exams: \(error)")
}
Expand Down
23 changes: 12 additions & 11 deletions Tumble/Observables/ViewModels/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,30 @@ final class HomeViewModel: ObservableObject {
@Published var todaysEventsCards: [WeekEventCardModel] = .init()
@Published var nextClass: Event? = nil

private var fetchedNewsDuringSession: Bool = false
private var initialisedSession: Bool = false
private let viewModelFactory: ViewModelFactory = .shared
private let appController: AppController = .shared
private var schedulesToken: NotificationToken?
private var cancellable: AnyCancellable? = nil
private var cancellables = Set<AnyCancellable>()

init() {
setupRealmListener()
setupNetworkPublisher()
setupPublishers()
}

/// Listen for change in network connection, if connected attempt
/// to fetch latest news from server
private func setupNetworkPublisher() {
private func setupPublishers() {
let networkConnectionPublisher = networkController.$connected.receive(on: RunLoop.main)
cancellable = networkConnectionPublisher
.sink { [weak self] connected in
let isUpdatingBookmarksPublisher = appController.$isUpdatingBookmarks.receive(on: RunLoop.main)
Publishers.CombineLatest(networkConnectionPublisher, isUpdatingBookmarksPublisher)
.sink { [weak self] connected, isUpdating in
guard let self else { return }
if connected && !self.fetchedNewsDuringSession {
if connected && !self.initialisedSession && !isUpdating {
self.setupRealmListener()
Task.detached(priority: .userInitiated) {
await self.fetchNews()
}
}
}
.store(in: &cancellables)
}

/// Initializes a listener that performs updates on the
Expand Down Expand Up @@ -126,7 +127,7 @@ final class HomeViewModel: ObservableObject {
}

deinit {
cancellable?.cancel()
cancellables.forEach({ $0.cancel() })
}

}
4 changes: 2 additions & 2 deletions Tumble/Observables/ViewModels/ParentViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ final class ParentViewModel: ObservableObject {
self.authSchoolId = authSchoolId

if connected && self.updateShouldOccur() && !appController.isUpdatingBookmarks {
AppLogger.shared.info("Updating all bookmarks ...")
AppLogger.shared.debug("Updating all bookmarks ...")
self.updateRealmSchedules()
}
}
Expand All @@ -105,8 +105,8 @@ final class ParentViewModel: ObservableObject {
func updateBookmarks(scheduleIds: [String]) async {

appController.isUpdatingBookmarks = true

defer { self.preferenceService.setLastUpdated(time: Date()) }

var updatedSchedules = 0
let scheduleCount: Int = scheduleIds.count

Expand Down

0 comments on commit e6fdfa1

Please sign in to comment.