<commit_before>//
//  StudyHomeViewController.swift
//  KanjiRyokucha
//
//  Created by <NAME> on 3/25/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import PKHUD
import ReactiveSwift
import RealmSwift

fileprivate typealias RefreshAction = Action<Void, Response, FetchError>
fileprivate typealias RefreshStarter = ActionStarter<Void, Response, FetchError>
fileprivate typealias SubmitAction = Action<StudyChanges, Response, FetchError>
fileprivate typealias SubmitStarter = ActionStarter<StudyChanges, Response, FetchError>

class StudyHomeViewController: UIViewController {
    
    private class func submitActionProducer(changes: StudyChanges) -> SignalProducer<Response,FetchError> {
        let submitBatchSize = 10
        
        guard changes.learned.count > 0 || changes.notLearned.count > 0 else { return SignalProducer.empty }
        
        return SignalProducer.empty // FIXME: make rq
    }
    
    var viewModel: SRSViewModel!
    let changeCount: MutableProperty<Int> = MutableProperty(0)
    private var submitAction: SubmitAction!
    private var submitStarter: SubmitStarter!
    private var refreshAction: RefreshAction!
    private var refreshStarter: RefreshStarter!
    let shouldEnableSubmit: MutableProperty<Bool> = MutableProperty(false)

    @IBOutlet weak var toStudyLabel: UILabel!
    @IBOutlet weak var learnedLabel: UILabel!
    @IBOutlet weak var startButton: UIButton!
    @IBOutlet weak var submitButton: UIButton!
    @IBOutlet weak var toSubmitLabel: UILabel!
    @IBOutlet weak var refreshButton: UIButton!
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Study"
        view.backgroundColor = .ryokuchaFaint
        
        wireUp()
    }
    
    func wireUp() {
        toStudyLabel.reactive.text <~ viewModel.toStudyCount.map { (count:Int) in "\(count) to study" }
        
        let failedSetup = viewModel.reviewTypeSetups[.failed]!
        learnedLabel.reactive.text <~ failedSetup.learnedCount.map { "\($0) learned" }
        
        changeCount <~ viewModel.studyChanges.map { $0.notLearned.count + $0.learned.count }
        
        toSubmitLabel.reactive.text <~ changeCount.map { "\($0)" }
        
        shouldEnableSubmit <~ changeCount.map { $0 > 0 }
        
        submitAction = SubmitAction(enabledIf: shouldEnableSubmit, StudyHomeViewController.submitActionProducer)

        submitStarter = SubmitStarter(control: submitButton,
                                      action: submitAction,
                                      inputProperty: viewModel.studyChanges)
        
        activityIndicator.reactive.isAnimating <~ submitAction.isExecuting
        activityIndicator.reactive.isHidden <~ submitAction.isExecuting.map { !$0 }
        
        refreshAction = RefreshAction { _ in
            return StudyRefreshRequest().requestProducer()!
        }
        refreshAction.react { [weak self] response in
            if let ids = response.model as? StudyIdsModel {
                self?.updateStudyData(studyIds: ids)
            }
        }
        
        refreshStarter = RefreshStarter(control: refreshButton,
                                        action: refreshAction,
                                        input: ())
        
        refreshStarter.useHUD()
    }
    
    func updateStudyData(studyIds: StudyIdsModel) {
        viewModel.studyEntries.value.forEach { studyEntry in
            studyEntry.synced = false
        }
        Database.write { realm in
            studyIds.ids.forEach { studyId in
                let isLearned = studyIds.learnedIds.contains(studyId)
                if let studyEntry = viewModel.studyEntries.value.first(where: { studyId == $0.cardId }) {
                    studyEntry.learned = isLearned
                    studyEntry.synced = true
                    realm.add(studyEntry, update: true)
                } else {
                    let studyEntry = StudyEntry()
                    studyEntry.cardId = studyId
                    studyEntry.keyword = "#\(studyId)" // TODO: need to get the keyword
                    studyEntry.learned = isLearned
                    studyEntry.synced = true
                    realm.add(studyEntry, update: false)
                    viewModel.studyEntries.value.append(studyEntry)
                }
            }
            let unsyncedEntries = viewModel.studyEntries.value.filter { !$0.synced }
            unsyncedEntries.forEach { studyEntry in
                if let index = viewModel.studyEntries.value.index(of: studyEntry) {
                    viewModel.studyEntries.value.remove(at: index)
                }
                realm.delete(studyEntry)
            }
        }
    }
}<commit_msg>fix updating db<commit_after>//
//  StudyHomeViewController.swift
//  KanjiRyokucha
//
//  Created by <NAME> on 3/25/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import PKHUD
import ReactiveSwift
import RealmSwift

fileprivate typealias RefreshAction = Action<Void, Response, FetchError>
fileprivate typealias RefreshStarter = ActionStarter<Void, Response, FetchError>
fileprivate typealias SubmitAction = Action<StudyChanges, Response, FetchError>
fileprivate typealias SubmitStarter = ActionStarter<StudyChanges, Response, FetchError>

class StudyHomeViewController: UIViewController {
    
    private class func submitActionProducer(changes: StudyChanges) -> SignalProducer<Response,FetchError> {
        let submitBatchSize = 10
        
        guard changes.learned.count > 0 || changes.notLearned.count > 0 else { return SignalProducer.empty }
        
        return SignalProducer.empty // FIXME: make rq
    }
    
    var viewModel: SRSViewModel!
    let changeCount: MutableProperty<Int> = MutableProperty(0)
    private var submitAction: SubmitAction!
    private var submitStarter: SubmitStarter!
    private var refreshAction: RefreshAction!
    private var refreshStarter: RefreshStarter!
    let shouldEnableSubmit: MutableProperty<Bool> = MutableProperty(false)

    @IBOutlet weak var toStudyLabel: UILabel!
    @IBOutlet weak var learnedLabel: UILabel!
    @IBOutlet weak var startButton: UIButton!
    @IBOutlet weak var submitButton: UIButton!
    @IBOutlet weak var toSubmitLabel: UILabel!
    @IBOutlet weak var refreshButton: UIButton!
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Study"
        view.backgroundColor = .ryokuchaFaint
        
        wireUp()
    }
    
    func wireUp() {
        toStudyLabel.reactive.text <~ viewModel.toStudyCount.map { (count:Int) in "\(count) to study" }
        
        let failedSetup = viewModel.reviewTypeSetups[.failed]!
        learnedLabel.reactive.text <~ failedSetup.learnedCount.map { "\($0) learned" }
        
        changeCount <~ viewModel.studyChanges.map { $0.notLearned.count + $0.learned.count }
        
        toSubmitLabel.reactive.text <~ changeCount.map { "\($0)" }
        
        shouldEnableSubmit <~ changeCount.map { $0 > 0 }
        
        submitAction = SubmitAction(enabledIf: shouldEnableSubmit, StudyHomeViewController.submitActionProducer)

        submitStarter = SubmitStarter(control: submitButton,
                                      action: submitAction,
                                      inputProperty: viewModel.studyChanges)
        
        activityIndicator.reactive.isAnimating <~ submitAction.isExecuting
        activityIndicator.reactive.isHidden <~ submitAction.isExecuting.map { !$0 }
        
        refreshAction = RefreshAction { _ in
            return StudyRefreshRequest().requestProducer()!
        }
        refreshAction.react { [weak self] response in
            if let ids = response.model as? StudyIdsModel {
                self?.updateStudyData(studyIds: ids)
            }
        }
        
        refreshStarter = RefreshStarter(control: refreshButton,
                                        action: refreshAction,
                                        input: ())
        
        refreshStarter.useHUD()
    }
    
    func updateStudyData(studyIds: StudyIdsModel) {
        var unsyncedIds = viewModel.studyEntries.value.map { $0.cardId }

        Database.write { realm in
            studyIds.ids.forEach { studyId in
                let isLearned = studyIds.learnedIds.contains(studyId)
                if let studyEntry = viewModel.studyEntries.value.first(where: { studyId == $0.cardId }) {
                    studyEntry.learned = isLearned
                    studyEntry.synced = true
                    realm.add(studyEntry, update: true)
                } else {
                    let studyEntry = StudyEntry()
                    studyEntry.cardId = studyId
                    studyEntry.keyword = "#\(studyId)" // TODO: need to get the keyword
                    studyEntry.learned = isLearned
                    studyEntry.synced = true
                    realm.add(studyEntry, update: false)
                    viewModel.studyEntries.value.append(studyEntry)
                }
                
                if let index = unsyncedIds.index(of: studyId) {
                    unsyncedIds.remove(at: index)
                }
            }
            unsyncedIds.forEach { unsyncedId in
                if let studyEntry = viewModel.studyEntries.value.first(where: { unsyncedId == $0.cardId }) {
                    if let index = viewModel.studyEntries.value.index(of: studyEntry) {
                        viewModel.studyEntries.value.remove(at: index)
                    }
                    realm.delete(studyEntry)
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08057259713701431, 'fraction_numerical': 0.0048057259713701434, 'mean_word_length': 2.0728872133207665, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1777745', 'n_tokens_mistral': 2711, 'n_tokens_neox': 2563, 'n_words': 566}