Skip to content

Commit 64b01cd

Browse files
authored
Comments related improvement to the Swift wrapper (#734)
* Add a few comments related helpers to the uniffi API * Support comment async sequence * Change functions to getters * Add `ExpressibleByStringLiteral` conformance * Remove argument labels
1 parent 206eab1 commit 64b01cd

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ public typealias WpUuid = WordPressAPIInternal.WpUuid
1414
public typealias WpNetworkRequest = WordPressAPIInternal.WpNetworkRequest
1515
public typealias WpNetworkResponse = WordPressAPIInternal.WpNetworkResponse
1616
public typealias WpNetworkHeaderMap = WordPressAPIInternal.WpNetworkHeaderMap
17+
public typealias WpResponseString = WordPressAPIInternal.WpResponseString
1718
public typealias Middleware = WordPressAPIInternal.WpApiMiddleware
1819
public typealias MiddlewarePipeline = WordPressAPIInternal.WpApiMiddlewarePipeline
1920
public typealias MiddlewarePipelineBuilder = WordPressAPIInternal.WpApiMiddlewarePipelineBuilder
2021
public typealias ApiDiscoveryAuthenticationMiddleware = WordPressAPIInternal.ApiDiscoveryAuthenticationMiddleware
2122
public typealias RetryAfterMiddleware = WordPressAPIInternal.RetryAfterMiddleware
2223
public typealias WpApiClientDelegate = WordPressAPIInternal.WpApiClientDelegate
2324
public typealias WpAppNotifier = WordPressAPIInternal.WpAppNotifier
25+
public typealias UserAvatarSize = WordPressAPIInternal.UserAvatarSize
2426

2527
// MARK: - Login
2628

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,35 @@ extension MiddlewarePipeline {
3232
self.init(middlewares: middlewares)
3333
}
3434
}
35+
36+
public extension CommentStatus {
37+
init(_ status: String) {
38+
self = commentStatusFromString(value: status)
39+
}
40+
41+
var status: String {
42+
commentStatusToString(status: self)
43+
}
44+
}
45+
46+
extension CommentStatus: ExpressibleByStringLiteral {
47+
public init(stringLiteral: String) {
48+
self.init(stringLiteral)
49+
}
50+
}
51+
52+
public extension CommentType {
53+
init(_ type: String) {
54+
self = commentTypeFromString(value: type)
55+
}
56+
57+
var type: String {
58+
commentTypeToString(commentType: self)
59+
}
60+
}
61+
62+
extension CommentType: ExpressibleByStringLiteral {
63+
public init(stringLiteral: String) {
64+
self.init(stringLiteral)
65+
}
66+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,25 @@ extension UsersRequestExecutor: PaginationAwareExecutor {
248248
public typealias ViewContextResponseType = UsersRequestListWithViewContextResponse
249249
public typealias EmbedContextResponseType = UsersRequestListWithEmbedContextResponse
250250
}
251+
252+
// MARK: - Comments
253+
extension CommentsRequestListWithEditContextResponse: PaginatableResponse, @unchecked Sendable {
254+
public typealias ParamsType = CommentListParams
255+
public typealias DataType = CommentWithEditContext
256+
}
257+
258+
extension CommentsRequestListWithViewContextResponse: PaginatableResponse, @unchecked Sendable {
259+
public typealias ParamsType = CommentListParams
260+
public typealias DataType = CommentWithViewContext
261+
}
262+
263+
extension CommentsRequestListWithEmbedContextResponse: PaginatableResponse, @unchecked Sendable {
264+
public typealias ParamsType = CommentListParams
265+
public typealias DataType = CommentWithEmbedContext
266+
}
267+
268+
extension CommentsRequestExecutor: PaginationAwareExecutor {
269+
public typealias EditContextResponseType = CommentsRequestListWithEditContextResponse
270+
public typealias ViewContextResponseType = CommentsRequestListWithViewContextResponse
271+
public typealias EmbedContextResponseType = CommentsRequestListWithEmbedContextResponse
272+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ public actor WordPressAPI {
9999
self.requestBuilder.posts()
100100
}
101101

102+
public var comments: CommentsRequestExecutor {
103+
self.requestBuilder.comments()
104+
}
105+
102106
public var media: MediaRequestExecutor {
103107
self.requestBuilder.media()
104108
}

wp_api/src/comments.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,26 @@ pub enum CommentStatus {
579579

580580
impl_as_query_value_from_to_string!(CommentStatus);
581581

582+
#[uniffi::export]
583+
fn comment_status_from_string(value: String) -> CommentStatus {
584+
CommentStatus::from_str(value.as_str()).unwrap_or(CommentStatus::Custom(value))
585+
}
586+
587+
#[uniffi::export]
588+
fn comment_status_to_string(status: CommentStatus) -> String {
589+
status.to_string()
590+
}
591+
592+
#[uniffi::export]
593+
fn comment_type_from_string(value: String) -> CommentType {
594+
CommentType::from_str(value.as_str()).unwrap_or(CommentType::Custom(value))
595+
}
596+
597+
#[uniffi::export]
598+
fn comment_type_to_string(comment_type: CommentType) -> String {
599+
comment_type.to_string()
600+
}
601+
582602
#[cfg(test)]
583603
mod tests {
584604
use super::*;

0 commit comments

Comments
 (0)