Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Preserve emojis order when adding #2698

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,32 @@ extension ReactionContent: Hashable {

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]) {
self.models = models
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 models {
for model in sortedModels {
map[model.content] = model
flatState += "\(model.content.rawValue)\(model.count)"
}
Expand Down
11 changes: 8 additions & 3 deletions Classes/Issues/Comments/Reactions/IssueReactionCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ final class IssueReactionCell: UICollectionViewCell {
accessibilityHint = isViewer
? NSLocalizedString("Tap to remove your reaction", comment: "")
: NSLocalizedString("Tap to react with this emoji", comment: "")
restoreDisplay()
}

func popIn() {
Expand All @@ -109,9 +110,7 @@ final class IssueReactionCell: UICollectionViewCell {
// hack to prevent changing to "0"
countLabel.text = "1"

countLabel.alpha = 1
emojiLabel.transform = .identity
emojiLabel.alpha = 1
restoreDisplay()
UIView.animate(withDuration: 0.2, delay: 0, animations: {
self.countLabel.alpha = 0
self.emojiLabel.transform = CGAffineTransform(scaleX: 0.3, y: 0.3)
Expand All @@ -130,6 +129,12 @@ final class IssueReactionCell: UICollectionViewCell {

// MARK: Private API

func restoreDisplay() {
countLabel.alpha = 1
emojiLabel.transform = .identity
emojiLabel.alpha = 1
}

@objc func showMenu(recognizer: UITapGestureRecognizer) {
guard recognizer.state == .began,
!detailText.isEmpty else { return }
Expand Down
27 changes: 13 additions & 14 deletions Classes/Issues/Comments/Reactions/ReactionsMenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,25 @@ final class EmojiCell: UICollectionViewCell {

}

private extension Array {
func split(toNumberOfChunks number: Int) -> [[Element]] {
let size = count / number
return (0 ..< number).map {
let from = $0 * size
let to = from + size
let isLast = ($0 == number - 1)
return Array(self[from ..< (isLast ? count : to)])
}
}
}

final class ReactionsMenuViewController: UICollectionViewController,
UICollectionViewDelegateFlowLayout {

private let reuseIdentifier = "cell"
private let size: CGFloat = 50

private let sectionedReactions: [[ReactionContent]] = [
[
.thumbsUp,
.thumbsDown,
.laugh,
.hooray
],
[
.confused,
.heart,
.rocket,
.eyes
]
]
private let sectionedReactions = IssueCommentReactionViewModel.allReactions.split(toNumberOfChunks: 2)

var selectedReaction: ReactionContent? {
guard let item = collectionView?.indexPathsForSelectedItems?.first else { return nil }
Expand Down