EmployeeDirectory-SwiftUI/EmployeeDirectory/Views/ContactButtonView.swift
2025-03-04 14:54:57 -06:00

81 lines
2.0 KiB
Swift

//
// ContactButtonView.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 3/4/25.
//
import SwiftUI
enum ContactType {
case phone, email
}
struct ContactButtonView: View {
let contactType: ContactType
let contactValue: String
let enabled: Bool
init(contactType: ContactType, contactValue: String, enabled: Bool = true) {
self.contactType = contactType
self.contactValue = contactValue
self.enabled = enabled
}
var body: some View {
if !enabled {
Label(labelText(for: contactValue), systemImage: iconName)
.foregroundColor(.gray)
.font(.caption)
} else {
Button(action: {
if let url = URL(string: urlString(for: contactValue)) {
UIApplication.shared.open(url)
}
}) {
Label(labelText(for: contactValue), systemImage: iconName)
.foregroundColor(.blue)
.font(.caption)
}
}
}
// MARK: - Helpers
private var iconName: String {
switch contactType {
case .phone:
return "phone.fill"
case .email:
return "envelope.fill"
}
}
private func labelText(for value: String) -> String {
switch contactType {
case .phone:
return "Call \(value)"
case .email:
return "Email \(value)"
}
}
private func urlString(for value: String) -> String {
switch contactType {
case .phone:
return "tel://\(value)"
case .email:
return "mailto:\(value)"
}
}
}
#Preview {
VStack (alignment: .leading) {
Text("Phone Number:")
ContactButtonView(contactType: .phone, contactValue: MockEmployeeService.sample.phoneNumber!.formatUSNumber()).padding()
Text("Email Address:")
ContactButtonView(contactType: .email, contactValue: MockEmployeeService.sample.emailAddress).padding()
}
}