Add links for email, phone and domain

This commit is contained in:
Christoph Hagen
2023-11-10 11:22:49 +01:00
parent 43a1789332
commit b0f2bdf430
6 changed files with 82 additions and 15 deletions

View File

@ -1,20 +1,20 @@
import SwiftUI
import SFSafeSymbols
struct LeftImageLabel: View {
let text: String
struct LeftImageLabel<Content>: View where Content: View {
let systemSymbol: SFSymbol
let content: Content
init(_ text: String, systemSymbol: SFSymbol) {
self.text = text
init(systemSymbol: SFSymbol, @ViewBuilder content: () -> Content) {
self.systemSymbol = systemSymbol
self.content = content()
}
var body: some View {
HStack(alignment: .firstTextBaseline, spacing: 0) {
Text(text)
HStack(alignment: .center, spacing: 0) {
content
Image(systemSymbol: systemSymbol)
.frame(width: 20)
}
@ -23,6 +23,6 @@ struct LeftImageLabel: View {
struct LeftImageLabel_Previews: PreviewProvider {
static var previews: some View {
LeftImageLabel("Home address", systemSymbol: .house)
LeftImageLabel(systemSymbol: .house) { Text("Home address") }
}
}

View File

@ -1,5 +1,8 @@
import SwiftUI
import SFSafeSymbols
import PhoneNumberKit
private let emailPattern = #"^\S+@\S+\.\S+$"#
struct TopView: View {
@ -8,6 +11,21 @@ struct TopView: View {
let style: HeaderStyle
let accent: Color
private let phoneNumberKit = PhoneNumberKit()
var isValidEmail: Bool {
info.email.range(of: emailPattern, options: .regularExpression) != nil
}
var isValidPhoneNumber: Bool {
do {
_ = try phoneNumberKit.parse(info.phone)
return true
} catch {
return false
}
}
var body: some View {
GeometryReader { geo in
@ -34,18 +52,26 @@ struct TopView: View {
shadow: style.imageShadowSize,
lineWidth: style.imageBorderWidth)
VStack(alignment: .trailing) {
LeftImageLabel(info.web, systemSymbol: .globe)
.frame(maxHeight: style.iconHeight)
LeftImageLabel(systemSymbol: .globe) {
Link(info.web, destination: URL(string: "https://" + info.web)!)
}
.frame(maxHeight: style.iconHeight)
Spacer()
LeftImageLabel(info.email, systemSymbol: .envelope)
.frame(maxHeight: style.iconHeight)
LeftImageLabel(systemSymbol: .envelope) {
Link(info.email, destination: URL(string: "mailto:" + info.email)!)
.disabled(!isValidEmail)
}
.frame(maxHeight: style.iconHeight)
Spacer()
LeftImageLabel(info.phone, systemSymbol: .phone)
.frame(maxHeight: style.iconHeight)
LeftImageLabel(systemSymbol: .phone) {
Link(info.phone, destination: URL(string: "tel:" + info.phone)!)
.disabled(!isValidPhoneNumber)
}
.frame(maxHeight: style.iconHeight)
Spacer()
HStack(spacing: 0) {
Spacer()
Text(info.github)
Link(info.github, destination: URL(string: "https://" + info.github)!)
Image("Github")
.resizable()
.aspectRatio(1.0, contentMode: .fit)
@ -55,6 +81,7 @@ struct TopView: View {
}
.font(.subheadline)
.frame(width: sideWidth)
.foregroundStyle(.primary)
}
}
}