11//
2- // AppStoreManager .swift
2+ // .swift
33// AppStoreManager
44//
55// Created by Visarut Tippun on 8/1/21.
@@ -18,25 +18,15 @@ enum AppStoreDefaults: String {
1818 case storedSkippedVersion
1919}
2020
21- struct AppStoreResponse : Decodable {
22- var resultCount : Int ?
23- var results : [ AppStoreResult ]
24- }
25-
26- struct AppStoreResult : Decodable {
27- var trackId : Int ?
28- var version : String ?
29- }
30-
3121public class AppStoreManager {
3222
3323 public static let shared = AppStoreManager ( )
3424
35- var title : String = " New version available "
36- var message : String ? = " There is an update available. Please update to use this application. "
25+ var title : String = AppStoreManagerConstant . alertTitle
26+ var message : String ? = AppStoreManagerConstant . alertMessage
3727
38- var skipButtonTitle : String = " Skip "
39- var updateButtonTitle : String = " Update "
28+ var skipButtonTitle : String = AppStoreManagerConstant . skipButtonTitle
29+ var updateButtonTitle : String = AppStoreManagerConstant . updateButtonTitle
4030
4131 var lastVersionCheckDate : Date ? {
4232 didSet{
@@ -67,44 +57,44 @@ public class AppStoreManager {
6757 return
6858 }
6959 let session = URLSession ( configuration: . default)
70- let task = session. dataTask ( with: url) { ( data, response, error) in
60+ let task = session. dataTask ( with: url) { [ weak self ] ( data, response, error) in
7161 if let er = error {
72- self . log ( er. localizedDescription)
62+ self ? . log ( er. localizedDescription)
7363 completion ( nil )
7464 }
7565 if let safeData = data,
7666 let responseData = try ? JSONDecoder ( ) . decode ( AppStoreResponse . self, from: safeData) ,
7767 let result = responseData. results. first {
78- self . log ( " AppStore ID: \( result. trackId ?? 0 ) " )
79- self . log ( " AppStore version: \( result. version ?? " " ) " )
80- self . appStoreResult = result
68+ self ? . log ( " AppStore ID: \( result. trackId ?? 0 ) " )
69+ self ? . log ( " AppStore version: \( result. version ?? " " ) " )
70+ self ? . appStoreResult = result
8171 completion ( result)
8272 } else {
83- self . appStoreResult = nil
73+ self ? . appStoreResult = nil
8474 completion ( nil )
8575 }
8676 }
8777 task. resume ( )
8878 }
8979
9080 public func checkNewVersion( _ type: VersionCheckType , isAvailable: @escaping ( Bool ) -> ( ) ) {
91- self . getStoreVersion { ( result) in
92- if let currentInstalledVersion = self . currentInstalledVersion,
81+ self . getStoreVersion { [ weak self ] ( result) in
82+ if let currentInstalledVersion = self ? . currentInstalledVersion,
9383 let appStoreVersion = result? . version {
9484 switch currentInstalledVersion. compare ( appStoreVersion, options: . numeric) {
9585 case . orderedAscending:
9686 switch type {
9787 case . immediately:
98- self . lastVersionCheckDate = Date ( )
88+ self ? . lastVersionCheckDate = Date ( )
9989 isAvailable ( true )
10090 default :
101- guard let lastVersionCheckDate = self . lastVersionCheckDate else {
102- self . lastVersionCheckDate = Date ( )
91+ guard let lastVersionCheckDate = self ? . lastVersionCheckDate else {
92+ self ? . lastVersionCheckDate = Date ( )
10393 isAvailable ( true )
10494 return
10595 }
10696 if Date . days ( since: lastVersionCheckDate) >= type. rawValue {
107- self . lastVersionCheckDate = Date ( )
97+ self ? . lastVersionCheckDate = Date ( )
10898 isAvailable ( true )
10999 } else {
110100 isAvailable ( false )
@@ -120,42 +110,42 @@ public class AppStoreManager {
120110 }
121111
122112 public func checkNewVersionAndShowAlert( _ type: VersionCheckType , at vc: UIViewController , canSkip: Bool , preferredStyle: UIAlertController . Style = . alert) {
123- self . getStoreVersion { ( result) in
124- if let currentInstalledVersion = self . currentInstalledVersion,
113+ self . getStoreVersion { [ weak self ] ( result) in
114+ if let currentInstalledVersion = self ? . currentInstalledVersion,
125115 let appStoreVersion = result? . version {
126116 switch currentInstalledVersion. compare ( appStoreVersion, options: . numeric) {
127117 case . orderedAscending:
128- self . lastVersionCheckDate = Date ( )
129- self . showAlertUpdate ( at: vc, canSkip: canSkip, preferredStyle: preferredStyle)
118+ self ? . lastVersionCheckDate = Date ( )
119+ self ? . showAlertUpdate ( at: vc, canSkip: canSkip, preferredStyle: preferredStyle)
130120 case . orderedDescending, . orderedSame:
131121 break
132122 }
133123 } else {
134- self . log ( " Can't get Version " )
124+ self ? . log ( " Can't get Version " )
135125 }
136126 }
137127 }
138128
139129 //MARK: - Alert
140130
141131 public func configureAlert( title: String ? , message: String ? ) {
142- self . title = title ?? " New version available "
132+ self . title = title ?? AppStoreManagerConstant . alertTitle
143133 self . message = message
144134 }
145135
146136 public func configureAlert( updateButtonTitle: String ? , skipButtonTitle: String ? ) {
147- self . updateButtonTitle = updateButtonTitle ?? " Update "
148- self . skipButtonTitle = skipButtonTitle ?? " Skip "
137+ self . updateButtonTitle = updateButtonTitle ?? AppStoreManagerConstant . updateButtonTitle
138+ self . skipButtonTitle = skipButtonTitle ?? AppStoreManagerConstant . skipButtonTitle
149139 }
150140
151141 public func showAlertUpdate( at vc: UIViewController , canSkip: Bool , preferredStyle: UIAlertController . Style = . alert) {
152- DispatchQueue . main. async {
153- let alertVc = UIAlertController ( title: self . title, message: self . message, preferredStyle: preferredStyle)
154- let skip = UIAlertAction ( title: " Skip " , style: . cancel) { ( _) in
142+ DispatchQueue . main. async { [ weak self ] in
143+ let alertVc = UIAlertController ( title: self ? . title, message: self ? . message, preferredStyle: preferredStyle)
144+ let skip = UIAlertAction ( title: AppStoreManagerConstant . skipButtonTitle , style: . cancel) { ( _) in
155145 //
156146 }
157- let update = UIAlertAction ( title: " Update " , style: . default) { ( _) in
158- self . openAppStore ( )
147+ let update = UIAlertAction ( title: AppStoreManagerConstant . updateButtonTitle , style: . default) { ( _) in
148+ self ? . openAppStore ( )
159149 }
160150 alertVc. addAction ( update)
161151 if canSkip {
@@ -169,12 +159,12 @@ public class AppStoreManager {
169159 if let appStoreId = self . appStoreResult? . trackId {
170160 self . openAppStore ( id: appStoreId)
171161 } else {
172- self . getStoreVersion { ( result) in
162+ self . getStoreVersion { [ weak self ] ( result) in
173163 guard let appStoreId = result? . trackId else {
174- self . log ( " Can't get an AppId " )
164+ self ? . log ( " Can't get an AppId " )
175165 return
176166 }
177- self . openAppStore ( id: appStoreId)
167+ self ? . openAppStore ( id: appStoreId)
178168 }
179169 }
180170 }
0 commit comments