<commit_before>// MIT license. Copyright (c) 2014 SwiftyFORM. All rights reserved.
import UIKit

protocol WillPopCommandProtocol {
	func execute(context: ViewControllerFormItemPopContext)
}


class WillPopCustomViewController: WillPopCommandProtocol {
	let object: AnyObject
	init(object: AnyObject) {
		self.object = object
	}
	
	func execute(context: ViewControllerFormItemPopContext) {
		if let vc = object as? ViewControllerFormItem {
			vc.willPopViewController?(context)
			return
		}
	}
}

class WillPopOptionViewController: WillPopCommandProtocol {
	let object: ViewControllerFormItem
	init(object: ViewControllerFormItem) {
		self.object = object
	}
	
	func execute(context: ViewControllerFormItemPopContext) {
		object.willPopViewController?(context)
	}
}


struct PopulateTableViewModel {
	var viewController: UIViewController
	var toolbarMode: ToolbarMode
}



class PopulateTableView: FormItemVisitor {
	let model: PopulateTableViewModel
	
	var cells: TableViewCellArray = TableViewCellArray.createEmpty()
	var sections = [TableViewSection]()
	var headerBlock: TableViewSectionPart.CreateBlock?
	
	init(model: PopulateTableViewModel) {
		self.model = model
	}
	
	func closeSection(footerBlock: TableViewSectionPart.CreateBlock) {
		var headerBlock: Void -> TableViewSectionPart = {
			return TableViewSectionPart.None
		}
		if let block = self.headerBlock {
			headerBlock = block
		}
		
		cells.reloadVisibleItems()
		let section = TableViewSection(cells: cells, headerBlock: headerBlock, footerBlock: footerBlock)
		sections.append(section)

		cells = TableViewCellArray.createEmpty()
		self.headerBlock = nil
	}
	
	
	// MARK: AttributedTextFormItem
	
	func visit(object: AttributedTextFormItem) {
		var model = AttributedTextCellModel()
		model.titleAttributedText = object.title
		model.valueAttributedText = object.value
		let cell = AttributedTextCell(model: model)
		cells.append(cell)
		
		weak var weakCell = cell
		object.syncCellWithValue = { (value: NSAttributedString?) in
			SwiftyFormLog("sync value \(value)")
			if let c = weakCell {
				var m = AttributedTextCellModel()
				m.titleAttributedText = c.model.titleAttributedText
				m.valueAttributedText = value
				c.model = m
				c.loadWithModel(m)
			}
		}
	}

	
	// MARK: ButtonFormItem
	
	func visit(object: ButtonFormItem) {
		var model = ButtonCellModel()
		model.title = object.title
		model.action = object.action
		let cell = ButtonCell(model: model)
		cells.append(cell)
	}
	

	// MARK: CustomFormItem
	
	func visit(object: CustomFormItem) {
		let context = CustomFormItem.Context(
			viewController: model.viewController
		)
		do {
			let cell = try object.createCell(context)
			cells.append(cell)
		} catch {
			print("ERROR: Could not create cell for custom form item: \(error)")

			var model = StaticTextCellModel()
			model.title = "CustomFormItem"
			model.value = "Exception"
			let cell = StaticTextCell(model: model)
			cells.append(cell)
		}
	}
	
	
	// MARK: DatePickerFormItem
	
	func visit(object: DatePickerFormItem) {
		var model = DatePickerCellModel()
		model.title = object.title
		model.toolbarMode = self.model.toolbarMode
		model.datePickerMode = mapDatePickerMode(object.datePickerMode)
		model.locale = object.locale
		model.minimumDate = object.minimumDate
		model.maximumDate = object.maximumDate
		
		weak var weakObject = object
		model.valueDidChange = { (date: NSDate) in
			SwiftyFormLog("value did change \(date)")
			weakObject?.innerValue = date
		}
		
		switch object.behavior {
		case .Collapsed, .Expanded:
			model.expandCollapseWhenSelectingRow = true
		case .ExpandedAlways:
			model.expandCollapseWhenSelectingRow = false
		}
		
		let cell = DatePickerCell(model: model)
		let cellExpanded = DatePickerCellExpanded()
		
		cells.append(cell)
		switch object.behavior {
		case .Collapsed:
			cells.appendHidden(cellExpanded)
		case .Expanded, .ExpandedAlways:<commit_msg>fixed warning<commit_after>// MIT license. Copyright (c) 2014 SwiftyFORM. All rights reserved.
import UIKit

protocol WillPopCommandProtocol {
	func execute(context: ViewControllerFormItemPopContext)
}


class WillPopCustomViewController: WillPopCommandProtocol {
	let object: AnyObject
	init(object: AnyObject) {
		self.object = object
	}
	
	func execute(context: ViewControllerFormItemPopContext) {
		if let vc = object as? ViewControllerFormItem {
			vc.willPopViewController?(context)
			return
		}
	}
}

class WillPopOptionViewController: WillPopCommandProtocol {
	let object: ViewControllerFormItem
	init(object: ViewControllerFormItem) {
		self.object = object
	}
	
	func execute(context: ViewControllerFormItemPopContext) {
		object.willPopViewController?(context)
	}
}


struct PopulateTableViewModel {
	var viewController: UIViewController
	var toolbarMode: ToolbarMode
}



class PopulateTableView: FormItemVisitor {
	let model: PopulateTableViewModel
	
	var cells: TableViewCellArray = TableViewCellArray.createEmpty()
	var sections = [TableViewSection]()
	var headerBlock: TableViewSectionPart.CreateBlock?
	
	init(model: PopulateTableViewModel) {
		self.model = model
	}
	
	func closeSection(footerBlock: TableViewSectionPart.CreateBlock) {
		var headerBlock: Void -> TableViewSectionPart = {
			return TableViewSectionPart.None
		}
		if let block = self.headerBlock {
			headerBlock = block
		}
		
		cells.reloadVisibleItems()
		let section = TableViewSection(cells: cells, headerBlock: headerBlock, footerBlock: footerBlock)
		sections.append(section)

		cells = TableViewCellArray.createEmpty()
		self.headerBlock = nil
	}
	
	
	// MARK: AttributedTextFormItem
	
	func visit(object: AttributedTextFormItem) {
		var model = AttributedTextCellModel()
		model.titleAttributedText = object.title
		model.valueAttributedText = object.value
		let cell = AttributedTextCell(model: model)
		cells.append(cell)
		
		weak var weakCell = cell
		object.syncCellWithValue = { (value: NSAttributedString?) in
			SwiftyFormLog("sync value \(value)")
			if let c = weakCell {
				var m = AttributedTextCellModel()
				m.titleAttributedText = c.model.titleAttributedText
				m.valueAttributedText = value
				c.model = m
				c.loadWithModel(m)
			}
		}
	}

	
	// MARK: ButtonFormItem
	
	func visit(object: ButtonFormItem) {
		var model = ButtonCellModel()
		model.title = object.title
		model.action = object.action
		let cell = ButtonCell(model: model)
		cells.append(cell)
	}
	

	// MARK: CustomFormItem
	
	func visit(object: CustomFormItem) {
		let context = CustomFormItem.Context(
			viewController: model.viewController
		)
		do {
			let cell = try object.createCell(context)
			cells.append(cell)
		} catch {
			print("ERROR: Could not create cell for custom form item: \(error)")

			var model = StaticTextCellModel()
			model.title = "CustomFormItem"
			model.value = "Exception"
			let cell = StaticTextCell(model: model)
			cells.append(cell)
		}
	}
	
	
	// MARK: DatePickerFormItem
	
	func visit(object: DatePickerFormItem) {
		let model = DatePickerCellModel()
		model.title = object.title
		model.toolbarMode = self.model.toolbarMode
		model.datePickerMode = mapDatePickerMode(object.datePickerMode)
		model.locale = object.locale
		model.minimumDate = object.minimumDate
		model.maximumDate = object.maximumDate
		
		weak var weakObject = object
		model.valueDidChange = { (date: NSDate) in
			SwiftyFormLog("value did change \(date)")
			weakObject?.innerValue = date
		}
		
		switch object.behavior {
		case .Collapsed, .Expanded:
			model.expandCollapseWhenSelectingRow = true
		case .ExpandedAlways:
			model.expandCollapseWhenSelectingRow = false
		}
		
		let cell = DatePickerCell(model: model)
		let cellExpanded = DatePickerCellExpanded()
		
		cells.append(cell)
		switch object.behavior {
		case .Collapsed:
			cells.appendHidden(cellExpanded)
		case .Expanded, .ExpandedAlways:
{'fraction_non_alphanumeric': 0.08715713919507818, 'fraction_numerical': 0.0010253781081773904, 'mean_word_length': 4.329918032786885, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4421675', 'n_tokens_mistral': 2744, 'n_tokens_neox': 2325, 'n_words': 536}