-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1676 from p2p-org/feature/pwn-793
Alert user about new update
- Loading branch information
Showing
10 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
p2p_wallet/Common/Services/ApplicationUpdate/ApplicationUpdateManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import Combine | ||
import Foundation | ||
|
||
class ApplicationUpdateManager { | ||
enum State { | ||
case updateAvailable(Version) | ||
case noUpdate | ||
} | ||
|
||
private let provider: ApplicationUpdateProvider | ||
|
||
init(provider: ApplicationUpdateProvider) { | ||
self.provider = provider | ||
} | ||
|
||
var currentInstalledVersion: Version? { | ||
let appVersionKey = "CFBundleShortVersionString" | ||
guard let appVersionValue = Bundle.main.object(forInfoDictionaryKey: appVersionKey) as? String else { | ||
return nil | ||
} | ||
|
||
return try? Version(from: appVersionValue) | ||
} | ||
|
||
func isUpdateAvailable() async -> State { | ||
guard | ||
let appVersion = currentInstalledVersion, | ||
let storeAppVersion = try? await provider.info() | ||
else { | ||
return .noUpdate | ||
} | ||
|
||
print("[ApplicationUpdateManager]", appVersion, storeAppVersion) | ||
|
||
// Check | ||
if storeAppVersion.major > appVersion.major { | ||
return .updateAvailable(storeAppVersion) | ||
} else if storeAppVersion.minor > appVersion.minor { | ||
return .updateAvailable(storeAppVersion) | ||
} else if storeAppVersion.patch > appVersion.patch { | ||
return .updateAvailable(storeAppVersion) | ||
} | ||
|
||
return .noUpdate | ||
} | ||
|
||
func awareUser(version: Version) async { | ||
UserDefaults.standard.set(version.string, forKey: "application_user_awareness") | ||
} | ||
|
||
func isUserAwareAboutUpdate(version: Version) async -> Bool { | ||
guard let userAwareness = UserDefaults.standard.object(forKey: "application_user_awareness") as? String else { | ||
return false | ||
} | ||
|
||
if version.string == userAwareness { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
p2p_wallet/Common/Services/ApplicationUpdate/ApplicationUpdateProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Firebase | ||
import Foundation | ||
|
||
protocol ApplicationUpdateProvider { | ||
func info() async throws -> Version | ||
} | ||
|
||
class FirebaseApplicationUpdateProvider: ApplicationUpdateProvider { | ||
func info() async throws -> Version { | ||
let remoteConfig = RemoteConfig.remoteConfig() | ||
let appVersion = remoteConfig.configValue(forKey: "app_version", source: .remote).stringValue | ||
|
||
guard let appVersion else { | ||
throw Version.VersionError.invalidFormat | ||
} | ||
|
||
return try Version(from: appVersion) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
p2p_wallet/Common/Services/ApplicationUpdate/Version.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Foundation | ||
|
||
struct Version: Decodable { | ||
// MARK: - Enumerations | ||
|
||
enum VersionError: Error { | ||
case invalidFormat | ||
} | ||
|
||
// MARK: - Public properties | ||
|
||
let major: Int | ||
let minor: Int | ||
let patch: Int | ||
|
||
// MARK: - Init | ||
|
||
init(from decoder: Decoder) throws { | ||
do { | ||
let container = try decoder.singleValueContainer() | ||
let version = try container.decode(String.self) | ||
try self.init(from: version) | ||
} catch { | ||
throw VersionError.invalidFormat | ||
} | ||
} | ||
|
||
init(from version: String) throws { | ||
let versionComponents = version.components(separatedBy: ".").map { Int($0) } | ||
guard versionComponents.count == 3 else { | ||
throw VersionError.invalidFormat | ||
} | ||
|
||
guard let major = versionComponents[0], let minor = versionComponents[1], | ||
let patch = versionComponents[2] | ||
else { | ||
throw VersionError.invalidFormat | ||
} | ||
|
||
self.major = major | ||
self.minor = minor | ||
self.patch = patch | ||
} | ||
|
||
var string: String { | ||
"\(major).\(minor).\(patch)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters