Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IPA library URL verification #881

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions PlayCover/Views/Settings/IPASourceSettings.swift
Expand Up @@ -181,6 +181,9 @@ struct AddSourceView: View {
@Binding var addSourceSheet: Bool
@EnvironmentObject var storeVM: StoreVM

@State var checkTask: Task<Void, Error>?
@State var urlSessionTask: URLSessionTask?

var body: some View {
VStack {
TextField(text: $newSource, label: {Text("preferences.textfield.url")})
Expand Down Expand Up @@ -238,6 +241,13 @@ struct AddSourceView: View {
.padding()
.frame(width: 400, height: 100)
.onChange(of: newSource) { source in
if let task = checkTask, !task.isCancelled {
if let session = urlSessionTask {
session.cancel()
}

task.cancel()
}
validateSource(source)
}
.onAppear {
Expand All @@ -259,13 +269,15 @@ struct AddSourceView: View {

sourceValidationState = .empty

Task {
checkTask = Task {
if var url = URL(string: source) {
if url.scheme == nil {
url = URL(string: "https://" + url.absoluteString) ?? url
}

URLSession.shared.dataTask(with: URLRequest(url: url)) { jsonData, response, error in
newSourceURL = url

urlSessionTask = URLSession.shared.dataTask(with: URLRequest(url: url)) { jsonData, response, error in
guard error == nil,
((response as? HTTPURLResponse)?.statusCode ?? 200) == 200,
let jsonData = jsonData else {
Expand All @@ -290,7 +302,9 @@ struct AddSourceView: View {
self.sourceValidationState = .badjson
}
}
}.resume()
}

urlSessionTask?.resume()

sourceValidationState = .checking

Expand Down