@@ -22,8 +22,8 @@ public class AppVersionManager {
2222 userDefaults. set ( lastOpenedVersion, forKey: " LastOpenedVersion " )
2323 }
2424 }
25- /// Whether or not this is the first activation
26- public var isTheFirstLaunch : Bool {
25+ /// Whether or not this is the first activation.
26+ public var isTheFirstActivation : Bool {
2727 get {
2828 return lastOpenedVersion == " "
2929 }
@@ -35,53 +35,62 @@ public class AppVersionManager {
3535 /// Variable to detect if the major version number has increased.
3636 public var isMajorVersionUpdated : Bool {
3737 get {
38- if !lastOpenedVersion. isEmpty {
39- let lastOpenedComponents = parseVersion ( lastOpenedVersion)
40- let currentComponents = parseVersion ( version)
41- return lastOpenedComponents. major < currentComponents. major
42- } else {
43- return false
44- }
38+ let lastOpenedComponents = filled ( splitByDot ( lastOpenedVersion) , count: 3 )
39+ let currentComponents = filled ( splitByDot ( version) , count: 3 )
40+ return lastOpenedComponents [ 0 ] < currentComponents [ 0 ]
4541 }
4642
4743 set {
4844 lastOpenedVersion = version
4945 }
5046 }
5147 /// Variable to detect if the minor version number or higher has increased.
52- public var isMinorVersionUpdated : Bool {
48+ public var isMinorOrPatchVersionUpdated : Bool {
5349 get {
54- if !lastOpenedVersion. isEmpty {
55- let lastOpenedComponents = parseVersion ( lastOpenedVersion)
56- let currentComponents = parseVersion ( version)
57- return lastOpenedComponents. major == currentComponents. major && lastOpenedComponents. minor < currentComponents. minor
58- } else {
59- return false
60- }
50+ let lastOpenedComponents = filled ( splitByDot ( lastOpenedVersion) , count: 3 )
51+ let currentComponents = filled ( splitByDot ( version) , count: 3 )
52+ return lastOpenedComponents [ 0 ] == currentComponents [ 0 ] &&
53+ ( lastOpenedComponents [ 1 ] < currentComponents [ 1 ] ||
54+ ( lastOpenedComponents [ 1 ] == currentComponents [ 1 ] && lastOpenedComponents [ 2 ] < currentComponents [ 2 ] ) )
6155 }
6256
6357 set {
6458 lastOpenedVersion = version
6559 }
6660 }
6761 /// Default initializer
62+ /// Creates a new AppVersionManager instance.
6863 public init ( ) {
69- self . version = Bundle . main
70- . object (
71- forInfoDictionaryKey: " CFBundleShortVersionString "
72- ) as! String
73- self . lastOpenedVersion = userDefaults
74- . string ( forKey: " LastOpenedVersion " ) ?? " "
75- }
76-
77- func parseVersion( _ versionString: String ) -> ( major: Int , minor: Int , patch: Int ) {
78- var components = versionString. split ( separator: " . " ) . compactMap { Int ( $0) }
79- components = ( 0 ..< 3 ) . map { $0 < components. count ? components [ $0] : 0 }
80- return ( major: components [ 0 ] , minor: components [ 1 ] , patch: components [ 2 ] )
64+ self . version = Bundle . main. object ( forInfoDictionaryKey: " CFBundleShortVersionString " ) as! String
65+ self . lastOpenedVersion = userDefaults. string ( forKey: " LastOpenedVersion " ) ?? " "
8166 }
8267}
83- /// AppVersionManager environment values
68+ /// AppVersionManager environment key
69+ /// The environment key for the AppVersionManager.
8470@available ( iOS 17 . 0 , macOS 14 . 0 , watchOS 10 . 0 , tvOS 17 . 0 , visionOS 1 . 0 , * )
71+ public struct AppVersionManagerKey : EnvironmentKey {
72+ public static var defaultValue = AppVersionManager ( )
73+ }
74+ /// AppVersionManager environment values
8575public extension EnvironmentValues {
86- @Entry var appVersionManager : AppVersionManager = AppVersionManager ( )
76+ /// Accessor for the AppVersionManager value in EnvironmentValues.
77+ var appVersionManager : AppVersionManager {
78+ get { self [ AppVersionManagerKey . self] }
79+ set { self [ AppVersionManagerKey . self] = newValue }
80+ }
8781}
82+ /// Function to split the version number dot by dot
83+ @available ( iOS 17 . 0 , macOS 14 . 0 , watchOS 10 . 0 , tvOS 17 . 0 , visionOS 1 . 0 , * )
84+ func splitByDot( _ versionNumber: String ) -> [ Int ] {
85+ return versionNumber. split ( separator: " . " ) . map { string -> Int in
86+ return Int ( string) ?? 0
87+ }
88+ }
89+ /// Function to unify the number of elements in an array
90+ @available ( iOS 17 . 0 , macOS 14 . 0 , watchOS 10 . 0 , tvOS 17 . 0 , visionOS 1 . 0 , * )
91+ func filled( _ target: [ Int ] , count: Int ) -> [ Int ] {
92+ return ( 0 ..< count) . map { i -> Int in
93+ ( i < target. count) ? target [ i] : 0
94+ }
95+ }
96+
0 commit comments