|
| 1 | +import Foundation |
| 2 | +import UIKit |
| 3 | +import Display |
| 4 | +import AsyncDisplayKit |
| 5 | +import SwiftSignalKit |
| 6 | +import TelegramCore |
| 7 | +import AccountContext |
| 8 | +import MapResourceToAvatarSizes |
| 9 | +import TelegramPresentationData |
| 10 | +import Postbox |
| 11 | + |
| 12 | +private final class BookmarksLauncherController: ViewController { |
| 13 | + private let context: AccountContext |
| 14 | + private var didLaunch = false |
| 15 | + |
| 16 | + init(context: AccountContext) { |
| 17 | + self.context = context |
| 18 | + super.init(navigationBarPresentationData: nil) |
| 19 | + self.navigationPresentation = .modal |
| 20 | + self.statusBar.statusBarStyle = .Ignore |
| 21 | + self.title = nil |
| 22 | + } |
| 23 | + |
| 24 | + required init(coder: NSCoder) { |
| 25 | + preconditionFailure() |
| 26 | + } |
| 27 | + |
| 28 | + override func viewDidLoad() { |
| 29 | + super.viewDidLoad() |
| 30 | + self.view.backgroundColor = .clear |
| 31 | + } |
| 32 | + |
| 33 | + override public func viewDidAppear(_ animated: Bool) { |
| 34 | + super.viewDidAppear(animated) |
| 35 | + if self.didLaunch { return } |
| 36 | + self.didLaunch = true |
| 37 | + self.ensureAndOpenBookmarks() |
| 38 | + } |
| 39 | + |
| 40 | + private func ensureAndOpenBookmarks() { |
| 41 | + let context = self.context |
| 42 | + let resolve: Signal<EnginePeer?, NoError> = resolveOrCreateBookmarksPeer(context: context) |
| 43 | + |
| 44 | + let _ : Disposable = (resolve |
| 45 | + |> deliverOnMainQueue).startStandalone(next: { [weak self] peer in |
| 46 | + guard let self else { return } |
| 47 | + if let peer, let navigationController = self.navigationController as? NavigationController { |
| 48 | + // Зафиксируем отображение как Messages |
| 49 | + let _ = self.context.engine.peers.updateForumViewAsMessages(peerId: peer.id, value: true).startStandalone() |
| 50 | + self.context.sharedContext.navigateToChatController( |
| 51 | + NavigateToChatControllerParams( |
| 52 | + navigationController: navigationController, |
| 53 | + context: self.context, |
| 54 | + chatLocation: .peer(peer), |
| 55 | + keepStack: .always |
| 56 | + ) |
| 57 | + ) |
| 58 | + } |
| 59 | + // This launcher controller can be removed from the stack |
| 60 | + if let navigationController = self.navigationController as? NavigationController { |
| 61 | + var controllers = navigationController.viewControllers |
| 62 | + controllers = controllers.filter { $0 !== self } |
| 63 | + navigationController.setViewControllers(controllers, animated: false) |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// MARK: - Helpers (strongly typed to avoid inference issues) |
| 70 | + |
| 71 | +private func resolveOrCreateBookmarksPeer(context: AccountContext) -> Signal<EnginePeer?, NoError> { |
| 72 | + let presentationData: PresentationData = context.sharedContext.currentPresentationData.with { $0 } |
| 73 | + let search: Signal<[EngineRenderedPeer], NoError> = context.engine.contacts.searchLocalPeers(query: "Bookmarks") |
| 74 | + |
| 75 | + let found: Signal<EnginePeer?, NoError> = (search |
| 76 | + |> take(1) |
| 77 | + |> map { (peers: [EngineRenderedPeer]) -> EnginePeer? in |
| 78 | + for rendered in peers { |
| 79 | + if let peer = rendered.peer, case let .channel(channel) = peer, channel.isForum { |
| 80 | + if peer.displayTitle(strings: presentationData.strings, displayOrder: presentationData.nameDisplayOrder) == "Bookmarks" { |
| 81 | + return peer |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + return nil |
| 86 | + }) |
| 87 | + |
| 88 | + return found |
| 89 | + |> mapToSignal { maybePeer -> Signal<EnginePeer?, NoError> in |
| 90 | + if let peer = maybePeer { |
| 91 | + // Ensure the Bookmarks group has the bookmarks icon as its avatar |
| 92 | + if let image = PresentationResourcesSettings.bookmarks, let data = image.jpegData(compressionQuality: 0.9) { |
| 93 | + let resource = LocalFileMediaResource(fileId: Int64.random(in: Int64.min ... Int64.max)) |
| 94 | + context.account.postbox.mediaBox.storeResourceData(resource.id, data: data, synchronous: true) |
| 95 | + let _ = (context.engine.peers.updatePeerPhoto( |
| 96 | + peerId: peer.id, |
| 97 | + photo: context.engine.peers.uploadedPeerPhoto(resource: resource), |
| 98 | + mapResourceToAvatarSizes: { res, reps in |
| 99 | + return mapResourceToAvatarSizes(postbox: context.account.postbox, resource: res, representations: reps) |
| 100 | + } |
| 101 | + ) |
| 102 | + |> deliverOnMainQueue).startStandalone() |
| 103 | + } |
| 104 | + return Signal<EnginePeer?, NoError>.single(peer) |
| 105 | + } else { |
| 106 | + return createBookmarksPeer(context: context) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +private func createBookmarksPeer(context: AccountContext) -> Signal<EnginePeer?, NoError> { |
| 112 | + // 1) Create forum supergroup → PeerId?, NoError |
| 113 | + let createdId: Signal<PeerId?, NoError> = (context.engine.peers.createSupergroup( |
| 114 | + title: "Bookmarks", |
| 115 | + description: "", |
| 116 | + isForum: true, |
| 117 | + ttlPeriod: nil |
| 118 | + ) |
| 119 | + |> map(Optional.init) |
| 120 | + |> `catch` { _ -> Signal<PeerId?, NoError> in |
| 121 | + return .single(nil) |
| 122 | + }) |
| 123 | + |
| 124 | + // 2) Resolve EnginePeer from id; set avatar; return EnginePeer?, NoError |
| 125 | + return createdId |
| 126 | + |> mapToSignal { (peerIdOpt: PeerId?) -> Signal<EnginePeer?, NoError> in |
| 127 | + guard let peerId = peerIdOpt else { |
| 128 | + return .single(nil) |
| 129 | + } |
| 130 | + return (context.engine.data.get(TelegramEngine.EngineData.Item.Peer.Peer(id: peerId)) |
| 131 | + |> take(1)) |
| 132 | + |> mapToSignal { (peerOpt: EnginePeer?) -> Signal<EnginePeer?, NoError> in |
| 133 | + if let peer = peerOpt, case let .channel(channel) = peer, channel.isForum { |
| 134 | + if let image = PresentationResourcesSettings.bookmarks, let data = image.jpegData(compressionQuality: 0.9) { |
| 135 | + let resource = LocalFileMediaResource(fileId: Int64.random(in: Int64.min ... Int64.max)) |
| 136 | + context.account.postbox.mediaBox.storeResourceData(resource.id, data: data, synchronous: true) |
| 137 | + let _ = (context.engine.peers.updatePeerPhoto( |
| 138 | + peerId: peer.id, |
| 139 | + photo: context.engine.peers.uploadedPeerPhoto(resource: resource), |
| 140 | + mapResourceToAvatarSizes: { res, reps in |
| 141 | + return mapResourceToAvatarSizes(postbox: context.account.postbox, resource: res, representations: reps) |
| 142 | + } |
| 143 | + ) |
| 144 | + |> deliverOnMainQueue).startStandalone() |
| 145 | + } |
| 146 | + // View as Messages (и запретим пользователю переключать меню далее) |
| 147 | + let _ = context.engine.peers.setChannelForumMode(id: peer.id, isForum: true, displayForumAsTabs: true).startStandalone() |
| 148 | + let _ = context.engine.peers.updateForumViewAsMessages(peerId: peer.id, value: true).startStandalone() |
| 149 | + } |
| 150 | + return .single(peerOpt) |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +public func bookmarksController(context: AccountContext) -> ViewController { |
| 156 | + return BookmarksLauncherController(context: context) |
| 157 | +} |
0 commit comments