176 lines
6.8 KiB
Swift
176 lines
6.8 KiB
Swift
//
|
|
// NotificationViewController.swift
|
|
// VDSSample
|
|
//
|
|
// Created by Nadigadda, Sumanth on 14/03/23.
|
|
//
|
|
|
|
import Foundation
|
|
import VDS
|
|
|
|
class NotificationViewController: BaseViewController<VDS.Notification> {
|
|
|
|
let label = Label()
|
|
let titleTextField = TextField()
|
|
let subTitleTextField = TextField()
|
|
let buttonGroupToggle = Toggle()
|
|
let firstButtonTextField = TextField()
|
|
let secondButtonTextField = TextField()
|
|
let hideCloseButtonToggle = Toggle()
|
|
|
|
let titleDefaultText = "This is title"
|
|
let subtitleDefaultText = "This is subtitle"
|
|
let firstButtonDefaultText = "Button 1"
|
|
let secondButtonDefaultText = "Button 2"
|
|
|
|
lazy var notificationTypePickerSelectorView = {
|
|
PickerSelectorView(title: "info",
|
|
picker: self.picker,
|
|
items: Notification.Style.allCases)
|
|
}()
|
|
|
|
var notification = Notification()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
stackView.insertArrangedSubview(notification, at: 0)
|
|
addContentTopView(view: component)
|
|
|
|
notification.title = titleDefaultText
|
|
notification.subTitle = subtitleDefaultText
|
|
|
|
component.title = titleDefaultText
|
|
component.subTitle = subtitleDefaultText
|
|
titleTextField.text = titleDefaultText
|
|
subTitleTextField.text = subtitleDefaultText
|
|
|
|
setupButtons(with: firstButtonDefaultText, secondButtonText: secondButtonDefaultText)
|
|
firstButtonTextField.text = firstButtonDefaultText
|
|
secondButtonTextField.text = secondButtonDefaultText
|
|
|
|
setupPicker()
|
|
}
|
|
|
|
override func setupForm() {
|
|
super.setupForm()
|
|
addFormRow(label: "Surface", view: surfacePickerSelectorView)
|
|
addFormRow(label: "Style", view: notificationTypePickerSelectorView)
|
|
addFormRow(label: "Title", view: titleTextField)
|
|
addFormRow(label: "SubTitle", view: subTitleTextField)
|
|
addFormRow(label: "Hide Button Group", view: buttonGroupToggle)
|
|
addFormRow(label: "Hide Close Button", view: hideCloseButtonToggle)
|
|
addFormRow(label: "Action", view: label)
|
|
addFormRow(label: "First Button Text", view: firstButtonTextField)
|
|
addFormRow(label: "Second Button Text", view: secondButtonTextField)
|
|
|
|
component.onCloseClick = { notification in
|
|
print("onCloseClick: \(notification.title)")
|
|
}
|
|
|
|
titleTextField.textPublisher.sink { [weak self] newString in
|
|
guard let self else { return }
|
|
self.component.title = newString
|
|
self.notification.title = newString
|
|
}.store(in: &subscribers)
|
|
|
|
subTitleTextField.textPublisher.sink { [weak self] newString in
|
|
guard let self else { return }
|
|
self.component.subTitle = newString
|
|
self.notification.subTitle = newString
|
|
}.store(in: &subscribers)
|
|
|
|
buttonGroupToggle.onChange = { [weak self] toggle in
|
|
guard let self else { return }
|
|
if toggle.isOn {
|
|
self.component.primaryButtonModel = nil
|
|
self.component.secondaryButtonModel = nil
|
|
self.notification.primaryButtonModel = nil
|
|
self.notification.secondaryButtonModel = nil
|
|
|
|
self.label.text = ""
|
|
} else {
|
|
self.setupButtons(with: self.firstButtonDefaultText, secondButtonText: self.secondButtonDefaultText)
|
|
}
|
|
}
|
|
|
|
firstButtonTextField.textPublisher.sink { [weak self] newString in
|
|
guard let self else { return }
|
|
if newString.isEmpty {
|
|
self.component.primaryButtonModel = nil
|
|
self.component.secondaryButtonModel = nil
|
|
} else {
|
|
self.setupButtons(with: newString, secondButtonText: self.secondButtonTextField.text)
|
|
}
|
|
|
|
}.store(in: &subscribers)
|
|
|
|
secondButtonTextField.textPublisher.sink { [weak self] newString in
|
|
guard let self else { return }
|
|
if !(self.firstButtonTextField.text?.isEmpty ?? true){
|
|
self.setupButtons(secondButtonText: newString)
|
|
}
|
|
}.store(in: &subscribers)
|
|
|
|
hideCloseButtonToggle.onChange = { [weak self] toggle in
|
|
guard let self else { return }
|
|
self.component.hideCloseButton = toggle.isOn
|
|
self.notification.hideCloseButton = toggle.isOn
|
|
}
|
|
|
|
}
|
|
|
|
func setupPicker() {
|
|
surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
guard let self else { return }
|
|
self.component.surface = item
|
|
self.notification.surface = item
|
|
self.contentTopView.backgroundColor = item.color
|
|
}
|
|
|
|
notificationTypePickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
guard let self else { return }
|
|
self.component.style = item
|
|
self.notification.style = item
|
|
}
|
|
}
|
|
|
|
func setupButtons(with firstButtonText: String? = nil, secondButtonText: String? = nil) {
|
|
if let firstButtonText {
|
|
component.primaryButtonModel = .init(text: firstButtonText, onClick: { [weak self] button in
|
|
self?.label.text = "\(button.text!) button click"
|
|
})
|
|
notification.primaryButtonModel = .init(text: firstButtonText, onClick: { [weak self] button in
|
|
self?.label.text = "\(button.text!) button click"
|
|
})
|
|
}
|
|
|
|
if let secondButtonText {
|
|
component.secondaryButtonModel = .init(text: secondButtonText, onClick: { [weak self] button in
|
|
self?.label.text = "\(button.text!) button click"
|
|
})
|
|
notification.secondaryButtonModel = .init(text: secondButtonText, onClick: { [weak self] button in
|
|
self?.label.text = "\(button.text!) button click"
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
extension NotificationViewController: ComponentSampleable {
|
|
static func makeSample() -> ComponentSample {
|
|
let component = Self.makeComponent()
|
|
|
|
let titleDefaultText = "This is title"
|
|
let subtitleDefaultText = "This is subtitle"
|
|
let firstButtonDefaultText = "Button 1"
|
|
let secondButtonDefaultText = "Button 2"
|
|
|
|
component.title = titleDefaultText
|
|
component.subTitle = subtitleDefaultText
|
|
component.primaryButtonModel = .init(text: firstButtonDefaultText, onClick: {b in print("\(b.text!) click")})
|
|
component.secondaryButtonModel = .init(text: secondButtonDefaultText, onClick: {b in print("\(b.text!) click")})
|
|
component.onCloseClick = {_ in print("notification on close click")}
|
|
return ComponentSample(component: component)
|
|
}
|
|
}
|