Skip to content

Commit 485531e

Browse files
committed
WIP
1 parent d9cbfa6 commit 485531e

File tree

8 files changed

+227
-184
lines changed

8 files changed

+227
-184
lines changed

native/swift/Example/Example.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
242132C82CE69CE80021D8E8 /* WordPressAPI in Frameworks */ = {isa = PBXBuildFile; productRef = 242132C72CE69CE80021D8E8 /* WordPressAPI */; };
11+
242CA0C12D03A7E200C0DD68 /* LoginReport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 242CA0C02D03A7DF00C0DD68 /* LoginReport.swift */; };
1112
242D648E2C3602C1007CA96C /* ListViewData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 242D648D2C3602C1007CA96C /* ListViewData.swift */; };
1213
242D64922C360687007CA96C /* RootListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 242D64912C360687007CA96C /* RootListView.swift */; };
1314
242D64942C3608C6007CA96C /* ListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 242D64932C3608C6007CA96C /* ListView.swift */; };
@@ -23,6 +24,7 @@
2324
/* End PBXBuildFile section */
2425

2526
/* Begin PBXFileReference section */
27+
242CA0C02D03A7DF00C0DD68 /* LoginReport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginReport.swift; sourceTree = "<group>"; };
2628
242D648D2C3602C1007CA96C /* ListViewData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListViewData.swift; sourceTree = "<group>"; };
2729
242D64912C360687007CA96C /* RootListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RootListView.swift; sourceTree = "<group>"; };
2830
242D64932C3608C6007CA96C /* ListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListView.swift; sourceTree = "<group>"; };
@@ -55,6 +57,7 @@
5557
242D64972C363960007CA96C /* UI */ = {
5658
isa = PBXGroup;
5759
children = (
60+
242CA0C02D03A7DF00C0DD68 /* LoginReport.swift */,
5861
2479BF872B621CB70014A01D /* Preview Content */,
5962
242D64932C3608C6007CA96C /* ListView.swift */,
6063
242D64912C360687007CA96C /* RootListView.swift */,
@@ -191,6 +194,7 @@
191194
files = (
192195
242D64922C360687007CA96C /* RootListView.swift in Sources */,
193196
2479BF812B621CB60014A01D /* ExampleApp.swift in Sources */,
197+
242CA0C12D03A7E200C0DD68 /* LoginReport.swift in Sources */,
194198
24A3C32F2BA8F96F00162AD1 /* LoginView.swift in Sources */,
195199
2479BF932B621E9B0014A01D /* ListViewModel.swift in Sources */,
196200
24A3C3362BAA874C00162AD1 /* LoginManager.swift in Sources */,

native/swift/Example/Example.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import Foundation
2+
import SwiftUI
3+
import WordPressAPI
4+
import AuthenticationServices
5+
import WordPressAPIInternal
6+
7+
struct AutoDiscoveryStepView: View {
8+
let label: String
9+
10+
let successIcon = "checkmark.circle"
11+
let failureIcon = "exclamationmark.circle"
12+
13+
let isSuccess: Bool
14+
15+
var body: some View {
16+
VStack(alignment: .leading) {
17+
HStack(alignment: .firstTextBaseline) {
18+
Image(systemName: isSuccess ? successIcon : failureIcon)
19+
.font(.title)
20+
Text(label).font(.title)
21+
Spacer()
22+
}
23+
}.padding(.horizontal)
24+
}
25+
}
26+
27+
struct AutoDiscoveryErrorView: View {
28+
let errorMessage: String
29+
30+
var body: some View {
31+
Text(errorMessage)
32+
}
33+
}
34+
35+
struct AutodiscoveryReportView: View {
36+
let report: AutoDiscoveryResult?
37+
38+
var body: some View {
39+
if let report {
40+
if let success = report.successfulAttempt {
41+
AutodiscoveryResultView(attempt: success)
42+
} else if let success = report.autoHttpsAttempt {
43+
AutodiscoveryResultView(attempt: success)
44+
} else {
45+
AutodiscoveryResultView(attempt: report.userInputAttempt)
46+
}
47+
} else {
48+
ProgressView()
49+
}
50+
}
51+
}
52+
53+
struct AutodiscoveryResultView: View {
54+
let attempt: AutoDiscoveryAttemptResult
55+
56+
var body: some View {
57+
if let url = attempt.domainWithSubdomain {
58+
Text(url)
59+
}
60+
61+
AutoDiscoveryStepView(label: "Site Connection", isSuccess: attempt.couldConnectToUrl)
62+
AutoDiscoveryStepView(label: "Can connect using HTTPS", isSuccess: attempt.couldUseHttps)
63+
AutoDiscoveryStepView(label: "Supports JSON ", isSuccess: attempt.foundApiRoot)
64+
AutoDiscoveryStepView(label: "Found authentication URL ", isSuccess: attempt.foundAuthenticationUrl)
65+
}
66+
}
67+
68+
#Preview("Live data") {
69+
70+
struct AsyncTestView: View {
71+
72+
@State var report: AutoDiscoveryResult?
73+
74+
private let loginApi = WordPressLoginClient(requestExecutor: URLSession.shared)
75+
76+
var body: some View {
77+
AutodiscoveryReportView(report: report)
78+
.task {
79+
let result = await loginApi.autodiscoveryResult(forSite: "http://optional-https.wpmt.co")
80+
self.report = result
81+
}
82+
}
83+
}
84+
85+
return AsyncTestView()
86+
}

native/swift/Example/Example/UI/LoginView.swift

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ struct LoginView: View {
99
private var url: String = ""
1010

1111
@State
12-
private var isLoggingIn: Bool = false
12+
private var isLoading: Bool = false
1313

1414
@State
1515
private var loginError: String?
1616

1717
@State
18-
private var loginTask: Task<Void, Error>?
18+
private var currentTask: Task<Void, Error>?
1919

2020
@Environment(\.webAuthenticationSession)
2121
private var webAuthenticationSession
@@ -43,43 +43,59 @@ struct LoginView: View {
4343
#endif
4444

4545
HStack {
46-
if isLoggingIn {
46+
if isLoading {
4747
ProgressView()
4848
.progressViewStyle(.circular)
4949
.controlSize(.small)
5050
.padding()
5151
} else {
52-
Button(action: self.startLogin, label: {
53-
Text("Sign In")
52+
Button(action: self.startAutodiscovery, label: {
53+
Text("Next")
5454
})
5555
}
5656
}
5757
}
5858
.padding()
5959
}
6060

61-
func startLogin() {
62-
self.loginError = nil
63-
self.isLoggingIn = true
61+
func startAutodiscovery() {
62+
self.currentTask = Task {
63+
self.isLoading = true
6464

65-
self.loginTask = Task {
6665
do {
67-
let loginClient = WordPressLoginClient(urlSession: .shared)
68-
let loginDetails = try await loginClient.login(
69-
site: url,
70-
appName: "WordPress SDK Example App",
71-
appId: nil
72-
)
66+
let loginClient = WordPressLoginClient(requestExecutor: URLSession.shared)
67+
let loginDetails = await loginClient.autodiscoveryResult(forSite: url)
68+
7369
debugPrint(loginDetails)
74-
try await loginManager.setLoginCredentials(to: loginDetails)
75-
} catch let err {
76-
handleLoginError(err)
7770
}
71+
72+
self.isLoading = false
7873
}
7974
}
8075

76+
func startLogin() {
77+
self.loginError = nil
78+
self.isLoading = true
79+
80+
// self.currentTask = Task {
81+
// do {
82+
//// let loginClient = WordPressLoginClient(requestExecutor: URLSession.shared)
83+
//// let loginDetails = try await loginClient.login(
84+
//// site: url,
85+
//// appName: "WordPress SDK Example App",
86+
//// appId: nil,
87+
//// contextProvider: AuthenticationHelper()
88+
//// ).get()
89+
//// debugPrint(loginDetails)
90+
//// try await loginManager.setLoginCredentials(to: loginDetails)
91+
// } catch let err {
92+
// handleLoginError(err)
93+
// }
94+
// }
95+
}
96+
8197
private func handleLoginError(_ error: Error) {
82-
self.isLoggingIn = false
98+
self.isLoading = false
8399
self.loginError = error.localizedDescription
84100
}
85101
}
@@ -88,4 +104,6 @@ class AuthenticationHelper: NSObject, ASWebAuthenticationPresentationContextProv
88104
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
89105
ASPresentationAnchor()
90106
}
107+
108+
// LoginView().environmentObject(LoginManager())
91109
}

native/swift/Sources/wordpress-api/Exports.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ public typealias WpNetworkHeaderMap = WordPressAPIInternal.WpNetworkHeaderMap
2020

2121
public typealias WpApiApplicationPasswordDetails = WordPressAPIInternal.WpApiApplicationPasswordDetails
2222
public typealias WpAuthentication = WordPressAPIInternal.WpAuthentication
23-
public typealias UrlDiscoveryError = WordPressAPIInternal.UrlDiscoveryError
24-
public typealias UrlDiscoverySuccess = WordPressAPIInternal.UrlDiscoverySuccess
25-
public typealias UrlDiscoveryAttemptError = WordPressAPIInternal.UrlDiscoveryAttemptError
23+
public typealias AutoDiscoveryResult = WordPressAPIInternal.AutoDiscoveryUniffiResult
2624

2725
// MARK: - Users
2826

@@ -124,4 +122,6 @@ public typealias WpSiteHealthTestsRequestExecutor = WordPressAPIInternal.WpSiteH
124122
extension WpSiteHealthTestsRequestExecutor: @unchecked Sendable {}
125123
// swiftlint:enable line_length
126124

125+
extension AutoDiscoveryResult: @unchecked Sendable {}
126+
127127
#endif

native/swift/Sources/wordpress-api/Foundation+Extensions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ public extension Date {
1717
wordpressDateFormatter.date(from: string)
1818
}
1919
}
20+
21+
public extension URL {
22+
var schemeAndHost: String? {
23+
guard let scheme = self.scheme, let host = self.host else {
24+
return nil
25+
}
26+
27+
return scheme.uppercased() + "" + "://" + host
28+
}
29+
}

0 commit comments

Comments
 (0)