Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
signalapp
GitHub Repository: signalapp/Signal-iOS
Path: blob/main/SignalUI/Stories/NewPrivateStoryRecipientsViewController.swift
1 views
//
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//

import Foundation
public import SignalServiceKit

public class NewPrivateStoryRecipientsViewController: BaseMemberViewController {
    var recipientSet: OrderedSet<PickedRecipient> = []

    override public var hasUnsavedChanges: Bool { !recipientSet.isEmpty }

    let selectItemsInParent: (([StoryConversationItem]) -> Void)?

    public init(selectItemsInParent: (([StoryConversationItem]) -> Void)? = nil) {
        self.selectItemsInParent = selectItemsInParent
        super.init()

        memberViewDelegate = self
    }

    // MARK: - View Lifecycle

    override public func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        updateBarButtons()
    }

    private func updateBarButtons() {
        navigationItem.leftBarButtonItem = .cancelButton { [weak self] in
            self?.dismissPressed()
        }

        navigationItem.rightBarButtonItem = .button(
            title: CommonStrings.nextButton,
            style: .plain,
            action: { [weak self] in
                self?.nextPressed()
            },
        )
        navigationItem.rightBarButtonItem?.isEnabled = hasUnsavedChanges

        if recipientSet.isEmpty {
            title = OWSLocalizedString(
                "NEW_PRIVATE_STORY_VIEW_TITLE",
                comment: "The title for the 'new private story' view.",
            )

        } else {
            let format = OWSLocalizedString(
                "NEW_PRIVATE_STORY_VIEW_TITLE_%d",
                tableName: "PluralAware",
                comment: "The title for the 'new private story' view if already some connections are selected. Embeds {{number}} of connections.",
            )
            title = String.localizedStringWithFormat(format, recipientSet.count)
        }
    }

    // MARK: - Actions

    private func nextPressed() {
        AssertIsOnMainThread()

        let vc = NewPrivateStoryConfirmViewController(
            recipientSet: recipientSet,
            selectItemsInParent: selectItemsInParent,
        )
        navigationController?.pushViewController(vc, animated: true)
    }
}

// MARK: -

extension NewPrivateStoryRecipientsViewController: MemberViewDelegate {
    public var memberViewRecipientSet: OrderedSet<PickedRecipient> { recipientSet }

    public var memberViewHasUnsavedChanges: Bool { hasUnsavedChanges }

    public func memberViewRemoveRecipient(_ recipient: PickedRecipient) {
        recipientSet.remove(recipient)
        updateBarButtons()
    }

    public func memberViewAddRecipient(_ recipient: PickedRecipient) -> Bool {
        recipientSet.append(recipient)
        updateBarButtons()
        return true
    }

    public func memberViewShouldShowMemberCount() -> Bool { false }

    public func memberViewShouldAllowBlockedSelection() -> Bool { false }

    public func memberViewMemberCountForDisplay() -> Int { recipientSet.count }

    public func memberViewIsPreExistingMember(_ recipient: PickedRecipient, transaction: DBReadTransaction) -> Bool { false }

    public func memberViewCustomIconNameForPickedMember(_ recipient: PickedRecipient) -> String? { nil }

    public func memberViewCustomIconColorForPickedMember(_ recipient: PickedRecipient) -> UIColor? { nil }

    public func memberViewDismiss() {
        dismiss(animated: true)
    }
}