element-ios/Riot/Modules/Call/Dialpad/DialpadViewController.swift

404 lines
15 KiB
Swift
Raw Normal View History

2021-01-12 11:38:36 +00:00
// File created from simpleScreenTemplate
// $ createSimpleScreen.sh Dialpad Dialpad
/*
Copyright 2020 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import UIKit
2021-01-13 18:51:47 +00:00
import libPhoneNumber_iOS
2021-01-12 11:38:36 +00:00
2021-01-13 18:51:47 +00:00
@objc protocol DialpadViewControllerDelegate: class {
2021-01-14 15:22:46 +00:00
@objc optional func dialpadViewControllerDidTapCall(_ viewController: DialpadViewController,
withPhoneNumber phoneNumber: String)
@objc optional func dialpadViewControllerDidTapClose(_ viewController: DialpadViewController)
@objc optional func dialpadViewControllerDidTapDigit(_ viewController: DialpadViewController, digit: String)
2021-01-12 11:38:36 +00:00
}
2021-01-13 18:51:47 +00:00
@objcMembers
class DialpadViewController: UIViewController {
2021-01-12 11:38:36 +00:00
// MARK: Outlets
@IBOutlet private weak var phoneNumberTextFieldTopConstraint: NSLayoutConstraint! {
didSet {
if !configuration.showsTitle && !configuration.showsCloseButton {
phoneNumberTextFieldTopConstraint.constant = 0
}
}
}
@IBOutlet private weak var closeButton: UIButton! {
didSet {
closeButton.isHidden = !configuration.showsCloseButton
}
}
@IBOutlet private weak var titleLabel: UILabel! {
didSet {
titleLabel.isHidden = !configuration.showsTitle
}
}
2021-01-13 18:51:47 +00:00
@IBOutlet private weak var phoneNumberTextField: UITextField! {
didSet {
phoneNumberTextField.text = nil
// avoid showing keyboard on text field
phoneNumberTextField.inputView = UIView()
phoneNumberTextField.inputAccessoryView = UIView()
2021-01-14 15:20:00 +00:00
phoneNumberTextField.isUserInteractionEnabled = configuration.editingEnabled
2021-01-13 18:51:47 +00:00
}
}
@IBOutlet private weak var lineView: UIView!
@IBOutlet private weak var digitsStackView: UIStackView!
2021-02-10 13:17:38 +00:00
@IBOutlet private var digitButtons: [DialpadButton]!
2021-01-13 18:51:47 +00:00
@IBOutlet private weak var backspaceButton: DialpadActionButton! {
didSet {
backspaceButton.type = .backspace
2021-01-14 15:20:00 +00:00
backspaceButton.isHidden = !configuration.showsBackspaceButton
2021-01-13 18:51:47 +00:00
}
}
@IBOutlet private weak var callButton: DialpadActionButton! {
didSet {
callButton.type = .call
2021-01-14 15:20:00 +00:00
callButton.isHidden = !configuration.showsCallButton
2021-01-13 18:51:47 +00:00
}
}
@IBOutlet private weak var spaceButton: UIButton! {
didSet {
spaceButton.isHidden = !configuration.showsBackspaceButton || !configuration.showsCallButton
}
}
2021-01-12 11:38:36 +00:00
// MARK: Private
2021-01-13 18:51:47 +00:00
private enum Constants {
static let sizeOniPad: CGSize = CGSize(width: 375, height: 667)
2021-01-14 09:13:25 +00:00
static let additionalTopInset: CGFloat = 20
2021-02-10 13:17:38 +00:00
static let digitButtonViewDatas: [Int: DialpadButton.ViewData] = [
2021-02-15 12:58:19 +00:00
-2: .init(title: "#", tone: 1211),
-1: .init(title: "*", tone: 1210),
0: .init(title: "0", tone: 1200, subtitle: "+"),
1: .init(title: "1", tone: 1201, showsSubtitleSpace: true),
2: .init(title: "2", tone: 1202, subtitle: "ABC"),
3: .init(title: "3", tone: 1203, subtitle: "DEF"),
4: .init(title: "4", tone: 1204, subtitle: "GHI"),
5: .init(title: "5", tone: 1205, subtitle: "JKL"),
6: .init(title: "6", tone: 1206, subtitle: "MNO"),
7: .init(title: "7", tone: 1207, subtitle: "PQRS"),
8: .init(title: "8", tone: 1208, subtitle: "TUV"),
9: .init(title: "9", tone: 1209, subtitle: "WXYZ")
2021-02-10 13:17:38 +00:00
]
2021-01-13 18:51:47 +00:00
}
private var wasCursorAtTheEnd: Bool = true
/// Phone number as formatted
private var phoneNumber: String = "" {
willSet {
2021-01-14 15:20:00 +00:00
if configuration.editingEnabled {
wasCursorAtTheEnd = isCursorAtTheEnd()
}
2021-01-13 18:51:47 +00:00
} didSet {
phoneNumberTextField.text = phoneNumber
2021-01-14 15:20:00 +00:00
if configuration.editingEnabled && wasCursorAtTheEnd {
2021-01-13 18:51:47 +00:00
moveCursorToTheEnd()
}
}
}
/// Phone number as non-formatted
var rawPhoneNumber: String {
2021-01-13 18:51:47 +00:00
return phoneNumber.vc_removingAllWhitespaces()
}
2021-01-12 11:38:36 +00:00
private var theme: Theme!
2021-01-14 15:20:00 +00:00
private var configuration: DialpadConfiguration!
2021-01-12 11:38:36 +00:00
// MARK: Public
weak var delegate: DialpadViewControllerDelegate?
// MARK: - Setup
2021-01-14 15:20:00 +00:00
class func instantiate(withConfiguration configuration: DialpadConfiguration = .default) -> DialpadViewController {
2021-01-12 11:38:36 +00:00
let viewController = StoryboardScene.DialpadViewController.initialScene.instantiate()
viewController.theme = ThemeService.shared().theme
2021-01-14 15:20:00 +00:00
viewController.configuration = configuration
2021-01-12 11:38:36 +00:00
return viewController
}
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
2021-01-13 18:51:47 +00:00
titleLabel.text = VectorL10n.dialpadTitle
2021-01-12 11:38:36 +00:00
self.registerThemeServiceDidChangeThemeNotification()
self.update(theme: self.theme)
// force orientation to portrait if phone
if UIDevice.current.isPhone {
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
}
2021-02-10 13:17:38 +00:00
for button in digitButtons {
if let viewData = Constants.digitButtonViewDatas[button.tag] {
button.render(withViewData: viewData)
}
}
2021-01-12 11:38:36 +00:00
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return self.theme.statusBarStyle
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
// limit orientation to portrait only for phone
if UIDevice.current.isPhone {
return .portrait
}
return super.supportedInterfaceOrientations
}
override var shouldAutorotate: Bool {
return false
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
if UIDevice.current.isPhone {
return .portrait
}
return super.preferredInterfaceOrientationForPresentation
}
2021-01-12 11:38:36 +00:00
// MARK: - Private
2021-01-13 18:51:47 +00:00
private func isCursorAtTheEnd() -> Bool {
guard let selectedRange = phoneNumberTextField.selectedTextRange else {
return true
2021-01-12 11:38:36 +00:00
}
2021-01-13 18:51:47 +00:00
if !selectedRange.isEmpty {
return false
}
let cursorEndPos = phoneNumberTextField.offset(from: phoneNumberTextField.beginningOfDocument, to: selectedRange.end)
return cursorEndPos == phoneNumber.count
}
private func moveCursorToTheEnd() {
guard let cursorPos = phoneNumberTextField.position(from: phoneNumberTextField.beginningOfDocument,
offset: phoneNumber.count) else { return }
phoneNumberTextField.selectedTextRange = phoneNumberTextField.textRange(from: cursorPos,
to: cursorPos)
}
private func reformatPhoneNumber() {
2021-01-14 15:20:00 +00:00
guard configuration.formattingEnabled, let phoneNumberUtil = NBPhoneNumberUtil.sharedInstance() else {
2021-01-13 18:51:47 +00:00
// no formatter
return
}
do {
// try formatting the number
if phoneNumber.hasPrefix("00") {
let range = phoneNumber.startIndex..<phoneNumber.index(phoneNumber.startIndex, offsetBy: 2)
phoneNumber.replaceSubrange(range, with: "+")
}
let nbPhoneNumber = try phoneNumberUtil.parse(rawPhoneNumber, defaultRegion: nil)
phoneNumber = try phoneNumberUtil.format(nbPhoneNumber, numberFormat: .INTERNATIONAL)
} catch {
// continue without formatting
}
2021-01-12 11:38:36 +00:00
}
private func update(theme: Theme) {
self.theme = theme
2021-01-13 18:51:47 +00:00
self.view.backgroundColor = theme.backgroundColor
2021-01-12 11:38:36 +00:00
if let navigationBar = self.navigationController?.navigationBar {
theme.applyStyle(onNavigationBar: navigationBar)
}
if theme.identifier == ThemeIdentifier.light.rawValue {
titleLabel.textColor = theme.noticeSecondaryColor
closeButton.setBackgroundImage(Asset.Images.closeButton.image.vc_tintedImage(usingColor: theme.tabBarUnselectedItemTintColor), for: .normal)
} else {
titleLabel.textColor = theme.baseTextSecondaryColor
closeButton.setBackgroundImage(Asset.Images.closeButton.image.vc_tintedImage(usingColor: theme.baseTextSecondaryColor), for: .normal)
}
2021-01-13 18:51:47 +00:00
phoneNumberTextField.textColor = theme.textPrimaryColor
lineView.backgroundColor = theme.lineBreakColor
2021-01-12 11:38:36 +00:00
2021-01-13 18:51:47 +00:00
updateThemesOfAllButtons(in: digitsStackView, with: theme)
}
private func updateThemesOfAllButtons(in view: UIView, with theme: Theme) {
if let button = view as? DialpadButton {
button.update(theme: theme)
} else {
for subview in view.subviews {
updateThemesOfAllButtons(in: subview, with: theme)
2021-01-12 11:38:36 +00:00
}
2021-01-13 18:51:47 +00:00
}
}
2021-01-12 11:38:36 +00:00
private func registerThemeServiceDidChangeThemeNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .themeServiceDidChangeTheme, object: nil)
}
2021-01-14 09:13:25 +00:00
private func topSafeAreaInset() -> CGFloat {
guard let window = UIApplication.shared.keyWindow else {
return Constants.additionalTopInset
}
return window.safeAreaInsets.top + Constants.additionalTopInset
}
2021-01-12 11:38:36 +00:00
// MARK: - Actions
@objc private func themeDidChange() {
self.update(theme: ThemeService.shared().theme)
}
2021-01-13 18:51:47 +00:00
@IBAction private func closeButtonAction(_ sender: UIButton) {
delegate?.dialpadViewControllerDidTapClose?(self)
2021-01-13 18:51:47 +00:00
}
@IBAction private func digitButtonAction(_ sender: DialpadButton) {
2021-02-15 12:58:19 +00:00
guard let digitViewData = Constants.digitButtonViewDatas[sender.tag] else {
return
}
let digit = digitViewData.title
2021-01-13 18:51:47 +00:00
defer {
delegate?.dialpadViewControllerDidTapDigit?(self, digit: digit)
}
2021-02-15 12:58:19 +00:00
if configuration.playTones {
AudioServicesPlaySystemSound(digitViewData.tone)
}
2021-01-14 15:20:00 +00:00
if !configuration.editingEnabled {
phoneNumber += digit
return
}
2021-01-13 18:51:47 +00:00
if let selectedRange = phoneNumberTextField.selectedTextRange {
if isCursorAtTheEnd() {
phoneNumber += digit
reformatPhoneNumber()
return
}
let cursorStartPos = phoneNumberTextField.offset(from: phoneNumberTextField.beginningOfDocument, to: selectedRange.start)
let cursorEndPos = phoneNumberTextField.offset(from: phoneNumberTextField.beginningOfDocument, to: selectedRange.end)
phoneNumber.replaceSubrange((phoneNumber.index(phoneNumber.startIndex, offsetBy: cursorStartPos))..<(phoneNumber.index(phoneNumber.startIndex, offsetBy: cursorEndPos)), with: digit)
guard let cursorPos = phoneNumberTextField.position(from: phoneNumberTextField.beginningOfDocument,
offset: cursorEndPos + digit.count) else { return }
reformatPhoneNumber()
phoneNumberTextField.selectedTextRange = phoneNumberTextField.textRange(from: cursorPos,
to: cursorPos)
} else {
phoneNumber += digit
reformatPhoneNumber()
}
}
@IBAction private func backspaceButtonAction(_ sender: DialpadActionButton) {
defer {
delegate?.dialpadViewControllerDidTapDigit?(self, digit: "")
}
2021-01-13 18:51:47 +00:00
if phoneNumber.isEmpty {
return
}
2021-01-14 15:20:00 +00:00
if !configuration.editingEnabled {
phoneNumber.removeLast()
return
}
2021-01-13 18:51:47 +00:00
if let selectedRange = phoneNumberTextField.selectedTextRange {
let cursorStartPos = phoneNumberTextField.offset(from: phoneNumberTextField.beginningOfDocument, to: selectedRange.start)
let cursorEndPos = phoneNumberTextField.offset(from: phoneNumberTextField.beginningOfDocument, to: selectedRange.end)
let rangePos: UITextPosition!
if selectedRange.isEmpty {
// just caret, remove one char from the cursor position
if cursorStartPos == 0 {
// already at the beginning of the text, no more text to remove here
return
}
phoneNumber.replaceSubrange((phoneNumber.index(phoneNumber.startIndex, offsetBy: cursorStartPos-1))..<(phoneNumber.index(phoneNumber.startIndex, offsetBy: cursorEndPos)), with: "")
rangePos = phoneNumberTextField.position(from: phoneNumberTextField.beginningOfDocument,
offset: cursorStartPos-1)
} else {
// really some text selected, remove selected range of text
phoneNumber.replaceSubrange((phoneNumber.index(phoneNumber.startIndex, offsetBy: cursorStartPos))..<(phoneNumber.index(phoneNumber.startIndex, offsetBy: cursorEndPos)), with: "")
rangePos = phoneNumberTextField.position(from: phoneNumberTextField.beginningOfDocument,
offset: cursorStartPos)
}
reformatPhoneNumber()
guard let cursorPos = rangePos else { return }
phoneNumberTextField.selectedTextRange = phoneNumberTextField.textRange(from: cursorPos,
to: cursorPos)
} else {
phoneNumber.removeLast()
reformatPhoneNumber()
}
2021-01-12 11:38:36 +00:00
}
2021-01-13 18:51:47 +00:00
@IBAction private func callButtonAction(_ sender: DialpadActionButton) {
2021-01-20 22:30:47 +00:00
phoneNumber = phoneNumberTextField.text ?? ""
2021-01-14 15:22:46 +00:00
delegate?.dialpadViewControllerDidTapCall?(self, withPhoneNumber: rawPhoneNumber)
2021-01-13 18:51:47 +00:00
}
}
2021-01-12 11:38:36 +00:00
2021-01-13 18:51:47 +00:00
// MARK: - CustomSizedPresentable
extension DialpadViewController: CustomSizedPresentable {
func customSize(withParentContainerSize containerSize: CGSize) -> CGSize {
2021-01-14 09:13:25 +00:00
if UIDevice.current.isPhone {
return CGSize(width: containerSize.width, height: containerSize.height - topSafeAreaInset())
}
2021-01-13 18:51:47 +00:00
return Constants.sizeOniPad
2021-01-12 11:38:36 +00:00
}
2021-01-13 18:51:47 +00:00
2021-01-14 09:13:25 +00:00
func position(withParentContainerSize containerSize: CGSize) -> CGPoint {
let mySize = customSize(withParentContainerSize: containerSize)
if UIDevice.current.isPhone {
return CGPoint(x: 0, y: topSafeAreaInset())
}
return CGPoint(x: (containerSize.width - mySize.width)/2,
y: (containerSize.height - mySize.height)/2)
}
2021-01-12 11:38:36 +00:00
}