-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Demo] Added SnackbarView UIKit demo
- Loading branch information
1 parent
594aff7
commit d0e024f
Showing
7 changed files
with
650 additions
and
9 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
...lasses/View/NewComponents/Configuration/EnumSelector/EnumSelectorActionSheetBuilder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
.Demo/Classes/View/NewComponents/Configuration/EnumSelector/EnumSelectorView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
]) | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...sses/View/NewComponents/Configuration/OptionalEnumSelector/OptionalEnumSelectorView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
.Demo/Classes/View/NewComponents/Configuration/Themes/Themes.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.