Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
fix: 🐛 only show link email and phone number as clickable (#31)
Browse files Browse the repository at this point in the history
* fix: 🐛 trim all blank for tel url format

* fix: 🐛 only show link email and phone number as clickable
  • Loading branch information
ShadowTourist authored Aug 9, 2021
1 parent 399f3b2 commit 395552d
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ extension String {
return nil
}
}

func toTelURLString() -> String {
if lowercased().hasPrefix("tel:") {
return replacingOccurrences(of: " ", with: "", options: .literal)
} else {
return "tel:" + replacingOccurrences(of: " ", with: "", options: .literal)
}
}
}

// MARK: - URL Extension
Expand Down
6 changes: 6 additions & 0 deletions Sources/SAPCAI/Foundation/Model/MessageData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ public protocol ValueData {
var rawValue: String? { get }
}

extension ValueData {
var isClickable: Bool {
type == .link || type == .email || type == .phoneNumber
}
}

/// Enumeration for possible postback types
public enum PostbackDataType: String {
/// text
Expand Down
10 changes: 1 addition & 9 deletions Sources/SAPCAI/UI/Common/SwiftUI/ButtonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct ButtonView: View {
URLNavigation(isUrlSheetPresented: self.$viewModel.urlOpenerData.isLinkModalPresented).performURLNavigation(value: self.button.value)
} else if self.button.dataType == .phoneNumber {
// link navigation feature code
self.viewModel.urlOpenerData.url = optimizePrefix(for: button.value)
self.viewModel.urlOpenerData.url = button.value.toTelURLString()
URLNavigation(isUrlSheetPresented: self.$viewModel.urlOpenerData.isLinkModalPresented).performURLNavigation(value: self.viewModel.urlOpenerData.url)
} else {
self.viewModel.postMessage(type: self.type, postbackData: self.button)
Expand Down Expand Up @@ -51,14 +51,6 @@ struct ButtonView: View {
}
})
}

func optimizePrefix(for phoneNumber: String) -> String {
if phoneNumber.lowercased().hasPrefix("tel:") {
return phoneNumber.filter { !$0.isWhitespace }
} else {
return "tel:" + phoneNumber
}
}
}

#if DEBUG
Expand Down
43 changes: 22 additions & 21 deletions Sources/SAPCAI/UI/Common/SwiftUI/CardSectionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extension CardSectionsView {
var body: some View {
var urlVal = secAttribute.value!
if secAttribute.type == .phoneNumber {
urlVal = "tel:" + secAttribute.value!
urlVal = secAttribute.value!.toTelURLString()
}
if secAttribute.type == .email {
urlVal = "mailto:" + secAttribute.value!
Expand All @@ -55,14 +55,7 @@ extension CardSectionsView {
}
Spacer()
if secAttribute.value != nil {
if secAttribute.type == .text {
Text(secAttribute.value!)
.font(.body)
.foregroundColor(self.themeManager.color(for: .primary1))
.lineLimit(1)
.padding([.top, .bottom], 10)
.padding([.trailing], 16)
} else {
if secAttribute.isClickable {
Button(action: {
self.viewModel.urlOpenerData.url = urlVal
URLNavigation(isUrlSheetPresented: self.$viewModel.urlOpenerData.isLinkModalPresented).performURLNavigation(value: urlVal)
Expand All @@ -73,6 +66,13 @@ extension CardSectionsView {
.padding([.top, .bottom], 10)
.padding([.trailing], 16)
})
} else {
Text(secAttribute.value!)
.font(.body)
.foregroundColor(self.themeManager.color(for: .primary1))
.lineLimit(1)
.padding([.top, .bottom], 10)
.padding([.trailing], 16)
}
}
}
Expand All @@ -86,7 +86,7 @@ extension CardSectionsView {
var body: some View {
var urlVal = secAttribute.value!
if secAttribute.type == .phoneNumber {
urlVal = "tel:" + secAttribute.value!
urlVal = secAttribute.value!.toTelURLString()
}
if secAttribute.type == .email {
urlVal = "mailto:" + secAttribute.value!
Expand All @@ -102,16 +102,7 @@ extension CardSectionsView {
.padding([.trailing], 16)
}
if secAttribute.value != nil {
if secAttribute.type == .text {
Text(secAttribute.value!)
.font(.body)
.foregroundColor(self.themeManager.color(for: .primary1))
.padding([.leading], 16)
.padding([.trailing], 16)
.padding([.bottom], 10)
.lineLimit(2)
.fixedSize(horizontal: false, vertical: true)
} else {
if secAttribute.isClickable {
Button(action: {
self.viewModel.urlOpenerData.url = urlVal
URLNavigation(isUrlSheetPresented: self.$viewModel.urlOpenerData.isLinkModalPresented).performURLNavigation(value: urlVal)
Expand All @@ -124,6 +115,15 @@ extension CardSectionsView {
.lineLimit(2)
.fixedSize(horizontal: false, vertical: true)
})
} else {
Text(secAttribute.value!)
.font(.body)
.foregroundColor(self.themeManager.color(for: .primary1))
.padding([.leading], 16)
.padding([.trailing], 16)
.padding([.bottom], 10)
.lineLimit(2)
.fixedSize(horizontal: false, vertical: true)
}
}
}
Expand All @@ -133,6 +133,7 @@ extension CardSectionsView {

struct CardSectionsView_Previews: PreviewProvider {
static var previews: some View {
CardSectionsView(section: UIModelDataSection("title", [UIModelDataValue(value: "val1", dataType: nil, rawValue: nil, label: nil, valueState: nil)]))
CardSectionsView(section: PreviewData.cardSectionData)
.environmentObject(ThemeManager.shared)
}
}
21 changes: 21 additions & 0 deletions Sources/SAPCAI/UI/PreviewData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@
viewModel.addMessages(contentsOf: [responseData])
return viewModel
}

static var cardSectionData: UIModelDataSection {
func makeDataValue(label: String? = nil, value: String? = nil, dataType: String? = nil) -> UIModelDataValue {
UIModelDataValue(value: value, dataType: dataType, rawValue: nil, label: label, valueState: nil)
}
return UIModelDataSection("title", [makeDataValue(label: "text",
value: "Just text",
dataType: "text"),
makeDataValue(label: "link address",
value: "https://www.sap.com",
dataType: "link"),
makeDataValue(label: "email address",
value: "example@sap.com",
dataType: "email"),
makeDataValue(label: "tel",
value: "+1 23 388913 23 ",
dataType: "phonenumber"),
makeDataValue(label: "real address",
value: "1234, CA, USA",
dataType: "address")])
}
}

#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@testable import SAPCAI
import XCTest

final class SimpleTypeExtensionTests: XCTestCase {
/// String Extension
func testStringtoTelString() {
var telString = "tel: +86 13463637643 "
XCTAssertEqual(telString.toTelURLString(), "tel:+8613463637643")

telString = " +1 650 44 79 539 "
XCTAssertEqual(telString.toTelURLString(), "tel:+16504479539")
}
}

0 comments on commit 395552d

Please sign in to comment.