<commit_before>//
//  SaveDeck.swift
//  HSTracker
//
//  Created by <NAME> on 24/03/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import CleanroomLogger
import RealmSwift
import AppKit

protocol SaveDeckDelegate: class {
    func deckSaveSaved()
    func deckSaveCanceled()
}

class SaveDeck: NSWindowController {

    @IBOutlet weak var deckName: NSTextField!

    var deck: Deck?
    var cards: [Card]?
    var exists = false
    weak private var _delegate: SaveDeckDelegate?

    func setDelegate(_ delegate: SaveDeckDelegate) {
        _delegate = delegate
    }

    override func windowDidLoad() {
        super.windowDidLoad()

        deckName.stringValue = deck!.name 

        if RealmHelper.getDeck(with: deck!.deckId) != nil {
            exists = true
        } else {
            Log.error?.message("Can not fetch deck")
        }
    }

    // MARK: - Actions
    @IBAction func save(_ sender: AnyObject) {
        guard let deck = deck, let cards = cards, cards.isValidDeck() else { return }

        RealmHelper.rename(deck: deck, to: deckName.stringValue)
        self.saveDeck(update: exists)
    }

    @IBAction func cancel(_ sender: AnyObject) {
        self._delegate?.deckSaveCanceled()
    }

    func saveDeck(update: Bool) {
        guard let deck = deck, let cards = cards, cards.isValidDeck() else { return }
		
		RealmHelper.update(deck: deck, with: cards)
        RealmHelper.add(deck: deck, update: update)
        
        NotificationCenter.default.post(name: Notification.Name(rawValue: "reload_decks"),
                                        object: deck)<commit_msg>Fixed crash when saving a new edited deck<commit_after>//
//  SaveDeck.swift
//  HSTracker
//
//  Created by <NAME> on 24/03/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import CleanroomLogger
import RealmSwift
import AppKit

protocol SaveDeckDelegate: class {
    func deckSaveSaved()
    func deckSaveCanceled()
}

class SaveDeck: NSWindowController {

    @IBOutlet weak var deckName: NSTextField!

    var deck: Deck?
    var cards: [Card]?
    var exists = false
    weak private var _delegate: SaveDeckDelegate?

    func setDelegate(_ delegate: SaveDeckDelegate) {
        _delegate = delegate
    }

    override func windowDidLoad() {
        super.windowDidLoad()

        deckName.stringValue = deck!.name 

        if let deck = RealmHelper.getDeck(with: deck!.deckId) {
			self.deck = deck
			exists = true
        } else {
            Log.error?.message("Can not fetch deck")
        }
    }

    // MARK: - Actions
    @IBAction func save(_ sender: AnyObject) {
        guard let deck = deck, let cards = cards, cards.isValidDeck() else { return }

		if self.exists {
			RealmHelper.rename(deck: deck, to: deckName.stringValue)
		} else {
			deck.name = deckName.stringValue
		}
        self.saveDeck(update: exists)
    }

    @IBAction func cancel(_ sender: AnyObject) {
        self._delegate?.deckSaveCanceled()
    }

    func saveDeck(update: Bool) {
        guard let deck = deck, let cards = cards, cards.isValidDeck() else { return }
		
		if update {
			RealmHelper.update(deck: deck, with: cards)
		} else {
			RealmHelper.add(deck: deck, with: cards)
		}
        
        NotificationCenter.default.post(name: Notification.Name(rawValue: "reload_decks"),
                                        object: deck)
{'fraction_non_alphanumeric': 0.10088235294117646, 'fraction_numerical': 0.0058823529411764705, 'mean_word_length': 2.452791878172589, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2258898', 'n_tokens_mistral': 1078, 'n_tokens_neox': 1005, 'n_words': 277}