Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jkmassel committed Jan 14, 2025
1 parent d9cbfa6 commit 38fb39a
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 184 deletions.
4 changes: 4 additions & 0 deletions native/swift/Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

/* Begin PBXFileReference section */
242CA0C02D03A7DF00C0DD68 /* LoginReport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginReport.swift; sourceTree = "<group>"; };
242D648D2C3602C1007CA96C /* ListViewData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListViewData.swift; sourceTree = "<group>"; };
242D64912C360687007CA96C /* RootListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RootListView.swift; sourceTree = "<group>"; };
242D64932C3608C6007CA96C /* ListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -55,6 +57,7 @@
242D64972C363960007CA96C /* UI */ = {
isa = PBXGroup;
children = (
242CA0C02D03A7DF00C0DD68 /* LoginReport.swift */,
2479BF872B621CB70014A01D /* Preview Content */,
242D64932C3608C6007CA96C /* ListView.swift */,
242D64912C360687007CA96C /* RootListView.swift */,
Expand Down Expand Up @@ -191,6 +194,7 @@
files = (
242D64922C360687007CA96C /* RootListView.swift in Sources */,
2479BF812B621CB60014A01D /* ExampleApp.swift in Sources */,
242CA0C12D03A7E200C0DD68 /* LoginReport.swift in Sources */,
24A3C32F2BA8F96F00162AD1 /* LoginView.swift in Sources */,
2479BF932B621E9B0014A01D /* ListViewModel.swift in Sources */,
24A3C3362BAA874C00162AD1 /* LoginManager.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"location" : "https://github.com/swiftlang/swift-syntax.git",
"state" : {
"revision" : "303e5c5c36d6a558407d364878df131c3546fad8",
"version" : "510.0.2"
"revision" : "cb53fa1bd3219b0b23ded7dfdd3b2baff266fd25",
"version" : "600.0.0"
}
},
{
"identity" : "swiftlint",
"kind" : "remoteSourceControl",
"location" : "https://github.com/realm/SwiftLint",
"state" : {
"revision" : "b515723b16eba33f15c4677ee65f3fef2ce8c255",
"version" : "0.55.1"
"revision" : "25f2776977e663305bee71309ea1e34d435065f1",
"version" : "0.57.1"
}
},
{
Expand Down
141 changes: 141 additions & 0 deletions native/swift/Example/Example/UI/LoginReport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import Foundation
import SwiftUI
import WordPressAPI
import AuthenticationServices
import WordPressAPIInternal

struct AutoDiscoveryResultView: View {
let results: [AutoDiscoveryAttemptViewData]

var body: some View {
Text("N")
}
}

struct AutoDiscoveryStepView: View {
let icon: String
let label: String

var body: some View {
VStack(alignment: .leading) {
HStack(alignment: .firstTextBaseline) {
Image(systemName: icon)
.font(.title)
Text(label).font(.title)
Spacer()
}
}.padding(.horizontal)
}
}

struct AutoDiscoveryErrorView: View {
let errorMessage: String

var body: some View {
Text(errorMessage)
}
}

struct AutodiscoveryReportView: View {
let report: AutoDiscoveryResult?

var body: some View {
if let report {
if let success = report.successfulAttempt {
if let url = success.domainWithSubdomain {
Text(url)
}

if success.couldConnectToUrl {
AutoDiscoveryStepView(icon: "checkmark.circle", label: "Site Connection")
}

if success.couldUseHttps {
AutoDiscoveryStepView(icon: "checkmark.circle", label: "Can connect using HTTPS")
}

if success.foundApiRoot {
AutoDiscoveryStepView(icon: "checkmark.circle", label: "Supports JSON API")
}

if success.foundAuthenticationUrl {
AutoDiscoveryStepView(icon: "checkmark.circle", label: "Found authentication URL")
}
} else {
Text("Unable to connect")
}
} else {
ProgressView()
}
}
}

struct AutoDiscoveryAttemptViewData {

enum ErrorType {
case network
case missingRootUrl
}

enum StepType {
case siteWasFound
case supportsHTTPS
case canFindRootURL
case apiRootHasAuthUrl
}

// let userInput: String

let isError: Bool

let errorType: ErrorType?
let errorMessage: String?

let step: StepType

// var url: URL? {
// URL(string: userInput)
// }

var icon: String {
// if url?.scheme == "https" {
// return "checkmark.shield"
// }

return switch errorType {
case .network: "network.slash"
case .missingRootUrl: "questionmark.circle.dashed"
case nil: "checkmark.circle"
}
}

var attemptTypeString: String {
switch step {
case .siteWasFound: "Site Connection"
case .supportsHTTPS: "Can connect using HTTPS"
case .canFindRootURL: "Supports JSON API"
case .apiRootHasAuthUrl: "Found authentication URL"
}
}
}

#Preview("Live data") {

struct AsyncTestView: View {

@State var report: AutoDiscoveryResult?

private let loginApi = WordPressLoginClient(requestExecutor: URLSession.shared)

var body: some View {
AutodiscoveryReportView(report: report)
.task {
let result = await loginApi.autodiscoveryResult(forSite: "http://vanilla.wpmt.co")

self.report = result
}
}
}

return AsyncTestView()
}
56 changes: 37 additions & 19 deletions native/swift/Example/Example/UI/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ struct LoginView: View {
private var url: String = ""

@State
private var isLoggingIn: Bool = false
private var isLoading: Bool = false

@State
private var loginError: String?

@State
private var loginTask: Task<Void, Error>?
private var currentTask: Task<Void, Error>?

@Environment(\.webAuthenticationSession)
private var webAuthenticationSession
Expand Down Expand Up @@ -43,43 +43,59 @@ struct LoginView: View {
#endif

HStack {
if isLoggingIn {
if isLoading {
ProgressView()
.progressViewStyle(.circular)
.controlSize(.small)
.padding()
} else {
Button(action: self.startLogin, label: {
Text("Sign In")
Button(action: self.startAutodiscovery, label: {
Text("Next")
})
}
}
}
.padding()
}

func startLogin() {
self.loginError = nil
self.isLoggingIn = true
func startAutodiscovery() {
self.currentTask = Task {
self.isLoading = true

self.loginTask = Task {
do {
let loginClient = WordPressLoginClient(urlSession: .shared)
let loginDetails = try await loginClient.login(
site: url,
appName: "WordPress SDK Example App",
appId: nil
)
let loginClient = WordPressLoginClient(requestExecutor: URLSession.shared)
let loginDetails = await loginClient.autodiscoveryResult(forSite: url)

debugPrint(loginDetails)
try await loginManager.setLoginCredentials(to: loginDetails)
} catch let err {
handleLoginError(err)
}

self.isLoading = false
}
}

func startLogin() {
self.loginError = nil
self.isLoading = true

// self.currentTask = Task {
// do {
//// let loginClient = WordPressLoginClient(requestExecutor: URLSession.shared)
//// let loginDetails = try await loginClient.login(
//// site: url,
//// appName: "WordPress SDK Example App",
//// appId: nil,
//// contextProvider: AuthenticationHelper()
//// ).get()
//// debugPrint(loginDetails)
//// try await loginManager.setLoginCredentials(to: loginDetails)
// } catch let err {
// handleLoginError(err)
// }
// }
}

private func handleLoginError(_ error: Error) {
self.isLoggingIn = false
self.isLoading = false
self.loginError = error.localizedDescription
}
}
Expand All @@ -88,4 +104,6 @@ class AuthenticationHelper: NSObject, ASWebAuthenticationPresentationContextProv
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
ASPresentationAnchor()
}

// LoginView().environmentObject(LoginManager())
}
6 changes: 3 additions & 3 deletions native/swift/Sources/wordpress-api/Exports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public typealias WpNetworkHeaderMap = WordPressAPIInternal.WpNetworkHeaderMap

public typealias WpApiApplicationPasswordDetails = WordPressAPIInternal.WpApiApplicationPasswordDetails
public typealias WpAuthentication = WordPressAPIInternal.WpAuthentication
public typealias UrlDiscoveryError = WordPressAPIInternal.UrlDiscoveryError
public typealias UrlDiscoverySuccess = WordPressAPIInternal.UrlDiscoverySuccess
public typealias UrlDiscoveryAttemptError = WordPressAPIInternal.UrlDiscoveryAttemptError
public typealias AutoDiscoveryResult = WordPressAPIInternal.AutoDiscoveryUniffiResult

// MARK: - Users

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

extension AutoDiscoveryResult: @unchecked Sendable {}

#endif
10 changes: 10 additions & 0 deletions native/swift/Sources/wordpress-api/Foundation+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ public extension Date {
wordpressDateFormatter.date(from: string)
}
}

public extension URL {
var schemeAndHost: String? {
guard let scheme = self.scheme, let host = self.host else {
return nil
}

return scheme.uppercased() + "" + "://" + host
}
}
Loading

0 comments on commit 38fb39a

Please sign in to comment.