Skip to content

Commit

Permalink
Merge pull request #14 from splendo/feature/ios-13
Browse files Browse the repository at this point in the history
Fixes for iOS 13 + UI improvements
  • Loading branch information
Daeda88 authored Feb 4, 2022
2 parents 3a979c8 + 1391eb9 commit 46352c9
Show file tree
Hide file tree
Showing 14 changed files with 249 additions and 131 deletions.
14 changes: 14 additions & 0 deletions stencils/Color+Color.stencil
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% if argument.includeResources %}
import SwiftUI
import {{ argument.sharedFrameworkName }}

extension {{ argument.sharedFrameworkName }}.Color {
var swiftUI: SwiftUI.Color {
if #available(iOS 15.0, *) {
return SwiftUI.Color(uiColor: self.uiColor)
} else {
return SwiftUI.Color(red: self.red, green: self.green, blue: self.blue, opacity: self.alpha)
}
}
}
{% endif %}
20 changes: 20 additions & 0 deletions stencils/DefaultValues.stencil
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import UIKit
import SwiftUI
import Foundation

extension Array: HasDefaultValue {
Expand Down Expand Up @@ -29,6 +30,25 @@ extension UIColor: HasDefaultValue {
static func `default`() -> Self { UIColor.clear as! Self }
}

extension SwiftUI.Color: HasDefaultValue {
static func `default`() -> Self { SwiftUI.Color.clear }
}

extension UIImage: HasDefaultValue {
static func `default`() -> Self {
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image as! Self
}
}

extension Image: HasDefaultValue {
static func `default`() -> Self {
return Image(uiImage: UIImage.default())
}
}

extension Date: HasDefaultValue {
static func `default`() -> Date { Date() }
}
Expand Down
18 changes: 18 additions & 0 deletions stencils/KalugaBackgroundStyle+SwiftUI.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ extension {{ argument.sharedFrameworkName }}.BackgroundStyle {
}
}

extension View {
public func background(_ backgroundStyle: {{ argument.sharedFrameworkName }}.BackgroundStyle) -> some View {
ModifiedContent(
content: self,
modifier: BackgroundStyleModifier(backgroundStyle: backgroundStyle)
)
}
}

// MARK: - Private methods

private extension {{ argument.sharedFrameworkName }}.BackgroundStyle {
Expand Down Expand Up @@ -222,6 +231,15 @@ private extension {{ argument.sharedFrameworkName }}.BackgroundStyle {
*/
}

fileprivate struct BackgroundStyleModifier: ViewModifier {
let backgroundStyle: {{ argument.sharedFrameworkName }}.BackgroundStyle

func body(content: Content) -> some View {
content
.background(backgroundStyle.toView())
}
}

// MARK: - RoundedShape

fileprivate struct RoundedShape: Shape {
Expand Down
112 changes: 46 additions & 66 deletions stencils/KalugaButton+SwiftUI.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -23,93 +23,73 @@ extension KalugaButton {
}) {
buttonView
}
.buttonStyle(CustomButtonStyle(kalugaButton: self))
.disabled(!self.isEnabled)
.buttonStyle(self.style, isEnabled: self.isEnabled)
}
}

extension View {
public func buttonStyle(_ buttonStyle: {{ argument.sharedFrameworkName }}.ButtonStyle, isEnabled: Bool = true) -> some View {
ModifiedContent(
content: self,
modifier: ButtonStyleModifier(buttonStyle: buttonStyle, isEnabled: isEnabled)
)
}
}

// MARK: - Private methods

private extension KalugaButton {

private var textStyle: TextStyle {
return TextStyle(font: style.font, color: style.defaultStyle.textColor, size: style.textSize, alignment: style.textAlignment)
}

private func makePlainButton(button: KalugaButton.Plain) -> AnyView {
// create the font
let font = Font(
style.font
.withSize(CGFloat(style.textSize))
)

// create the text alignment
let textAlignment: Alignment = {
switch style.textAlignment {
case TextAlignment.left:
return .leading
case TextAlignment.right:
return .trailing
case TextAlignment.end:
return UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ?
.trailing :
.leading
case TextAlignment.start:
return .leading
case TextAlignment.center:
return .center
default:
preconditionFailure("Unknown text alignment type!")
}
}()

// assemble the button
return AnyView(
Text(button.text)
.font(font)
.frame(alignment: textAlignment)
.textStyle(textStyle)
)
}

private func makeStyledButton(button: KalugaButton.Styled) -> AnyView {
return AnyView(
button.text.toText()
KalugaLabel.Styled(text: button.text, style: textStyle).toAttributedText()
)
}
}

// MARK: - KalugaButton.CustomButtonStyle
// MARK: - CustomButtonStyle

private extension KalugaButton {
fileprivate struct CustomButtonStyle: SwiftUI.ButtonStyle {
private let buttonStyle: {{ argument.sharedFrameworkName }}.ButtonStyle
private let isEnabled: Bool
init(buttonStyle: {{ argument.sharedFrameworkName }}.ButtonStyle, isEnabled: Bool = true) {
self.buttonStyle = buttonStyle
self.isEnabled = isEnabled
}
func makeBody(configuration: Self.Configuration) -> some View {
let stateStyle = stateStyle(isPressed: configuration.isPressed)
return configuration.label
.padding()
.foregroundColor(stateStyle.textColor.swiftUI)
.background(stateStyle.backgroundStyle)
}

private func stateStyle(isPressed: Bool) -> ButtonStateStyle {
if !isEnabled { return buttonStyle.disabledStyle }
else if isPressed { return buttonStyle.pressedStyle }
else { return buttonStyle.defaultStyle }
}
}

fileprivate struct ButtonStyleModifier: ViewModifier {
let buttonStyle: {{ argument.sharedFrameworkName }}.ButtonStyle
let isEnabled: Bool

struct CustomButtonStyle: SwiftUI.ButtonStyle {

private let kalugaButton: KalugaButton

init(kalugaButton: KalugaButton) {
self.kalugaButton = kalugaButton
}

func makeBody(configuration: Self.Configuration) -> some View {
let foregroundColor = makeForegroundColor(isPressed: configuration.isPressed)
let shape = makeShape(isPressed: configuration.isPressed)

return configuration.label
.padding()
.foregroundColor(foregroundColor)
.background(shape)
}

private func makeForegroundColor(isPressed: Bool) -> SwiftUI.Color {
if !kalugaButton.isEnabled { return Color(kalugaButton.style.disabledStyle.textColor.uiColor) }
else if isPressed { return Color(kalugaButton.style.pressedStyle.textColor.uiColor) }
else { return Color(kalugaButton.style.defaultStyle.textColor.uiColor) }
}

private func makeShape(isPressed: Bool) -> some View {
let buttonStateStyle: ButtonStateStyle
if !kalugaButton.isEnabled { buttonStateStyle = kalugaButton.style.disabledStyle }
else if isPressed { buttonStateStyle = kalugaButton.style.pressedStyle }
else { buttonStateStyle = kalugaButton.style.defaultStyle}

return buttonStateStyle.backgroundStyle.toView()
}
func body(content: Content) -> some View {
content
.buttonStyle(CustomButtonStyle(buttonStyle: buttonStyle, isEnabled: isEnabled))
.disabled(!isEnabled)
}
}
{% endif %}
75 changes: 44 additions & 31 deletions stencils/KalugaLabel+SwiftUI.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,63 @@ extension KalugaLabel {
}
}

extension View {
public func textStyle(_ textStyle: TextStyle) -> some View {
ModifiedContent(
content: self,
modifier: TextStyleModifier(textStyle: textStyle)
)
}
}

extension {{ argument.sharedFrameworkName }}.TextAlignment {

var textAlignment: SwiftUI.TextAlignment {
switch self {
case TextAlignment.left:
return .leading
case TextAlignment.right:
return .trailing
case TextAlignment.end:
return UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ?
.trailing :
.leading
case TextAlignment.start:
return .leading
case TextAlignment.center:
return .center
default:
preconditionFailure("Unknown text alignment type!")
}
}
}

// MARK: - Private methods

private extension KalugaLabel {

private func makePlainLabel(label: KalugaLabel.Plain) -> AnyView {
// create the font
let font = Font(
style.font
.withSize(CGFloat(style.size))
)

// create the text alignment
let textAlignment: Alignment = {
switch style.alignment {
case TextAlignment.left:
return .leading
case TextAlignment.right:
return .trailing
case TextAlignment.end:
return UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ?
.trailing :
.leading
case TextAlignment.start:
return .leading
case TextAlignment.center:
return .center
default:
preconditionFailure("Unknown text alignment type!")
}
}()

// assemble the button
return AnyView(
Text(label.text)
.font(font)
.foregroundColor(Color(style.color.uiColor))
.frame(alignment: textAlignment)
.textStyle(style)
)
}

private func makeStyledLabel(label: KalugaLabel.Styled) -> AnyView {
return AnyView(
label.text.toText()
label.toAttributedText()
)
}
}

fileprivate struct TextStyleModifier: ViewModifier {
let textStyle: TextStyle

func body(content: Content) -> some View {
content
.font(Font(textStyle.font.withSize(CGFloat(textStyle.size))))
.foregroundColor(Color(textStyle.color.uiColor))
.multilineTextAlignment(textStyle.alignment.textAlignment)
}
}
{% endif %}
19 changes: 7 additions & 12 deletions stencils/KalugaStyledString+SwiftUI.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@ import {{ argument.sharedFrameworkName }}

// MARK: - StyledString

extension {{ argument.sharedFrameworkName }}.StyledString {

func toText() -> some View {
AttributedText(attributeString)
extension KalugaLabel.Styled {
func toAttributedText() -> some View {
AttributedText(self)
}
}

// MARK: - AttributedText

fileprivate struct AttributedText: UIViewRepresentable {

private let attributedString: NSAttributedString

init(_ attributedString: NSAttributedString) {
self.attributedString = attributedString
private let styledLabel: KalugaLabel.Styled
init(_ styledLabel: KalugaLabel.Styled) {
self.styledLabel = styledLabel
}

func makeUIView(context: Context) -> UILabel {
let label = UILabel()
label.lineBreakMode = .byClipping
Expand All @@ -29,9 +25,8 @@ fileprivate struct AttributedText: UIViewRepresentable {
label.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
return label
}

func updateUIView(_ uiView: UILabel, context: Context) {
uiView.attributedText = attributedString
TextStyleKt.bindLabel(uiView, label: styledLabel)
}
}
{% endif %}
9 changes: 8 additions & 1 deletion stencils/ListObservable.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ class ListObservable<Output: Equatable>: ObservableObject {
defer {
self.input = newValue
}
guard newValue != nil, self.input != newValue else {
guard newValue != nil else {
if self.input != nil {
self.value.removeAll()
self.value.append(contentsOf: defaultValue)
}
return
}
guard self.input != newValue else {
return
}
let mapped = mapper(newValue!)
Expand Down
9 changes: 8 additions & 1 deletion stencils/ListSubject.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ class ListSubject<Output: Equatable>: ObservableObject {
defer {
self.input = newValue
}
guard newValue != nil, self.input != newValue else {
guard newValue != nil else {
if self.input != nil {
self.value.removeAll()
self.value.append(contentsOf: defaultValue)
}
return
}
guard self.input != newValue else {
return
}
let mapped = mapper(newValue!)
Expand Down
Loading

0 comments on commit 46352c9

Please sign in to comment.