<commit_before>//
//  LinkWalletViewController.swift
//  breadwallet
//
//  Created by <NAME> on 2018-07-23.
//  Copyright © 2018 breadwallet LLC. All rights reserved.
//

import UIKit

private let circleRadius: CGFloat = 40.0

class LinkWalletViewController : UIViewController {

    private let body = UIStackView()
    private let footer = UIStackView()
    private let buttonStack = UIStackView()
    private let footerBackground = UIView(color: .darkerBackground)
    private let bodyBackground = UIView(color: .darkerBackground)
    private let approve = BRDButton(title: S.LinkWallet.approve, type: .primary)
    private let decline = BRDButton(title: S.LinkWallet.decline, type: .secondary)
    private let logo = UIImageView(image: #imageLiteral(resourceName: "LogoCutout").withRenderingMode(.alwaysTemplate))
    private let logoBackground = MotionGradientView()
    private let logoFooter = UILabel(font: .customBold(size: 20.0), color: UIColor.fromHex("FAA43A"))
    private let titleLabel = UILabel(font: .customBody(size: 14.0), color: .white)
    private let note1 = UILabel.wrapping(font: .customBody(size: 14.0), color: .newWhite)
    private let note2 = UILabel(font: .customBold(size: 18.0), color: .white)
    private let note3 = UILabel.wrapping(font: .customBody(size: 14.0), color: .newWhite)
    private let info1 = UILabel(font: .customBold(size: 14.0), color: .white)
    private let info2 = UILabel.wrapping(font: .customBody(size: 14.0), color: .white)
    private let scrollView = UIScrollView()
    private let header = UIStackView()
    private var animator: UIViewPropertyAnimator? = nil
    private let statusView = UIView()
    private let errorMessage = UILabel.wrapping(font: .customBody(size: 15.0), color: .white)
    private let circle = LinkStatusCircle(colour: .white)
    
    private let pairingRequest: WalletPairingRequest
    private let serviceDefinition: ServiceDefinition

    init(pairingRequest: WalletPairingRequest, serviceDefinition: ServiceDefinition) {
        self.pairingRequest = pairingRequest
        self.serviceDefinition = serviceDefinition
        super.init(nibName: nil, bundle: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        addSubviews()
        addConstraints()
        setInitialData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if pairingRequest.returnToURL != nil {
            Store.trigger(name: .registerForPushNotificationToken)
        }
    }

    private func addSubviews() {
        view.addSubview(header)
        view.addSubview(scrollView)
        scrollView.addSubview(body)
        header.addArrangedSubview(titleLabel)
        header.addArrangedSubview(logoBackground)
        header.addArrangedSubview(logoFooter)
        header.addArrangedSubview(note1)
        header.addArrangedSubview(info1)
        body.addSubview(bodyBackground)
        body.addArrangedSubview(note2)
        body.addArrangedSubview(info2)
        logoBackground.addSubview(logo)
        view.addSubview(footer)
        footer.addSubview(footerBackground)
        footer.addArrangedSubview(note3)
        footer.addArrangedSubview(buttonStack)
        buttonStack.addArrangedSubview(decline)
        buttonStack.addArrangedSubview(approve)
    }

    private func addConstraints() {
        header.constrain([
            header.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            header.topAnchor.constraint(equalTo: safeTopAnchor),
            header.trailingAnchor.constraint(equalTo: view.trailingAnchor)])
        scrollView.alwaysBounceVertical = true
        scrollView.constrain([
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            scrollView.topAnchor.constraint(equalTo: header.bottomAnchor),
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            scrollView.bottomAnchor.constraint(equalTo: footer.topAnchor)])
        body.constrain([
            body.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
            body.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
            body.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
            body.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 30.0),
            body.widthAnchor.constraint(equalTo: scrollView.widthAnchor),])
        logoBackground.constrain([
            logoBackground.heightAnchor.constraint(equalTo: logoBackground.widthAnchor, multiplier: logo.image!.size.height/logo.image!.size.width),
            logoBackground.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.36)])
        logo.constrain(toSuperviewEdges: nil)
        footer.constrain([
            footer.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            footer.bottomAnchor.constraint(equalTo: safeBottomAnchor),
            footer.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            footer.heightAnchor.constraint(equalToConstant: 140.0)])
        footerBackground.constrain(toSuperviewEdges: nil)
        bodyBackground.constrain(toSuperviewEdges: UIEdgeInsetsMake(0, C.padding[2], 0, -C.padding[2]))
    }

    private func setInitialData() {
        view.backgroundColor = .darkBackground
        logo.tintColor = .darkBackground
        scrollView.delegate = self
        scrollView.showsVerticalScrollIndicator = false
        setupStackViews()
        setButtonActions()
        setupLabels()
    }

    private func setupLabels() {
        titleLabel.text = S.LinkWallet.title.uppercased()
        note1.text = S.LinkWallet.domainTitle
        info1.text = serviceDefinition.domains.joined(separator: ",")
        note2.text = S.LinkWallet.permissionsTitle
        
        let capabilitiesList: [String] = serviceDefinition.capabilities.filter { !$0.description.isEmpty }.map {
            let scopes = $0.scopes.map { $0.description }.joined(separator: ", ")
            let format = Bundle.main.localizedString(forKey: $0.description, value: $0.description, table: nil)
            return "• " + String(format: format, scopes)
        }
        
        info2.text = capabilitiesList.joined(separator: "\n")
        note3.text = S.LinkWallet.disclaimer
        note1.textAlignment = .center<commit_msg>[IOS-889] Fixes truncated domain list in wallet linking screen.<commit_after>//
//  LinkWalletViewController.swift
//  breadwallet
//
//  Created by <NAME> on 2018-07-23.
//  Copyright © 2018 breadwallet LLC. All rights reserved.
//

import UIKit

private let circleRadius: CGFloat = 40.0

class LinkWalletViewController : UIViewController {

    private let body = UIStackView()
    private let footer = UIStackView()
    private let buttonStack = UIStackView()
    private let footerBackground = UIView(color: .darkerBackground)
    private let bodyBackground = UIView(color: .darkerBackground)
    private let approve = BRDButton(title: S.LinkWallet.approve, type: .primary)
    private let decline = BRDButton(title: S.LinkWallet.decline, type: .secondary)
    private let logo = UIImageView(image: #imageLiteral(resourceName: "LogoCutout").withRenderingMode(.alwaysTemplate))
    private let logoBackground = MotionGradientView()
    private let logoFooter = UILabel(font: .customBold(size: 20.0), color: UIColor.fromHex("FAA43A"))
    private let titleLabel = UILabel(font: .customBody(size: 14.0), color: .white)
    private let note1 = UILabel.wrapping(font: .customBody(size: 14.0), color: .newWhite)
    private let note2 = UILabel(font: .customBold(size: 18.0), color: .white)
    private let note3 = UILabel.wrapping(font: .customBody(size: 14.0), color: .newWhite)
    private let info1 = UILabel(font: .customBold(size: 14.0), color: .white)
    private let info2 = UILabel.wrapping(font: .customBody(size: 14.0), color: .white)
    private let scrollView = UIScrollView()
    private let header = UIStackView()
    private var animator: UIViewPropertyAnimator? = nil
    private let statusView = UIView()
    private let errorMessage = UILabel.wrapping(font: .customBody(size: 15.0), color: .white)
    private let circle = LinkStatusCircle(colour: .white)
    
    private let pairingRequest: WalletPairingRequest
    private let serviceDefinition: ServiceDefinition

    init(pairingRequest: WalletPairingRequest, serviceDefinition: ServiceDefinition) {
        self.pairingRequest = pairingRequest
        self.serviceDefinition = serviceDefinition
        super.init(nibName: nil, bundle: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        addSubviews()
        addConstraints()
        setInitialData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if pairingRequest.returnToURL != nil {
            Store.trigger(name: .registerForPushNotificationToken)
        }
    }

    private func addSubviews() {
        view.addSubview(header)
        view.addSubview(scrollView)
        scrollView.addSubview(body)
        header.addArrangedSubview(titleLabel)
        header.addArrangedSubview(logoBackground)
        header.addArrangedSubview(logoFooter)
        header.addArrangedSubview(note1)
        header.addArrangedSubview(info1)
        body.addSubview(bodyBackground)
        body.addArrangedSubview(note2)
        body.addArrangedSubview(info2)
        logoBackground.addSubview(logo)
        view.addSubview(footer)
        footer.addSubview(footerBackground)
        footer.addArrangedSubview(note3)
        footer.addArrangedSubview(buttonStack)
        buttonStack.addArrangedSubview(decline)
        buttonStack.addArrangedSubview(approve)
    }

    private func addConstraints() {
        header.constrain([
            header.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            header.topAnchor.constraint(equalTo: safeTopAnchor),
            header.trailingAnchor.constraint(equalTo: view.trailingAnchor)])
        scrollView.alwaysBounceVertical = true
        scrollView.constrain([
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            scrollView.topAnchor.constraint(equalTo: header.bottomAnchor),
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            scrollView.bottomAnchor.constraint(equalTo: footer.topAnchor)])
        body.constrain([
            body.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
            body.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
            body.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
            body.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 30.0),
            body.widthAnchor.constraint(equalTo: scrollView.widthAnchor),])
        logoBackground.constrain([
            logoBackground.heightAnchor.constraint(equalTo: logoBackground.widthAnchor, multiplier: logo.image!.size.height/logo.image!.size.width),
            logoBackground.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.36)])
        logo.constrain(toSuperviewEdges: nil)
        footer.constrain([
            footer.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            footer.bottomAnchor.constraint(equalTo: safeBottomAnchor),
            footer.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            footer.heightAnchor.constraint(equalToConstant: 140.0)])
        footerBackground.constrain(toSuperviewEdges: nil)
        bodyBackground.constrain(toSuperviewEdges: UIEdgeInsetsMake(0, C.padding[2], 0, -C.padding[2]))
    }

    private func setInitialData() {
        view.backgroundColor = .darkBackground
        logo.tintColor = .darkBackground
        scrollView.delegate = self
        scrollView.showsVerticalScrollIndicator = false
        setupStackViews()
        setButtonActions()
        setupLabels()
    }

    private func setupLabels() {
        titleLabel.text = S.LinkWallet.title.uppercased()
        note1.text = S.LinkWallet.domainTitle
        info1.text = serviceDefinition.domains.joined(separator: ", ")
        info1.numberOfLines = 0  // make sure the list of domains is not truncated
        note2.text = S.LinkWallet.permissionsTitle
        
        let capabilitiesList: [String] = serviceDefinition.capabilities.filter { !$0.description.isEmpty }.map {
            let scopes = $0.scopes.map { $0.description }.joined(separator: ", ")
            let format = Bundle.main.localizedString(forKey: $0.description, value: $0.description, table: nil)
            return "• " + String(format: format, scopes)
        }
        
        info2.text = capabilitiesList.joined(separator: "\n")
        note3.text = S.LinkWallet.disclaimer
        note1.textAlignment = .center
{'fraction_non_alphanumeric': 0.09808837355061109, 'fraction_numerical': 0.012300219366969602, 'mean_word_length': 3.2507492507492506, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '373246', 'n_tokens_mistral': 3718, 'n_tokens_neox': 3572, 'n_words': 686}