Skip to content
This repository was archived by the owner on Mar 7, 2026. It is now read-only.

Commit 13bb819

Browse files
authored
Improve Sources Manager
1 parent 560448c commit 13bb819

3 files changed

Lines changed: 50 additions & 29 deletions

File tree

Sources/prostore/viewModelsAndHelpers/SourcesViewModel.swift

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

Sources/prostore/views/AppsView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ final class RepoViewModel: ObservableObject {
265265
for url in sourceURLs {
266266
do {
267267
var request = URLRequest(url: url)
268-
request.setValue("AppTestersListView/1.0 (iOS)", forHTTPHeaderField: "User-Agent")
268+
request.setValue("ProStore/1.0 (iOS)", forHTTPHeaderField: "User-Agent")
269269
let (data, response) = try await URLSession.shared.data(for: request)
270270
if let http = response as? HTTPURLResponse, !(200...299).contains(http.statusCode) {
271271
throw NSError(domain: "RepoFetcher", code: http.statusCode, userInfo: [NSLocalizedDescriptionKey: "HTTP \(http.statusCode)"])

Sources/prostore/views/SourcesManagerView.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ struct SourcesManagerView: View {
108108
.navigationTitle("Sources Manager")
109109
.navigationBarTitleDisplayMode(.inline)
110110
.toolbar {
111+
ToolbarItem(placement: .navigationBarTrailing) {
112+
Button("Load Defaults") {
113+
sourcesViewModel.addDefaultSourcesIfNeeded()
114+
}
115+
}
116+
111117
ToolbarItem(placement: .navigationBarTrailing) {
112118
EditButton()
113119
}
@@ -119,12 +125,6 @@ struct SourcesManagerView: View {
119125
Image(systemName: "arrow.clockwise")
120126
}
121127
}
122-
123-
ToolbarItem(placement: .navigationBarLeading) {
124-
Button("Load Defaults") {
125-
sourcesViewModel.addDefaultSourcesIfNeeded()
126-
}
127-
}
128128
}
129129
.onAppear {
130130
sourcesViewModel.validateAllSources()

0 commit comments

Comments
 (0)