Commit 57a3919a by Luis Zarza

call and send sms swipe actions added in contact profile screen

parent 75f30cac
......@@ -23,7 +23,6 @@ class ContactProfileVC: UIViewController {
var contactProfileData:ContactProfile?
@IBOutlet weak var nameLbl: UILabel!
@IBOutlet weak var profilePic: UIImageView!
@IBOutlet weak var contactPhoneNumbersTable: UITableView!
override func viewDidLoad() {
......@@ -78,6 +77,63 @@ class ContactProfileVC: UIViewController {
}
}
func callAction(at indexPath:IndexPath) -> UIContextualAction{
//get phoneNumber from datasource
let phoneNumber = contactProfileData!.phonNumbersList[indexPath.row]
//format phone number, replace invalid characters
let formatedNumber = formatPhoneNumber(phoneNumber:phoneNumber)
let action = UIContextualAction(style: .normal, title: "Call", handler: { (action,view,completion) in
//make a phone call
guard let phoneCallURL = URL(string: "tel://+\(formatedNumber)") else { return }
UIApplication.shared.open(phoneCallURL)
//show alert
let alert = UIAlertController(title: "Calling \(phoneNumber)", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Finish", style: .default, handler: nil))
self.present(alert, animated: true)
//hide leading swipe actions
completion(true)
})
//set icon and backgorund
action.image = UIImage(systemName: "phone.fill.arrow.up.right")
action.backgroundColor = .green
return action
}
func messageAction(at indexPath:IndexPath) -> UIContextualAction{
let contact = contactProfileData!.phonNumbersList[indexPath.row]
let action = UIContextualAction(style: .normal, title: "Call", handler: { (action,view,completion) in
//show alert
let alert = UIAlertController(title: "Send a SMS To: \(contact)", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Finish", style: .default, handler: nil))
self.present(alert, animated: true)
//hide leading swipe actions
completion(true)
})
action.image = UIImage(systemName: "envelope.fill")
action.backgroundColor = .orange
return action
}
func formatPhoneNumber(phoneNumber:String)->String{
var formatedNumber = phoneNumber.replacingOccurrences(of: "(", with: "")
formatedNumber = phoneNumber.replacingOccurrences(of: ")", with: "")
formatedNumber = phoneNumber.replacingOccurrences(of: "+", with: "")
formatedNumber = phoneNumber.replacingOccurrences(of: " ", with: "-")
return formatedNumber
}
}
extension ContactProfileVC: UITableViewDelegate, UITableViewDataSource{
......@@ -115,4 +171,16 @@ extension ContactProfileVC: UITableViewDelegate, UITableViewDataSource{
}
}
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
if indexPath.section == 0{
let call = callAction(at: indexPath)
let sendMessage = messageAction(at: indexPath)
return UISwipeActionsConfiguration(actions: [call,sendMessage])
}
return nil
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment