Skip to content

Commit

Permalink
[RadioButton#853] Add demo controllers for testing radio and checkbox…
Browse files Browse the repository at this point in the history
… together
  • Loading branch information
aycil-alican committed Mar 22, 2024
1 parent 0303043 commit d1c3fd0
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//
// RadioCheckboxUIViewController.swift
// SparkDemo
//
// Created by alican.aycil on 22.03.24.
// Copyright © 2024 Adevinta. All rights reserved.
//

import UIKit
import SparkCore
import Combine

final class RadioCheckboxUIViewController: UIViewController {

private let theme = SparkThemePublisher.shared.theme
private var cancellables = Set<AnyCancellable>()

private lazy var alignment: CheckboxUIView = {
let view = CheckboxUIView(
theme: self.theme,
text: "Alignment: Click to change postion.",
checkedImage: DemoIconography.shared.checkmark.uiImage,
isEnabled: true,
selectionState: .selected,
alignment: .left
)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

private lazy var lineView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.lightGray
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

private lazy var checkboxGroup: CheckboxGroupUIView = {
var items = [
CheckboxGroupItemDefault(title: "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", id: "1", selectionState: .selected, isEnabled: true),
CheckboxGroupItemDefault(title: "Hello World", id: "2", selectionState: .selected, isEnabled: true)
]
let view = CheckboxGroupUIView(
checkedImage: DemoIconography.shared.checkmark.uiImage,
items: items,
alignment: alignment.isSelected ? CheckboxAlignment.left : CheckboxAlignment.right,
theme: self.theme,
intent: .main,
accessibilityIdentifierPrefix: "Checkbox"
)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

private lazy var radioButtonGroup: RadioButtonUIGroupView = {
var items = [
RadioButtonUIItem(id: 0, label: "Hello World"),
RadioButtonUIItem(id: 1, label: "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.")
]
let view = RadioButtonUIGroupView(
theme: self.theme,
intent: .main,
selectedID: 0,
items: items,
labelAlignment: alignment.isSelected ? RadioButtonLabelAlignment.trailing : RadioButtonLabelAlignment.leading,
groupLayout: .vertical
)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Radio Checkbox UIKit"

self.setupView()
self.subscribe()
}

private func setupView() {
self.view.backgroundColor = UIColor.systemBackground

self.view.addSubview(alignment)
self.view.addSubview(lineView)
self.view.addSubview(checkboxGroup)
self.view.addSubview(radioButtonGroup)

NSLayoutConstraint.activate([

self.alignment.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 16),
self.alignment.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 16),
self.alignment.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -16),

self.lineView.topAnchor.constraint(equalTo: self.alignment.bottomAnchor, constant: 32),
self.lineView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 16),
self.lineView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -16),
self.lineView.heightAnchor.constraint(equalToConstant: 1),

self.checkboxGroup.topAnchor.constraint(equalTo: self.lineView.bottomAnchor, constant: 32),
self.checkboxGroup.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 16),
self.checkboxGroup.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -16),

self.radioButtonGroup.topAnchor.constraint(equalTo: self.checkboxGroup.bottomAnchor, constant: 16),
self.radioButtonGroup.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 16),
self.radioButtonGroup.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -16)
])
}

private func subscribe() {
self.alignment.publisher.subscribe(in: &self.cancellables) { [weak self] isSelected in
self?.radioButtonGroup.labelAlignment = isSelected == .selected ? RadioButtonLabelAlignment.trailing : RadioButtonLabelAlignment.leading
self?.checkboxGroup.alignment = isSelected == .selected ? CheckboxAlignment.left : CheckboxAlignment.right
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// RadioCheckboxView.swift
// SparkDemo
//
// Created by alican.aycil on 22.03.24.
// Copyright © 2024 Adevinta. All rights reserved.
//

import Spark
import SparkCore
import SwiftUI

struct RadioCheckboxView: View {

// MARK: - Properties
@State private var theme: Theme = SparkThemePublisher.shared.theme
@State private var alignment: CheckboxAlignment = .left
@State private var selectedIcon = CheckboxListView.Icons.checkedImage
@State private var selectedID: Int? = 0
@State private var items: [any CheckboxGroupItemProtocol] = [
CheckboxGroupItemDefault(title: "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", id: "1", selectionState: .selected, isEnabled: true),
CheckboxGroupItemDefault(title: "Hello World", id: "2", selectionState: .selected, isEnabled: true)
]
@State private var radioGroupItems: [RadioButtonItem<Int>] = [
RadioButtonItem(id: 0, label: "Hello World"),
RadioButtonItem(id: 1, label: "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.")
]


// MARK: - View
var body: some View {
Component(
name: "Radio Checkbox SwiftUI",
configuration: {

EnumSelector(
title: "Alignment",
dialogTitle: "Select a Alignment",
values: CheckboxAlignment.allCases,
value: self.$alignment
)
},
integration: {
VStack(alignment: .leading) {
CheckboxGroupView(
checkedImage: self.selectedIcon.image,
items: self.$items,
layout: .vertical,
alignment: self.alignment,
theme: self.theme,
intent: .main,
accessibilityIdentifierPrefix: "checkbox-group"
)

RadioButtonGroupView(
theme: self.theme,
intent: .main,
selectedID: self.$selectedID,
items: self.radioGroupItems,
labelAlignment: self.alignment == .left ? .trailing : .leading,
groupLayout: .vertical
)
}
}
)
}
}
83 changes: 56 additions & 27 deletions spark/Demo/Classes/View/ListView/ListComponentsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ final class ListComponentsViewController: UICollectionViewController {
private func setupData() {
/// CollectionView append sections and items
var snapShot = SnapShot()
snapShot.appendSections([.all])
snapShot.appendItems(uiComponentAllCases.map{ $0.rawValue }, toSection: .all)
snapShot.appendSections([.list, .random])
snapShot.appendItems(uiComponentAllCases.map{ $0.rawValue }, toSection: .list)
snapShot.appendItems(Row.allCases.map{ $0.name }, toSection: .random)
collectionViewDataSource.apply(snapShot)
}
}
Expand All @@ -76,58 +77,86 @@ extension ListComponentsViewController {

var viewController: UIViewController!

switch uiComponentAllCases[indexPath.row] {
switch Section.allCases[indexPath.section] {
case .list:
viewController = self.listSectionControllers(index: indexPath.row)
case .random:
viewController = self.randomSectionControllers(index: indexPath.row)
}

guard viewController != nil else { return }
self.navigationController?.pushViewController(viewController, animated: true)
}

private func listSectionControllers(index: Int) -> UIViewController? {

switch uiComponentAllCases[index] {
case .badge:
viewController = ListViewController<BadgeCell, BadgeConfiguration>()
return ListViewController<BadgeCell, BadgeConfiguration>()
case .button:
viewController = ListViewController<ButtonCell, ButtonConfiguration>()
return ListViewController<ButtonCell, ButtonConfiguration>()
case .checkbox:
viewController = ListViewController<CheckboxCell, CheckboxConfiguration>()
return ListViewController<CheckboxCell, CheckboxConfiguration>()
case .checkboxGroup:
viewController = ListViewController<CheckboxGroupCell, CheckboxGroupConfiguration>()
return ListViewController<CheckboxGroupCell, CheckboxGroupConfiguration>()
case .chip:
viewController = ListViewController<ChipCell, ChipConfiguration>()
return ListViewController<ChipCell, ChipConfiguration>()
case .icon:
viewController = ListViewController<IconCell, IconConfiguration>()
return ListViewController<IconCell, IconConfiguration>()
case .progressBarIndeterminate:
viewController = ListViewController<ProgressBarIndeterminateCell, ProgressBarIndeterminateConfiguration>()
return ListViewController<ProgressBarIndeterminateCell, ProgressBarIndeterminateConfiguration>()
case .progressBarSingle:
viewController = ListViewController<ProgressBarSingleCell, ProgressBarSingleConfiguration>()
return ListViewController<ProgressBarSingleCell, ProgressBarSingleConfiguration>()
case .radioButton:
viewController = ListViewController<RadioButtonCell, RadioButtonConfiguration>()
return ListViewController<RadioButtonCell, RadioButtonConfiguration>()
case .radioButtonGroup:
viewController = ListViewController<RadioButtonGroupCell, RadioButtonGroupConfiguration>()
return ListViewController<RadioButtonGroupCell, RadioButtonGroupConfiguration>()
case .ratingDisplay:
viewController = ListViewController<RatingDisplayCell, RatingDisplayConfiguration>()
return ListViewController<RatingDisplayCell, RatingDisplayConfiguration>()
case .ratingInput:
viewController = ListViewController<RatingInputCell, RatingInputConfiguration>()
return ListViewController<RatingInputCell, RatingInputConfiguration>()
case .spinner:
viewController = ListViewController<SpinnerCell, SpinnerConfiguration>()
return ListViewController<SpinnerCell, SpinnerConfiguration>()
case .star:
viewController = ListViewController<StarCell, StarCellConfiguration>()
return ListViewController<StarCell, StarCellConfiguration>()
case .switchButton:
viewController = ListViewController<SwitchButtonCell, SwitchButtonConfiguration>()
return ListViewController<SwitchButtonCell, SwitchButtonConfiguration>()
case .tab:
viewController = ListViewController<TabCell, TabConfiguration>()
return ListViewController<TabCell, TabConfiguration>()
case .tag:
viewController = ListViewController<TagCell, TagConfiguration>()
return ListViewController<TagCell, TagConfiguration>()
case .textField:
viewController = ListViewController<TextFieldCell, TextFieldConfiguration>()
return ListViewController<TextFieldCell, TextFieldConfiguration>()
case .addOnTextField:
viewController = ListViewController<AddOnTextFieldCell, AddOnTextFieldConfiguration>()
return ListViewController<AddOnTextFieldCell, AddOnTextFieldConfiguration>()
default:
break
return nil
}
}

private func randomSectionControllers(index: Int) -> UIViewController? {
switch Row.allCases[index] {
case .radioCheckboxSwiftui:
return UIHostingController(
rootView: RadioCheckboxView().environment(\.navigationController, self.navigationController)
)
case .radioCheckboxUikit:
return RadioCheckboxUIViewController()
}
guard viewController != nil else { return }
self.navigationController?.pushViewController(viewController, animated: true)
}
}

// MARK: - Enums
private extension ListComponentsViewController {

enum Section {
case all
enum Section: CaseIterable {
case list
case random
}

enum Row: CaseIterable {
case radioCheckboxSwiftui
case radioCheckboxUikit
}
}

Expand Down

0 comments on commit d1c3fd0

Please sign in to comment.