如何使用智能跟随按钮使instagram像通知一样

时间:2019-07-19 16:38:44

标签: ios swift firebase firebase-realtime-database

我想达到什么目的?

此:请注意,当我点击此页面时,没有人知道并记住我关注某些用户的情况。 enter image description here

更新-编辑:

->因此,我尝试了以下操作:我在通知节点上添加了一个“ amFollowing” bool节点,然后在获取时,我将bool val添加到了每个notif对象,然后将以下相关notifs传递给了单元格,该单元格将随后是否显示跟随按钮,以及是否显示跟随按钮的样式(跟随还是跟随),我不确定这是否是一个好选择(我仍在测试速度),尽管我有时可以看到跟随按钮显示为白色缺少在界面生成器中添加的文本,因此令人困惑...

因此我制作了版本,其中我进行了Firebase调用。尽管我发现它有一些问题,但它似乎可以正常工作,所以我想提出一些更好的建议


我想知道如何在通知页面(如Instagram)中设置智能跟随按钮。目前,我已经在注释单元格上设置了跟随按钮,但缺点是它们不聪明。

我的意思是,例如,他们不记得您是否关注了当前用户。相反,它们在应显示跟随时始终显示默认的跟随视图。

我想知道如何实现此解决方案。

当前,我对“跟随”按钮执行以下操作:

获取(在变体中发生):

if !(childVal.childSnapshot(forPath: "NewFollower").value is NSNull) {


          //new follower
            print("is new follower")
            var user = User(theuserID: uid)

//            dispatchGroup.enter()
            ref.child("users2").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
                user.fullName = (snapshot.childSnapshot(forPath: "fullName").value as? String) ?? ""
                user.username = "\(snapshot.childSnapshot(forPath: "username").value as! String)"
                user.profileImageUrlString = "\(snapshot.childSnapshot(forPath: "profileImage").value as! String)"
                user.bio = (snapshot.childSnapshot(forPath: "Bio").value as? String) ?? ""
                dispatchGroup.leave()
            })

            print(user.username, " this is the username")
            let notification = NotificationObj(user: user)
            notification.postID = postID
            notification.timeStamp = (childVal.childSnapshot(forPath: "timeStamp").value as! Double)

            self.arrayOfNotifications.append(notification)
            //completion(notification)

        }

委托方法:

            //new follower
        cell.setupNewFollowerCell(username: arrayOfNotifications[indexPath.row].user.username!)
        cell.user = arrayOfNotifications[indexPath.row].user

tableView的单元格:

    @objc private func followTap(_ sender: UIPanGestureRecognizer) {
    print("follow tap")
    FollowClass(uidOfUser: uid!, unfollowing: hasFollowedAlready)

    if hasFollowedAlready {
        hasFollowedAlready = false
        followBtn.backgroundColor? = UIColor.red
        followBtn.layer.borderWidth = 0
        followBtn.titleLabel?.textColor = UIColor.white
    } else {
        hasFollowedAlready = true
        followBtn.backgroundColor? = UIColor.white
        followBtn.layer.borderWidth = 0.3
        followBtn.layer.borderColor = UIColor(red: 112/255, green: 112/255, blue: 112/255, alpha: 1).cgColor
        followBtn.titleLabel?.textColor = UIColor.black
    }
}

一个基本思路是在我的用户“关注”节点中为当前用户获取内容,并检查该用户是否存在于该节点中,并告诉我们该btn的显示内容。问题在于,这是又一次网络调用,考虑到Firebase具有异步性质,我假设这将使事情变得混乱...

我有什么选择?我该怎么办?

0 个答案:

没有答案