@ -1,151 +1,253 @@
import SwiftUI
// MARK: - 邮 件 输 入 配 置
struct EmailInputConfig {
let emailAddress : Binding < String >
let emailSubject : Binding < String >
let emailBody : Binding < String >
let emailCc : Binding < String >
let emailBcc : Binding < String >
}
// MARK: - W i F i 输 入 配 置
struct WiFiInputConfig {
let ssid : Binding < String >
let password : Binding < String >
let encryptionType : Binding < WiFiInputView . WiFiEncryptionType >
}
// MARK: - 联 系 人 输 入 配 置
struct ContactInputConfig {
let firstName : Binding < String >
let lastName : Binding < String >
let phone : Binding < String >
let email : Binding < String >
let company : Binding < String >
let title : Binding < String >
let address : Binding < String >
let website : Binding < String >
}
// MARK: - 位 置 输 入 配 置
struct LocationInputConfig {
let latitude : Binding < String >
let longitude : Binding < String >
let locationName : Binding < String >
}
// MARK: - 日 历 输 入 配 置
struct CalendarInputConfig {
let eventTitle : Binding < String >
let eventDescription : Binding < String >
let startDate : Binding < Date >
let endDate : Binding < Date >
let location : Binding < String >
}
// MARK: - 社 交 输 入 配 置
struct SocialInputConfig {
let username : Binding < String >
let message : Binding < String >
}
// MARK: - 电 话 输 入 配 置
struct PhoneInputConfig {
let phoneNumber : Binding < String >
let phoneMessage : Binding < String >
}
// MARK: - U R L 输 入 配 置
struct URLInputConfig {
let url : Binding < String >
}
// MARK: - 文 本 输 入 配 置
struct TextInputConfig {
let content : Binding < String >
}
// MARK: - 输 入 组 件 工 厂
struct InputComponentFactory {
// 根 据 Q R 码 类 型 返 回 相 应 的 输 入 组 件
// 创 建 邮 件 输 入 组 件
static func createEmailInput ( with config : EmailInputConfig ) -> AnyView {
return AnyView (
EmailInputView (
emailAddress : config . emailAddress ,
emailSubject : config . emailSubject ,
emailBody : config . emailBody ,
emailCc : config . emailCc ,
emailBcc : config . emailBcc
)
)
}
// 创 建 W i F i 输 入 组 件
static func createWiFiInput ( with config : WiFiInputConfig ) -> AnyView {
return AnyView (
WiFiInputView (
ssid : config . ssid ,
password : config . password ,
encryptionType : config . encryptionType
)
)
}
// 创 建 联 系 人 输 入 组 件
static func createContactInput ( with config : ContactInputConfig ) -> AnyView {
return AnyView (
ContactInputView (
firstName : config . firstName ,
lastName : config . lastName ,
phone : config . phone ,
email : config . email ,
company : config . company ,
title : config . title ,
address : config . address ,
website : config . website
)
)
}
// 创 建 位 置 输 入 组 件
static func createLocationInput ( with config : LocationInputConfig ) -> AnyView {
return AnyView (
LocationInputView (
latitude : config . latitude ,
longitude : config . longitude ,
locationName : config . locationName
)
)
}
// 创 建 日 历 输 入 组 件
static func createCalendarInput ( with config : CalendarInputConfig ) -> AnyView {
return AnyView (
CalendarInputView (
eventTitle : config . eventTitle ,
eventDescription : config . eventDescription ,
startDate : config . startDate ,
endDate : config . endDate ,
location : config . location
)
)
}
// 创 建 社 交 输 入 组 件
static func createSocialInput ( with config : SocialInputConfig , platform : SocialInputView . SocialPlatform ) -> AnyView {
return AnyView (
SocialInputView (
username : config . username ,
message : config . message ,
platform : platform
)
)
}
// 创 建 电 话 输 入 组 件
static func createPhoneInput ( with config : PhoneInputConfig , inputType : PhoneInputView . PhoneInputType ) -> AnyView {
return AnyView (
PhoneInputView (
phoneNumber : config . phoneNumber ,
message : config . phoneMessage ,
inputType : inputType
)
)
}
// 创 建 U R L 输 入 组 件
static func createURLInput ( with config : URLInputConfig ) -> AnyView {
return AnyView (
URLInputView (
url : config . url
)
)
}
// 创 建 文 本 输 入 组 件
static func createTextInput ( with config : TextInputConfig , placeholder : String , maxCharacters : Int ) -> AnyView {
return AnyView (
TextInputView (
content : config . content ,
placeholder : placeholder ,
maxCharacters : maxCharacters
)
)
}
// 根 据 Q R 码 类 型 返 回 相 应 的 输 入 组 件 - 需 要 从 C r e a t e Q R C o d e V i e w 传 入 具 体 的 绑 定
static func createInputComponent (
for qrCodeType : QRCodeType ,
content : Binding < String > ,
emailAddress : Binding < String > ,
emailSubject : Binding < String > ,
emailBody : Binding < String > ,
emailCc : Binding < String > ,
emailBcc : Binding < String > ,
focusedEmailField : FocusState < EmailInputView . EmailField ? > ,
isContentFieldFocused : FocusState < Bool > ,
ssid : Binding < String > ,
password : Binding < String > ,
encryptionType : Binding < WiFiInputView . WiFiEncryptionType > ,
focusedWiFiField : FocusState < WiFiInputView . WiFiField ? > ,
firstName : Binding < String > ,
lastName : Binding < String > ,
phone : Binding < String > ,
email : Binding < String > ,
company : Binding < String > ,
title : Binding < String > ,
address : Binding < String > ,
website : Binding < String > ,
focusedContactField : FocusState < ContactInputView . ContactField ? > ,
latitude : Binding < String > ,
longitude : Binding < String > ,
locationName : Binding < String > ,
focusedLocationField : FocusState < LocationInputView . LocationField ? > ,
eventTitle : Binding < String > ,
eventDescription : Binding < String > ,
startDate : Binding < Date > ,
endDate : Binding < Date > ,
location : Binding < String > ,
focusedCalendarField : FocusState < CalendarInputView . CalendarField ? > ,
username : Binding < String > ,
message : Binding < String > ,
focusedSocialField : FocusState < SocialInputView . SocialField ? > ,
phoneNumber : Binding < String > ,
phoneMessage : Binding < String > ,
focusedPhoneField : FocusState < PhoneInputView . PhoneField ? > ,
url : Binding < String > ,
isUrlFieldFocused : FocusState < Bool >
emailConfig : EmailInputConfig ? = nil ,
wifiConfig : WiFiInputConfig ? = nil ,
contactConfig : ContactInputConfig ? = nil ,
locationConfig : LocationInputConfig ? = nil ,
calendarConfig : CalendarInputConfig ? = nil ,
socialConfig : SocialInputConfig ? = nil ,
phoneConfig : PhoneInputConfig ? = nil ,
urlConfig : URLInputConfig ? = nil ,
textConfig : TextInputConfig ? = nil
) -> AnyView {
switch qrCodeType {
case . mail :
return AnyView (
EmailInputView (
emailAddress : emailAddress ,
emailSubject : emailSubject ,
emailBody : emailBody ,
emailCc : emailCc ,
emailBcc : emailBcc ,
focusedEmailField : focusedEmailField
)
)
guard let config = emailConfig else {
return AnyView ( EmptyView ( ) )
}
return createEmailInput ( with : config )
case . wifi :
return AnyView (
WiFiInputView (
ssid : ssid ,
password : password ,
encryptionType : encryptionType ,
focusedField : focusedWiFiField
)
)
guard let config = wifiConfig else {
return AnyView ( EmptyView ( ) )
}
return createWiFiInput ( with : config )
case . vcard , . mecard :
return AnyView (
ContactInputView (
firstName : firstName ,
lastName : lastName ,
phone : phone ,
email : email ,
company : company ,
title : title ,
address : address ,
website : website ,
focusedField : focusedContactField
)
)
guard let config = contactConfig else {
return AnyView ( EmptyView ( ) )
}
return createContactInput ( with : config )
case . location :
return AnyView (
LocationInputView (
latitude : latitude ,
longitude : longitude ,
locationName : locationName ,
focusedField : focusedLocationField
)
)
guard let config = locationConfig else {
return AnyView ( EmptyView ( ) )
}
return createLocationInput ( with : config )
case . calendar :
return AnyView (
CalendarInputView (
eventTitle : eventTitle ,
eventDescription : eventDescription ,
startDate : startDate ,
endDate : endDate ,
location : location ,
focusedField : focusedCalendarField
)
)
guard let config = calendarConfig else {
return AnyView ( EmptyView ( ) )
}
return createCalendarInput ( with : config )
case . instagram , . facebook , . twitter , . tiktok , . snapchat , . whatsapp , . viber , . spotify :
guard let config = socialConfig else {
return AnyView ( EmptyView ( ) )
}
let platform = SocialInputView . SocialPlatform ( rawValue : qrCodeType . rawValue . capitalized ) ? ? . instagram
return AnyView (
SocialInputView (
username : username ,
message : message ,
platform : platform ,
focusedField : focusedSocialField
)
)
return createSocialInput ( with : config , platform : platform )
case . phone , . sms :
guard let config = phoneConfig else {
return AnyView ( EmptyView ( ) )
}
let inputType : PhoneInputView . PhoneInputType = qrCodeType = = . phone ? . phone : . sms
return AnyView (
PhoneInputView (
phoneNumber : phoneNumber ,
message : phoneMessage ,
inputType : inputType ,
focusedField : focusedPhoneField
)
)
return createPhoneInput ( with : config , inputType : inputType )
case . url :
return AnyView (
URLInputView (
url : url ,
isUrlFieldFocused : isUrlFieldFocused
)
)
guard let config = urlConfig else {
return AnyView ( EmptyView ( ) )
}
return createURLInput ( with : config )
default :
// 默 认 使 用 通 用 文 本 输 入 组 件
return AnyView (
TextInputView (
content : content ,
isContentFieldFocused : isContentFieldFocused ,
placeholder : getPlaceholderText ( for : qrCodeType ) ,
maxCharacters : getMaxCharacters ( for : qrCodeType )
)
guard let config = textConfig else {
return AnyView ( EmptyView ( ) )
}
return createTextInput (
with : config ,
placeholder : getPlaceholderText ( for : qrCodeType ) ,
maxCharacters : getMaxCharacters ( for : qrCodeType )
)
}
}