AppVersionManager.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // AppVersionManager.swift
  3. // LoopFollow
  4. //
  5. // Created by Jonas Björkert on 2024-05-11.
  6. // Copyright © 2024 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. class AppVersionManager {
  10. private let githubService = GitHubService()
  11. /// Checks for the availability of a new app version and if the current version is blacklisted.
  12. /// - Parameter completion: Returns latest version, a boolean for newer version existence, and blacklist status.
  13. /// Usage: `versionManager.checkForNewVersion { latestVersion, isNewer, isBlacklisted in ... }`
  14. func checkForNewVersion(completion: @escaping (String?, Bool, Bool) -> Void) {
  15. let currentVersion = version()
  16. let now = Date()
  17. // Retrieve cache
  18. let latestVersionChecked = UserDefaultsRepository.latestVersionChecked.value ?? Date.distantPast
  19. let latestVersion = UserDefaultsRepository.latestVersion.value
  20. let currentVersionBlackListed = UserDefaultsRepository.currentVersionBlackListed.value
  21. let cachedForVersion = UserDefaultsRepository.cachedForVersion.value
  22. // Reset notifications if version has changed
  23. if let cachedVersion = cachedForVersion, cachedVersion != currentVersion {
  24. UserDefaultsRepository.lastBlacklistNotificationShown.value = Date.distantPast
  25. UserDefaultsRepository.lastVersionUpdateNotificationShown.value = Date.distantPast
  26. }
  27. // Check if the cache is still valid
  28. if let cachedVersion = cachedForVersion, cachedVersion == currentVersion,
  29. now.timeIntervalSince(latestVersionChecked) < 24 * 3600, let latestVersion = latestVersion {
  30. let isNewer = isVersion(latestVersion, newerThan: currentVersion)
  31. completion(latestVersion, isNewer, currentVersionBlackListed)
  32. return
  33. }
  34. // Fetch new data if cache is outdated or not for current version
  35. fetchDataAndUpdateCache(currentVersion: currentVersion, completion: completion)
  36. }
  37. private func fetchDataAndUpdateCache(currentVersion: String, completion: @escaping (String?, Bool, Bool) -> Void) {
  38. githubService.fetchData(for: .versionConfig) { versionData in
  39. self.githubService.fetchData(for: .blacklistedVersions) { blacklistData in
  40. DispatchQueue.main.async {
  41. let fetchedVersion = versionData.flatMap { String(data: $0, encoding: .utf8) }
  42. .flatMap { self.parseVersionFromConfig(contents: $0) }
  43. let isNewer = fetchedVersion.map { self.isVersion($0, newerThan: currentVersion) } ?? false
  44. let isBlacklisted = (try? blacklistData.flatMap { try JSONDecoder().decode(Blacklist.self, from: $0) })
  45. .map { $0.blacklistedVersions.map { $0.version }.contains(currentVersion) } ?? false
  46. // Update cache with new data
  47. UserDefaultsRepository.latestVersion.value = fetchedVersion
  48. UserDefaultsRepository.latestVersionChecked.value = Date()
  49. UserDefaultsRepository.currentVersionBlackListed.value = isBlacklisted
  50. UserDefaultsRepository.cachedForVersion.value = currentVersion
  51. // Call completion with new data
  52. completion(fetchedVersion, isNewer, isBlacklisted)
  53. }
  54. }
  55. }
  56. }
  57. private func parseVersionFromConfig(contents: String) -> String? {
  58. let lines = contents.split(separator: "\n")
  59. for line in lines {
  60. if line.contains("LOOP_FOLLOW_MARKETING_VERSION") {
  61. let components = line.split(separator: "=").map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
  62. if components.count > 1 {
  63. return components[1]
  64. }
  65. }
  66. }
  67. return nil
  68. }
  69. private func isVersion(_ fetchedVersion: String, newerThan currentVersion: String) -> Bool {
  70. let fetchedVersionComponents = fetchedVersion.split(separator: ".").map { Int($0) ?? 0 }
  71. let currentVersionComponents = currentVersion.split(separator: ".").map { Int($0) ?? 0 }
  72. let maxCount = max(fetchedVersionComponents.count, currentVersionComponents.count)
  73. for i in 0..<maxCount {
  74. let fetched = i < fetchedVersionComponents.count ? fetchedVersionComponents[i] : 0
  75. let current = i < currentVersionComponents.count ? currentVersionComponents[i] : 0
  76. if fetched > current {
  77. return true
  78. } else if fetched < current {
  79. return false
  80. }
  81. }
  82. return false
  83. }
  84. func version() -> String {
  85. if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
  86. return version
  87. }
  88. return "Unknown"
  89. }
  90. struct Blacklist: Decodable {
  91. let blacklistedVersions: [VersionEntry]
  92. }
  93. struct VersionEntry: Decodable {
  94. let version: String
  95. }
  96. }