AppVersionManager.swift 5.0 KB

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