Skip to content

Commit

Permalink
Add last updated preference and implement in schedule update
Browse files Browse the repository at this point in the history
  • Loading branch information
adisve committed Oct 18, 2023
1 parent 8481723 commit ca2b3b8
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 7 deletions.
9 changes: 9 additions & 0 deletions Tumble/Data/Repository/Preferences/PreferenceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,20 @@ class PreferenceService {
UserDefaults.standard.synchronize()
}

func setLastUpdated(time: Date) {
UserDefaults.standard.set(time, forKey: StoreKey.lastUpdated.rawValue)
UserDefaults.standard.synchronize()
}

// ----------- GET -----------
func getDefault(key: String) -> Any? {
return UserDefaults.standard.object(forKey: key)
}

func getLastUpdated() -> Date? {
return UserDefaults.standard.object(forKey: StoreKey.lastUpdated.rawValue) as? Date
}

func getDefaultViewType() -> ViewType {
let hasView: Bool = isKeyPresentInUserDefaults(key: StoreKey.viewType.rawValue)
if !hasView {
Expand Down
1 change: 1 addition & 0 deletions Tumble/Domain/Model/Preferences/StoreKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ enum StoreKey: String {
case viewType = "view_type"
case userOnboarded = "user_onboarded"
case networkSettings = "network_settings"
case lastUpdated = "last_updated"
}
2 changes: 1 addition & 1 deletion Tumble/Observables/Controllers/AppController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ class AppController: ObservableObject {

@Published var eventSheet: EventDetailsSheetModel?
@Published var selectedAppTab: TabbarTabType = .home
@Published var updatingBookmarks: Bool = true
@Published var updatingBookmarks: Bool = false
}
1 change: 1 addition & 0 deletions Tumble/Observables/ViewModels/BookmarksViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final class BookmarksViewModel: ObservableObject {
@Published var defaultViewType: ViewType = .list
@Published var eventSheet: EventDetailsSheetModel? = nil
@Published var status: BookmarksViewStatus = .loading

@Published var bookmarkData: BookmarkData = BookmarkData(days: [], calendarEventsByDate: [:], weeks: [:])

private var schedulesToken: NotificationToken? = nil
Expand Down
20 changes: 16 additions & 4 deletions Tumble/Observables/ViewModels/ParentViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ final class ParentViewModel: ObservableObject {
@Published var authSchoolId: Int = -1
@Published var userNotOnBoarded: Bool = false

private var attemptedUpdateDuringSession: Bool = false
private var schedulesToken: NotificationToken? = nil
private var cancellables = Set<AnyCancellable>()

Expand Down Expand Up @@ -78,21 +77,34 @@ final class ParentViewModel: ObservableObject {
self.userNotOnBoarded = !userOnBoarded
self.authSchoolId = authSchoolId

if connected && !self.attemptedUpdateDuringSession {
if connected && updateShouldOccur() {
self.updateRealmSchedules()
}
}
.store(in: &cancellables)
}

func updateShouldOccur() -> Bool {
if let lastUpdate = preferenceService.getLastUpdated() {
if let threeHoursAgo = Calendar.current.date(byAdding: .hour, value: -3, to: Date()) {
if lastUpdate <= threeHoursAgo {
return true
} else {
return false
}
}
}
return true
}

/// Attempts to update the locally stored schedules
/// by retrieving all the schedules by id from the backend.
@MainActor
func updateBookmarks(scheduleIds: [String]) async {

appController.updatingBookmarks = true

defer { self.attemptedUpdateDuringSession = true }
defer { self.preferenceService.setLastUpdated(time: Date()) }
var updatedSchedules = 0
let scheduleCount: Int = scheduleIds.count

Expand Down Expand Up @@ -142,7 +154,7 @@ final class ParentViewModel: ObservableObject {
private func updateRealmSchedules() {
// Get schedules from Realm database
let schedules = realmManager.getAllLiveSchedules()
if !schedules.isEmpty && !self.attemptedUpdateDuringSession {
if !schedules.isEmpty {
// Filter out invalidated schedules and get their IDs
let scheduleIds = Array(schedules).filter { !$0.isInvalidated }.map { $0.scheduleId }

Expand Down
2 changes: 1 addition & 1 deletion Tumble/Presentation/Views/AppParent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import SwiftUI
import PopupView
import MijickPopupView

/// All navigation occurs from this view
struct AppParent: View {
Expand Down
2 changes: 1 addition & 1 deletion Tumble/TumbleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import SwiftUI
import PopupView
import MijickPopupView

@main
struct TumbleApp: App {
Expand Down

0 comments on commit ca2b3b8

Please sign in to comment.