<commit_before>//
//  ActionsViewModel.swift
//  URLRemote
//
//  Created by <NAME> on 13/12/16.
//  Copyright © 2016 Svacha, Michal. All rights reserved.
//

import Foundation
import FirebaseDatabase
import FirebaseAuth
import ObjectMapper
import Bond
import ReactiveKit

///
class ActionsViewModel {
    let bndBag = DisposeBag()
    var dataSource: FirebaseDataSource?
    
    init() {
        FIRAuth.auth()?.addStateDidChangeListener { _, user in
            if let user = user {
                print("signed in as \(user.description)")
                
                if let data = self.dataSource, data.user.uid == user.uid {
                    // do nothing
                } else {
                    self.dataSource = FirebaseDataSource(user: user, database: FIRDatabase.database())
                    NotificationCenter.default.post(
                        name: NSNotification.Name(rawValue: "USER_LOGGED_IN"),
                        object: nil)
                }
            } else {
                NotificationCenter.default.post(
                    name: NSNotification.Name(rawValue: "USER_LOGGED_OUT"),
                    object: nil)
            }
        }
        
        NotificationCenter.default.bnd_notification(name: NSNotification.Name(rawValue: "CREATED_ENTRY"))
            .observeNext { notification in
                let entry = notification.object as! Entry
                self.dataSource?.write(entry)
            }.dispose(in: self.bndBag)
    }
    
    func logout() {
        self.dataSource = nil
        try? FIRAuth.auth()?.signOut()
    }
    
    func addTestItem() {
        let testEntry = Entry()
        testEntry.name = "test"<commit_msg>bug/Fix proper data removal upon logout<commit_after>//
//  ActionsViewModel.swift
//  URLRemote
//
//  Created by <NAME> on 13/12/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import FirebaseDatabase
import FirebaseAuth
import ObjectMapper
import Bond
import ReactiveKit

///
class ActionsViewModel {
    let bndBag = DisposeBag()
    var dataSource: FirebaseDataSource?
    
    init() {
        FIRAuth.auth()?.addStateDidChangeListener { _, user in
            if let user = user {
                print("signed in as \(user.description)")
                
                if let data = self.dataSource, data.user.uid == user.uid {
                    // do nothing
                } else {
                    self.dataSource = FirebaseDataSource(user: user, database: FIRDatabase.database())
                    NotificationCenter.default.post(
                        name: NSNotification.Name(rawValue: "USER_LOGGED_IN"),
                        object: nil)
                }
            } else {
                NotificationCenter.default.post(
                    name: NSNotification.Name(rawValue: "USER_LOGGED_OUT"),
                    object: nil)
            }
        }
        
        NotificationCenter.default.bnd_notification(name: NSNotification.Name(rawValue: "CREATED_ENTRY"))
            .observeNext { notification in
                let entry = notification.object as! Entry
                self.dataSource?.write(entry)
            }.dispose(in: self.bndBag)
    }
    
    func logout() {
        try? FIRAuth.auth()?.signOut()
    }
    
    func addTestItem() {
        let testEntry = Entry()
        testEntry.name = "test"
{'fraction_non_alphanumeric': 0.09563164108618655, 'fraction_numerical': 0.0059031877213695395, 'mean_word_length': 1.6559561128526645, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '2645322', 'n_tokens_mistral': 939, 'n_tokens_neox': 872, 'n_words': 205}