Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TextInput-1087] Add TextInputBorder from previous TextField repo #4

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Sources/Core/Common/Enum/TextInputBorderStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// TextInputBorderStyle.swift
// Spark
//
// Created by Jacklyn Situmorang on 18.10.23.
// Copyright © 2023 Adevinta. All rights reserved.
//

import UIKit

enum TextInputBorderStyle: CaseIterable {
case roundedRect
case none

init(_ borderStyle: UITextField.BorderStyle) {
switch borderStyle {
case .roundedRect:
self = .roundedRect
default:
self = .none
}
}
}

extension UITextField.BorderStyle {
init(_ borderStyle: TextInputBorderStyle) {
switch borderStyle {
case .roundedRect:
self = .roundedRect
case .none:
self = .none
}

}
}
4 changes: 2 additions & 2 deletions Sources/Core/Common/Model/TextInputBorderLayout.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// TextFieldBorderLayout.swift
// TextInputBorderLayout.swift
// SparkTextField
//
// Created by louis.borlee on 25/09/2023.
Expand All @@ -8,7 +8,7 @@

import Foundation

struct TextFieldBorderLayout: Equatable {
struct TextInputBorderLayout: Equatable {

// MARK: - Properties

Expand Down
4 changes: 2 additions & 2 deletions Sources/Core/Common/Model/TextInputSpacings.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// TextFieldSpacings.swift
// TextInputSpacings.swift
// SparkTextField
//
// Created by louis.borlee on 25/09/2023.
Expand All @@ -8,7 +8,7 @@

import Foundation

struct TextFieldSpacings: Equatable {
struct TextInputSpacings: Equatable {

// MARK: - Properties

Expand Down

This file was deleted.

This file was deleted.

40 changes: 40 additions & 0 deletions Sources/Core/Common/UseCase/TextInputGetBorderLayoutUseCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// TextInputGetBorderLayoutUseCase.swift
// SparkTextInput
//
// Created by louis.borlee on 25/09/2023.
// Copyright © 2023 Adevinta. All rights reserved.
//

import Foundation
import SparkTheming

// sourcery: AutoMockable
protocol TextInputGetBorderLayoutUseCasable {
func execute(
theme: Theme,
borderStyle: TextInputBorderStyle,
isFocused: Bool
) -> TextInputBorderLayout
}

final class TextInputGetBorderLayoutUseCase: TextInputGetBorderLayoutUseCasable {
func execute(
theme: Theme,
borderStyle: TextInputBorderStyle,
isFocused: Bool
) -> TextInputBorderLayout {
switch borderStyle {
case .none:
return .init(
radius: theme.border.radius.none,
width: theme.border.width.none
)
case .roundedRect:
return .init(
radius: theme.border.radius.large,
width: isFocused ? theme.border.width.medium : theme.border.width.small
)
}
}
}
37 changes: 37 additions & 0 deletions Sources/Core/Common/UseCase/TextInputGetSpacingsUseCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// TextInputGetSpacingsUseCase.swift
// SparkTextField
//
// Created by louis.borlee on 25/09/2023.
// Copyright © 2023 Adevinta. All rights reserved.
//

import Foundation
import SparkTheming

// sourcery: AutoMockable
protocol TextInputGetSpacingsUseCasable {
func execute(theme: Theme, borderStyle: TextInputBorderStyle) -> TextInputSpacings
}

final class TextInputGetSpacingsUseCase: TextInputGetSpacingsUseCasable {

// MARK: - Methods

func execute(theme: Theme, borderStyle: TextInputBorderStyle) -> TextInputSpacings {
switch borderStyle {
case .none:
return .init(
left: theme.layout.spacing.none,
content: theme.layout.spacing.medium,
right: theme.layout.spacing.none
)
case .roundedRect:
return .init(
left: theme.layout.spacing.large,
content: theme.layout.spacing.medium,
right: theme.layout.spacing.large
)
}
}
}
17 changes: 15 additions & 2 deletions Sources/Core/Common/ViewModel/TextInputViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,22 @@ class TextInputViewModel: ObservableObject {
self.setFont()
}
}

var intent: TextInputIntent {
didSet {
guard oldValue != self.intent else { return }
self.setColors()
}
}

var borderStyle: TextInputBorderStyle {
didSet {
guard oldValue != self.borderStyle else { return }
self.setBorderLayout()
self.setSpacings()
}
}

var isFocused: Bool = false {
didSet {
guard oldValue != self.isFocused && !self.isReadOnly else { return }
Expand Down Expand Up @@ -85,12 +94,14 @@ class TextInputViewModel: ObservableObject {
init(
theme: Theme,
intent: TextInputIntent,
borderStyle: TextInputBorderStyle,
getColorsUseCase: any TextInputGetColorsUseCasable = TextInputGetColorsUseCase(),
getBorderLayoutUseCase: any TextInputGetBorderLayoutUseCasable = TextInputGetBorderLayoutUseCase(),
getSpacingsUseCase: any TextInputGetSpacingsUseCasable = TextInputGetSpacingsUseCase()
) {
self.theme = theme
self.intent = intent
self.borderStyle = borderStyle

self.getColorsUseCase = getColorsUseCase
self.getBorderLayoutUseCase = getBorderLayoutUseCase
Expand All @@ -112,12 +123,13 @@ class TextInputViewModel: ObservableObject {
// BorderLayout
let borderLayout = getBorderLayoutUseCase.execute(
theme: theme,
borderStyle: borderStyle,
isFocused: self.isFocused)
self.borderWidth = borderLayout.width
self.borderRadius = borderLayout.radius

// Spacings
let spacings = getSpacingsUseCase.execute(theme: theme)
let spacings = getSpacingsUseCase.execute(theme: theme, borderStyle: borderStyle)
self.leftSpacing = spacings.left
self.contentSpacing = spacings.content
self.rightSpacing = spacings.right
Expand Down Expand Up @@ -147,14 +159,15 @@ class TextInputViewModel: ObservableObject {
func setBorderLayout() {
let borderLayout = self.getBorderLayoutUseCase.execute(
theme: self.theme,
borderStyle: self.borderStyle, // .none
isFocused: self.isFocused
)
self.borderWidth = borderLayout.width
self.borderRadius = borderLayout.radius
}

func setSpacings() {
let spacings = self.getSpacingsUseCase.execute(theme: self.theme)
let spacings = self.getSpacingsUseCase.execute(theme: self.theme, borderStyle: self.borderStyle)
self.leftSpacing = spacings.left
self.contentSpacing = spacings.content
self.rightSpacing = spacings.right
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ final class TextFieldAddonsViewModel: ObservableObject, Updateable {

@Published private(set) var dim: CGFloat

var textFieldViewModel: TextInputViewModelForAddons
var textFieldViewModel: TextFieldViewModelForAddons

init(theme: Theme,
intent: TextInputIntent,
getColorsUseCase: TextInputGetColorsUseCasable = TextInputGetColorsUseCase(),
getBorderLayoutUseCase: TextInputGetBorderLayoutUseCasable = TextInputGetBorderLayoutUseCase(),
getSpacingsUseCase: TextInputGetSpacingsUseCasable = TextInputGetSpacingsUseCase()) {
let viewModel = TextInputViewModelForAddons(
let viewModel = TextFieldViewModelForAddons(
theme: theme,
intent: intent,
getColorsUseCase: getColorsUseCase,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// TextInputViewModelForAddons.swift
// TextFieldViewModelForAddons.swift
// SparkTextField
//
// Created by louis.borlee on 14/02/2024.
Expand All @@ -10,7 +10,7 @@ import UIKit
import Combine
import SparkTheming

final class TextInputViewModelForAddons: TextInputViewModel {
final class TextFieldViewModelForAddons: TextInputViewModel {

override var backgroundColor: any ColorToken {
get {
Expand Down Expand Up @@ -38,20 +38,20 @@ final class TextInputViewModelForAddons: TextInputViewModel {
@Published private(set) var addonsRightSpacing: CGFloat = .zero
@Published private(set) var addonsDim: CGFloat = 1.0

override init(
init(
theme: Theme,
intent: TextInputIntent,
intent: TextFieldIntent,
getColorsUseCase: TextInputGetColorsUseCasable = TextInputGetColorsUseCase(),
getBorderLayoutUseCase: TextInputGetBorderLayoutUseCasable = TextInputGetBorderLayoutUseCase(),
getSpacingsUseCase: TextInputGetSpacingsUseCasable = TextInputGetSpacingsUseCase()
) {
super.init(
theme: theme,
intent: intent,
borderStyle: .none,
getColorsUseCase: getColorsUseCase,
getBorderLayoutUseCase: getBorderLayoutUseCase,
getSpacingsUseCase: getSpacingsUseCase
)
getSpacingsUseCase: getSpacingsUseCase)

self.addonsBackgroundColor = super.backgroundColor
self.setBorderLayout()
Expand All @@ -62,17 +62,17 @@ final class TextInputViewModelForAddons: TextInputViewModel {
override func setBorderLayout() {
let borderLayout = self.getBorderLayoutUseCase.execute(
theme: self.theme,
isFocused: self.isFocused
)
borderStyle: .roundedRect,
isFocused: self.isFocused)

self.addonsBorderWidth = borderLayout.width
self.addonsBorderRadius = borderLayout.radius
}

override func setSpacings() {
let spacings = self.getSpacingsUseCase.execute(
theme: self.theme
)
theme: self.theme,
borderStyle: .roundedRect)
self.addonsLeftSpacing = spacings.left
self.addonsContentSpacing = spacings.content
self.addonsRightSpacing = spacings.right
Expand Down
Loading
Loading