@@ -168,32 +168,53 @@ class SourcesViewModel: ObservableObject {
168168 editingSource = nil
169169 newSourceURL = " "
170170 }
171+
172+ func validateSource( _ source: Source ) {
173+ validationStates [ source. urlString] = . loading
171174
172- func validateSource( _ source: Source ) {
173- validationStates [ source. urlString] = . loading
174-
175- guard let url = source. url else {
176- validationStates [ source. urlString] = . invalid( NSError ( domain: " Invalid URL " , code: 0 , userInfo: nil ) )
177- return
178- }
179-
180- var request = URLRequest ( url: url)
181- request. setValue ( " AppTestersListView/1.0 (iOS) " , forHTTPHeaderField: " User-Agent " )
182- request. timeoutInterval = 10
183-
184- URLSession . shared. dataTask ( with: request) { [ weak self] data, response, error in
185- DispatchQueue . main. async {
186- if let error = error {
187- self ? . validationStates [ source. urlString] = . invalid( error)
188- } else if let httpResponse = response as? HTTPURLResponse , !( 200 ... 299 ) . contains ( httpResponse. statusCode) {
189- let error = NSError ( domain: " HTTP Error " , code: httpResponse. statusCode, userInfo: nil )
190- self ? . validationStates [ source. urlString] = . invalid( error)
191- } else {
175+ guard let url = source. url else {
176+ validationStates [ source. urlString] = . invalid( NSError ( domain: " Invalid URL " , code: 0 , userInfo: nil ) )
177+ return
178+ }
179+
180+ var request = URLRequest ( url: url)
181+ request. setValue ( " ProStore/1.0 (iOS) " , forHTTPHeaderField: " User-Agent " )
182+ request. timeoutInterval = 10
183+
184+ URLSession . shared. dataTask ( with: request) { [ weak self] data, response, error in
185+ DispatchQueue . main. async {
186+ if let error = error {
187+ self ? . validationStates [ source. urlString] = . invalid( error)
188+ return
189+ }
190+
191+ if let httpResponse = response as? HTTPURLResponse , !( 200 ... 299 ) . contains ( httpResponse. statusCode) {
192+ let error = NSError ( domain: " HTTP Error " , code: httpResponse. statusCode, userInfo: nil )
193+ self ? . validationStates [ source. urlString] = . invalid( error)
194+ return
195+ }
196+
197+ guard let data = data else {
198+ let error = NSError ( domain: " No Data " , code: 0 , userInfo: nil )
199+ self ? . validationStates [ source. urlString] = . invalid( error)
200+ return
201+ }
202+
203+ do {
204+ if let jsonObject = try JSONSerialization . jsonObject ( with: data) as? [ String : Any ] ,
205+ let appsArray = jsonObject [ " apps " ] as? [ Any ] {
206+ // Valid JSON and contains an "apps" array
192207 self ? . validationStates [ source. urlString] = . valid
208+ } else {
209+ let error = NSError ( domain: " Invalid AltStore Source " , code: 0 , userInfo: [ NSLocalizedDescriptionKey: " JSON does not contain an apps array " ] )
210+ self ? . validationStates [ source. urlString] = . invalid( error)
193211 }
212+ } catch {
213+ self ? . validationStates [ source. urlString] = . invalid( error)
194214 }
195- } . resume ( )
196- }
215+ }
216+ } . resume ( )
217+ }
197218
198219 func validateAllSources( ) {
199220 for source in sources {
0 commit comments