Skip to content

Commit

Permalink
[Demo] Added SnackbarView UIKit demo
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisBorleeAdevinta committed Sep 24, 2024
1 parent 594aff7 commit d0e024f
Show file tree
Hide file tree
Showing 7 changed files with 650 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// EnumSelectorActionSheetBuilder.swift
// SparkDemo
//
// Created by louis.borlee on 20/09/2024.
// Copyright © 2024 Adevinta. All rights reserved.
//

import UIKit

final class EnumSelectorActionSheetBuilder<Enum> where Enum: CaseIterable & Hashable {

private let title: String?

init(title: String?) {
self.title = title
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func build(action: @escaping (Enum) -> Void) -> UIAlertController{
let alertController = UIAlertController(
title: self.title,
message: nil,
preferredStyle: .actionSheet
)
Enum.allCases.forEach { `case` in
alertController.addAction(
.init(
title: `case`.name,
style: .default,
handler: { _ in
action(`case`)
}
)
)
}
return alertController
}

func build(action: @escaping (Enum?) -> Void) -> UIAlertController{
let alertController = UIAlertController(
title: self.title,
message: nil,
preferredStyle: .actionSheet
)
Enum.allCases.forEach { `case` in
alertController.addAction(
.init(
title: `case`.name,
style: .default,
handler: { _ in
action(`case`)
}
)
)
}
alertController.addAction(
.init(
title: "Default",
style: .default,
handler: { _ in
action(nil)
}
)
)
return alertController
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// EnumSelectorView.swift
// SparkDemo
//
// Created by louis.borlee on 20/09/2024.
// Copyright © 2024 Adevinta. All rights reserved.
//

import UIKit

final class EnumSelectorView<Enum>: UIView, ObservableObject where Enum: CaseIterable & Hashable {

private let title: String
@Published var currentCase: Enum

private let button = UIButton()

private weak var presenter: UIViewController?

init(title: String, currentCase: Enum, presenter: UIViewController?) {
self.title = title
self.currentCase = currentCase
self.presenter = presenter
super.init(frame: .zero)

self.setupView()
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func setupView() {
let label = UILabel()
label.text = self.title
label.font = SparkTheme.shared.typography.subhead.uiFont
label.adjustsFontForContentSizeCategory = true

self.button.setTitle(self.currentCase.name, for: .normal)
self.button.configuration = .plain()

self.button.addAction(.init(handler: { [weak self] _ in
guard let self else { return }
let actionSheetBuilder = EnumSelectorActionSheetBuilder<Enum>(
title: nil)
let alertController = actionSheetBuilder.build() { [weak self] newCase in
guard let self else { return }
self.currentCase = newCase
self.button.setTitle(newCase.name, for: .normal)
}
alertController.addAction(
.init(
title: "Dismiss",
style: .cancel,
handler: { _ in
alertController.dismiss(animated: true)
}
)
)
self.presenter?.present(alertController, animated: true)
}), for: .touchUpInside)

let stackView = UIStackView(arrangedSubviews: [label, self.button])
stackView.alignment = .center
stackView.translatesAutoresizingMaskIntoConstraints = false

self.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
stackView.topAnchor.constraint(equalTo: self.topAnchor),
stackView.bottomAnchor.constraint(equalTo: self.bottomAnchor)
])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// OptionalEnumSelectorView.swift
// SparkDemo
//
// Created by louis.borlee on 20/09/2024.
// Copyright © 2024 Adevinta. All rights reserved.
//

import UIKit

final class OptionalEnumSelectorView<Enum>: UIView, ObservableObject where Enum: CaseIterable & Hashable {

private let title: String
@Published var currentCase: Enum?

private let button = UIButton()

private weak var presenter: UIViewController?

init(title: String, currentCase: Enum?, presenter: UIViewController?) {
self.title = title
self.currentCase = currentCase
self.presenter = presenter
super.init(frame: .zero)

self.setupView()
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func setupView() {
let label = UILabel()
label.text = self.title
label.font = SparkTheme.shared.typography.subhead.uiFont
label.adjustsFontForContentSizeCategory = true

self.button.setTitle(self.currentCase?.name ?? "Default", for: .normal)
self.button.configuration = .plain()

self.button.addAction(.init(handler: { [weak self] _ in
guard let self else { return }
let actionSheetBuilder = EnumSelectorActionSheetBuilder<Enum>(
title: nil)
let alertController = actionSheetBuilder.build() { [weak self] newCase in
guard let self else { return }
self.currentCase = newCase
self.button.setTitle(newCase?.name ?? "Default", for: .normal)
}
alertController.addAction(
.init(
title: "Dismiss",
style: .cancel,
handler: { _ in
alertController.dismiss(animated: true)
}
)
)
self.presenter?.present(alertController, animated: true)
}), for: .touchUpInside)

let stackView = UIStackView(arrangedSubviews: [label, self.button])
stackView.alignment = .center
stackView.translatesAutoresizingMaskIntoConstraints = false

self.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
stackView.topAnchor.constraint(equalTo: self.topAnchor),
stackView.bottomAnchor.constraint(equalTo: self.bottomAnchor)
])
}
}
21 changes: 21 additions & 0 deletions .Demo/Classes/View/NewComponents/Configuration/Themes/Themes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Themes.swift
// SparkDemo
//
// Created by louis.borlee on 20/09/2024.
// Copyright © 2024 Adevinta. All rights reserved.
//

import Foundation

enum Themes: CaseIterable, Hashable {
case spark
case sky

var current: any Theme {
switch self {
case .sky: return SkyTheme()
case .spark: return SparkTheme.shared
}
}
}
12 changes: 6 additions & 6 deletions .Demo/Classes/View/NewComponents/NewComponentsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,24 @@ extension Components {
func viewController() -> some View {
switch self {
case .snackbar:
return HostingView(viewController: .init(nibName: nil, bundle: nil))
return HostingView(viewController: { SnackbarDemoUIView() })
}
}

private struct HostingView<ViewController: UIViewController>: UIViewControllerRepresentable {

private let viewController: UIViewControllerType
private var viewController: () -> UIViewControllerType

init(viewController: UIViewControllerType) {
init(viewController: @escaping () -> UIViewControllerType) {
self.viewController = viewController
self.viewController.view.backgroundColor = .systemBackground
}

func makeUIViewController(context: Context) -> ViewController {
return self.viewController
return self.viewController()
}

func updateUIViewController(_ uiViewController: ViewController, context: Context) {}
func updateUIViewController(_ uiViewController: ViewController, context: Context) {
}
}
}

Expand Down
Loading

0 comments on commit d0e024f

Please sign in to comment.