-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppVersionManager.swift
More file actions
96 lines (90 loc) · 3.41 KB
/
AppVersionManager.swift
File metadata and controls
96 lines (90 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// AppVersionManager.swift
//
//
// Created by 茅根啓介 on 2023/03/09.
//
import Foundation
import Observation
import SwiftUI
/// A general-purpose operation recorder capable of determining the initial startup of an application or the startup of an application after an upgrade.
@available(iOS 17.0,macOS 14.0,watchOS 10.0,tvOS 17.0,visionOS 1.0,*)
@Observable
public class AppVersionManager {
private let userDefaults = UserDefaults.standard
/// Current app version
public let version: String
/// App version when the user last opened yout app
public var lastOpenedVersion: String {
didSet {
userDefaults.set(lastOpenedVersion, forKey: "LastOpenedVersion")
}
}
/// Whether or not this is the first activation.
public var isTheFirstActivation: Bool {
get {
return lastOpenedVersion == ""
}
set {
lastOpenedVersion = version
}
}
/// Variable to detect if the major version number has increased.
public var isMajorVersionUpdated: Bool {
get {
let lastOpenedComponents = filled(splitByDot(lastOpenedVersion), count: 3)
let currentComponents = filled(splitByDot(version), count: 3)
return lastOpenedComponents[0] < currentComponents[0]
}
set {
lastOpenedVersion = version
}
}
/// Variable to detect if the minor version number or higher has increased.
public var isMinorOrPatchVersionUpdated: Bool {
get {
let lastOpenedComponents = filled(splitByDot(lastOpenedVersion), count: 3)
let currentComponents = filled(splitByDot(version), count: 3)
return lastOpenedComponents[0] == currentComponents[0] &&
(lastOpenedComponents[1] < currentComponents[1] ||
(lastOpenedComponents[1] == currentComponents[1] && lastOpenedComponents[2] < currentComponents[2]))
}
set {
lastOpenedVersion = version
}
}
/// Default initializer
/// Creates a new AppVersionManager instance.
public init() {
self.version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
self.lastOpenedVersion = userDefaults.string(forKey: "LastOpenedVersion") ?? ""
}
}
/// AppVersionManager environment key
/// The environment key for the AppVersionManager.
@available(iOS 17.0,macOS 14.0,watchOS 10.0,tvOS 17.0,visionOS 1.0,*)
public struct AppVersionManagerKey: EnvironmentKey {
public static var defaultValue = AppVersionManager()
}
/// AppVersionManager environment values
public extension EnvironmentValues {
/// Accessor for the AppVersionManager value in EnvironmentValues.
var appVersionManager: AppVersionManager {
get { self[AppVersionManagerKey.self] }
set { self[AppVersionManagerKey.self] = newValue }
}
}
/// Function to split the version number dot by dot
@available(iOS 17.0,macOS 14.0,watchOS 10.0,tvOS 17.0,visionOS 1.0,*)
func splitByDot(_ versionNumber: String) -> [Int] {
return versionNumber.split(separator: ".").map { string -> Int in
return Int(string) ?? 0
}
}
/// Function to unify the number of elements in an array
@available(iOS 17.0,macOS 14.0,watchOS 10.0,tvOS 17.0,visionOS 1.0,*)
func filled(_ target: [Int], count: Int) -> [Int] {
return (0..<count).map { i -> Int in
(i < target.count) ? target[i] : 0
}
}