fatbobman/SwipeCell

Pass parameters

fatihdurmaz opened this issue · 2 comments

How can I pass parameters to the action event of the button?

import SwiftUI
import SwiftUISnackbar
import SwipeCell

struct PostListView: View {

@State private var share = false
@State private var delete = false

@StateObject var viewModel: PostViewModel
init(apiService:  ApiServiceProtocol) {
    _viewModel = StateObject(wrappedValue: PostViewModel(postApiService: .init(apiService: apiService)))
}

var body: some View {
    let button1 = SwipeCellButton(
        buttonStyle: .titleAndImage,
        title: "Share",
        systemImage: "square.and.arrow.up",
        titleColor: .white,
        imageColor: .white,
        view: nil,
        backgroundColor: .orange,
        action: {
            share.toggle()
        },
        feedback: true
    )
    let button2 = SwipeCellButton(
        buttonStyle: .titleAndImage,
        title: "Delete",
        systemImage: "trash",
        titleColor: .white,
        imageColor: .white,
        view: nil,
        backgroundColor: .red,
        action: { delete.toggle() },
        feedback: true
    )
    let slot1 = SwipeCellSlot(slots: [button1, button2])
    
    List(viewModel.posts) { post in
        NavigationLink(destination: PostDetailView(post: post)) {
            HStack {
                Text("\(post.id)")
                    .padding()
                    .foregroundColor(.white)
                    .background(.blue)
                    .clipShape(.circle)
                
                VStack(alignment: .leading) {
                    Text(post.title)
                        .bold()
                        .font(.subheadline)
                }
            }
            .swipeCell(cellPosition: .right, leftSlot: nil, rightSlot: slot1)
        }
    }
    .onAppear{
        viewModel.fetchAllPosts()
    }
    .overlay {
        if viewModel.posts.isEmpty {
            ContentUnavailableView{
                Label("No Results", systemImage: "tray.fill")
                    .bold()
            } description: {
                Text("Not found posts")
            }
        }
    }
    
    .toolbar {
        Button(action: {
            let newPost = Post(userId: 2, id: 24, title: "Deneme", body: "Deneme İçeriği")
            viewModel.addPost(post: newPost)
            
        }, label: {
            Image(systemName: "plus.circle")
                .tint(.black)
        })
    }
    .snackbar(isShowing: $viewModel.isShowing, title: viewModel.title, text: viewModel.message, style: viewModel.isError ? .error : .default)
}

func deletePost(at offsets: IndexSet) {
    for index in offsets {
        let postIdToDelete = viewModel.posts[index].id
        viewModel.deletePost(postId: postIdToDelete)
    }
}

}

declare button in ForEach or List , like this:

List(viewModel.posts){ post in
    let button1 = SwipeCellButton(
        buttonStyle: .titleAndImage,
        title: "Share",
        systemImage: "square.and.arrow.up",
        titleColor: .white,
        imageColor: .white,
        view: nil,
        backgroundColor: .orange,
        action: {
            some share action  // share post
        },
        feedback: true
    )
    let button2 = SwipeCellButton(
        buttonStyle: .titleAndImage,
        title: "Delete",
        systemImage: "trash",
        titleColor: .white,
        imageColor: .white,
        view: nil,
        backgroundColor: .red,
        action: { some delete cation }, // delete post 
        feedback: true
    )
    let slot1 = SwipeCellSlot(slots: [button1, button2])
    
    NavigationLink(destination: PostDetailView(post: post)) {
       ...
    }
}