Ahmad Naser Turnkey Ecosystem Ahmad Naser
February 10, 2017

Eureka swift3 Dynamic form builder example part1

Published in the Ahmad Naser ecosystem. Estimated reading time: 3 minutes.

In swift three the current valid versoin for Eureka is 2.0.0-beta.1, you could use cocoapods in order to use the library in your ios10 projects with the following command

pod 'Eureka', "~> 2.0.0-beta.1"

 

Here is an example of the below script
Eureka swift3

//
//  OrdersStepOneVC.swift
//  iOSMindizorApp
//
//  Created by AHMAD NASER on 2/10/17.
//  Copyright © 2017 Ahmad Naser Turnkey Solutions LLC. All rights reserved.
//

import UIKit
import Eureka
import Alamofire
import SwiftSpinner

class OrdersStepOneVC: FormViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        Section(){ section in
            section.header = {
                var header = HeaderFooterView<UIView>(.callback({
                    let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
                    view.backgroundColor = .red
                    return view
                }))
                header.height = { 100 }
                return header
            }()
        }
        
        form +++ Section("Section1")
            <<< TextRow(){ row in
                row.title = "Text Row"
                row.placeholder = "Enter text here"
            }
            
            
            <<< SegmentedRow<String>("gender"){
                $0.options = ["male", "female"]
                $0.title = "m"
                $0.value = "f"
                }.onChange{ row in
              print(row.value)
            }
            
        
            <<< PhoneRow(){
                $0.title = "Phone Row"
                $0.placeholder = "And numbers here"
            }
            +++ Section("Section2")
            <<< DateRow(){
                $0.title = "Date Row"
                $0.value = NSDate(timeIntervalSinceReferenceDate: 0) as Date
            }
           <<< SwitchRow("switchRowTag"){
                        $0.title = "Show message"
                        }
           <<< LabelRow(){
               $0.hidden = Condition.function(["switchRowTag"], { form in
                                return !((form.rowBy(tag: "switchRowTag") as? SwitchRow)?.value ?? false)
                            })
                            $0.title = "Switch is on!"
            
            
            form +++ Section("Multiple Choices")
                <<< MultipleSelectorRow<String>() {
                    $0.title = "Favourite Foods"
                    $0.options = ["🍝", "🍟", "🍕", "🍚"]
                    $0.value = ["🍝", "🍕" ]
            }
            
            
            form +++ Section("Languages")
                <<< MultipleSelectorRow<String>() {
                    $0.title = "Choosed langs"
                    $0.options = ["dn","er", "ec", "en", "ar"]
                    $0.value = ["dn", "er" ]
            }
            
            
            form +++ Section("Login Form")
                <<< TextRow() { $0.placeholder = "Username" }
                <<< PasswordRow() { $0.placeholder = "Password" }
                <<< ButtonRow() {
                    $0.title = "Login"
                    $0.onCellSelection { cell, row in
                  self.presentAlert(message: "Will login")
                    }
            }
            
            
        }
        
        addFormWithToggle(toForm: form)
        
       let continents = ["Africa", "Antarctica", "Asia", "Australia", "Europe", "North America", "South America"]
        form +++ SelectableSection<ListCheckRow<String>>("Where do you live", selectionType: .multipleSelection)
        
     
        for option in continents {
            form.last! <<< ListCheckRow<String>(option){ listRow in
                listRow.title = option
                listRow.selectableValue = option
                listRow.value = nil
                
            }
        }
        
        
        let itemNames = ["one","two","three"]
        
        // Eureka From Set-up
        form
            +++ Section("Select item values")
        
        for itemName in itemNames{
            
            form.last!
                <<< TextRow() {
                    $0.title = itemName
                    $0.placeholder = itemName

            }
        }
        
        
        // Eureka From Set-up
        form
            +++ Section("Select item values")
        
        for itemName in itemNames{
            
            form.last!
                
                <<< StepperRow() {
                    $0.tag = itemName
                    $0.title = itemName
                    $0.value = 0
            }
        }
        
        
        
        
    }
    
    private func addFormWithToggle(toForm form: Form) {
        let switchRowTag = "switch_row"
        let setStartDateRowTag = "set_start_date_time"
        let setEndDateRowTag = "set_end_date_time"
        
        let switchRow = SwitchRow(switchRowTag) {
            $0.title = "All Day Event"
            $0.value = true
        }

        let hiddenCondition = Condition.function([switchRowTag]) { form in
            guard let switchCell = form.rowBy(tag:switchRowTag) as? SwitchRow else {
                return false
            }
            
            return switchCell.value == true
        }
        
        let s = Section()
        s.append(switchRow)
        s.append(DateTimeInlineRow(setStartDateRowTag) {
            $0.title = "Starts"
            $0.value =  NSDate.oneHourFromNow() as Date
            $0.hidden = hiddenCondition
        })
        s.append(DateTimeInlineRow(setEndDateRowTag) {
            $0.title = "Ends"
            $0.value =  NSDate.hoursFormNow(numberOfHours: 4) as Date
            $0.hidden = hiddenCondition
        })
        form.append(s)
    }
    private func presentAlert(title: String? = nil, message: String) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alertController, animated: true, completion: nil)
    }
}
extension NSDate {
    
    static func oneHourFromNow() -> NSDate {
        return NSDate.hoursFormNow(numberOfHours: 1)
    }
    
    static func hoursFormNow(numberOfHours: Int) -> NSDate {
        let interval = Double(numberOfHours) * 60
        return NSDate().addingTimeInterval(interval)
    }
    
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *