AppVersionManager.swift 4.9 KB

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