AppVersionManager.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // Check if the cache is still valid
  23. if let cachedVersion = cachedForVersion, cachedVersion == currentVersion,
  24. now.timeIntervalSince(latestVersionChecked) < 24 * 3600, let latestVersion = latestVersion {
  25. let isNewer = isVersion(latestVersion, newerThan: currentVersion)
  26. completion(latestVersion, isNewer, currentVersionBlackListed)
  27. return
  28. }
  29. // Fetch new data if cache is outdated or not for current version
  30. fetchDataAndUpdateCache(currentVersion: currentVersion, completion: completion)
  31. }
  32. private func fetchDataAndUpdateCache(currentVersion: String, completion: @escaping (String?, Bool, Bool) -> Void) {
  33. githubService.fetchData(for: .versionConfig) { versionData in
  34. self.githubService.fetchData(for: .blacklistedVersions) { blacklistData in
  35. DispatchQueue.main.async {
  36. let fetchedVersion = versionData.flatMap { String(data: $0, encoding: .utf8) }
  37. .flatMap { self.parseVersionFromConfig(contents: $0) }
  38. let isNewer = fetchedVersion.map { self.isVersion($0, newerThan: currentVersion) } ?? false
  39. let isBlacklisted = (try? blacklistData.flatMap { try JSONDecoder().decode(Blacklist.self, from: $0) })
  40. .map { $0.blacklistedVersions.map { $0.version }.contains(currentVersion) } ?? false
  41. // Update cache with new data
  42. UserDefaults.standard.set(fetchedVersion, forKey: "latestVersion")
  43. UserDefaults.standard.set(Date(), forKey: "latestVersionChecked")
  44. UserDefaults.standard.set(isBlacklisted, forKey: "isCurrentVersionBlacklisted")
  45. UserDefaults.standard.set(currentVersion, forKey: "cachedForVersion")
  46. // Call completion with new data
  47. completion(fetchedVersion, isNewer, isBlacklisted)
  48. }
  49. }
  50. }
  51. }
  52. private func parseVersionFromConfig(contents: String) -> String? {
  53. let lines = contents.split(separator: "\n")
  54. for line in lines {
  55. if line.contains("LOOP_FOLLOW_MARKETING_VERSION") {
  56. let components = line.split(separator: "=").map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
  57. if components.count > 1 {
  58. return components[1]
  59. }
  60. }
  61. }
  62. return nil
  63. }
  64. private func isVersion(_ fetchedVersion: String, newerThan currentVersion: String) -> Bool {
  65. let fetchedVersionComponents = fetchedVersion.split(separator: ".").map { Int($0) ?? 0 }
  66. let currentVersionComponents = currentVersion.split(separator: ".").map { Int($0) ?? 0 }
  67. let maxCount = max(fetchedVersionComponents.count, currentVersionComponents.count)
  68. for i in 0..<maxCount {
  69. let fetched = i < fetchedVersionComponents.count ? fetchedVersionComponents[i] : 0
  70. let current = i < currentVersionComponents.count ? currentVersionComponents[i] : 0
  71. if fetched > current {
  72. return true
  73. } else if fetched < current {
  74. return false
  75. }
  76. }
  77. return false
  78. }
  79. func version() -> String {
  80. if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
  81. return version
  82. }
  83. return "Unknown"
  84. }
  85. struct Blacklist: Decodable {
  86. let blacklistedVersions: [VersionEntry]
  87. }
  88. struct VersionEntry: Decodable {
  89. let version: String
  90. }
  91. }