Signal-iOS/SignalUI/ViewControllers/CountryCodeViewController.swift
Evan Hahn 370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00

149 lines
4.0 KiB
Swift

//
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
import SignalCoreKit
@objc
public protocol CountryCodeViewControllerDelegate: AnyObject {
func countryCodeViewController(_ vc: CountryCodeViewController,
didSelectCountry: RegistrationCountryState)
}
// MARK: -
@objc
public class CountryCodeViewController: OWSTableViewController2 {
@objc
public weak var countryCodeDelegate: CountryCodeViewControllerDelegate?
@objc
public var interfaceOrientationMask: UIInterfaceOrientationMask = UIDevice.current.defaultSupportedOrientations
public override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
interfaceOrientationMask
}
private let searchBar = OWSSearchBar()
// MARK: -
override public func viewDidLoad() {
// Configure searchBar() before super.viewDidLoad().
searchBar.delegate = self
searchBar.placeholder = OWSLocalizedString("SEARCH_BYNAMEORNUMBER_PLACEHOLDER_TEXT", comment: "")
searchBar.sizeToFit()
let searchBarWrapper = UIStackView()
searchBarWrapper.axis = .vertical
searchBarWrapper.alignment = .fill
searchBarWrapper.addArrangedSubview(searchBar)
self.topHeader = searchBarWrapper
super.viewDidLoad()
self.delegate = self
self.title = OWSLocalizedString("COUNTRYCODE_SELECT_TITLE", comment: "")
self.navigationItem.leftBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .stop,
target: self,
action: #selector(didPressCancel),
accessibilityIdentifier: "cancel")
createViews()
}
private func createViews() {
AssertIsOnMainThread()
updateTableContents()
}
private func updateTableContents() {
AssertIsOnMainThread()
let countryStates = RegistrationCountryState.buildCountryStates(searchText: searchBar.text)
let contents = OWSTableContents()
let section = OWSTableSection()
for countryState in countryStates {
let accessibilityIdentifier = "country.\(countryState.countryCode)"
section.add(OWSTableItem.item(name: countryState.countryName,
accessoryText: countryState.callingCode,
accessibilityIdentifier: accessibilityIdentifier) { [weak self] in
self?.countryWasSelected(countryState: countryState)
})
}
contents.addSection(section)
self.contents = contents
}
private func countryWasSelected(countryState: RegistrationCountryState) {
AssertIsOnMainThread()
countryCodeDelegate?.countryCodeViewController(self, didSelectCountry: countryState)
searchBar.resignFirstResponder()
self.dismiss(animated: true)
}
@objc
private func didPressCancel() {
self.dismiss(animated: true)
}
}
// MARK: -
extension CountryCodeViewController: UISearchBarDelegate {
public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
AssertIsOnMainThread()
searchTextDidChange()
}
public func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
AssertIsOnMainThread()
searchTextDidChange()
}
public func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
AssertIsOnMainThread()
searchTextDidChange()
}
public func searchBarResultsListButtonClicked(_ searchBar: UISearchBar) {
AssertIsOnMainThread()
searchTextDidChange()
}
public func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
AssertIsOnMainThread()
searchTextDidChange()
}
private func searchTextDidChange() {
updateTableContents()
}
}
// MARK: -
extension CountryCodeViewController: OWSTableViewControllerDelegate {
public func tableViewWillBeginDragging() {
searchBar.resignFirstResponder()
}
}