Skip to content

Commit

Permalink
Merge pull request #28 from ra1028/builder-closure
Browse files Browse the repository at this point in the history
Builder closure
  • Loading branch information
ra1028 authored Jul 29, 2019
2 parents c3ead29 + 80dc5e3 commit a6062ed
Show file tree
Hide file tree
Showing 47 changed files with 493 additions and 298 deletions.
10 changes: 6 additions & 4 deletions Examples/Example-iOS/Sources/Emoji/EmojiViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ final class EmojiViewController: UIViewController {
Section(
id: ID.emoji,
header: ViewNode(Header(title: "EMOJIS")),
cells: emojiCodes.enumerated().map { offset, code in
CellNode(EmojiLabel(code: code) { [weak self] in
self?.emojiCodes.remove(at: offset)
})
cells: emojiCodes
.enumerated()
.map { offset, code in
CellNode(EmojiLabel(code: code) { [weak self] in
self?.emojiCodes.remove(at: offset)
})
}
)
)
Expand Down
140 changes: 80 additions & 60 deletions Examples/Example-iOS/Sources/Form/FormViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,67 +59,87 @@ final class FormViewController: UIViewController {
}

func render() {
renderer.render(
Section(
id: ID.about,
header: ViewNode(Header(title: "ABOUT")),
cells: [
CellNode(FormTextField(title: "Name", text: state.name) { [weak self] text in
self?.state.name = text
}),
CellNode(FormLabel(title: "Gender", text: state.gender?.rawValue) { [weak self] in
self?.state.isOpenGenderPicker.toggle()
}),
!state.isOpenGenderPicker ? nil : CellNode(id: ID.genderPicker, FormTextPicker(texts: Gender.allTexts) { [weak self] text in
self?.state.gender = Gender(rawValue: text)
}),
CellNode(FormLabel(title: "Birthday", text: state.birthday?.longText) { [weak self] in
self?.state.isOpenBirthdayPicker.toggle()
}),
!state.isOpenBirthdayPicker ? nil : CellNode(id: ID.birthdayPicker, FormDatePicker(date: state.birthday ?? Date()) { [weak self] date in
self?.state.birthday = date
})
]
),
Section(
id: ID.note,
header: ViewNode(Header(title: "NOTE")),
cells: [
CellNode(id: ID.note, FormTextView(text: state.note) { [weak self] text in
self?.state.note = text

UIView.performWithoutAnimation {
self?.tableView.beginUpdates()
self?.tableView.endUpdates()
}
})
]
),
Section(
id: ID.detail,
header: ViewNode(Header(title: "DETAILS")),
cells: [
CellNode(FormSwitch(title: "Show Details", isOn: state.isOpenDetails) { [weak self] isOn in
self?.state.isOpenDetails = isOn
})
]
),
!state.isOpenDetails ? nil : Section(
id: ID.detailsInput,
header: ViewNode(Spacing(height: 12)),
cells: [
CellNode(FormTextField(title: "Location", text: state.location) { [weak self] text in
self?.state.location = text
}),
CellNode(FormTextField(title: "Email", text: state.email, keyboardType: .emailAddress) { [weak self] text in
self?.state.email = text
}),
CellNode(FormTextField(title: "Job", text: state.job) { [weak self] text in
self?.state.job = text
})
renderer.render { sections in
sections += [
Section(id: ID.about) { section in
section.header = ViewNode(Header(title: "ABOUT"))

section.cells += [
CellNode(FormTextField(title: "Name", text: state.name) { [weak self] text in
self?.state.name = text
}),
CellNode(FormLabel(title: "Gender", text: state.gender?.rawValue) { [weak self] in
self?.state.isOpenGenderPicker.toggle()
})
]

if state.isOpenGenderPicker {
section.cells += [
CellNode(id: ID.genderPicker, FormTextPicker(texts: Gender.allTexts) { [weak self] text in
self?.state.gender = Gender(rawValue: text)
})
]
}

section.cells += [
CellNode(FormLabel(title: "Birthday", text: state.birthday?.longText) { [weak self] in
self?.state.isOpenBirthdayPicker.toggle()
})
]

if state.isOpenBirthdayPicker {
section.cells += [
CellNode(id: ID.birthdayPicker, FormDatePicker(date: state.birthday ?? Date()) { [weak self] date in
self?.state.birthday = date
})
]
}
},
Section(
id: ID.note,
header: ViewNode(Header(title: "NOTE")),
cells: [
CellNode(id: ID.note, FormTextView(text: state.note) { [weak self] text in
self?.state.note = text

UIView.performWithoutAnimation {
self?.tableView.beginUpdates()
self?.tableView.endUpdates()
}
})
]
),
Section(
id: ID.detail,
header: ViewNode(Header(title: "DETAILS")),
cells: [
CellNode(FormSwitch(title: "Show Details", isOn: state.isOpenDetails) { [weak self] isOn in
self?.state.isOpenDetails = isOn
})
]
)
]

if state.isOpenDetails {
sections += [
Section(
id: ID.detailsInput,
header: ViewNode(Spacing(height: 12)),
cells: [
CellNode(FormTextField(title: "Location", text: state.location) { [weak self] text in
self?.state.location = text
}),
CellNode(FormTextField(title: "Email", text: state.email, keyboardType: .emailAddress) { [weak self] text in
self?.state.email = text
}),
CellNode(FormTextField(title: "Job", text: state.job) { [weak self] text in
self?.state.job = text
})
]
)
]
)
)
}
}
}

@objc func keyboardWillChangeFrame(notification: Notification) {
Expand Down
42 changes: 18 additions & 24 deletions Examples/Example-iOS/Sources/Hello/HelloViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,27 @@ final class HelloViewController: UIViewController {
}

func render() {
if !isToggled {
renderer.render(
Section(
id: ID.greet,
header: ViewNode(Header(title: "GREET")),
cells: [
renderer.render(
Section(id: ID.greet) { section in
section.header = ViewNode(Header(title: "GREET"))

if isToggled {
section.cells = [
CellNode(HelloMessage(name: "Jules")),
CellNode(HelloMessage(name: "Vincent"))
]
}
else {
section.cells = [
CellNode(HelloMessage(name: "Vincent")),
CellNode(HelloMessage(name: "Jules")),
CellNode(HelloMessage(name: "Butch"))
],
footer: ViewNode(Footer(text: "💡 Tap anywhere"))
)
)
}
else {
renderer.render(
Section(
id: ID.greet,
header: ViewNode(Header(title: "GREET")),
cells: [
CellNode(HelloMessage(name: "Jules")),
CellNode(HelloMessage(name: "Vincent"))
],
footer: ViewNode(Footer(text: "💡 Tap anywhere"))
)
)
}
]
}

section.footer = ViewNode(Footer(text: "💡 Tap anywhere"))
}
)
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
Expand Down
27 changes: 10 additions & 17 deletions Examples/Example-iOS/Sources/Pangram/PangramViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,22 @@ final class PangramViewController: UIViewController {
}

func render() {
if isSorted {
renderer.render(
"ABC DEF GHI JKL MNO PQR STU VWY XZ".split(separator: " ").enumerated().map { offset, word in
renderer.render { sections in
let pangram = isSorted
? "ABC DEF GHI JKL MNO PQR STU VWY XZ"
: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"

sections = pangram
.split(separator: " ")
.enumerated()
.map { offset, word in
Section(
id: offset,
cells: word.map { text in
CellNode(PangramLabel(text: String(text)))
}
)
}
)
}
else {
renderer.render(
"THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG".split(separator: " ").enumerated().map { offset, word in
Section(
id: offset,
cells: word.map { text in
CellNode(PangramLabel(text: String(text)))
}
)
}
)
}
}
}

Expand Down
26 changes: 13 additions & 13 deletions Examples/Example-iOS/Sources/Todo/TodoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ final class TodoViewController: UIViewController, UITextViewDelegate {

func render() {
renderer.render(
Section(
id: ID.task,
header: state.todos.isEmpty
Section(id: ID.task) { section in
section.header = state.todos.isEmpty
? ViewNode(TodoEmpty())
: ViewNode(Header(title: "TASKS")),
cells: state.todos.enumerated().map { offset, todo in
: ViewNode(Header(title: "TASKS"))

section.cells = state.todos.enumerated().map { offset, todo in
CellNode(TodoText(todo: todo, isCompleted: false) { [weak self] event in
switch event {
case .toggleCompleted:
Expand All @@ -69,13 +69,13 @@ final class TodoViewController: UIViewController, UITextViewDelegate {
}
})
}
),
Section(
id: ID.completed,
header: state.completed.isEmpty
? nil
: ViewNode(Header(title: "COMPLETED")),
cells: state.completed.enumerated().map { offset, todo in
},
Section(id: ID.completed) { section in
if !state.completed.isEmpty {
section.header = ViewNode(Header(title: "COMPLETED"))
}

section.cells = state.completed.enumerated().map { offset, todo in
CellNode(TodoText(todo: todo, isCompleted: true) { [weak self] event in
switch event {
case .toggleCompleted:
Expand All @@ -87,7 +87,7 @@ final class TodoViewController: UIViewController, UITextViewDelegate {
}
})
}
)
}
)
}

Expand Down
Loading

0 comments on commit a6062ed

Please sign in to comment.