session-ios/Session/Conversations/Views & Modals/BlockedModal.swift

94 lines
4.0 KiB
Swift
Raw Normal View History

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import UIKit
import GRDB
import SessionUIKit
import SessionUtilitiesKit
import SessionMessagingKit
2021-02-12 01:56:46 +01:00
final class BlockedModal: Modal {
2021-02-12 01:56:46 +01:00
private let publicKey: String
// MARK: Lifecycle
init(publicKey: String) {
self.publicKey = publicKey
super.init(nibName: nil, bundle: nil)
}
override init(nibName: String?, bundle: Bundle?) {
preconditionFailure("Use init(publicKey:) instead.")
}
required init?(coder: NSCoder) {
preconditionFailure("Use init(publicKey:) instead.")
}
override func populateContentView() {
// Name
let name = Profile.displayName(id: publicKey)
2021-02-12 01:56:46 +01:00
// Title
let titleLabel = UILabel()
titleLabel.textColor = Colors.text
2022-03-04 05:23:02 +01:00
titleLabel.font = .boldSystemFont(ofSize: Values.mediumFontSize)
2021-05-07 03:05:16 +02:00
titleLabel.text = String(format: NSLocalizedString("modal_blocked_title", comment: ""), name)
2021-02-12 01:56:46 +01:00
titleLabel.textAlignment = .center
// Message
let messageLabel = UILabel()
messageLabel.textColor = Colors.text
messageLabel.font = .systemFont(ofSize: Values.smallFontSize)
2021-05-07 03:05:16 +02:00
let message = String(format: NSLocalizedString("modal_blocked_explanation", comment: ""), name)
2021-02-12 01:56:46 +01:00
let attributedMessage = NSMutableAttributedString(string: message)
attributedMessage.addAttributes([ .font : UIFont.boldSystemFont(ofSize: Values.smallFontSize) ], range: (message as NSString).range(of: name))
messageLabel.attributedText = attributedMessage
messageLabel.numberOfLines = 0
messageLabel.lineBreakMode = .byWordWrapping
messageLabel.textAlignment = .center
// Unblock button
let unblockButton = UIButton()
unblockButton.set(.height, to: Values.mediumButtonHeight)
2021-02-22 05:10:01 +01:00
unblockButton.layer.cornerRadius = Modal.buttonCornerRadius
2021-02-12 01:56:46 +01:00
unblockButton.backgroundColor = Colors.buttonBackground
unblockButton.titleLabel!.font = .systemFont(ofSize: Values.smallFontSize)
unblockButton.setTitleColor(Colors.text, for: UIControl.State.normal)
2021-05-07 03:05:16 +02:00
unblockButton.setTitle(NSLocalizedString("modal_blocked_button_title", comment: ""), for: UIControl.State.normal)
2021-02-12 01:56:46 +01:00
unblockButton.addTarget(self, action: #selector(unblock), for: UIControl.Event.touchUpInside)
// Button stack view
let buttonStackView = UIStackView(arrangedSubviews: [ cancelButton, unblockButton ])
buttonStackView.axis = .horizontal
buttonStackView.spacing = Values.mediumSpacing
buttonStackView.distribution = .fillEqually
2022-03-04 05:23:02 +01:00
// Content stack view
let contentStackView = UIStackView(arrangedSubviews: [ titleLabel, messageLabel ])
contentStackView.axis = .vertical
contentStackView.spacing = Values.largeSpacing
2021-02-12 01:56:46 +01:00
// Main stack view
2022-03-04 05:23:02 +01:00
let spacing = Values.largeSpacing - Values.smallFontSize / 2
let mainStackView = UIStackView(arrangedSubviews: [ contentStackView, buttonStackView ])
2021-02-12 01:56:46 +01:00
mainStackView.axis = .vertical
2022-03-04 05:23:02 +01:00
mainStackView.spacing = spacing
2021-02-12 01:56:46 +01:00
contentView.addSubview(mainStackView)
mainStackView.pin(.leading, to: .leading, of: contentView, withInset: Values.largeSpacing)
mainStackView.pin(.top, to: .top, of: contentView, withInset: Values.largeSpacing)
contentView.pin(.trailing, to: .trailing, of: mainStackView, withInset: Values.largeSpacing)
2022-03-04 05:23:02 +01:00
contentView.pin(.bottom, to: .bottom, of: mainStackView, withInset: spacing)
2021-02-12 01:56:46 +01:00
}
// MARK: - Interaction
2021-02-12 01:56:46 +01:00
@objc private func unblock() {
let publicKey: String = self.publicKey
Storage.shared.writeAsync { db in
try Contact
.filter(id: publicKey)
.updateAll(db, Contact.Columns.isBlocked.set(to: false))
try MessageSender
.syncConfiguration(db, forceSyncNow: true)
.retainUntilComplete()
}
2021-02-12 01:56:46 +01:00
presentingViewController?.dismiss(animated: true, completion: nil)
}
}