1

I am currently building an Instagram clone. I am quit new so please forgive me if this question is answered easily.

I just want to have a specific score attached to each post. I already managed to give each new post a score 0f 0 to start with and each time its liked it increases by 100, disliked it decreases by 100. for every comment it grows by 50 points. Because I want to order them smartly and want it to show different posts at the top over time I wanted to include a third variable which influences the score.

I want it to decrease the score by -10 each hour since its been uploaded.

The increase and decrease is done in my function incrementLikes() /incrementComments() . I know that I can't modify the value of score for the time since its uploaded there, but I don't know where else.

My date extension (prob where I can do it?)

extension Date {

func timeAgoDisplay() -> String {

    let secondsAgo = Int(Date().timeIntervalSince(self))

    let minute = 60
    let hour = 60 * minute
    let day = hour * 24
    let week = day * 7
    let month = week * 4

    let quotient : Int
    let unit: String

    if secondsAgo < minute {
        quotient = secondsAgo
        unit = "Sekunde"
    } else if secondsAgo < hour {
        quotient = secondsAgo / minute
        unit = "Minute"
    } else if secondsAgo < day {
        quotient = secondsAgo / hour
        unit = "Stunde"
    } else if secondsAgo < week {
        quotient = secondsAgo / day
        unit = "Tage"
    } else if secondsAgo < month {
        quotient = secondsAgo / week
        unit = "Woche"
    } else  {
        quotient = secondsAgo / month
        unit = "Monat"
    }

    return "Vor \(quotient) \(unit)\(quotient == 1 ? "" : "n")"
}

}

my function in homeTableViewCell where I set the date

func updateView() {

    captionLabel.userHandleLinkTapHandler = { label,string, range in
        let mention = String(string.characters.dropFirst())

        API.User.observeUserByUsername(username: mention.lowercased(), completion: { (user) in
            self.delegate?.goToProfileUserViewController(userId: user.id!)

        })
    }

    guard let count = post?.commentCount else {return}
    if count == 0 {
        commentButton.setTitle("Schreibe den ersten Kommentar", for: UIControlState.normal)
    }else if count == 1 {
        commentButton.setTitle("Sieh dir den ersten Kommentar an", for: UIControlState.normal)

    } else {
        commentButton.setTitle("Alle \(count) Kommentare ansehen", for: UIControlState.normal)
    }

    let timeAgoDisplay = post?.creationDate?.timeAgoDisplay()

    timeLabel.text = timeAgoDisplay
    }

thanks for your help :)

AL.
  • 36,815
  • 10
  • 142
  • 281
Zash__
  • 293
  • 4
  • 16

1 Answers1

1

The idea of updating the score every hour using a time is based on the idea that the app is constantly running, which seems flawed. You could instead have something like

var score: Int {
    return likes*100 - dislikes*100 + comments*50 - hoursSinceUpload()*10
}

where hoursSinceUpload is computed by something like (see Getting the difference between two NSDates in (months/days/hours/minutes/seconds) for reference)

func hoursSinceUpload() -> Int {
    return Calendar.current.dateComponents([.hour], from: uploadDate, to: Date()).hour
}
Community
  • 1
  • 1
Gerriet
  • 1,302
  • 14
  • 20