<commit_before>//
//  DataViewController.swift
//  SomeApp
//
//  Created by Perry on 2/13/16.
//  Copyright © 2016 PerrchicK. All rights reserved.
//

import UIKit

class DataViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SyncedUserDefaultsDelegate, UITextFieldDelegate {
    let UserDefaultsStringKey = "MyKeyToSaveObjectInUSerDefaults"
    let PersistableUserFileName = "PersistableUserFileName"
    enum TableViewType: String {
        case firebase
        case coreData
    }

    private var presentedAlert: UIAlertController?

    lazy var dbStateTableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)

        tableView.stretchToSuperViewEdges(UIEdgeInsets(top: navigationBarHeight + 40, left: 20, bottom: -20, right: -20))

        tableView.isPresented = false
        return tableView
    }()

    @IBOutlet weak var scrollView: UIScrollView!

    /* Saved in Core Data */
    @IBOutlet weak var firstNameTextField: UITextField!
    @IBOutlet weak var lastNameTextField: UITextField!
    @IBOutlet weak var nicknameTextField: UITextField!
    @IBOutlet weak var emailTextField: UITextField!
    /* Saved in Core Data */
    fileprivate var isScreenUp = false

    @IBOutlet weak var userDefaultsTextField: UITextField!

    fileprivate var users:[SomeApp.AppUser]!
    fileprivate lazy var syncedUserDefaults: SyncedUserDefaults = DataManager.generateSyncedUserDefaults()

    fileprivate lazy var firebaseKeys: [String] = {
        return []
    }()

    lazy var navigationBarHeight: CGFloat = navigationController?.navigationBar.frame.height ?? 0 + 1.0

    override func viewDidLoad() {
        super.viewDidLoad()

        refreshUsersArray()

        self.dbStateTableView.register(UITableViewCell.self, forCellReuseIdentifier: "CellReuseIdentifier")
        self.dbStateTableView.isHidden = true
        scrollView.keyboardDismissMode = .interactive

        let contentInsets = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
        scrollView.contentInset = contentInsets
//        scrollView.scrollIndicatorInsets = contentInsets
        scrollView.alwaysBounceVertical = false

        userDefaultsTextField.text = UserDefaults.standard.object(forKey: UserDefaultsStringKey) as? String
        view.onClick { [weak self] _ in
            self?.view.endEditing(true)
        }

        dbStateTableView.layer.cornerRadius = 5

        firstNameTextField.delegate = self
        lastNameTextField.delegate = self
        nicknameTextField.delegate = self
        emailTextField.delegate = self
        
        let encodedUserFilePath = URL(fileURLWithPath: DataManager.applicationLibraryPath.appendingPathComponent(PersistableUserFileName))
        if let encodedUserData = try? Data(contentsOf: encodedUserFilePath), let encodedUser = NSKeyedUnarchiver.unarchiveObject(with: encodedUserData) as? PersistableUser {
            📘(encodedUser)
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        let notificationCenter = NotificationCenter.default
        notificationCenter.addObserver(self, selector: #selector(DataViewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        notificationCenter.addObserver(self, selector: #selector(DataViewController.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

        syncedUserDefaults.delegate = self
        
        if let persistableUsersData = UserDefaults.standard.object(forKey: PerrFuncs.className(PersistableUser.self)) as? Data,
            let persistableUsers = NSKeyedUnarchiver.unarchiveObject(with: persistableUsersData) as? [PersistableUser] {
            📘(persistableUsers)
        }
        
        if !(FirebaseHelper.isActivated ?? false) {
            ToastMessage.show(messageText: "Firebase is not integrated")
        }

        //UserDefaults.saveForever(value: "cool forever", forKey: "Perry")
        let valueFromKeychain = UserDefaults.loadFromEternity(fromKey: "Perry") ?? "<empty>"
        
        //localStorage["Perry"] = "cool for a lifetime"
        let valueFromUserDefaults: String = localStorage["Perry"] ?? "<empty>"
        
        📘("valueFromKeychain == \(valueFromKeychain)")
        📘("valueFromUserDefaults == \(valueFromUserDefaults)")
    }

    override func viewWillDisappear(_ animated: Bool) {<commit_msg>[FIX] The users' list data object was saved as a data of array. The app should load an array, not a single user.<commit_after>//
//  DataViewController.swift
//  SomeApp
//
//  Created by Perry on 2/13/16.
//  Copyright © 2016 PerrchicK. All rights reserved.
//

import UIKit

class DataViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SyncedUserDefaultsDelegate, UITextFieldDelegate {
    let UserDefaultsStringKey = "MyKeyToSaveObjectInUSerDefaults"
    let PersistableUserFileName = "PersistableUserFileName"
    enum TableViewType: String {
        case firebase
        case coreData
    }

    private var presentedAlert: UIAlertController?

    lazy var dbStateTableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)

        tableView.stretchToSuperViewEdges(UIEdgeInsets(top: navigationBarHeight + 40, left: 20, bottom: -20, right: -20))

        tableView.isPresented = false
        return tableView
    }()

    @IBOutlet weak var scrollView: UIScrollView!

    /* Saved in Core Data */
    @IBOutlet weak var firstNameTextField: UITextField!
    @IBOutlet weak var lastNameTextField: UITextField!
    @IBOutlet weak var nicknameTextField: UITextField!
    @IBOutlet weak var emailTextField: UITextField!
    /* Saved in Core Data */
    fileprivate var isScreenUp = false

    @IBOutlet weak var userDefaultsTextField: UITextField!

    fileprivate var users:[SomeApp.AppUser]!
    fileprivate lazy var syncedUserDefaults: SyncedUserDefaults = DataManager.generateSyncedUserDefaults()

    fileprivate lazy var firebaseKeys: [String] = {
        return []
    }()

    lazy var navigationBarHeight: CGFloat = navigationController?.navigationBar.frame.height ?? 0 + 1.0

    override func viewDidLoad() {
        super.viewDidLoad()

        refreshUsersArray()

        self.dbStateTableView.register(UITableViewCell.self, forCellReuseIdentifier: "CellReuseIdentifier")
        self.dbStateTableView.isHidden = true
        scrollView.keyboardDismissMode = .interactive

        let contentInsets = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
        scrollView.contentInset = contentInsets
//        scrollView.scrollIndicatorInsets = contentInsets
        scrollView.alwaysBounceVertical = false

        userDefaultsTextField.text = UserDefaults.standard.object(forKey: UserDefaultsStringKey) as? String
        view.onClick { [weak self] _ in
            self?.view.endEditing(true)
        }

        dbStateTableView.layer.cornerRadius = 5

        firstNameTextField.delegate = self
        lastNameTextField.delegate = self
        nicknameTextField.delegate = self
        emailTextField.delegate = self
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        let encodedUserFilePath = URL(fileURLWithPath: DataManager.applicationLibraryPath.appendingPathComponent(PersistableUserFileName))
        do {
            let encodedUserData = try Data(contentsOf: encodedUserFilePath)
            if let encodedUsers = NSKeyedUnarchiver.unarchiveObject(with: encodedUserData) as? [PersistableUser] {
                📘(encodedUsers)
            } else {
                📘("Failed to create user object from data object")
            }
        } catch {
            📘("Failed to load user data from file, error: \(error)")
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        let notificationCenter = NotificationCenter.default
        notificationCenter.addObserver(self, selector: #selector(DataViewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        notificationCenter.addObserver(self, selector: #selector(DataViewController.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

        syncedUserDefaults.delegate = self
        
        if let persistableUsersData = UserDefaults.standard.object(forKey: PerrFuncs.className(PersistableUser.self)) as? Data,
            let persistableUsers = NSKeyedUnarchiver.unarchiveObject(with: persistableUsersData) as? [PersistableUser] {
            📘(persistableUsers)
        }
        
        if !(FirebaseHelper.isActivated ?? false) {
            ToastMessage.show(messageText: "Firebase is not integrated")
        }

        //UserDefaults.saveForever(value: "cool forever", forKey: "Perry")
        let valueFromKeychain = UserDefaults.loadFromEternity(fromKey: "Perry") ?? "<empty>"
        
        //localStorage["Perry"] = "cool for a lifetime"
        let valueFromUserDefaults: String = localStorage["Perry"] ?? "<empty>"
        
        📘("valueFromKeychain == \(valueFromKeychain)")
        📘("valueFromUserDefaults == \(valueFromUserDefaults)")
    }

    override func viewWillDisappear(_ animated: Bool) {
{'fraction_non_alphanumeric': 0.08486190174994729, 'fraction_numerical': 0.005481762597512123, 'mean_word_length': 3.2164444444444444, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6537522', 'n_tokens_mistral': 2751, 'n_tokens_neox': 2564, 'n_words': 590}