Skip to content

Commit

Permalink
♻️ (LekaApp): Refactor UpdateManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Nov 28, 2024
1 parent 8f7eccf commit 1bf32f9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
28 changes: 15 additions & 13 deletions Apps/LekaApp/Sources/MainApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class AppDelegate: NSObject, UIApplicationDelegate {
}
}

// MARK: - UpdateManager

class UpdateManager: ObservableObject {
static let shared = UpdateManager()

@Published var appUpdateStatus: UpdateStatusFetcher.Status = .upToDate
@Published var osUpdateStatus: UpdateStatusFetcher.Status = .upToDate
}

// MARK: - LekaApp

@main
Expand All @@ -46,17 +55,10 @@ struct LekaApp: App {

// MARK: Internal

class UpdateStatus: ObservableObject {
static let shared = UpdateStatus()

@Published var status: UpdateStatusFetcher.Status = .upToDate
@Published var isOSUpdateAvailable: Bool = false
}

@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate

@Environment(\.colorScheme) var colorScheme
@StateObject var updateStatus: UpdateStatus = .shared
@StateObject var updateManager: UpdateManager = .shared
@ObservedObject var styleManager: StyleManager = .shared

var body: some Scene {
Expand Down Expand Up @@ -89,28 +91,28 @@ struct LekaApp: App {
guard let status = try? result.get() else {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self.showMainView = true
self.updateStatus.status = .upToDate
UpdateManager.shared.appUpdateStatus = .upToDate
}
return
}

DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self.showMainView = true
self.updateStatus.status = status
UpdateManager.shared.appUpdateStatus = status
}
}

switch Device.current {
case .iPad5,
.iPadPro9Inch:
self.updateStatus.isOSUpdateAvailable = false
UpdateManager.shared.osUpdateStatus = .upToDate
case .iPad6:
if Device.current.systemVersion!.compare("17.7.2") == .orderedAscending {
self.updateStatus.isOSUpdateAvailable = true
UpdateManager.shared.osUpdateStatus = .newerVersion
}
default:
if Device.current.systemVersion!.compare("18.1.1") == .orderedAscending {
self.updateStatus.isOSUpdateAvailable = true
UpdateManager.shared.osUpdateStatus = .newerVersion
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions Apps/LekaApp/Sources/Views/MainView/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,10 @@ struct MainView: View {
}
.onDisappear {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
switch self.updateStatus.status {
case .updateAvailable:
self.showingAppUpdateAlert = true
default:
if self.updateStatus.isOSUpdateAvailable {
self.showingOSUpdateAlert = true
}
if case .updateAvailable = UpdateManager.shared.appUpdateStatus {
self.showingAppUpdateAlert = true
} else if case .updateAvailable = UpdateManager.shared.osUpdateStatus {
self.showingOSUpdateAlert = true
}
}
}
Expand Down Expand Up @@ -435,7 +432,6 @@ struct MainView: View {

@StateObject private var caregiverManagerViewModel = CaregiverManagerViewModel()
@StateObject private var rootAccountViewModel = RootAccountManagerViewModel()
@StateObject var updateStatus: LekaApp.UpdateStatus = .shared

@State private var showingAppUpdateAlert: Bool = false
@State private var showingOSUpdateAlert: Bool = false
Expand Down
4 changes: 1 addition & 3 deletions Apps/LekaApp/Sources/Views/MainView/SettingsLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import SwiftUI
struct SettingsLabel: View {
// MARK: Internal

@StateObject var appUpdateStatus: LekaApp.UpdateStatus = .shared

var body: some View {
Label(String(l10n.SettingsLabel.buttonLabel.characters), systemImage: "gear")
.frame(width: 200, height: 44)
Expand All @@ -29,7 +27,7 @@ struct SettingsLabel: View {
.clipShape(.circle)
.offset(x: 95, y: -20)
.opacity({
if case .updateAvailable = self.appUpdateStatus.status {
if case .updateAvailable = UpdateManager.shared.appUpdateStatus {
return 1.0
}
return 0.0
Expand Down
3 changes: 1 addition & 2 deletions Apps/LekaApp/Sources/Views/Settings/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct SettingsView: View {

var body: some View {
Form {
if case .updateAvailable = self.appUpdateStatus.status {
if case .updateAvailable = UpdateManager.shared.appUpdateStatus {
Section {
VStack(alignment: .center) {
HStack(spacing: 20) {
Expand Down Expand Up @@ -246,7 +246,6 @@ struct SettingsView: View {
@ObservedObject private var styleManager: StyleManager = .shared
@ObservedObject private var navigation = Navigation.shared

@StateObject var appUpdateStatus: LekaApp.UpdateStatus = .shared
@StateObject private var rootAccountViewModel = RootAccountManagerViewModel()

private func reset() {
Expand Down

0 comments on commit 1bf32f9

Please sign in to comment.