-
Notifications
You must be signed in to change notification settings - Fork 12
Many Components Sample
Maximilian Mackh edited this page Jul 1, 2020
·
1 revision
import UIKit
import BaseComponents
struct Post: Codable {
var userId: Int?
var id: Int?
var title: String?
var body: String?
}
public class PostCell: UITableViewCell {
public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
textLabel?.lines(0)
.size(.body, .bold)
detailTextLabel?.lines(3)
.size(.subheadline)
.color(.text, .black)
accessoryType = .disclosureIndicator
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func bindObject(_ obj: AnyObject) {
let post = obj as! Post
let title = post.title!
let subtitle = "Posted by News"
let headline = title.appendingFormat("\n%@", subtitle)
let range = (headline as NSString).range(of: subtitle)
let attributedString = NSMutableAttributedString(string: headline)
attributedString.addAttribute(.font, value: UIFont.size(.footnote), range: range)
attributedString.addAttribute(.foregroundColor, value: UIColor.lightGray, range: range)
textLabel?.attributedText = attributedString
detailTextLabel?.text = post.body?.replacingOccurrences(of: "\n", with: "")
}
public override func layoutSubviews() {
super.layoutSubviews()
}
}
public class KeybardViewController: UIViewController {
lazy var dataRender: DataRender = {
let configuration = DataRenderConfiguration(cellClass: PostCell.self)
let dataRender = DataRender(configuration: configuration)
dataRender.rowHeight = UITableView.automaticDimension
dataRender.clipsToBounds = true
return dataRender
}()
override public func viewDidLoad() {
super.viewDidLoad()
title = "Welcome to BaseComponents"
view.backgroundColor = UIColor.white
let splitView = view.addSplitView { (splitView) in
splitView.insertSafeAreaInsetsPadding(form: self.view, paddingDirection: .top)
let segmentedControl = UISegmentedControl(items: ["Hello","World","Swift"])
segmentedControl.selectedSegmentIndex = 0
segmentedControl.addAction(for: .valueChanged) { (segmentedControl) in
let item = segmentedControl.titleForSegment(at: segmentedControl.selectedSegmentIndex)!
print(item)
}
splitView.addSubview(segmentedControl, layoutType: .fixed, value: 44.0, edgeInsets: UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10))
dataRender.onRefresh { [weak self] (render) in
let request = NetFetchRequest(urlString: "https://jsonplaceholder.typicode.com/posts") { (response) in
self?.dataRender.refreshing = false
self?.view.showProgressView(false)
if let posts = response.bind([Post].self) {
NotificationView.show(.success, in: self?.dataRender, for: 2, message: "Updated to the latest News", position: .bottom)
self?.dataRender.renderArray(posts)
}
else {
NotificationView.show(.error, in: self?.view, for: 2, message: "Check your Network Connection")
}
}
NetFetch.fetch(request)
}
dataRender.onSelect { (itemProperites) in
let post = itemProperites.object as! Post
print("Read:",post.body!)
}
splitView.addSubview(dataRender, layoutType: .percentage, value: 100)
dataRender.refreshing = true
splitView.addSubview(UIView().color(.background, .init(white: 0.89, alpha: 1)), layoutType: .fixed, value: 0.5)
splitView.addSplitView(configurationHandler: { (splitView) in
splitView.direction = .horizontal
let valueLabel = PerformLabel("0.00")
.size(.body, [.monoSpaceDigit,.bold])
.lines(0)
let slider = UISlider()
.addAction(for: .valueChanged) { (slider) in
valueLabel.text = NSString(format: "%.02f", slider.value) as String
}
splitView.addSubview(slider, layoutType: .percentage, value: 100, edgeInsets: UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 0))
splitView.addSubview(valueLabel, layoutType: .automatic, edgeInsets: UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15))
}) { (parentRect) -> SplitViewLayoutInstruction in
return .init(layoutType: .fixed, value: 44)
}
splitView.addSubview(UIView().color(.background, .init(white: 0.89, alpha: 1)), layoutType: .fixed, value: 0.5)
let textField = UITextField(placeholder: "Enter a new Title")
.align(.center)
.size(.body)
.addAction(for: .editingChanged) { (textField) in
print(textField.text!)
}
.addAction(for: .editingDidEnd, { (textField) in
self.title = textField.text
})
.shouldReturn { (textField) -> (Bool) in
textField.resignFirstResponder()
return true
}
splitView.addSubview(textField, layoutType: .fixed, value: 84.0)
let paddingView = UIView()
splitView.addSubview(paddingView) { (parentRect) -> SplitViewLayoutInstruction in
var bottomInset: CGFloat = 0.0;
if #available(iOS 11.0, *) {
bottomInset = self.view.safeAreaInsets.bottom
}
if KeyboardManager.visibility == .visible {
bottomInset = 0
}
return SplitViewLayoutInstruction(layoutType: .fixed, value: bottomInset)
}
}
splitView.direction = .vertical
KeyboardManager.manage(rootView: view, resizableChildSplitView: splitView)
view.showProgressView(true, type: .appleStyle)
}
}