AppVersionManager.swift 5.1 KB

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