-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathSubviewBuilder.swift
53 lines (45 loc) · 1.72 KB
/
SubviewBuilder.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
//
// SubviewBuilder.swift
// Eject
//
// Created by Brian King on 10/19/16.
// Copyright © 2016 Brian King. All rights reserved.
//
import Foundation
struct SubviewConfiguration: CodeGenerator {
var objectIdentifier: String
var subview: Reference
var dependentIdentifiers: Set<String> {
return [objectIdentifier, subview.identifier]
}
func generateCode(in document: XIBDocument) throws -> String? {
let object = try document.lookupReference(for: objectIdentifier)
let variable = document.variable(for: object)
var representation = ""
let subviewVariable = document.variable(for: subview)
if object.definition.className == "UIStackView" {
representation.append("\(variable).addArrangedSubview(\(subviewVariable))")
}
else {
representation.append("\(variable).addSubview(\(subviewVariable))")
}
return representation
}
}
struct SubviewBuilder: Builder, ContainerBuilder {
func buildElement(attributes: inout [String: String], document: XIBDocument, parent: Reference?) throws -> Reference? {
guard let parent = parent else { throw XIBParser.Error.needParent }
return parent
}
func didAddChild(object: Reference, to parent: Reference, document: XIBDocument) throws {
guard !document.placeholders.contains(object.identifier) else {
// If the object is a placeholder, assume that it has already been added to the view hierarchy.
return
}
try document.addStatement(
for: object.identifier,
generator: SubviewConfiguration(objectIdentifier: parent.identifier, subview: object),
phase: .subviews
)
}
}