mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
6.0 KiB
Swift
127 lines
6.0 KiB
Swift
5 years ago
|
|
||
|
final class FakeChatView : UIView {
|
||
|
private let spacing = Values.mediumSpacing
|
||
|
|
||
5 years ago
|
var contentOffset: CGPoint {
|
||
|
get { return scrollView.contentOffset }
|
||
|
set { scrollView.contentOffset = newValue }
|
||
|
}
|
||
|
|
||
5 years ago
|
private lazy var chatBubbles = [
|
||
5 years ago
|
getChatBubble(withText: NSLocalizedString("view_fake_chat_bubble_1", comment: ""), wasSentByCurrentUser: true),
|
||
|
getChatBubble(withText: NSLocalizedString("view_fake_chat_bubble_2", comment: ""), wasSentByCurrentUser: false),
|
||
|
getChatBubble(withText: NSLocalizedString("view_fake_chat_bubble_3", comment: ""), wasSentByCurrentUser: true),
|
||
|
getChatBubble(withText: NSLocalizedString("view_fake_chat_bubble_4", comment: ""), wasSentByCurrentUser: false),
|
||
|
getChatBubble(withText: NSLocalizedString("view_fake_chat_bubble_5", comment: ""), wasSentByCurrentUser: false)
|
||
5 years ago
|
]
|
||
|
|
||
|
private lazy var scrollView: UIScrollView = {
|
||
|
let result = UIScrollView()
|
||
5 years ago
|
result.showsHorizontalScrollIndicator = false
|
||
5 years ago
|
result.showsVerticalScrollIndicator = false
|
||
|
return result
|
||
|
}()
|
||
|
|
||
|
override init(frame: CGRect) {
|
||
|
super.init(frame: frame)
|
||
|
setUpViewHierarchy()
|
||
|
animate()
|
||
|
}
|
||
|
|
||
|
required init?(coder: NSCoder) {
|
||
|
super.init(coder: coder)
|
||
|
setUpViewHierarchy()
|
||
|
animate()
|
||
|
}
|
||
|
|
||
|
private func setUpViewHierarchy() {
|
||
|
let stackView = UIStackView(arrangedSubviews: chatBubbles)
|
||
|
stackView.axis = .vertical
|
||
|
stackView.spacing = spacing
|
||
|
stackView.alignment = .fill
|
||
|
stackView.set(.width, to: UIScreen.main.bounds.width)
|
||
|
stackView.layoutMargins = UIEdgeInsets(top: 8, leading: Values.veryLargeSpacing, bottom: 8, trailing: Values.veryLargeSpacing)
|
||
|
stackView.isLayoutMarginsRelativeArrangement = true
|
||
|
scrollView.addSubview(stackView)
|
||
|
stackView.pin(to: scrollView)
|
||
|
addSubview(scrollView)
|
||
|
scrollView.pin(to: self)
|
||
|
}
|
||
|
|
||
|
private func getChatBubble(withText text: String, wasSentByCurrentUser: Bool) -> UIView {
|
||
|
let result = UIView()
|
||
|
let bubbleView = UIView()
|
||
|
bubbleView.set(.width, to: Values.fakeChatBubbleWidth)
|
||
|
bubbleView.layer.cornerRadius = Values.fakeChatBubbleCornerRadius
|
||
5 years ago
|
bubbleView.layer.shadowColor = UIColor.black.cgColor
|
||
5 years ago
|
bubbleView.layer.shadowRadius = isLightMode ? 4 : 8
|
||
|
bubbleView.layer.shadowOpacity = isLightMode ? 0.16 : 0.24
|
||
5 years ago
|
bubbleView.layer.shadowOffset = CGSize.zero
|
||
5 years ago
|
let backgroundColor = wasSentByCurrentUser ? Colors.fakeChatBubbleBackground : Colors.accent
|
||
5 years ago
|
bubbleView.backgroundColor = backgroundColor
|
||
|
let label = UILabel()
|
||
5 years ago
|
let textColor = wasSentByCurrentUser ? Colors.text : Colors.fakeChatBubbleText
|
||
5 years ago
|
label.textColor = textColor
|
||
|
label.font = .boldSystemFont(ofSize: Values.mediumFontSize)
|
||
|
label.numberOfLines = 0
|
||
|
label.lineBreakMode = .byWordWrapping
|
||
|
label.text = text
|
||
|
bubbleView.addSubview(label)
|
||
5 years ago
|
label.pin(to: bubbleView, withInset: 12)
|
||
5 years ago
|
result.addSubview(bubbleView)
|
||
|
bubbleView.pin(.top, to: .top, of: result)
|
||
|
result.pin(.bottom, to: .bottom, of: bubbleView)
|
||
|
if wasSentByCurrentUser {
|
||
5 years ago
|
bubbleView.pin(.trailing, to: .trailing, of: result)
|
||
5 years ago
|
} else {
|
||
5 years ago
|
result.pin(.leading, to: .leading, of: bubbleView)
|
||
5 years ago
|
}
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
private func animate() {
|
||
|
let animationDuration = Values.fakeChatAnimationDuration
|
||
|
let delayBetweenMessages = Values.fakeChatDelay
|
||
|
chatBubbles.forEach { $0.alpha = 0 }
|
||
|
Timer.scheduledTimer(withTimeInterval: Values.fakeChatStartDelay, repeats: false) { [weak self] _ in
|
||
|
self?.showChatBubble(at: 0)
|
||
5 years ago
|
Timer.scheduledTimer(withTimeInterval: 1.5 * delayBetweenMessages, repeats: false) { _ in
|
||
5 years ago
|
self?.showChatBubble(at: 1)
|
||
5 years ago
|
Timer.scheduledTimer(withTimeInterval: 1.5 * delayBetweenMessages, repeats: false) { _ in
|
||
5 years ago
|
self?.showChatBubble(at: 2)
|
||
|
UIView.animate(withDuration: animationDuration) {
|
||
|
guard let self = self else { return }
|
||
|
self.scrollView.contentOffset = CGPoint(x: 0, y: self.chatBubbles[0].height() + self.spacing)
|
||
|
}
|
||
5 years ago
|
Timer.scheduledTimer(withTimeInterval: 1.5 * delayBetweenMessages, repeats: false) { _ in
|
||
5 years ago
|
self?.showChatBubble(at: 3)
|
||
|
UIView.animate(withDuration: animationDuration) {
|
||
|
guard let self = self else { return }
|
||
|
self.scrollView.contentOffset = CGPoint(x: 0, y: self.chatBubbles[0].height() + self.spacing + self.chatBubbles[1].height() + self.spacing)
|
||
|
}
|
||
5 years ago
|
Timer.scheduledTimer(withTimeInterval: delayBetweenMessages, repeats: false) { _ in
|
||
|
self?.showChatBubble(at: 4)
|
||
|
UIView.animate(withDuration: animationDuration) {
|
||
|
guard let self = self else { return }
|
||
|
self.scrollView.contentOffset = CGPoint(x: 0, y: self.chatBubbles[0].height() + self.spacing + self.chatBubbles[1].height() + self.spacing + self.chatBubbles[2].height() + self.spacing)
|
||
|
}
|
||
|
}
|
||
5 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private func showChatBubble(at index: Int) {
|
||
|
let chatBubble = chatBubbles[index]
|
||
|
UIView.animate(withDuration: Values.fakeChatAnimationDuration) {
|
||
|
chatBubble.alpha = 1
|
||
|
}
|
||
|
let scale = Values.fakeChatMessagePopAnimationStartScale
|
||
|
chatBubble.transform = CGAffineTransform(scaleX: scale, y: scale)
|
||
|
UIView.animate(withDuration: Values.fakeChatAnimationDuration, delay: 0, usingSpringWithDamping: 0.68, initialSpringVelocity: 4, options: .curveEaseInOut, animations: {
|
||
|
chatBubble.transform = CGAffineTransform(scaleX: 1, y: 1)
|
||
|
}, completion: nil)
|
||
|
}
|
||
|
}
|