session-ios/Session/Shared/CaptionView.swift

188 lines
5.2 KiB
Swift
Raw Normal View History

2019-03-30 14:24:40 +01:00
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
import UIKit
import SessionUIKit
2018-11-14 17:09:24 +01:00
public protocol CaptionContainerViewDelegate: AnyObject {
2018-11-14 17:09:24 +01:00
func captionContainerViewDidUpdateText(_ captionContainerView: CaptionContainerView)
}
public class CaptionContainerView: UIView {
weak var delegate: CaptionContainerViewDelegate?
var currentText: String? {
get { return currentCaptionView.text }
set {
currentCaptionView.text = newValue
delegate?.captionContainerViewDidUpdateText(self)
}
}
var pendingText: String? {
get { return pendingCaptionView.text }
set {
pendingCaptionView.text = newValue
delegate?.captionContainerViewDidUpdateText(self)
}
}
func updatePagerTransition(ratioComplete: CGFloat) {
if let currentText = self.currentText, currentText.count > 0 {
2018-11-14 17:09:24 +01:00
currentCaptionView.alpha = 1 - ratioComplete
} else {
currentCaptionView.alpha = 0
}
if let pendingText = self.pendingText, pendingText.count > 0 {
2018-11-14 17:09:24 +01:00
pendingCaptionView.alpha = ratioComplete
} else {
pendingCaptionView.alpha = 0
}
}
func completePagerTransition() {
updatePagerTransition(ratioComplete: 1)
// promote "pending" to "current" caption view.
let oldCaptionView = self.currentCaptionView
self.currentCaptionView = self.pendingCaptionView
self.pendingCaptionView = oldCaptionView
self.pendingText = nil
self.currentCaptionView.alpha = 1
self.pendingCaptionView.alpha = 0
2018-11-14 17:09:24 +01:00
}
// MARK: Initializers
override init(frame: CGRect) {
super.init(frame: frame)
setContentHuggingHigh()
setCompressionResistanceHigh()
addSubview(currentCaptionView)
currentCaptionView.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .top)
currentCaptionView.autoPinEdge(toSuperviewEdge: .top, withInset: 0, relation: .greaterThanOrEqual)
pendingCaptionView.alpha = 0
addSubview(pendingCaptionView)
pendingCaptionView.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .top)
pendingCaptionView.autoPinEdge(toSuperviewEdge: .top, withInset: 0, relation: .greaterThanOrEqual)
}
2018-11-15 18:34:38 +01:00
public required init?(coder aDecoder: NSCoder) {
2018-11-14 17:09:24 +01:00
fatalError("init(coder:) has not been implemented")
}
// MARK: Subviews
private var pendingCaptionView: CaptionView = CaptionView()
private var currentCaptionView: CaptionView = CaptionView()
}
private class CaptionView: UIView {
var text: String? {
get { return textView.text }
set {
if let captionText = newValue, captionText.count > 0 {
textView.text = captionText
} else {
textView.text = nil
}
}
}
// MARK: Subviews
let textView: CaptionTextView = {
let textView = CaptionTextView()
textView.font = UIFont.preferredFont(forTextStyle: .body)
textView.themeTextColor = .textPrimary
textView.themeBackgroundColor = .clear
2018-11-14 17:09:24 +01:00
textView.isEditable = false
textView.isSelectable = false
return textView
}()
Merge remote-tracking branch 'upstream/dev' into feature/theming # Conflicts: # Session.xcodeproj/project.pbxproj # Session/Closed Groups/NewClosedGroupVC.swift # Session/Conversations/ConversationVC+Interaction.swift # Session/Conversations/Message Cells/CallMessageCell.swift # Session/Conversations/Views & Modals/JoinOpenGroupModal.swift # Session/Home/HomeVC.swift # Session/Home/New Conversation/NewDMVC.swift # Session/Home/NewConversationButtonSet.swift # Session/Meta/Translations/de.lproj/Localizable.strings # Session/Meta/Translations/en.lproj/Localizable.strings # Session/Meta/Translations/es.lproj/Localizable.strings # Session/Meta/Translations/fa.lproj/Localizable.strings # Session/Meta/Translations/fi.lproj/Localizable.strings # Session/Meta/Translations/fr.lproj/Localizable.strings # Session/Meta/Translations/hi.lproj/Localizable.strings # Session/Meta/Translations/hr.lproj/Localizable.strings # Session/Meta/Translations/id-ID.lproj/Localizable.strings # Session/Meta/Translations/it.lproj/Localizable.strings # Session/Meta/Translations/ja.lproj/Localizable.strings # Session/Meta/Translations/nl.lproj/Localizable.strings # Session/Meta/Translations/pl.lproj/Localizable.strings # Session/Meta/Translations/pt_BR.lproj/Localizable.strings # Session/Meta/Translations/ru.lproj/Localizable.strings # Session/Meta/Translations/si.lproj/Localizable.strings # Session/Meta/Translations/sk.lproj/Localizable.strings # Session/Meta/Translations/sv.lproj/Localizable.strings # Session/Meta/Translations/th.lproj/Localizable.strings # Session/Meta/Translations/vi-VN.lproj/Localizable.strings # Session/Meta/Translations/zh-Hant.lproj/Localizable.strings # Session/Meta/Translations/zh_CN.lproj/Localizable.strings # Session/Open Groups/JoinOpenGroupVC.swift # Session/Open Groups/OpenGroupSuggestionGrid.swift # Session/Settings/SettingsVC.swift # Session/Shared/BaseVC.swift # Session/Shared/OWSQRCodeScanningViewController.m # Session/Shared/ScanQRCodeWrapperVC.swift # Session/Shared/UserCell.swift # SessionMessagingKit/Configuration.swift # SessionShareExtension/SAEScreenLockViewController.swift # SessionUIKit/Style Guide/Gradients.swift # SignalUtilitiesKit/Media Viewing & Editing/OWSViewController+ImageEditor.swift # SignalUtilitiesKit/Screen Lock/ScreenLockViewController.m
2022-09-26 03:16:47 +02:00
let scrollFadeView: GradientView = {
let result: GradientView = GradientView()
result.themeBackgroundGradient = [
.clear,
.black
]
return result
}()
2018-11-14 17:09:24 +01:00
// MARK: Initializers
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(textView)
textView.autoPinEdgesToSuperviewMargins()
addSubview(scrollFadeView)
scrollFadeView.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .top)
scrollFadeView.autoSetDimension(.height, toSize: 20)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: UIView overrides
override func layoutSubviews() {
super.layoutSubviews()
scrollFadeView.isHidden = !textView.doesContentNeedScroll
}
// MARK: -
class CaptionTextView: UITextView {
var kMaxHeight: CGFloat = ScaleFromIPhone5(200)
override var text: String! {
didSet {
invalidateIntrinsicContentSize()
}
}
override var font: UIFont? {
didSet {
invalidateIntrinsicContentSize()
}
}
var doesContentNeedScroll: Bool {
return self.bounds.height == kMaxHeight
}
override func layoutSubviews() {
super.layoutSubviews()
// Enable/disable scrolling depending on wether we've clipped
// content in `intrinsicContentSize`
2018-11-15 17:29:49 +01:00
isScrollEnabled = doesContentNeedScroll
2018-11-14 17:09:24 +01:00
}
override var intrinsicContentSize: CGSize {
var size = super.intrinsicContentSize
2019-03-30 14:22:31 +01:00
if size.height == UIView.noIntrinsicMetric {
2018-11-14 17:09:24 +01:00
size.height = layoutManager.usedRect(for: textContainer).height + textContainerInset.top + textContainerInset.bottom
}
size.height = min(kMaxHeight, size.height)
return size
}
}
}