This repository was archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathIssueCommentReactionViewModel.swift
72 lines (59 loc) · 1.87 KB
/
IssueCommentReactionViewModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// IssueCommentReactionViewModel.swift
// Freetime
//
// Created by Ryan Nystrom on 6/1/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import Foundation
import IGListKit
extension ReactionContent: Hashable {
public var hashValue: Int {
return rawValue.hashValue
}
}
final class IssueCommentReactionViewModel: ListDiffable {
static let allReactions: [ReactionContent] = [
.thumbsUp,
.thumbsDown,
.laugh,
.hooray,
.confused,
.heart,
.rocket,
.eyes
]
let models: [ReactionViewModel]
private let map: [ReactionContent: ReactionViewModel]
private let flatState: String
init(models: [ReactionViewModel]) {
let reactions = IssueCommentReactionViewModel.allReactions
let indexOfModel: (ReactionViewModel) -> Int = {
reactions.firstIndex(of: $0.content) ?? 0
}
let sortedModels = models.sorted { indexOfModel($0) < indexOfModel($1) }
self.models = sortedModels
var map = [ReactionContent: ReactionViewModel]()
var flatState = ""
for model in sortedModels {
map[model.content] = model
flatState += "\(model.content.rawValue)\(model.count)"
}
self.map = map
self.flatState = flatState
}
// MARK: Public API
func viewerDidReact(reaction: ReactionContent) -> Bool {
return map[reaction]?.viewerDidReact == true
}
// MARK: ListDiffable
func diffIdentifier() -> NSObjectProtocol {
// assume only one model per usage
return "reactions" as NSObjectProtocol
}
func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
if self === object { return true }
guard let object = object as? IssueCommentReactionViewModel else { return false }
return flatState == object.flatState
}
}