mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
152 lines
5.3 KiB
Swift
152 lines
5.3 KiB
Swift
import Foundation
|
|
import Display
|
|
import SwiftSignalKit
|
|
import Postbox
|
|
import TelegramCore
|
|
|
|
private final class ConvertToSupergroupArguments {
|
|
let convert: () -> Void
|
|
|
|
init(convert: @escaping () -> Void) {
|
|
self.convert = convert
|
|
}
|
|
}
|
|
|
|
private enum ConvertToSupergroupSection: Int32 {
|
|
case info
|
|
case action
|
|
}
|
|
|
|
private enum ConvertToSupergroupEntry: ItemListNodeEntry {
|
|
case info
|
|
case action
|
|
case actionInfo
|
|
|
|
var section: ItemListSectionId {
|
|
switch self {
|
|
case .info:
|
|
return ConvertToSupergroupSection.info.rawValue
|
|
case .action, .actionInfo:
|
|
return ConvertToSupergroupSection.action.rawValue
|
|
}
|
|
}
|
|
|
|
var stableId: Int32 {
|
|
switch self {
|
|
case .info:
|
|
return 0
|
|
case .action:
|
|
return 1
|
|
case .actionInfo:
|
|
return 2
|
|
}
|
|
}
|
|
|
|
static func ==(lhs: ConvertToSupergroupEntry, rhs: ConvertToSupergroupEntry) -> Bool {
|
|
return lhs.stableId == rhs.stableId
|
|
}
|
|
|
|
static func <(lhs: ConvertToSupergroupEntry, rhs: ConvertToSupergroupEntry) -> Bool {
|
|
return lhs.stableId < rhs.stableId
|
|
}
|
|
|
|
func item(_ arguments: ConvertToSupergroupArguments) -> ListViewItem {
|
|
switch self {
|
|
case .info:
|
|
return ItemListTextItem(text: .plain("In supergroups:\n• New members can see the full message history\n• Deleted messages will disappear for all members\n• Admins can pin important messages\n• Creator can set a public link for the group"), sectionId: self.section)
|
|
case .action:
|
|
return ItemListActionItem(title: "Convert to Supergroup", kind: .generic, alignment: .natural, sectionId: self.section, style: .blocks, action: {
|
|
arguments.convert()
|
|
})
|
|
case .actionInfo:
|
|
return ItemListTextItem(text: .plain("Note: this action can't be undone"), sectionId: self.section)
|
|
}
|
|
}
|
|
}
|
|
|
|
private struct ConvertToSupergroupState: Equatable {
|
|
let isConverting: Bool
|
|
|
|
init() {
|
|
self.isConverting = false
|
|
}
|
|
|
|
init(isConverting: Bool) {
|
|
self.isConverting = isConverting
|
|
}
|
|
|
|
static func ==(lhs: ConvertToSupergroupState, rhs: ConvertToSupergroupState) -> Bool {
|
|
if lhs.isConverting != rhs.isConverting {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
|
|
private func convertToSupergroupEntries() -> [ConvertToSupergroupEntry] {
|
|
var entries: [ConvertToSupergroupEntry] = []
|
|
|
|
entries.append(.info)
|
|
entries.append(.action)
|
|
entries.append(.actionInfo)
|
|
|
|
return entries
|
|
}
|
|
|
|
public func convertToSupergroupController(account: Account, peerId: PeerId) -> ViewController {
|
|
var replaceControllerImpl: ((ViewController) -> Void)?
|
|
|
|
let statePromise = ValuePromise(ConvertToSupergroupState(), ignoreRepeated: true)
|
|
let stateValue = Atomic(value: ConvertToSupergroupState())
|
|
let updateState: ((ConvertToSupergroupState) -> ConvertToSupergroupState) -> Void = { f in
|
|
statePromise.set(stateValue.modify { f($0) })
|
|
}
|
|
|
|
let actionsDisposable = DisposableSet()
|
|
|
|
let convertDisposable = MetaDisposable()
|
|
actionsDisposable.add(convertDisposable)
|
|
|
|
let arguments = ConvertToSupergroupArguments(convert: {
|
|
var alreadyConverting = false
|
|
updateState { state in
|
|
if state.isConverting {
|
|
alreadyConverting = true
|
|
}
|
|
return ConvertToSupergroupState(isConverting: true)
|
|
}
|
|
|
|
if !alreadyConverting {
|
|
convertDisposable.set((convertGroupToSupergroup(account: account, peerId: peerId) |> deliverOnMainQueue).start(next: { createdPeerId in
|
|
replaceControllerImpl?(ChatController(account: account, peerId: createdPeerId))
|
|
}))
|
|
}
|
|
})
|
|
|
|
let signal = combineLatest((account.applicationContext as! TelegramApplicationContext).presentationData, statePromise.get())
|
|
|> deliverOnMainQueue
|
|
|> map { presentationData, state -> (ItemListControllerState, (ItemListNodeState<ConvertToSupergroupEntry>, ConvertToSupergroupEntry.ItemGenerationArguments)) in
|
|
|
|
var rightNavigationButton: ItemListNavigationButton?
|
|
if state.isConverting {
|
|
rightNavigationButton = ItemListNavigationButton(title: "", style: .activity, enabled: true, action: {})
|
|
}
|
|
|
|
let controllerState = ItemListControllerState(theme: presentationData.theme, title: .text("Supergroup"), leftNavigationButton: nil, rightNavigationButton: rightNavigationButton, backNavigationButton: ItemListBackButton(title: "Back"))
|
|
let listState = ItemListNodeState(entries: convertToSupergroupEntries(), style: .blocks)
|
|
|
|
return (controllerState, (listState, arguments))
|
|
}
|
|
|> afterDisposed {
|
|
actionsDisposable.dispose()
|
|
}
|
|
|
|
let controller = ItemListController(account: account, state: signal)
|
|
replaceControllerImpl = { [weak controller] c in
|
|
if let controller = controller {
|
|
(controller.navigationController as? NavigationController)?.replaceAllButRootController(c, animated: true)
|
|
}
|
|
}
|
|
return controller
|
|
}
|