<commit_before>//
//  GenericFormViewController.swift
//  belis-app
//
//  Created by <NAME> on 18.11.15.
//  Copyright © 2015 cismet. All rights reserved.
//

import Foundation
import SwiftForms

class GenericFormViewController: FormViewController {
    var saveHandler : (()->())?
    var cancelHandler : (()->())?
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Speichern", style: .plain, target: self, action: #selector(GenericFormViewController.save))
            let image=GlyphTools.sharedInstance().getGlyphedImage("icon-chevron-left", fontsize: 11, size: CGSize(width: 14, height: 14))
            self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain , target: self, action: #selector(GenericFormViewController.cancel))
        
    }
    
    
    func save() {
        self.dismiss(animated: true) { () -> Void in
            if let sh=self.saveHandler {
                sh()
            }
            else {
                //should not  happen >> log it
            }
        }
    }
    
    func cancel() {
        self.dismiss(animated: true) { () -> Void in
            if let ch=self.cancelHandler {
                ch()
            }
            else {
                //should not  happen >> log it
            }
        }
    }
    
}


<commit_msg>working on #47: introduce a preSaveCheck and preCancelCheck in the GenericFormViewConroller<commit_after>//
//  GenericFormViewController.swift
//  belis-app
//
//  Created by <NAME> on 18.11.15.
//  Copyright © 2015 cismet. All rights reserved.
//

import Foundation
import SwiftForms

class GenericFormViewController: FormViewController {
    var saveHandler : (()->())?
    var cancelHandler : (()->())?
    
    var preSaveCheck: ()->CheckResult={
        return CheckResult(passed: true)
    }

    var preCancelCheck: ()->CheckResult={
        return CheckResult(passed: true)
    }

    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Speichern", style: .plain, target: self, action: #selector(GenericFormViewController.save))
            let image=GlyphTools.sharedInstance().getGlyphedImage("icon-chevron-left", fontsize: 11, size: CGSize(width: 14, height: 14))
            self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain , target: self, action: #selector(GenericFormViewController.cancel))
    }
    
    func save() {
        let check=preSaveCheck()
        if (check.passed) {
            self.dismiss(animated: true) { () -> Void in
                if let sh=self.saveHandler {
                    sh()
                }
                else {
                    //should not  happen >> log it
                }
            }
        }
        else {
            let alert = UIAlertController(title: check.title, message: check.message, preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler:{ (ACTION :UIAlertAction)in
            }))
            present(alert, animated: true, completion: nil)
        }
    }
    
    func cancel() {
        self.dismiss(animated: true) { () -> Void in
            if let ch=self.cancelHandler {
                ch()
            }
            else {
                //should not  happen >> log it
            }
        }
    }
    
}

class CheckResult {
    var passed: Bool
    var message: String
    var title: String
    
    init(passed: Bool, title:String="", withMessage:String = ""){
        self.passed=passed
        self.title=title
        self.message=withMessage
    }
}

{'fraction_non_alphanumeric': 0.11161501637692114, 'fraction_numerical': 0.008566389518770472, 'mean_word_length': 2.037490436113236, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '920187', 'n_tokens_mistral': 1177, 'n_tokens_neox': 1105, 'n_words': 255}