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

Try adding Combine again #396

Draft
wants to merge 11 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 */; };
242132CB2CE7D0BE0021D8E8 /* Combine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 242132C92CE7D0220021D8E8 /* Combine.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 */
242132C92CE7D0220021D8E8 /* Combine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Combine.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 @@ -90,6 +92,7 @@
2479BF7F2B621CB60014A01D /* Example */ = {
isa = PBXGroup;
children = (
242132C92CE7D0220021D8E8 /* Combine.swift */,
242D64972C363960007CA96C /* UI */,
242D64982C363996007CA96C /* Resources */,
2479BF802B621CB60014A01D /* ExampleApp.swift */,
Expand Down Expand Up @@ -187,6 +190,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
242132CB2CE7D0BE0021D8E8 /* Combine.swift in Sources */,
242D64922C360687007CA96C /* RootListView.swift in Sources */,
2479BF812B621CB60014A01D /* ExampleApp.swift in Sources */,
24A3C32F2BA8F96F00162AD1 /* LoginView.swift in Sources */,
Expand Down
56 changes: 56 additions & 0 deletions native/swift/Example/Example/Combine.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Foundation
import Combine
import SwiftUI
import WordPressAPI

@Observable class CombineListViewModel: ListViewModel {

public typealias StreamProvider = () throws -> ListViewDataStream

var listItems: [String: ListViewData] = [:]
var isLoading: Bool = false

var shouldPresentAlert: Bool = false

var error: MyError?
var cancellables: Set<AnyCancellable> = []

private let streamProvider: StreamProvider

init(streamProvider: @escaping StreamProvider) {
self.streamProvider = streamProvider
}

func task() async {
self.error = nil

do {
let currentStream = try self.streamProvider()

currentStream.publisher
.sink { completion in
switch completion {
case .finished:
self.isLoading = false

case .failure(let error):
self.error = MyError(underlyingError: error)
self.shouldPresentAlert = true
}
} receiveValue: { allItems in
for item in allItems {
self.listItems[item.id] = item
}
}
.store(in: &cancellables)

} catch {
self.error = MyError(underlyingError: error)
self.shouldPresentAlert = true
}
}
}

struct ListViewDataStream {
let publisher: AnyPublisher<[ListViewData], Error>
}
9 changes: 9 additions & 0 deletions native/swift/Example/Example/ExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ struct ExampleApp: App {
let sequence = try WordPressAPI.globalInstance.posts.sequenceWithEditContext(params: postListParams)
return ListViewSequence(underlyingSequence: sequence)
}),
RootListData(name: "Posts with Combine", publisher: {
let publisher = try WordPressAPI.globalInstance
.posts
.publisherWithEditContext(params: postListParams)
.tryMap({ $0.map(\.asListViewData) })
.eraseToAnyPublisher()

return ListViewDataStream(publisher: publisher)
}),
RootListData(name: "Media", sequence: {
let sequence = try WordPressAPI.globalInstance.media.sequenceWithEditContext(params: mediaListParams)
return ListViewSequence(underlyingSequence: sequence)
Expand Down
18 changes: 9 additions & 9 deletions native/swift/Example/Example/ListViewData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protocol ListViewDataConvertable: Identifiable {
var asListViewData: ListViewData { get }
}

extension UserWithEditContext: ListViewDataConvertable {
extension UserWithEditContext: @retroactive Identifiable, ListViewDataConvertable {
var asListViewData: ListViewData {
ListViewData(id: "user-\(self.id)", title: self.name, subtitle: self.email, fields: [
"First Name": self.firstName,
Expand All @@ -27,23 +27,23 @@ extension UserWithEditContext: ListViewDataConvertable {
}
}

extension UserWithViewContext: ListViewDataConvertable {
extension UserWithViewContext: @retroactive Identifiable, ListViewDataConvertable {
var asListViewData: ListViewData {
ListViewData(id: "user-\(self.id)", title: self.name, subtitle: self.slug, fields: [
"Name": self.name
])
}
}

extension UserWithEmbedContext: ListViewDataConvertable {
extension UserWithEmbedContext: @retroactive Identifiable, ListViewDataConvertable {
var asListViewData: ListViewData {
ListViewData(id: "user-\(self.id)", title: self.name, subtitle: self.slug, fields: [
"Name": self.name
])
}
}

extension PluginWithEditContext: ListViewDataConvertable {
extension PluginWithEditContext: @retroactive Identifiable, ListViewDataConvertable {
public var id: String {
self.plugin.slug
}
Expand All @@ -56,7 +56,7 @@ extension PluginWithEditContext: ListViewDataConvertable {
}
}

extension ApplicationPasswordWithEditContext: ListViewDataConvertable {
extension ApplicationPasswordWithEditContext: @retroactive Identifiable, ListViewDataConvertable {
public var id: String {
self.uuid.uuid
}
Expand All @@ -76,7 +76,7 @@ extension ApplicationPasswordWithEditContext: ListViewDataConvertable {
}
}

extension SiteHealthTest: ListViewDataConvertable {
extension SiteHealthTest: @retroactive Identifiable, ListViewDataConvertable {
public var id: String {
self.label
}
Expand All @@ -86,7 +86,7 @@ extension SiteHealthTest: ListViewDataConvertable {
}
}

extension SiteHealthDirectorySizes: ListViewDataConvertable {
extension SiteHealthDirectorySizes: @retroactive Identifiable, ListViewDataConvertable {
public var id: String {
[
self.databaseSize.size,
Expand Down Expand Up @@ -117,7 +117,7 @@ extension SiteHealthDirectorySizes: ListViewDataConvertable {
}
}

extension PostTypeDetailsWithViewContext: ListViewDataConvertable {
extension PostTypeDetailsWithViewContext: @retroactive Identifiable, ListViewDataConvertable {
public var id: String {
self.slug
}
Expand Down Expand Up @@ -147,7 +147,7 @@ extension SiteSettingsWithEditContext {
}
}

extension PostWithEditContext: ListViewDataConvertable {
extension PostWithEditContext: @retroactive Identifiable, ListViewDataConvertable {
public var id: String {
self.slug
}
Expand Down
17 changes: 17 additions & 0 deletions native/swift/Example/Example/UI/RootListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ struct RootListViewItem: View {
Text(name)
}
}

case .publisher(let name, let streamProvider):
VStack(alignment: .leading, spacing: 4.0) {
NavigationLink {
ListView(
viewModel: CombineListViewModel(streamProvider: streamProvider)
)
} label: {
Text(name)
}
}
}
}
}
Expand All @@ -47,11 +58,13 @@ enum RootListData: Identifiable {

case callback(String, TaskListViewModel.FetchDataTask)
case sequence(String, SequenceListViewModel.SequenceProvider)
case publisher(String, CombineListViewModel.StreamProvider)

var id: String {
switch self {
case .callback(let id, _): id
case .sequence(let id, _): id
case .publisher(let id, _): id
}
}

Expand All @@ -62,6 +75,10 @@ enum RootListData: Identifiable {
init(name: String, sequence: @escaping SequenceListViewModel.SequenceProvider) {
self = .sequence(name, sequence)
}

init(name: String, publisher: @escaping CombineListViewModel.StreamProvider) {
self = .publisher(name, publisher)
}
}

#Preview {
Expand Down
1 change: 1 addition & 0 deletions native/swift/Sources/wordpress-api/Exports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public typealias PostTypeWithEmbedContext = WordPressAPIInternal.PostTypeDetails
public typealias PostTypeDetailsWithEditContext = WordPressAPIInternal.PostTypeDetailsWithEditContext
public typealias PostTypeDetailsWithViewContext = WordPressAPIInternal.PostTypeDetailsWithViewContext
public typealias PostTypeDetailsWithEmbedContext = WordPressAPIInternal.PostTypeDetailsWithEmbedContext
public typealias PostTypesRequestExecutor = WordPressAPIInternal.PostTypesRequestExecutor

// MARK: - Posts
public typealias SparsePost = WordPressAPIInternal.SparsePost
Expand Down
Loading