<commit_before>//
//  PGPKeyArmorSettingTableViewController.swift
//  pass
//
//  Created by <NAME> on 17/2/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import passKit

class PGPKeyArmorSettingTableViewController: AutoCellHeightUITableViewController, UITextViewDelegate, QRScannerControllerDelegate {
    @IBOutlet weak var armorPublicKeyTextView: UITextView!
    @IBOutlet weak var armorPrivateKeyTextView: UITextView!
    @IBOutlet weak var scanPublicKeyCell: UITableViewCell!
    @IBOutlet weak var scanPrivateKeyCell: UITableViewCell!

    let passwordStore = PasswordStore.shared
    let keychain = AppKeychain.shared
    
    class ScannedPGPKey {
        enum KeyType {
            case publicKey, privateKey
        }
        var keyType = KeyType.publicKey
        var segments = [String]()
        var message = ""

        func reset(keytype: KeyType) {
            self.keyType = keytype
            self.segments.removeAll()
            message = "LookingForStartingFrame.".localize()
        }

        func addSegment(segment: String) -> (accept: Bool, message: String) {
            let keyTypeStr = self.keyType == .publicKey ? "Public" : "Private"
            let theOtherKeyTypeStr = self.keyType == .publicKey ? "Private" : "Public"
            
            // Skip duplicated segments.
            guard segment != self.segments.last else {
                return (accept: false, message: self.message)
            }

            // Check whether we have found the first block.
            guard !self.segments.isEmpty || segment.contains("-----BEGIN PGP \(keyTypeStr.uppercased()) KEY BLOCK-----") else {
                // Check whether we are scanning the wrong key type.
                if segment.contains("-----BEGIN PGP \(theOtherKeyTypeStr.uppercased()) KEY BLOCK-----") {
                    self.message = "Scan\(keyTypeStr)Key.".localize()
                }
                return (accept: false, message: self.message)
            }
            
            // Update the list of scanned segment and return.
            self.segments.append(segment)
            if segment.contains("-----END PGP .* KEY BLOCK-----") {
                self.message = "Done".localize()
                return (accept: true, message: self.message)
            } else {
                self.message = "ScannedQrCodes(%d)".localize(self.segments.count)
                print(self.message)
                return (accept: false, message: self.message)
            }
        }
    }
    var scanned = ScannedPGPKey()

    override func viewDidLoad() {
        super.viewDidLoad()

        scanPublicKeyCell?.textLabel?.text = "ScanPublicKeyQrCodes".localize()
        scanPublicKeyCell?.textLabel?.textColor = Colors.systemBlue
        scanPublicKeyCell?.selectionStyle = .default
        scanPublicKeyCell?.accessoryType = .disclosureIndicator

        scanPrivateKeyCell?.textLabel?.text = "ScanPrivateKeyQrCodes".localize()
        scanPrivateKeyCell?.textLabel?.textColor = Colors.systemBlue
        scanPrivateKeyCell?.selectionStyle = .default
        scanPrivateKeyCell?.accessoryType = .disclosureIndicator
    }

    @IBAction func save(_ sender: Any) {
        guard armorPublicKeyTextView.text.isEmpty == false else {
            Utils.alert(title: "CannotSave".localize(), message: "SetPublicKey.".localize(), controller: self, completion: nil)
            return<commit_msg>Fix QR code scanning problem<commit_after>//
//  PGPKeyArmorSettingTableViewController.swift
//  pass
//
//  Created by <NAME> on 17/2/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import passKit

class PGPKeyArmorSettingTableViewController: AutoCellHeightUITableViewController, UITextViewDelegate, QRScannerControllerDelegate {
    @IBOutlet weak var armorPublicKeyTextView: UITextView!
    @IBOutlet weak var armorPrivateKeyTextView: UITextView!
    @IBOutlet weak var scanPublicKeyCell: UITableViewCell!
    @IBOutlet weak var scanPrivateKeyCell: UITableViewCell!

    let passwordStore = PasswordStore.shared
    let keychain = AppKeychain.shared
    
    class ScannedPGPKey {
        enum KeyType {
            case publicKey, privateKey
        }
        var keyType = KeyType.publicKey
        var segments = [String]()
        var message = ""

        func reset(keytype: KeyType) {
            self.keyType = keytype
            self.segments.removeAll()
            message = "LookingForStartingFrame.".localize()
        }

        func addSegment(segment: String) -> (accept: Bool, message: String) {
            let keyTypeStr = self.keyType == .publicKey ? "Public" : "Private"
            let theOtherKeyTypeStr = self.keyType == .publicKey ? "Private" : "Public"
            
            // Skip duplicated segments.
            guard segment != self.segments.last else {
                return (accept: false, message: self.message)
            }

            // Check whether we have found the first block.
            guard !self.segments.isEmpty || segment.contains("-----BEGIN PGP \(keyTypeStr.uppercased()) KEY BLOCK-----") else {
                // Check whether we are scanning the wrong key type.
                if segment.contains("-----BEGIN PGP \(theOtherKeyTypeStr.uppercased()) KEY BLOCK-----") {
                    self.message = "Scan\(keyTypeStr)Key.".localize()
                }
                return (accept: false, message: self.message)
            }
            
            // Update the list of scanned segment and return.
            self.segments.append(segment)
            if segment.contains("-----END PGP \(keyTypeStr.uppercased()) KEY BLOCK-----") {
                self.message = "Done".localize()
                return (accept: true, message: self.message)
            } else {
                self.message = "ScannedQrCodes(%d)".localize(self.segments.count)
                return (accept: false, message: self.message)
            }
        }
    }
    var scanned = ScannedPGPKey()

    override func viewDidLoad() {
        super.viewDidLoad()

        scanPublicKeyCell?.textLabel?.text = "ScanPublicKeyQrCodes".localize()
        scanPublicKeyCell?.textLabel?.textColor = Colors.systemBlue
        scanPublicKeyCell?.selectionStyle = .default
        scanPublicKeyCell?.accessoryType = .disclosureIndicator

        scanPrivateKeyCell?.textLabel?.text = "ScanPrivateKeyQrCodes".localize()
        scanPrivateKeyCell?.textLabel?.textColor = Colors.systemBlue
        scanPrivateKeyCell?.selectionStyle = .default
        scanPrivateKeyCell?.accessoryType = .disclosureIndicator
    }

    @IBAction func save(_ sender: Any) {
        guard armorPublicKeyTextView.text.isEmpty == false else {
            Utils.alert(title: "CannotSave".localize(), message: "SetPublicKey.".localize(), controller: self, completion: nil)
            return
{'fraction_non_alphanumeric': 0.10378047000437893, 'fraction_numerical': 0.003211210042329587, 'mean_word_length': 2.2443181818181817, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '4507557', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1781, 'n_words': 429}