ios – How to delete user only post


I am trying to delete my user post but I only want to allow my user to delete their post not the other users post. This is my user post the delete (trash) button is being displayed for the user .
My problem is the delete button is also being shown for the other users allowing me delete their post which is counterproductive. I don’t want to others users to see their delete button I only want the person who uploaded the image should see that button and be able to delete that post

How do I change this code for the delete button only to be shown for the user post and not the other users ?


@ObservedObject var viewModel: FeedCellViewModel
@State private var isFollowed = false
    @State private var height: CGFloat = 200

    private var user: Userss? {
        return viewModel.post.user
    }
    
    private var post: Post {
        return viewModel.post
    }

var body: some View {
 HStack(spacing: 16) {
 if isFollowed {
                            Button(action: {
                                Task { didLike ? try await viewModel.unlike() : try await viewModel.like() }
                            }, label: {
                                Image(systemName: didLike ? "heart.fill" : "heart")
                                    .resizable()
                                    .scaledToFill()
                                    .foregroundColor(didLike ? .red : Color.blue)
                                    .frame(width: 20, height: 20)
                                    .font(.system(size: 20))
                                    .padding(4)
                            })
                            
                            NavigationLink(destination: CommentView(post: post)) {
                                Image(systemName: "bubble.right")
                                    .resizable()
                                    .scaledToFill()
                                    .frame(width: 20, height: 20)
                                    .font(.system(size: 20))
                                    .padding(4)
                                    .foregroundColor(Color.black)
                            }
                            
                           
                        }
                        if !isFollowed {
                            Button(action: {
                                Task { didLike ? try await viewModel.unlike() : try await viewModel.like() }
                            }, label: {
                                Image(systemName: didLike ? "heart.fill" : "heart")
                                    .resizable()
                                    .scaledToFill()
                                    .foregroundColor(didLike ? .red : Color.blue)
                                    .frame(width: 20, height: 20)
                                    .font(.system(size: 20))
                                    .padding(4)
                            })
                            
                            NavigationLink(destination: CommentView(post: post)) {
                                Image(systemName: "bubble.right")
                                    .resizable()
                                    .scaledToFill()
                                    .frame(width: 20, height: 20)
                                    .font(.system(size: 20))
                                    .padding(4)
                                    .foregroundColor(Color.black)
                            }
                            Button {
                                
                            } label: {
                                Image(systemName: "trash")
                                    .resizable()
                                    .scaledToFill()
                                    .frame(width: 20, height: 20)
                                    .font(.system(size: 20))
                                    .padding(4)
                                    .foregroundColor(Color.black)
                                
                                
                                
                            }
                            
                            
                            
                            .onAppear {
                                Task {
                                    if let user = post.user {
                                        let isFollowed = await UserService.checkIfUserIsFollowed(user)
                                        self.isFollowed = isFollowed
                                        height += isFollowed ? 32 : 0
                                    }
                                }
                            }
                            

UserService

class UserService {
    
    @Published var currentUser: Userss?
    
    static let shared = UserService()
    private static let userCache = NSCache<NSString, NSData>()

    static func checkIfUserIsFollowedWithUid(_ uid: String) async -> Bool {
        guard let currentUid = Auth.auth().currentUser?.uid else { return false }
        let collection = FirestoreConstants.FollowingCollection.document(currentUid).collection("user-following")
        guard let snapshot = try? await collection.document(uid).getDocument() else { return false }
        return snapshot.exists
    }
    
    static func checkIfUserIsFollowed(_ user: Userss) async -> Bool {
        guard let currentUid = Auth.auth().currentUser?.uid else { return false }
        let collection = FirestoreConstants.FollowingCollection.document(currentUid).collection("user-following")
        guard let snapshot = try? await collection.document(user.id).getDocument() else { return false }
        return snapshot.exists
    }

Post

import FirebaseFirestoreSwift
import Firebase

struct Post: Identifiable, Hashable, Codable {
    @DocumentID var id: String?
    let ownerUid: String
    let text: String
    let caption: String
    var likes: Int
    var replyCount: Int
    let image: String?
    let timestamp: Timestamp
    
    var user: Userss?
    
    var didLike: Bool? = false
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img