Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight message #670

Merged
merged 5 commits into from
Sep 9, 2020
Merged
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
4 changes: 4 additions & 0 deletions Chatto/Source/Chat Items/BaseChatItemPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,8 @@ open class BaseChatItemPresenter<CellT: UICollectionViewCell>: ChatItemPresenter
open func performMenuControllerAction(_ action: Selector) {
assert(self.canPerformMenuControllerAction(action))
}

// MARK: - ChatItemSpotlighting

open func spotlight() {}
}
10 changes: 9 additions & 1 deletion Chatto/Source/Chat Items/ChatItemProtocolDefinitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ public protocol ChatItemMenuPresenterProtocol {
func performMenuControllerAction(_ action: Selector)
}

public protocol ChatItemPresenterProtocol: AnyObject, ChatItemMenuPresenterProtocol {
public protocol ChatItemSpotlighting {
func spotlight()
}

extension ChatItemSpotlighting {
public func spotlight() {}
}

public protocol ChatItemPresenterProtocol: AnyObject, ChatItemMenuPresenterProtocol, ChatItemSpotlighting {
static func registerCells(_ collectionView: UICollectionView)

var isItemUpdateSupported: Bool { get }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ extension CGFloat {

extension BaseChatViewController {

private static var nextDidEndScrollingAnimationHandlersKey: Int = 0
private var nextDidEndScrollingAnimationHandlers: [() -> Void] {
get { objc_getAssociatedObject(self, &Self.nextDidEndScrollingAnimationHandlersKey) as? [() -> Void] ?? [] }
set { objc_setAssociatedObject(self, &Self.nextDidEndScrollingAnimationHandlersKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
}

public func isScrolledAtBottom() -> Bool {
guard let collectionView = self.collectionView else { return true }
guard collectionView.numberOfSections > 0 && collectionView.numberOfItems(inSection: 0) > 0 else { return true }
Expand Down Expand Up @@ -113,7 +119,10 @@ extension BaseChatViewController {
collectionView.contentOffset = CGPoint(x: 0, y: collectionView.contentOffset.y + diffY)
}

public func scrollToItem(withId itemId: String, position: UICollectionView.ScrollPosition = .centeredVertically, animated: Bool = false) {
public func scrollToItem(withId itemId: String,
position: UICollectionView.ScrollPosition = .centeredVertically,
animated: Bool = false,
spotlight: Bool = false) {
guard let collectionView = self.collectionView else { return }
guard let itemIndex = self.chatItemCompanionCollection.indexOf(itemId) else { return }

Expand Down Expand Up @@ -143,9 +152,28 @@ extension BaseChatViewController {
}
}

if spotlight {
guard let presenter = self.chatItemCompanionCollection[itemId]?.presenter else { return }
let contentOffsetWillBeChanged = !collectionView.indexPathsForVisibleItems.contains(indexPath)
if contentOffsetWillBeChanged {
self.nextDidEndScrollingAnimationHandlers.append { [weak presenter] in
presenter?.spotlight()
}
} else {
presenter.spotlight()
}
}

collectionView.scrollToItem(at: indexPath, at: position, animated: animated)
}

open func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
for handler in self.nextDidEndScrollingAnimationHandlers {
handler()
}
self.nextDidEndScrollingAnimationHandlers = []
}

open func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard let collectionView = self.collectionView else { return }
if collectionView.isDragging {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ open class CompoundMessagePresenter<ViewModelBuilderT, InteractionHandlerT>
self.menuPresenter?.performMenuControllerAction(action)
}

// MARK: - ChatItemSpotlighting

override open func spotlight() {
self.cell?.bubbleView?.spotlight()
}

// MARK: - MessageContentPresenterDelegate

public func presenterDidInvalidateLayout(_ presenter: MessageContentPresenterProtocol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ public final class CompoundBubbleView: UIView, MaximumLayoutWidthSpecificable, B
didSet { self.setNeedsLayout() }
}

// MARK: - Spotlighting

public func spotlight() {
// Covers the case when bubble is hidden
guard self.backgroundColor != nil else { return }

guard let viewModel = self.viewModel,
let style = self.style,
let spotlightedColor = style.spotlightedBackgroundColor(forViewModel: viewModel),
let originalColor = style.backgroundColor(forViewModel: viewModel) else { return }

let durationInMs = Int(style.spotlightDuration(forViewModel: viewModel) * 1000)

self.animateBackgroundColorChange(to: spotlightedColor)
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(durationInMs)) { [weak self] in
self?.animateBackgroundColorChange(to: originalColor)
}
}

private static let backgroundColorChangeAnimationDuration: TimeInterval = 0.1
private func animateBackgroundColorChange(to color: UIColor) {
UIView.animate(withDuration: Self.backgroundColorChangeAnimationDuration) { [weak self] in
self?.backgroundColor = color
}
}

// MARK: - MaximumLayoutWidthSpecificable

public var preferredMaxLayoutWidth: CGFloat = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public protocol CompoundBubbleViewStyleProtocol {
typealias ViewModel = MessageViewModelProtocol
var hideBubbleForSingleContent: Bool { get }
func backgroundColor(forViewModel viewModel: ViewModel) -> UIColor?
func spotlightedBackgroundColor(forViewModel viewModel: ViewModel) -> UIColor?
func spotlightDuration(forViewModel viewModel: ViewModel) -> TimeInterval
func maskingImage(forViewModel viewModel: ViewModel) -> UIImage?
func borderImage(forViewModel viewModel: ViewModel) -> UIImage?
func tailWidth(forViewModel viewModel: ViewModel) -> CGFloat
Expand Down Expand Up @@ -55,13 +57,19 @@ public final class DefaultCompoundBubbleViewStyle: CompoundBubbleViewStyleProtoc

private let baseStyle: BaseMessageCollectionViewCellDefaultStyle
private let bubbleMasks: BubbleMasks
private let spotlightBrightnessScale: CGFloat
private let spotlightDuration: TimeInterval

public init(baseStyle: BaseMessageCollectionViewCellDefaultStyle = BaseMessageCollectionViewCellDefaultStyle(),
bubbleMasks: BubbleMasks = .default,
hideBubbleForSingleContent: Bool = false) {
hideBubbleForSingleContent: Bool = false,
spotlightBrightnessScale: CGFloat = 0.85,
spotlightDuration: TimeInterval = 0.5) {
self.baseStyle = baseStyle
self.bubbleMasks = bubbleMasks
self.hideBubbleForSingleContent = hideBubbleForSingleContent
self.spotlightBrightnessScale = spotlightBrightnessScale
self.spotlightDuration = spotlightDuration
}

// MARK: CompoundBubbleViewStyleProtocol
Expand All @@ -72,6 +80,14 @@ public final class DefaultCompoundBubbleViewStyle: CompoundBubbleViewStyleProtoc
return viewModel.isIncoming ? self.baseStyle.baseColorIncoming : self.baseStyle.baseColorOutgoing
}

public func spotlightedBackgroundColor(forViewModel viewModel: ViewModel) -> UIColor? {
self.backgroundColor(forViewModel: viewModel)?.changeBrightness(to: self.spotlightBrightnessScale)
}

public func spotlightDuration(forViewModel viewModel: ViewModel) -> TimeInterval {
self.spotlightDuration
}

public func maskingImage(forViewModel viewModel: ViewModel) -> UIImage? {
return self.bubbleMasks.image(incoming: viewModel.isIncoming,
showTail: viewModel.decorationAttributes.isShowingTail)
Expand Down Expand Up @@ -111,3 +127,23 @@ extension DefaultCompoundBubbleViewStyle.BubbleMasks {
)
}
}

private extension UIColor {
func changeBrightness(to scale: CGFloat) -> UIColor {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0

guard self.getRed(&red, green: &green, blue: &blue, alpha: &alpha) else {
assertionFailure("Couldn't get colors of: \(self)")
return self
}

red *= scale
green *= scale
blue *= scale

return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
}