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

[JIRA:HCPSDKFIORIUIKIT-2882] TextInput Refactor #957

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftUI

class ActivationScreenDataModel: ActivationScreenModel, ObservableObject {
// Changes in nested observable object will not trigger refresh. Need to send notification by explicitly calling `send()`
@Published var textInput: TextInputModel?
@Published var textInput: _TextInputModel?
lazy var action: _ActionModel? = ActionDataModel { [unowned self] in
print("ActivationScreen Primary button clicked, email: \(self.textInput!.textInputValue)")
}
Expand All @@ -31,7 +31,7 @@ class ActivationScreenDataModel: ActivationScreenModel, ObservableObject {
}

extension ActivationScreenDataModel {
class TextInputDataModel: TextInputModel, ObservableObject {
class TextInputDataModel: _TextInputModel, ObservableObject {
@Published var textInputValue: String = ""

var onCommit: (() -> Void)? = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftUI

class WelcomeScreenDataModel: WelcomeScreenModel, ObservableObject {
// Changes in nested observable object will not trigger refresh. Need to send notification by explicitly calling `send()`
@Published var textInput: TextInputModel?
@Published var textInput: _TextInputModel?
lazy var action: _ActionModel? = ActionDataModel { [unowned self] in
print("Primary button clicked: \(self.textInput!.textInputValue)")
}
Expand Down Expand Up @@ -33,7 +33,7 @@ class WelcomeScreenDataModel: WelcomeScreenModel, ObservableObject {
}

extension WelcomeScreenDataModel {
class TextInputDataModel: TextInputModel, ObservableObject {
class TextInputDataModel: _TextInputModel, ObservableObject {
@Published var textInputValue: String = ""

var onCommit: (() -> Void)? = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protocol __Action: _ComponentMultiPropGenerating {
var didSelectAction_: (() -> Void)? { get }
}

protocol _TextInput: _ComponentMultiPropGenerating, AnyObject {
protocol __TextInput: _ComponentMultiPropGenerating, AnyObject {
// sourcery: bindingPropertyOptional = .constant("")
var textInputValue_: String { get set }
// sourcery: no_view
Expand Down
10 changes: 7 additions & 3 deletions Sources/FioriSwiftUICore/Models/ModelDefinitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ public protocol _ActionModel: ActionComponent {}
public protocol ActionModel: ActionComponent {}

// sourcery: generated_component_not_configurable
public protocol TextInputModel: TextInputComponent {}
public protocol _TextInputModel: TextInputComponent {}

/// Deprecated TextInputModel
@available(*, unavailable, renamed: "_TextInputModel", message: "Will be removed in the future release. Please create TextInput with other initializers instead.")
public protocol TextInputModel {}

// sourcery: generated_component
public protocol _ActivityItemModel: IconComponent, SubtitleComponent {}
Expand Down Expand Up @@ -182,7 +186,7 @@ public protocol ContactItemModel {}
// sourcery: generated_component_composite
public protocol WelcomeScreenModel: TitleComponent, DescriptionTextComponent, SubtitleComponent, FootnoteComponent, IconComponent {
// sourcery: genericParameter.name = TextInputView
var textInput: TextInputModel? { get }
var textInput: _TextInputModel? { get }

// sourcery: genericParameter.name = ActionView
var action: _ActionModel? { get }
Expand All @@ -200,7 +204,7 @@ public protocol ActivationScreenModel: TitleComponent, DescriptionTextComponent,
var secondaryAction: _ActionModel? { get }

// sourcery: genericParameter.name = TextInputView
var textInput: TextInputModel? { get }
var textInput: _TextInputModel? { get }
}

// sourcery: generated_component_composite
Expand Down
4 changes: 2 additions & 2 deletions Sources/FioriSwiftUICore/Views/TextInput+View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import SwiftUI

extension Fiori {
enum TextInput {
enum _TextInput {
typealias TextInputValue = EmptyModifier
static let textInputValue = TextInputValue()
}
}

extension TextInput: View {
extension _TextInput: View {
public var body: some View {
TextField("Default",
text: self._textInputValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,14 @@ protocol _ActivityItemComponent: _IconComponent, _SubtitleComponent {
// sourcery: CompositeComponent
protocol _ContactItemComponent: _TitleComponent, _SubtitleComponent, _DescriptionComponent, _DetailImageComponent, _ActivityItemsComponent {}

// sourcery: CompositeComponent
protocol _TextInputComponent {
// sourcery: @Binding
var textInputValue: String { get }
// sourcery: no_view
var onCommit: (() -> Void)? { get }
}

// sourcery: CompositeComponent
protocol _RangeSliderControlComponent: _LowerThumbComponent, _UpperThumbComponent, _ActiveTrackComponent, _InactiveTrackComponent {
// sourcery: @Binding
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import FioriThemeManager
import Foundation
import SwiftUI

// Base Layout style
public struct TextInputBaseStyle: TextInputStyle {
public func makeBody(_ configuration: TextInputConfiguration) -> some View {
// Add default layout here
TextField("Default",
text: configuration.$textInputValue,
onCommit: configuration.onCommit ?? {})
}
}

// Default fiori styles
extension TextInputFioriStyle {
struct ContentFioriStyle: TextInputStyle {
func makeBody(_ configuration: TextInputConfiguration) -> some View {
TextInput(configuration)
.modifier(TextFieldClearButton(textValue: configuration.$textInputValue))
.textFieldStyle(BottomTextFieldStyle())
.font(.fiori(forTextStyle: .body))
.foregroundColor(.preferredColor(.primaryLabel))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,6 @@ public protocol SwitchPickerComponent : AnyObject {
var hint: String? { get }
}

public protocol TextInputComponent : AnyObject {
// sourcery: bindingPropertyOptional=.constant("")
var textInputValue: String { get set }
// sourcery: no_view
var onCommit: (() -> Void)? { get }
}

public protocol ActionComponent {
var actionText: String? { get }
// sourcery: no_view
Expand All @@ -253,3 +246,10 @@ public protocol ActionComponent {
public protocol ProgressIndicatorComponent {
var progressIndicatorText: String? { get }
}

public protocol TextInputComponent : AnyObject {
// sourcery: bindingPropertyOptional=.constant("")
var textInputValue: String { get set }
// sourcery: no_view
var onCommit: (() -> Void)? { get }
}
hengyi-zhang marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ struct ActionItemsModifierKey: EnvironmentKey {
public static let defaultValue = AnyViewModifier { $0 }
}

struct TextInputValueModifierKey: EnvironmentKey {
struct ActionTextModifierKey: EnvironmentKey {
public static let defaultValue = AnyViewModifier { $0 }
}

struct ActionTextModifierKey: EnvironmentKey {
struct ProgressIndicatorTextModifierKey: EnvironmentKey {
public static let defaultValue = AnyViewModifier { $0 }
}

struct ProgressIndicatorTextModifierKey: EnvironmentKey {
struct TextInputValueModifierKey: EnvironmentKey {
public static let defaultValue = AnyViewModifier { $0 }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,6 @@ extension EnvironmentValues {
set { self[ActionItemsModifierKey.self] = newValue }
}

public var textInputValueModifier: AnyViewModifier {
get { return self[TextInputValueModifierKey.self] }
set { self[TextInputValueModifierKey.self] = newValue }
}

public var actionTextModifier: AnyViewModifier {
get { return self[ActionTextModifierKey.self] }
set { self[ActionTextModifierKey.self] = newValue }
Expand All @@ -214,6 +209,11 @@ extension EnvironmentValues {
set { self[ProgressIndicatorTextModifierKey.self] = newValue }
}

public var textInputValueModifier: AnyViewModifier {
get { return self[TextInputValueModifierKey.self] }
set { self[TextInputValueModifierKey.self] = newValue }
}

public var actionModifier: AnyViewModifier {
get { return self[ActionModifierKey.self] }
set { self[ActionModifierKey.self] = newValue }
Expand Down Expand Up @@ -498,11 +498,6 @@ public extension View {
self.environment(\.actionItemsModifier, AnyViewModifier(transform))
}

@ViewBuilder
func textInputValueModifier<V: View>(_ transform: @escaping (AnyViewModifier.Content) -> V) -> some View {
self.environment(\.textInputValueModifier, AnyViewModifier(transform))
}

@ViewBuilder
func actionTextModifier<V: View>(_ transform: @escaping (AnyViewModifier.Content) -> V) -> some View {
self.environment(\.actionTextModifier, AnyViewModifier(transform))
Expand All @@ -513,6 +508,11 @@ public extension View {
self.environment(\.progressIndicatorTextModifier, AnyViewModifier(transform))
}

@ViewBuilder
func textInputValueModifier<V: View>(_ transform: @escaping (AnyViewModifier.Content) -> V) -> some View {
self.environment(\.textInputValueModifier, AnyViewModifier(transform))
}

@ViewBuilder
func actionModifier<V: View>(_ transform: @escaping (AnyViewModifier.Content) -> V) -> some View {
self.environment(\.actionModifier, AnyViewModifier(transform))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Generated using Sourcery 2.1.7 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
import Foundation
import SwiftUI

public struct TextInput {
@Binding var textInputValue: String
let onCommit: (() -> Void)?

@Environment(\.textInputStyle) var style

fileprivate var _shouldApplyDefaultStyle = true

public init(textInputValue: Binding<String>,
onCommit: (() -> Void)? = nil)
{
self._textInputValue = textInputValue
self.onCommit = onCommit
}
}

public extension TextInput {
init(_ configuration: TextInputConfiguration) {
self.init(configuration, shouldApplyDefaultStyle: false)
}

internal init(_ configuration: TextInputConfiguration, shouldApplyDefaultStyle: Bool) {
self._textInputValue = configuration.$textInputValue
self.onCommit = configuration.onCommit
self._shouldApplyDefaultStyle = shouldApplyDefaultStyle
}
}

extension TextInput: View {
public var body: some View {
if self._shouldApplyDefaultStyle {
self.defaultStyle()
} else {
self.style.resolve(configuration: .init(textInputValue: self.$textInputValue, onCommit: self.onCommit)).typeErased
.transformEnvironment(\.textInputStyleStack) { stack in
if !stack.isEmpty {
stack.removeLast()
}
}
}
}
}

private extension TextInput {
func shouldApplyDefaultStyle(_ bool: Bool) -> some View {
var s = self
s._shouldApplyDefaultStyle = bool
return s
}

func defaultStyle() -> some View {
TextInput(.init(textInputValue: self.$textInputValue, onCommit: self.onCommit))
.shouldApplyDefaultStyle(false)
.textInputStyle(TextInputFioriStyle.ContentFioriStyle())
.typeErased
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Generated using Sourcery 2.1.7 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
import Foundation
import SwiftUI

public protocol TextInputStyle: DynamicProperty {
associatedtype Body: View

func makeBody(_ configuration: TextInputConfiguration) -> Body
}

struct AnyTextInputStyle: TextInputStyle {
let content: (TextInputConfiguration) -> any View

init(@ViewBuilder _ content: @escaping (TextInputConfiguration) -> any View) {
self.content = content
}

public func makeBody(_ configuration: TextInputConfiguration) -> some View {
self.content(configuration).typeErased
}
}

public struct TextInputConfiguration {
@Binding public var textInputValue: String
public let onCommit: (() -> Void)?
}

public struct TextInputFioriStyle: TextInputStyle {
public func makeBody(_ configuration: TextInputConfiguration) -> some View {
TextInput(configuration)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6471,6 +6471,20 @@ public extension TextFieldFormViewStyle where Self == TextFieldFormViewTitleForm
}
}

// MARK: TextInputStyle

public extension TextInputStyle where Self == TextInputBaseStyle {
static var base: TextInputBaseStyle {
TextInputBaseStyle()
}
}

public extension TextInputStyle where Self == TextInputFioriStyle {
static var fiori: TextInputFioriStyle {
TextInputFioriStyle()
}
}

// MARK: TextInputFieldStyle

public extension TextInputFieldStyle where Self == TextInputFieldBaseStyle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2586,6 +2586,27 @@ extension EnvironmentValues {
}
}

// MARK: TextInputStyle

struct TextInputStyleStackKey: EnvironmentKey {
static let defaultValue: [any TextInputStyle] = []
}

extension EnvironmentValues {
var textInputStyle: any TextInputStyle {
self.textInputStyleStack.last ?? .base.concat(.fiori)
}

var textInputStyleStack: [any TextInputStyle] {
get {
self[TextInputStyleStackKey.self]
}
set {
self[TextInputStyleStackKey.self] = newValue
}
}
}

// MARK: TextInputFieldStyle

struct TextInputFieldStyleStackKey: EnvironmentKey {
Expand Down
Loading
Loading