Courses • Unity Assets • PlayStore • Hosting • Channel • بالعربي

Ahmad Naser Navigation
  • Home
    • Apps
    • Arabic Website
    • Our Academy
    • Udemy Courses
    • Certification
    • Youtube
    • Code Snippets
  • About
    • About Us
    • Services
    • Apps
    • Clients
    • Projects
    • Professional Training
  • Courses
  • My Account
  • Shop
    • Go Shopping
    • Online Coaching
    • One-To-One Training Pricing
    • Hosting
    • Cart
    • Checkout
  • Blog
  • Contact
  • 0 Items$0.00
  • Home
    • Apps
    • Arabic Website
    • Our Academy
    • Udemy Courses
    • Certification
    • Youtube
    • Code Snippets
  • About
    • About Us
    • Services
    • Apps
    • Clients
    • Projects
    • Professional Training
  • Courses
  • My Account
  • Shop
    • Go Shopping
    • Online Coaching
    • One-To-One Training Pricing
    • Hosting
    • Cart
    • Checkout
  • Blog
  • Contact
  • 0 Items$0.00
Home Gist Eureka swift3 Dynamic form builder example part2
back to snippest | Comment | Share
Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//
//  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 {
    let screenHeight = UIScreen.main.bounds.height
    let screenWidth = UIScreen.main.bounds.width
    // Colors
    let lightGray = UIColor(red: 239/255, green: 239/255, blue: 244/255, alpha: 1)
    let darkGray = UIColor(red: 146/255, green: 146/255, blue: 146/255, alpha: 1)
    let blue = UIColor(red: 0, green: 122/255, blue:1, alpha:1)
    let green = UIColor(red: 0, green: 155/255, blue: 0, alpha: 1)
    let white = UIColor.white
    let black = UIColor.black
    let benuGreen = UIColor(red: 88/255, green: 166/255, blue: 24/255, alpha: 1)
 
    let skyBlue = UIColor(red: 162/255, green: 222/255, blue: 248/255, alpha: 1)
    let appTintColor =  UIColor(red: 88/255, green: 166/255, blue: 24/255, alpha: 1)
    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("اختر لغات البطاقات")
                <<< MultipleSelectorRow<String>("languages") {
                    $0.title = "Choosed langs"
                    $0.options = ["العربية","الفرنسية", "الألمانية",
                                  "الروسية","البرتغالية", "الأندونسية",
                                  "اليابانية","الفلبينية", "الانجليزية",]
                    $0.value = ["العربية", "الانجليزية" ]
                    }.onPresent { from, to in
                        
 
                    }.onChange {_ in
                        let values = self.form.values() as? [String : Any?]
                        var languages = self.getSelectedLanguages(languages: values?["languages"] as! Set<String>)
                        
                        print(languages[0])
                        print(languages.count)
                        
                        
            }
            
      
            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
                
            }
        }
        
        
 
         self.AddLanguagesTexts(toForm: self.form)
        
         self.AddBottomPart(toForm: self.form)
        
        
    }
    
    
    private func AddBottomPart(toForm form: Form) {
        // Error and Buttons section
        form   +++ Section() { section in
            section.tag = "error"
            var header = HeaderFooterView<UIView>(.class)
            header.height =  { 50 }
            
            let label = UILabel(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 50))
            label.textColor = UIColor.red
            label.textAlignment = .center
            
            header.onSetupView = { view, section in
                label.removeFromSuperview()
                //  view.addSubview(label)
                label.text = "error"
                //    label.isHidden = !self.showError
            }
            section.header = header
            }
            
            <<< ButtonRow() {
                $0.title = "#next"
                }.cellSetup() {cell, row in
                    cell.backgroundColor = self.appTintColor
                    cell.tintColor = UIColor.white
                    let bgColorView = UIView()
                    bgColorView.backgroundColor = self.appTintColor
                    cell.selectedBackgroundView = bgColorView
                }.onCellSelection {  cell, row in
                    //  self.nextButtonTapped()
        }
        
        
 
    }
    
    
    
    
      private func AddLanguagesTexts(toForm form: Form) {
 
        
    let itemNames = ["one","two","three"]
       var i = 1
        
        
         let values = form.values()
        
        if let v = values["item1"] {
 
 
            /*
            for itemName in itemNames{
              
             var r =   form.rowBy(tag: "item \(i)")
                r?.title = "changed"
                i = i + 1
 
            }
            */
 
        }else {
            
            
            
            
            
            
            // Eureka From Set-up
            form
                +++ Section("vvvv")
           i = 1
            for itemName in itemNames{
                
                form.last!
                    <<< TextRow("item \(i)") {
                        $0.title = itemName
                        $0.placeholder = itemName
                        //$0.hidden = true
                        i = i + 1
                }
            }
        
        }
        
        
        
        
        // 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)
    }
    
    
    
     func getSelectedLanguages(languages: Set<String>) -> [String] {
        var result = [String]()
        for lang in languages {
result.append(lang)
        }
        return result
    }
}
 
 
 
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)
    }
    
}