Created
May 8, 2018 07:31
-
-
Save vivekguptaraw/a4b97f13de596215ae9f1beca6826d93 to your computer and use it in GitHub Desktop.
Data Manager for creating controls array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DataManager.swift | |
// DynamicUIDemo | |
// | |
// Created by Vivek Gupta on 20/04/18. | |
// Copyright © 2018 Vivek Gupta. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
class TableData{ | |
var dataSourceArray: [[ControlCreateModel]] = [] | |
var cellSize = CGSize.zero | |
init(json: [[ControlCreateModel]]) { | |
self.dataSourceArray = json | |
} | |
} | |
class DataManager{ | |
static let shared: DataManager = DataManager() | |
init() { | |
} | |
func readDataForCell(completion: (([TableData]) -> Void)? = nil){ | |
let data = jsonObject //Mark: jsonObject is declared in the dynamicJson.swift on gist | |
var tableArray = [TableData]() | |
if let result = data as? [[[String: Any]]]{ | |
for cellData in result{ | |
var arr: [ControlCreateModel] = [] | |
var cellSize = CGSize.zero | |
cellSize.width = UIScreen.main.bounds.width | |
for item in cellData{ | |
if (item as? [String: Any]) != nil{ | |
let model = ControlCreateModel(jsonDict: item) | |
if let childArray = model.childControlsArray{ | |
_ = childArray.map{ obj in | |
if obj.isKind(of: UIViewControlView.self){ | |
let vw = obj as! UIViewControlView | |
if vw.height > model.size.height{ | |
model.size.height += vw.height - model.size.height + (model.padding * 2) | |
}else if vw.height < model.size.height - (vw.padding * 2) { | |
model.size.height = vw.height + (model.padding * 2) | |
} | |
} | |
} | |
} | |
cellSize.height += model.size.height | |
arr.append(model) | |
} | |
} | |
let tableData = TableData(json: [arr]) | |
tableData.cellSize = cellSize | |
tableArray.append(tableData) | |
} | |
} | |
completion?(tableArray) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment