<commit_before>//
//  BooklistViewController.swift
//  BookMark
//
//  Created by <NAME> on 2017/1/8.
//  Copyright © 2017年 Cu. All rights reserved.
//

import UIKit
import BarcodeScanner

    // MARK: - FakeData

let bookDict1: [String:Any] = [ "name" : "booktitle1" , "ISBNcode" : "12345678", "pic" : "", "notesCount": 3 ]
let bookDict2: [String:Any] = [ "name" : "booktitle2" , "ISBNcode" : "23456789", "pic" : "", "notesCount": 12 ]
var allBookArray: [Any] = [bookDict1, bookDict2]

class BooklistViewController: UITableViewController, BarcodeScannerCodeDelegate {

    @IBAction func addBookButton(_ sender: UIButton) {
    
        //Camera
        //Barcode
        
        
        
        let controller = BarcodeScannerController()
        controller.codeDelegate = self
        
        navigationController?.pushViewController(controller, animated: true)
        
        alertForCreateAccount()
        
    }

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        

    }

    
    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return allBookArray.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BooklistCell", for: indexPath) as! BooklistCustomCell

        if let currentBook = allBookArray[indexPath.row] as? NSDictionary {
            
            cell.bookNameLabel.text = currentBook.value(forKey: "name") as? String
            cell.ISBNCode.text = currentBook.value(forKey: "ISBNcode") as? String
            cell.numberOfNotes.text = currentBook.value(forKey: "notesCount") as? String
            
            
        } else {
            fatalError("book not found")
        }
        

        return cell
    }
    

       /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

    func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
        print(code)
        controller.reset()
    }
    
    func alertForCreateAccount() {
        
        var booknameCreated: String = ""
        let bookISBNCreated: String = ""
        let bookImageCreated: String = ""
        
        
        let alert = UIAlertController(title: "Add new book", message: "", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Add", style: UIAlertActionStyle.default, handler: { action in
            
            let bookNameTextField = alert.textFields![0] as UITextField
            
            if let name = bookNameTextField.text {booknameCreated = name}
            
            if booknameCreated.isEmpty != true {
                
                let newBookDict: [String:Any] = [
                
                    "name" : booknameCreated,
                    "ISBNCode" : bookISBNCreated,
                    "pic" : bookImageCreated,
                    "notesCount" : 0
                ]
                
                allBookArray.append(newBookDict)
                
            } else {
                
                self.signupErrorAlert(title: "Oops!", message: "Title cannot be blank!")
                
            }
            
        }))
        
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: { action in
        }))
        
        alert.addTextField { (textField : UITextField!) -> Void in
            textField.placeholder = "Enter book name"
        }
        
        self.present(alert, animated: true, completion: nil)
        
    }

    func signupErrorAlert(title: String, message: String) {
        
        // Called upon signup error to let the user know signup didn't work.
        
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
        alert.addAction(action)
        present(alert, animated: true, completion: nil)
    }
    
    
}
<commit_msg>complete Barcode and adding new books with selectable cells<commit_after>//
//  BooklistViewController.swift
//  BookMark
//
//  Created by <NAME> on 2017/1/8.
//  Copyright © 2017年 Cu. All rights reserved.
//

import UIKit
import BarcodeScanner

    // MARK: - FakeData

let bookDict1: [String:Any] = [ "name" : "booktitle1" , "ISBNcode" : "12345678", "pic" : "", "notesCount": 3 ]
let bookDict2: [String:Any] = [ "name" : "booktitle2" , "ISBNcode" : "23456789", "pic" : "", "notesCount": 12 ]
var allBookArray: [Any] = [bookDict1, bookDict2]

class BooklistViewController: UITableViewController, BarcodeScannerCodeDelegate, BarcodeScannerDismissalDelegate {

    @IBAction func addBookButton(_ sender: UIButton) {
    
        //Camera
        //Barcode
        
        let controller = BarcodeScannerController()
        controller.codeDelegate = self
//        controller.errorDelegate = self
        controller.dismissalDelegate = self
        
        present(controller, animated: true, completion: nil)
        
        
    }

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        

    }

    
    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return allBookArray.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BooklistCell", for: indexPath) as! BooklistCustomCell

        if let currentBook = allBookArray[indexPath.row] as? NSDictionary {
            
            cell.bookNameLabel.text = currentBook.value(forKey: "name") as? String
            cell.ISBNCode.text = currentBook.value(forKey: "ISBNcode") as? String
            cell.numberOfNotes.text = currentBook.value(forKey: "notesCount") as? String
            
            
        } else {
            fatalError("book not found")
        }
        

        return cell
    }
    
    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

        let rowIndex = indexPath.row
        
        print(allBookArray[rowIndex])

    }

       /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

    func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
        print(code)
//        controller.reset()

        controller.dismiss(animated: true, completion: nil)
        
        self.alertForAddingBook(ISBN: code)

        
    }
    
    func barcodeScannerDidDismiss(_ controller: BarcodeScannerController) {
        controller.dismiss(animated: true, completion: nil)
    }
    
    func alertForAddingBook(ISBN: String) {
        
        var booknameCreated: String = ""
        let bookISBNCreated: String = ISBN
        let bookImageCreated: String = ""
        
        
        let alert = UIAlertController(title: "Add new book", message: bookISBNCreated, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Add", style: UIAlertActionStyle.default, handler: { action in
            
            let bookNameTextField = alert.textFields![0] as UITextField
            
            if let name = bookNameTextField.text {booknameCreated = name}
            
            if booknameCreated.isEmpty != true {
                
                let newBookDict: [String:Any] = [
                
                    "name" : booknameCreated,
                    "ISBNcode" : bookISBNCreated,
                    "pic" : bookImageCreated,
                    "notesCount" : 0
                ]
                
                allBookArray.append(newBookDict)
                print(allBookArray)
                self.tableView.reloadData()
                
            } else {
                
                self.signupErrorAlert(title: "Oops!", message: "Title cannot be blank!")
                
            }
            
        }))
        
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { action in
        }))
        
        alert.addTextField { (textField : UITextField!) -> Void in
            textField.placeholder = "Enter book name here"
        }
        
        self.present(alert, animated: true, completion: nil)
        
    }

    func signupErrorAlert(title: String, message: String) {
        
        // Called upon signup error to let the user know signup didn't work.
        
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
        alert.addAction(action)
        present(alert, animated: true, completion: nil)
    }
    
    
}

{'fraction_non_alphanumeric': 0.09097502849445653, 'fraction_numerical': 0.007667599212516837, 'mean_word_length': 1.9762565525747764, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 11, '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': '6067390', 'n_tokens_mistral': 2696, 'n_tokens_neox': 2549, 'n_words': 652}