fuzzzlove/SwiftUIWordpressClient

Show more than 10 posts

Opened this issue · 2 comments

Hello
Thats a great project. How can I show more than 10 posts? Its currently showing 10 posts only and my wp site has about 35 posts.

FGMGIT commented

Hi @moh1122 may be late.

You can make URL call with that parameter in the PostsListScreen+ViewModel.swift:

let url = URL.makeEndpointURL(relativePath: "posts?_fields=author,id,date,title,_links,_embedded&_embed&per_page=35")!

You can also filter by category:

let url = URL.makeEndpointURL(relativePath: "posts?_fields=author,id,date,title,_links,_embedded&_embed&categories=YOURCATEGORYNUMBER&per_page=YOURDESIREDPOSTNUMBER")!

I'm trying to get that working passing the value of category and posts dynamically, in similar way than it's down in PostScreen+ViewModel.swift with no success. I'm passing the values in the initial init PostsListScreen+ViewModel.swift but I'm not finding the way to pass the same init value to the view PostsListScreen.swift

PostScreen+ViewModel.swift

init(category: String) {
          
            let url = URL.makeEndpointURL(relativePath: "posts?_fields=author,id,date,title,_links,_embedded&_embed\(category)&per_page=15")!

But when PostsListScreen.swift loads lost the initial value for category and no way to pass same value to that view.

@fuzzzlove Any help will be welcome.

Hi @FGMGIT- Have you tried dependency injection?

I have not got a chance to test this, but try adding an initializer in the viewmodel that accepts a category parameter.

`final class ViewModel: ObservableObject {

// ... Existing code

init(category: String) {
    let url = URL.makeEndpointURL(relativePath: "posts?_fields=author,id,date,title,_links,_embedded&_embed\(category)&per_page=35")!
    
    // ... Existing code
}

// ... Existing code

}
`

You could then modify your view to pass the same category value

`struct PostsListScreen: View {

@ObservedObject private var viewModel: ViewModel
// ... Existing properties here

init(category: String) {
    self.viewModel = ViewModel(category: category)
    // ... Any other initial setup you need
}

var body: some View {
    // ... Existing body here
}

}
`

Now you should be able to create a screen with a specific category:

PostsListScreen(category: "someCategory")