mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
414 lines
21 KiB
Swift
414 lines
21 KiB
Swift
import Foundation
|
|
import Display
|
|
import QuickLook
|
|
import Postbox
|
|
import SwiftSignalKit
|
|
import AsyncDisplayKit
|
|
import TelegramCore
|
|
import Photos
|
|
|
|
enum WallpaperListType {
|
|
case wallpapers(WallpaperPresentationOptions?)
|
|
case colors
|
|
}
|
|
|
|
enum WallpaperListSource {
|
|
case list(wallpapers: [TelegramWallpaper], central: TelegramWallpaper, type: WallpaperListType)
|
|
case wallpaper(TelegramWallpaper)
|
|
case slug(String, TelegramMediaFile?)
|
|
case asset(PHAsset, UIImage?)
|
|
case contextResult(ChatContextResult)
|
|
case customColor(Int32?)
|
|
}
|
|
|
|
enum WallpaperGalleryEntry: Equatable {
|
|
case wallpaper(TelegramWallpaper)
|
|
case asset(PHAsset, UIImage?)
|
|
case contextResult(ChatContextResult)
|
|
|
|
public static func ==(lhs: WallpaperGalleryEntry, rhs: WallpaperGalleryEntry) -> Bool {
|
|
switch lhs {
|
|
case let .wallpaper(wallpaper):
|
|
if case .wallpaper(wallpaper) = rhs {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
case let .asset(lhsAsset, _):
|
|
if case let .asset(rhsAsset, _) = rhs, lhsAsset.localIdentifier == rhsAsset.localIdentifier {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
case let .contextResult(lhsResult):
|
|
if case let .contextResult(rhsResult) = rhs, lhsResult.id == rhsResult.id {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class WallpaperGalleryOverlayNode: ASDisplayNode {
|
|
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
|
|
let result = super.hitTest(point, with: event)
|
|
if result != self.view {
|
|
return result
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
class WallpaperGalleryController: ViewController {
|
|
private var galleryNode: GalleryControllerNode {
|
|
return self.displayNode as! GalleryControllerNode
|
|
}
|
|
|
|
private let context: AccountContext
|
|
private let source: WallpaperListSource
|
|
var apply: ((WallpaperGalleryEntry, WallpaperPresentationOptions, CGRect?) -> Void)?
|
|
|
|
private let _ready = Promise<Bool>()
|
|
override var ready: Promise<Bool> {
|
|
return self._ready
|
|
}
|
|
private var didSetReady = false
|
|
|
|
private let disposable = MetaDisposable()
|
|
|
|
private var presentationData: PresentationData
|
|
private var presentationDataDisposable: Disposable?
|
|
|
|
private var entries: [WallpaperGalleryEntry] = []
|
|
private var centralEntryIndex: Int?
|
|
|
|
private let centralItemControlsColor = Promise<UIColor>()
|
|
private let centralItemStatus = Promise<MediaResourceStatus>()
|
|
private let centralItemAttributesDisposable = DisposableSet();
|
|
|
|
private var validLayout: (ContainerViewLayout, CGFloat)?
|
|
|
|
private var overlayNode: WallpaperGalleryOverlayNode?
|
|
private var messageNodes: [ListViewItemNode]?
|
|
private var toolbarNode: ThemeGalleryToolbarNode?
|
|
|
|
init(context: AccountContext, source: WallpaperListSource) {
|
|
self.context = context
|
|
self.source = source
|
|
self.presentationData = context.currentPresentationData.with { $0 }
|
|
|
|
super.init(navigationBarPresentationData: NavigationBarPresentationData(presentationData: presentationData))
|
|
|
|
self.title = self.presentationData.strings.Wallpaper_Title
|
|
self.statusBar.statusBarStyle = self.presentationData.theme.rootController.statusBar.style.style
|
|
|
|
switch source {
|
|
case let .list(wallpapers, central, type):
|
|
self.entries = wallpapers.map { .wallpaper($0) }
|
|
self.centralEntryIndex = wallpapers.index(of: central)!
|
|
|
|
//if case let .wallpapers(wallpaperMode) = type, let mode = wallpaperMode {
|
|
// self.segmentedControl.selectedSegmentIndex = Int(clamping: mode.rawValue)
|
|
//}
|
|
case let .slug(slug, file):
|
|
if let file = file {
|
|
self.entries = [.wallpaper(.file(id: 0, accessHash: 0, isCreator: false, isDefault: false, slug: slug, file: file))]
|
|
self.centralEntryIndex = 0
|
|
}
|
|
case let .wallpaper(wallpaper):
|
|
self.entries = [.wallpaper(wallpaper)]
|
|
self.centralEntryIndex = 0
|
|
case let .asset(asset, thumbnailImage):
|
|
self.entries = [.asset(asset, thumbnailImage)]
|
|
self.centralEntryIndex = 0
|
|
case let .contextResult(result):
|
|
self.entries = [.contextResult(result)]
|
|
self.centralEntryIndex = 0
|
|
case let .customColor(color):
|
|
let initialColor = color ?? 0x000000
|
|
self.entries = [.wallpaper(.color(initialColor))]
|
|
self.centralEntryIndex = 0
|
|
}
|
|
|
|
self.presentationDataDisposable = (context.presentationData
|
|
|> deliverOnMainQueue).start(next: { [weak self] presentationData in
|
|
if let strongSelf = self {
|
|
let previousTheme = strongSelf.presentationData.theme
|
|
let previousStrings = strongSelf.presentationData.strings
|
|
|
|
strongSelf.presentationData = presentationData
|
|
if previousTheme !== presentationData.theme || previousStrings !== presentationData.strings {
|
|
strongSelf.updateThemeAndStrings()
|
|
}
|
|
}
|
|
})
|
|
|
|
self.centralItemAttributesDisposable.add(self.centralItemStatus.get().start(next: { [weak self] status in
|
|
if let strongSelf = self {
|
|
let enabled: Bool
|
|
switch status {
|
|
case .Local:
|
|
enabled = true
|
|
default:
|
|
enabled = false
|
|
}
|
|
strongSelf.toolbarNode?.setDoneEnabled(enabled)
|
|
}
|
|
}))
|
|
}
|
|
|
|
required init(coder aDecoder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
deinit {
|
|
self.disposable.dispose()
|
|
self.presentationDataDisposable?.dispose()
|
|
self.centralItemAttributesDisposable.dispose()
|
|
}
|
|
|
|
private func updateThemeAndStrings() {
|
|
self.title = self.presentationData.strings.Wallpaper_Title
|
|
self.statusBar.statusBarStyle = self.presentationData.theme.rootController.statusBar.style.style
|
|
self.toolbarNode?.updateThemeAndStrings(theme: self.presentationData.theme, strings: self.presentationData.strings)
|
|
}
|
|
|
|
private func dismiss(forceAway: Bool) {
|
|
let completion: () -> Void = { [weak self] in
|
|
self?.presentingViewController?.dismiss(animated: false, completion: nil)
|
|
}
|
|
|
|
self.galleryNode.modalAnimateOut(completion: completion)
|
|
}
|
|
|
|
override func loadDisplayNode() {
|
|
let controllerInteraction = GalleryControllerInteraction(presentController: { [weak self] controller, arguments in
|
|
if let strongSelf = self {
|
|
strongSelf.present(controller, in: .window(.root), with: arguments, blockInteraction: true)
|
|
}
|
|
}, dismissController: { [weak self] in
|
|
self?.dismiss(forceAway: true)
|
|
}, replaceRootController: { controller, ready in
|
|
})
|
|
self.displayNode = GalleryControllerNode(controllerInteraction: controllerInteraction, pageGap: 0.0)
|
|
self.displayNodeDidLoad()
|
|
|
|
self.galleryNode.statusBar = self.statusBar
|
|
self.galleryNode.navigationBar = self.navigationBar
|
|
self.galleryNode.dismiss = { [weak self] in
|
|
self?.presentingViewController?.dismiss(animated: false, completion: nil)
|
|
}
|
|
|
|
self.galleryNode.pager.centralItemIndexUpdated = { [weak self] index in
|
|
if let strongSelf = self {
|
|
if let node = strongSelf.galleryNode.pager.centralItemNode() as? WallpaperGalleryItemNode {
|
|
strongSelf.centralItemStatus.set(node.status.get())
|
|
}
|
|
}
|
|
}
|
|
|
|
self.galleryNode.backgroundNode.backgroundColor = nil
|
|
self.galleryNode.backgroundNode.isOpaque = false
|
|
self.galleryNode.isBackgroundExtendedOverNavigationBar = true
|
|
|
|
let presentationData = context.currentPresentationData.with { $0 }
|
|
let toolbarNode = ThemeGalleryToolbarNode(theme: presentationData.theme, strings: presentationData.strings)
|
|
let overlayNode = WallpaperGalleryOverlayNode()
|
|
self.overlayNode = overlayNode
|
|
self.galleryNode.overlayNode = overlayNode
|
|
self.galleryNode.addSubnode(overlayNode)
|
|
|
|
self.toolbarNode = toolbarNode
|
|
overlayNode.addSubnode(toolbarNode)
|
|
|
|
toolbarNode.cancel = { [weak self] in
|
|
self?.dismiss(forceAway: true)
|
|
}
|
|
toolbarNode.done = { [weak self] in
|
|
if let strongSelf = self {
|
|
if let centralItemNode = strongSelf.galleryNode.pager.centralItemNode() {
|
|
if !strongSelf.entries.isEmpty {
|
|
let entry = strongSelf.entries[centralItemNode.index]
|
|
switch entry {
|
|
case let .wallpaper(wallpaper):
|
|
let _ = (updatePresentationThemeSettingsInteractively(postbox: strongSelf.context.account.postbox, { current in
|
|
return PresentationThemeSettings(chatWallpaper: wallpaper, chatWallpaperOptions: [], theme: current.theme, themeAccentColor: current.themeAccentColor, fontSize: current.fontSize, automaticThemeSwitchSetting: current.automaticThemeSwitchSetting, disableAnimations: current.disableAnimations)
|
|
}) |> deliverOnMainQueue).start(completed: {
|
|
self?.dismiss(forceAway: true)
|
|
})
|
|
default:
|
|
break
|
|
}
|
|
strongSelf.apply?(entry, [], nil)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
let ready = self.galleryNode.pager.ready() |> timeout(2.0, queue: Queue.mainQueue(), alternate: .single(Void())) |> afterNext { [weak self] _ in
|
|
self?.didSetReady = true
|
|
}
|
|
self._ready.set(ready |> map { true })
|
|
}
|
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
super.viewDidAppear(animated)
|
|
|
|
self.galleryNode.modalAnimateIn()
|
|
|
|
if let centralItemNode = self.galleryNode.pager.centralItemNode() as? WallpaperGalleryItemNode {
|
|
self.centralItemStatus.set(centralItemNode.status.get())
|
|
}
|
|
}
|
|
|
|
override func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
|
|
super.containerLayoutUpdated(layout, transition: transition)
|
|
|
|
self.galleryNode.frame = CGRect(origin: CGPoint(), size: layout.size)
|
|
self.galleryNode.containerLayoutUpdated(layout, navigationBarHeight: self.navigationHeight, transition: transition)
|
|
self.overlayNode?.frame = self.galleryNode.bounds
|
|
|
|
var items: [ChatMessageItem] = []
|
|
let peerId = PeerId(namespace: Namespaces.Peer.CloudUser, id: 1)
|
|
let otherPeerId = self.context.account.peerId
|
|
var peers = SimpleDictionary<PeerId, Peer>()
|
|
let messages = SimpleDictionary<MessageId, Message>()
|
|
peers[peerId] = TelegramUser(id: peerId, accessHash: nil, firstName: self.presentationData.strings.Appearance_PreviewReplyAuthor, lastName: "", username: nil, phone: nil, photo: [], botInfo: nil, restrictionInfo: nil, flags: [])
|
|
peers[otherPeerId] = TelegramUser(id: otherPeerId, accessHash: nil, firstName: self.presentationData.strings.Appearance_PreviewReplyAuthor, lastName: "", username: nil, phone: nil, photo: [], botInfo: nil, restrictionInfo: nil, flags: [])
|
|
let controllerInteraction = ChatControllerInteraction(openMessage: { _, _ in
|
|
return false }, openPeer: { _, _, _ in }, openPeerMention: { _ in }, openMessageContextMenu: { _, _, _, _ in }, navigateToMessage: { _, _ in }, clickThroughMessage: { }, toggleMessagesSelection: { _, _ in }, sendMessage: { _ in }, sendSticker: { _, _ in }, sendGif: { _ in }, requestMessageActionCallback: { _, _, _ in }, activateSwitchInline: { _, _ in }, openUrl: { _, _, _ in }, shareCurrentLocation: {}, shareAccountContact: {}, sendBotCommand: { _, _ in }, openInstantPage: { _ in }, openWallpaper: { _ in }, openHashtag: { _, _ in }, updateInputState: { _ in }, updateInputMode: { _ in }, openMessageShareMenu: { _ in
|
|
}, presentController: { _, _ in }, navigationController: {
|
|
return nil
|
|
}, presentGlobalOverlayController: { _, _ in }, callPeer: { _ in }, longTap: { _ in }, openCheckoutOrReceipt: { _ in }, openSearch: { }, setupReply: { _ in
|
|
}, canSetupReply: { _ in
|
|
return false
|
|
}, navigateToFirstDateMessage: { _ in
|
|
}, requestRedeliveryOfFailedMessages: { _ in
|
|
}, addContact: { _ in
|
|
}, rateCall: { _, _ in
|
|
}, requestSelectMessagePollOption: { _, _ in
|
|
}, openAppStorePage: {
|
|
}, requestMessageUpdate: { _ in
|
|
}, cancelInteractiveKeyboardGestures: {
|
|
}, automaticMediaDownloadSettings: AutomaticMediaDownloadSettings.defaultSettings,
|
|
pollActionState: ChatInterfacePollActionState())
|
|
|
|
let chatPresentationData = ChatPresentationData(theme: ChatPresentationThemeData(theme: self.presentationData.theme, wallpaper: self.presentationData.chatWallpaper), fontSize: self.presentationData.fontSize, strings: self.presentationData.strings, dateTimeFormat: self.presentationData.dateTimeFormat, nameDisplayOrder: self.presentationData.nameDisplayOrder, disableAnimations: false)
|
|
|
|
let topMessageText: String
|
|
let bottomMessageText: String
|
|
switch self.source {
|
|
case .wallpaper, .slug:
|
|
topMessageText = presentationData.strings.WallpaperPreview_PreviewTopText
|
|
bottomMessageText = presentationData.strings.WallpaperPreview_PreviewBottomText
|
|
case let .list(_, _, type):
|
|
switch type {
|
|
case .wallpapers:
|
|
topMessageText = presentationData.strings.WallpaperPreview_SwipeTopText
|
|
bottomMessageText = presentationData.strings.WallpaperPreview_SwipeBottomText
|
|
case .colors:
|
|
topMessageText = presentationData.strings.WallpaperPreview_SwipeColorsTopText
|
|
bottomMessageText = presentationData.strings.WallpaperPreview_SwipeColorsBottomText
|
|
}
|
|
case .asset, .contextResult:
|
|
topMessageText = presentationData.strings.WallpaperPreview_CropTopText
|
|
bottomMessageText = presentationData.strings.WallpaperPreview_CropBottomText
|
|
case .customColor:
|
|
topMessageText = presentationData.strings.WallpaperPreview_CustomColorTopText
|
|
bottomMessageText = presentationData.strings.WallpaperPreview_CustomColorBottomText
|
|
}
|
|
|
|
items.append(ChatMessageItem(presentationData: chatPresentationData, context: self.context, chatLocation: .peer(peerId), associatedData: ChatMessageItemAssociatedData(automaticDownloadPeerType: .contact, automaticDownloadNetworkType: .cellular, isRecentActions: false), controllerInteraction: controllerInteraction, content: .message(message: Message(stableId: 2, stableVersion: 0, id: MessageId(peerId: peerId, namespace: 0, id: 2), globallyUniqueId: nil, groupingKey: nil, groupInfo: nil, timestamp: 66001, flags: [], tags: [], globalTags: [], localTags: [], forwardInfo: nil, author: peers[otherPeerId], text: bottomMessageText, attributes: [], media: [], peers: peers, associatedMessages: messages, associatedMessageIds: []), read: true, selection: .none, isAdmin: false), disableDate: true))
|
|
|
|
items.append(ChatMessageItem(presentationData: chatPresentationData, context: self.context, chatLocation: .peer(peerId), associatedData: ChatMessageItemAssociatedData(automaticDownloadPeerType: .contact, automaticDownloadNetworkType: .cellular, isRecentActions: false), controllerInteraction: controllerInteraction, content: .message(message: Message(stableId: 1, stableVersion: 0, id: MessageId(peerId: peerId, namespace: 0, id: 1), globallyUniqueId: nil, groupingKey: nil, groupInfo: nil, timestamp: 66000, flags: [.Incoming], tags: [], globalTags: [], localTags: [], forwardInfo: nil, author: peers[peerId], text: topMessageText, attributes: [], media: [], peers: peers, associatedMessages: messages, associatedMessageIds: []), read: true, selection: .none, isAdmin: false), disableDate: true))
|
|
|
|
let params = ListViewItemLayoutParams(width: layout.size.width, leftInset: layout.safeInsets.left, rightInset: layout.safeInsets.right)
|
|
if let messageNodes = self.messageNodes {
|
|
for i in 0 ..< items.count {
|
|
let itemNode = messageNodes[i]
|
|
items[i].updateNode(async: { $0() }, node: {
|
|
return itemNode
|
|
}, params: params, previousItem: i == 0 ? nil : items[i - 1], nextItem: i == (items.count - 1) ? nil : items[i + 1], animation: .None, completion: { (layout, apply) in
|
|
let nodeFrame = CGRect(origin: itemNode.frame.origin, size: CGSize(width: layout.size.width, height: layout.size.height))
|
|
|
|
itemNode.contentSize = layout.contentSize
|
|
itemNode.insets = layout.insets
|
|
itemNode.frame = nodeFrame
|
|
itemNode.isUserInteractionEnabled = false
|
|
|
|
apply(ListViewItemApply(isOnScreen: true))
|
|
})
|
|
}
|
|
} else {
|
|
var messageNodes: [ListViewItemNode] = []
|
|
for i in 0 ..< items.count {
|
|
var itemNode: ListViewItemNode?
|
|
items[i].nodeConfiguredForParams(async: { $0() }, params: params, synchronousLoads: false, previousItem: i == 0 ? nil : items[i - 1], nextItem: i == (items.count - 1) ? nil : items[i + 1], completion: { node, apply in
|
|
itemNode = node
|
|
apply().1(ListViewItemApply(isOnScreen: true))
|
|
})
|
|
itemNode!.subnodeTransform = CATransform3DMakeRotation(CGFloat.pi, 0.0, 0.0, 1.0)
|
|
itemNode!.isUserInteractionEnabled = false
|
|
messageNodes.append(itemNode!)
|
|
self.overlayNode?.addSubnode(itemNode!)
|
|
}
|
|
self.messageNodes = messageNodes
|
|
}
|
|
|
|
var bottomInset = layout.intrinsicInsets.bottom + 49.0
|
|
|
|
var optionsAvailable = true
|
|
if let centralItemNode = self.galleryNode.pager.centralItemNode() {
|
|
if !self.entries.isEmpty {
|
|
let entry = self.entries[centralItemNode.index]
|
|
switch entry {
|
|
case let .wallpaper(wallpaper):
|
|
switch wallpaper {
|
|
case .color:
|
|
optionsAvailable = false
|
|
default:
|
|
break
|
|
}
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
transition.updateFrame(node: self.toolbarNode!, frame: CGRect(origin: CGPoint(x: 0.0, y: layout.size.height - 49.0 - layout.intrinsicInsets.bottom), size: CGSize(width: layout.size.width, height: 49.0 + layout.intrinsicInsets.bottom)))
|
|
self.toolbarNode!.updateLayout(size: CGSize(width: layout.size.width, height: 49.0), layout: layout, transition: transition)
|
|
|
|
if let messageNodes = self.messageNodes {
|
|
var bottomOffset: CGFloat = layout.size.height - bottomInset - 9.0
|
|
// if optionsAvailable {
|
|
// bottomOffset -= segmentedControlSize.height + 37.0
|
|
// }
|
|
for itemNode in messageNodes {
|
|
transition.updateFrame(node: itemNode, frame: CGRect(origin: CGPoint(x: 0.0, y: bottomOffset - itemNode.frame.height), size: itemNode.frame.size))
|
|
bottomOffset -= itemNode.frame.height
|
|
}
|
|
}
|
|
|
|
let replace = self.validLayout == nil
|
|
self.validLayout = (layout, 0.0)
|
|
|
|
if replace {
|
|
self.galleryNode.pager.replaceItems(self.entries.map({ WallpaperGalleryItem(context: self.context, entry: $0) }), centralItemIndex: self.centralEntryIndex)
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension GalleryControllerNode {
|
|
func modalAnimateIn(completion: (() -> Void)? = nil) {
|
|
self.layer.animatePosition(from: CGPoint(x: self.layer.position.x, y: self.layer.position.y + self.layer.bounds.size.height), to: self.layer.position, duration: 0.5, timingFunction: kCAMediaTimingFunctionSpring)
|
|
}
|
|
|
|
func modalAnimateOut(completion: (() -> Void)? = nil) {
|
|
self.layer.animatePosition(from: self.layer.position, to: CGPoint(x: self.layer.position.x, y: self.layer.position.y + self.layer.bounds.size.height), duration: 0.2, timingFunction: kCAMediaTimingFunctionEaseInEaseOut, removeOnCompletion: false, completion: { _ in
|
|
completion?()
|
|
})
|
|
}
|
|
}
|