Skip to content

Commit 998fbed

Browse files
committed
Changes for new Nextcloud Notes Login
- Asynchronous method wrappers. Various method wrappers which enable calls in fashion of modern concurrency. - Identifiable conformance of NKShareAccounts. This enables the presentation of NKShareAccounts in a SwiftUI list. Signed-off-by: Iva Horn <iva.horn@icloud.com>
1 parent 8aac3fd commit 998fbed

4 files changed

Lines changed: 74 additions & 2 deletions

File tree

Sources/NextcloudKit/NKError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension OCSPath {
3232
static var ocsXMLMsg: Self { ["d:error", "s:message"] }
3333
}
3434

35-
public struct NKError: Sendable, Equatable {
35+
public struct NKError: Error, Equatable {
3636
static let internalError = -9999
3737
// Chunk error
3838
public static let chunkNoEnoughMemory = -9998

Sources/NextcloudKit/NKShareAccounts.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,33 @@ import Foundation
66
#if os(iOS)
77
import UIKit
88

9+
///
10+
/// Facility to read and write partial account information shared among apps of the same security group.
11+
/// This is the foundation for the quick account selection feature on login.
12+
///
913
public class NKShareAccounts: NSObject {
10-
public class DataAccounts: NSObject {
14+
///
15+
/// Data transfer object to pass between ``NKShareAccounts`` and calling code.
16+
///
17+
public class DataAccounts: NSObject, Identifiable {
18+
///
19+
/// The server address of the account.
20+
///
1121
public var url: String
22+
23+
///
24+
/// The login name for the account.
25+
///
1226
public var user: String
27+
28+
///
29+
/// The display name of the account.
30+
///
1331
public var name: String?
32+
33+
///
34+
/// The ccount profile picture.
35+
///
1436
public var image: UIImage?
1537

1638
public init(withUrl url: String, user: String, name: String? = nil, image: UIImage? = nil) {
@@ -34,6 +56,9 @@ public class NKShareAccounts: NSObject {
3456
internal let fileName: String = "accounts.json"
3557
internal let directoryAccounts: String = "Library/Application Support/NextcloudAccounts"
3658

59+
///
60+
/// Store shared account information in the app group container.
61+
///
3762
/// - Parameters:
3863
/// - directory: the group directory of share the accounts (group.com.nextcloud.apps), use the func containerURL(forSecurityApplicationGroupIdentifier groupIdentifier: String) -> URL? // Available for OS X in 10.8.3.
3964
/// - app: the name of app
@@ -81,6 +106,9 @@ public class NKShareAccounts: NSObject {
81106
return nil
82107
}
83108

109+
///
110+
/// Read the shared account information from the app group container.
111+
///
84112
/// - Parameters:
85113
/// - directory: the group directory of share the accounts (group.com.nextcloud.apps), use the func containerURL(forSecurityApplicationGroupIdentifier groupIdentifier: String) -> URL? // Available for OS X in 10.8.3.
86114
/// - application: the UIApplication used for verify if the app(s) is still installed

Sources/NextcloudKit/NextcloudKit+API.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ public extension NextcloudKit {
151151
case failure(NKError)
152152
}
153153

154+
///
155+
/// Asynchronous method wrapper for ``getServerStatus(serverUrl:options:taskHandler:completion:)``.
156+
///
157+
func getServerStatus(serverUrl: String, options: NKRequestOptions = NKRequestOptions(), taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }) async -> ServerInfoResult {
158+
await withCheckedContinuation { continuation in
159+
getServerStatus(serverUrl: serverUrl, options: options, taskHandler: taskHandler) { _, serverInfoResult in
160+
continuation.resume(returning: serverInfoResult)
161+
}
162+
}
163+
}
164+
154165
func getServerStatus(serverUrl: String,
155166
options: NKRequestOptions = NKRequestOptions(),
156167
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },

Sources/NextcloudKit/NextcloudKit+Login.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,39 @@ public extension NextcloudKit {
9999

100100
// MARK: - Login Flow V2
101101

102+
///
103+
/// Requests the initiation of a login process and retrieves required information.
104+
///
105+
/// - Returns: A tuple consisting of the `endpoint` to poll for the login status with the `token`. Additionally, the `login` to open for the user to log in.
106+
///
107+
func getLoginFlowV2(serverUrl: String, options: NKRequestOptions = NKRequestOptions(), taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }) async throws -> (endpoint: URL, login: URL, token: String) {
108+
try await withCheckedThrowingContinuation { continuation in
109+
getLoginFlowV2(serverUrl: serverUrl, options: options, taskHandler: taskHandler) { token, endpointString, loginString, _, error in
110+
if error != .success {
111+
continuation.resume(throwing: error)
112+
return
113+
}
114+
115+
guard let endpointString, let endpointURL = URL(string: endpointString) else {
116+
continuation.resume(throwing: NKError.urlError)
117+
return
118+
}
119+
120+
guard let loginString, let loginURL = URL(string: loginString) else {
121+
continuation.resume(throwing: NKError.urlError)
122+
return
123+
}
124+
125+
guard let token else {
126+
continuation.resume(throwing: NKError.invalidData)
127+
return
128+
}
129+
130+
continuation.resume(returning: (endpointURL, loginURL, token))
131+
}
132+
}
133+
}
134+
102135
func getLoginFlowV2(serverUrl: String,
103136
options: NKRequestOptions = NKRequestOptions(),
104137
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },

0 commit comments

Comments
 (0)