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 1 commit
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,33 @@ 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 sortedModels = models.sorted { (m1, m2) -> Bool in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:
instead of m1, m2, why not lhs, rhs?
additionally for m1Idx, m2Idx, why not lhsContent and rhsContent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lhs & rhs totally makes sense!
Not sure about xContent as it's an index of content in predefined array for order.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@viktorasl what is the status of this? 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I thought I've fixed this. On it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So sorry it took so long. Fixed now.

let m1Idx = reactions.firstIndex(of: m1.content) ?? 0
let m2Idx = reactions.firstIndex(of: m2.content) ?? 0
return m1Idx < m2Idx
}
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