mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
303 lines
14 KiB
Swift
303 lines
14 KiB
Swift
import Foundation
|
|
import UIKit
|
|
import SwiftSignalKit
|
|
import AsyncDisplayKit
|
|
import Display
|
|
import Postbox
|
|
import TelegramCore
|
|
import TelegramPresentationData
|
|
import TelegramUIPreferences
|
|
import AccountContext
|
|
import AppBundle
|
|
import AvatarNode
|
|
import CheckNode
|
|
import Markdown
|
|
|
|
private let textFont = Font.regular(13.0)
|
|
private let boldTextFont = Font.semibold(13.0)
|
|
|
|
private func formattedText(_ text: String, color: UIColor, linkColor: UIColor, textAlignment: NSTextAlignment = .natural) -> NSAttributedString {
|
|
return parseMarkdownIntoAttributedString(text, attributes: MarkdownAttributes(body: MarkdownAttributeSet(font: textFont, textColor: color), bold: MarkdownAttributeSet(font: boldTextFont, textColor: color), link: MarkdownAttributeSet(font: textFont, textColor: linkColor), linkAttribute: { _ in return nil}), textAlignment: textAlignment)
|
|
}
|
|
|
|
private final class WebAppTermsAlertContentNode: AlertContentNode {
|
|
private let strings: PresentationStrings
|
|
private let title: String
|
|
private let text: String
|
|
|
|
private let titleNode: ImmediateTextNode
|
|
private let textNode: ASTextNode
|
|
|
|
private let acceptTermsCheckNode: InteractiveCheckNode
|
|
private let acceptTermsLabelNode: ASTextNode
|
|
|
|
private let actionNodesSeparator: ASDisplayNode
|
|
private let actionNodes: [TextAlertContentActionNode]
|
|
private let actionVerticalSeparators: [ASDisplayNode]
|
|
|
|
private var validLayout: CGSize?
|
|
|
|
override var dismissOnOutsideTap: Bool {
|
|
return self.isUserInteractionEnabled
|
|
}
|
|
|
|
var acceptedTerms: Bool = false {
|
|
didSet {
|
|
self.acceptTermsCheckNode.setSelected(self.acceptedTerms, animated: true)
|
|
if let firstAction = self.actionNodes.first {
|
|
firstAction.actionEnabled = self.acceptedTerms
|
|
}
|
|
}
|
|
}
|
|
|
|
init(context: AccountContext, theme: AlertControllerTheme, ptheme: PresentationTheme, strings: PresentationStrings, title: String, text: String, actions: [TextAlertAction]) {
|
|
self.strings = strings
|
|
self.title = title
|
|
self.text = text
|
|
|
|
self.titleNode = ImmediateTextNode()
|
|
self.titleNode.displaysAsynchronously = false
|
|
self.titleNode.maximumNumberOfLines = 1
|
|
self.titleNode.textAlignment = .center
|
|
|
|
self.textNode = ASTextNode()
|
|
self.textNode.displaysAsynchronously = false
|
|
self.textNode.maximumNumberOfLines = 0
|
|
|
|
self.acceptTermsCheckNode = InteractiveCheckNode(theme: CheckNodeTheme(backgroundColor: theme.accentColor, strokeColor: theme.contrastColor, borderColor: theme.controlBorderColor, overlayBorder: false, hasInset: false, hasShadow: false))
|
|
self.acceptTermsLabelNode = ASTextNode()
|
|
self.acceptTermsLabelNode.maximumNumberOfLines = 4
|
|
self.acceptTermsLabelNode.isUserInteractionEnabled = true
|
|
|
|
self.actionNodesSeparator = ASDisplayNode()
|
|
self.actionNodesSeparator.isLayerBacked = true
|
|
|
|
self.actionNodes = actions.map { action -> TextAlertContentActionNode in
|
|
return TextAlertContentActionNode(theme: theme, action: action)
|
|
}
|
|
|
|
var actionVerticalSeparators: [ASDisplayNode] = []
|
|
if actions.count > 1 {
|
|
for _ in 0 ..< actions.count - 1 {
|
|
let separatorNode = ASDisplayNode()
|
|
separatorNode.isLayerBacked = true
|
|
actionVerticalSeparators.append(separatorNode)
|
|
}
|
|
}
|
|
self.actionVerticalSeparators = actionVerticalSeparators
|
|
|
|
super.init()
|
|
|
|
self.addSubnode(self.titleNode)
|
|
self.addSubnode(self.textNode)
|
|
|
|
self.addSubnode(self.acceptTermsCheckNode)
|
|
self.addSubnode(self.acceptTermsLabelNode)
|
|
|
|
self.addSubnode(self.actionNodesSeparator)
|
|
|
|
for actionNode in self.actionNodes {
|
|
self.addSubnode(actionNode)
|
|
}
|
|
|
|
for separatorNode in self.actionVerticalSeparators {
|
|
self.addSubnode(separatorNode)
|
|
}
|
|
|
|
if let firstAction = self.actionNodes.first {
|
|
firstAction.actionEnabled = false
|
|
}
|
|
|
|
self.acceptTermsCheckNode.valueChanged = { [weak self] value in
|
|
if let strongSelf = self {
|
|
strongSelf.acceptedTerms = !strongSelf.acceptedTerms
|
|
}
|
|
}
|
|
|
|
self.updateTheme(theme)
|
|
}
|
|
|
|
override func didLoad() {
|
|
super.didLoad()
|
|
|
|
self.acceptTermsLabelNode.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.acceptTap(_:))))
|
|
}
|
|
|
|
@objc private func acceptTap(_ gestureRecognizer: UITapGestureRecognizer) {
|
|
if self.acceptTermsCheckNode.isUserInteractionEnabled {
|
|
self.acceptedTerms = !self.acceptedTerms
|
|
}
|
|
}
|
|
|
|
override func updateTheme(_ theme: AlertControllerTheme) {
|
|
self.titleNode.attributedText = NSAttributedString(string: self.title, font: Font.semibold(17.0), textColor: theme.primaryColor, paragraphAlignment: .center)
|
|
self.textNode.attributedText = NSAttributedString(string: self.text, font: Font.regular(13.0), textColor: theme.primaryColor, paragraphAlignment: .center)
|
|
|
|
let text = "I agree to the [Terms of Use]()"
|
|
self.acceptTermsLabelNode.attributedText = formattedText(text, color: theme.primaryColor, linkColor: theme.accentColor)
|
|
|
|
self.actionNodesSeparator.backgroundColor = theme.separatorColor
|
|
for actionNode in self.actionNodes {
|
|
actionNode.updateTheme(theme)
|
|
}
|
|
for separatorNode in self.actionVerticalSeparators {
|
|
separatorNode.backgroundColor = theme.separatorColor
|
|
}
|
|
|
|
if let size = self.validLayout {
|
|
_ = self.updateLayout(size: size, transition: .immediate)
|
|
}
|
|
}
|
|
|
|
override func updateLayout(size: CGSize, transition: ContainedViewLayoutTransition) -> CGSize {
|
|
var size = size
|
|
size.width = min(size.width, 270.0)
|
|
|
|
self.validLayout = size
|
|
|
|
var origin: CGPoint = CGPoint(x: 0.0, y: 17.0)
|
|
|
|
let titleSize = self.titleNode.updateLayout(CGSize(width: size.width - 32.0, height: size.height))
|
|
transition.updateFrame(node: self.titleNode, frame: CGRect(origin: CGPoint(x: floorToScreenPixels((size.width - titleSize.width) / 2.0), y: origin.y), size: titleSize))
|
|
origin.y += titleSize.height + 4.0
|
|
|
|
var entriesHeight: CGFloat = 0.0
|
|
|
|
let textSize = self.textNode.measure(CGSize(width: size.width - 48.0, height: size.height))
|
|
transition.updateFrame(node: self.textNode, frame: CGRect(origin: CGPoint(x: floorToScreenPixels((size.width - textSize.width) / 2.0), y: origin.y), size: textSize))
|
|
origin.y += textSize.height
|
|
|
|
if self.acceptTermsLabelNode.supernode != nil {
|
|
origin.y += 21.0
|
|
entriesHeight += 21.0
|
|
|
|
let checkSize = CGSize(width: 22.0, height: 22.0)
|
|
let condensedSize = CGSize(width: size.width - 76.0, height: size.height)
|
|
|
|
let spacing: CGFloat = 12.0
|
|
let acceptTermsSize = self.acceptTermsLabelNode.measure(condensedSize)
|
|
let acceptTermsTotalWidth = checkSize.width + spacing + acceptTermsSize.width
|
|
let acceptTermsOriginX = floorToScreenPixels((size.width - acceptTermsTotalWidth) / 2.0)
|
|
|
|
transition.updateFrame(node: self.acceptTermsCheckNode, frame: CGRect(origin: CGPoint(x: acceptTermsOriginX, y: origin.y - 3.0), size: checkSize))
|
|
transition.updateFrame(node: self.acceptTermsLabelNode, frame: CGRect(origin: CGPoint(x: acceptTermsOriginX + checkSize.width + spacing, y: origin.y), size: acceptTermsSize))
|
|
origin.y += acceptTermsSize.height
|
|
entriesHeight += acceptTermsSize.height
|
|
}
|
|
|
|
let actionButtonHeight: CGFloat = 44.0
|
|
var minActionsWidth: CGFloat = 0.0
|
|
let maxActionWidth: CGFloat = floor(size.width / CGFloat(self.actionNodes.count))
|
|
let actionTitleInsets: CGFloat = 8.0
|
|
|
|
var effectiveActionLayout = TextAlertContentActionLayout.vertical
|
|
for actionNode in self.actionNodes {
|
|
let actionTitleSize = actionNode.titleNode.updateLayout(CGSize(width: maxActionWidth, height: actionButtonHeight))
|
|
if case .horizontal = effectiveActionLayout, actionTitleSize.height > actionButtonHeight * 0.6667 {
|
|
effectiveActionLayout = .vertical
|
|
}
|
|
switch effectiveActionLayout {
|
|
case .horizontal:
|
|
minActionsWidth += actionTitleSize.width + actionTitleInsets
|
|
case .vertical:
|
|
minActionsWidth = max(minActionsWidth, actionTitleSize.width + actionTitleInsets)
|
|
}
|
|
}
|
|
|
|
let insets = UIEdgeInsets(top: 18.0, left: 18.0, bottom: 18.0, right: 18.0)
|
|
|
|
let contentWidth = max(size.width, minActionsWidth)
|
|
|
|
var actionsHeight: CGFloat = 0.0
|
|
switch effectiveActionLayout {
|
|
case .horizontal:
|
|
actionsHeight = actionButtonHeight
|
|
case .vertical:
|
|
actionsHeight = actionButtonHeight * CGFloat(self.actionNodes.count)
|
|
}
|
|
|
|
let resultSize = CGSize(width: contentWidth, height: titleSize.height + textSize.height + entriesHeight + actionsHeight + 3.0 + insets.top + insets.bottom)
|
|
|
|
transition.updateFrame(node: self.actionNodesSeparator, frame: CGRect(origin: CGPoint(x: 0.0, y: resultSize.height - actionsHeight - UIScreenPixel), size: CGSize(width: resultSize.width, height: UIScreenPixel)))
|
|
|
|
var actionOffset: CGFloat = 0.0
|
|
let actionWidth: CGFloat = floor(resultSize.width / CGFloat(self.actionNodes.count))
|
|
var separatorIndex = -1
|
|
var nodeIndex = 0
|
|
for actionNode in self.actionNodes {
|
|
if separatorIndex >= 0 {
|
|
let separatorNode = self.actionVerticalSeparators[separatorIndex]
|
|
switch effectiveActionLayout {
|
|
case .horizontal:
|
|
transition.updateFrame(node: separatorNode, frame: CGRect(origin: CGPoint(x: actionOffset - UIScreenPixel, y: resultSize.height - actionsHeight), size: CGSize(width: UIScreenPixel, height: actionsHeight - UIScreenPixel)))
|
|
case .vertical:
|
|
transition.updateFrame(node: separatorNode, frame: CGRect(origin: CGPoint(x: 0.0, y: resultSize.height - actionsHeight + actionOffset - UIScreenPixel), size: CGSize(width: resultSize.width, height: UIScreenPixel)))
|
|
}
|
|
}
|
|
separatorIndex += 1
|
|
|
|
let currentActionWidth: CGFloat
|
|
switch effectiveActionLayout {
|
|
case .horizontal:
|
|
if nodeIndex == self.actionNodes.count - 1 {
|
|
currentActionWidth = resultSize.width - actionOffset
|
|
} else {
|
|
currentActionWidth = actionWidth
|
|
}
|
|
case .vertical:
|
|
currentActionWidth = resultSize.width
|
|
}
|
|
|
|
let actionNodeFrame: CGRect
|
|
switch effectiveActionLayout {
|
|
case .horizontal:
|
|
actionNodeFrame = CGRect(origin: CGPoint(x: actionOffset, y: resultSize.height - actionsHeight), size: CGSize(width: currentActionWidth, height: actionButtonHeight))
|
|
actionOffset += currentActionWidth
|
|
case .vertical:
|
|
actionNodeFrame = CGRect(origin: CGPoint(x: 0.0, y: resultSize.height - actionsHeight + actionOffset), size: CGSize(width: currentActionWidth, height: actionButtonHeight))
|
|
actionOffset += actionButtonHeight
|
|
}
|
|
|
|
transition.updateFrame(node: actionNode, frame: actionNodeFrame)
|
|
|
|
nodeIndex += 1
|
|
}
|
|
|
|
return resultSize
|
|
}
|
|
}
|
|
|
|
public func webAppTermsAlertController(context: AccountContext, updatedPresentationData: (initial: PresentationData, signal: Signal<PresentationData, NoError>)?, peer: EnginePeer, completion: @escaping () -> Void) -> AlertController {
|
|
let theme = defaultDarkColorPresentationTheme
|
|
let presentationData: PresentationData
|
|
if let updatedPresentationData {
|
|
presentationData = updatedPresentationData.initial
|
|
} else {
|
|
presentationData = context.sharedContext.currentPresentationData.with { $0 }
|
|
}
|
|
let strings = presentationData.strings
|
|
|
|
var dismissImpl: ((Bool) -> Void)?
|
|
let actions: [TextAlertAction] = [TextAlertAction(type: .defaultAction, title: "Continue", action: {
|
|
completion()
|
|
dismissImpl?(true)
|
|
}), TextAlertAction(type: .genericAction, title: presentationData.strings.Common_Cancel, action: {
|
|
dismissImpl?(true)
|
|
})]
|
|
|
|
let title = "Warning"
|
|
let text = "You are about to use a mini app operated by an independent party not affiliated with Telegram. You must agree to the Terms of Use of mini apps to continue."
|
|
|
|
let contentNode = WebAppTermsAlertContentNode(context: context, theme: AlertControllerTheme(presentationData: presentationData), ptheme: theme, strings: strings, title: title, text: text, actions: actions)
|
|
|
|
let controller = AlertController(theme: AlertControllerTheme(presentationData: presentationData), contentNode: contentNode)
|
|
dismissImpl = { [weak controller] animated in
|
|
if animated {
|
|
controller?.dismissAnimated()
|
|
} else {
|
|
controller?.dismiss()
|
|
}
|
|
}
|
|
return controller
|
|
}
|