generated from StanfordSpezi/SpeziTemplateApplication
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96d0069
commit 4d50852
Showing
5 changed files
with
156 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// | ||
// EmailHelper.swift | ||
// Balance | ||
// | ||
// Created by Gonzalo Perisset on 17/07/2023. | ||
// | ||
|
||
import MessageUI | ||
import SwiftUI | ||
|
||
// swiftlint:disable modifier_order | ||
// swiftlint:disable force_unwrapping | ||
// swiftlint:disable unused_closure_parameter | ||
// swiftlint:disable legacy_objc_type | ||
class EmailHelper: NSObject { | ||
static let shared = EmailHelper() | ||
private override init() {} | ||
} | ||
|
||
extension EmailHelper { | ||
func send(subject: String, body: String, file: URL, to: [String]) { | ||
let scenes = UIApplication.shared.connectedScenes | ||
let windowScene = scenes.first as? UIWindowScene | ||
|
||
guard let viewController = windowScene?.windows.first?.rootViewController else { | ||
return | ||
} | ||
|
||
if !MFMailComposeViewController.canSendMail() { | ||
let subjectEncoded = subject.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! | ||
let bodyEncoded = body.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! | ||
let mails = to.joined(separator: ",") | ||
|
||
let alert = UIAlertController(title: "Cannot open Mail!", message: "", preferredStyle: .actionSheet) | ||
|
||
var haveExternalMailbox = false | ||
|
||
if let url = createEmailUrl(to: mails, subject: subjectEncoded, body: bodyEncoded), UIApplication.shared.canOpenURL(url) { | ||
haveExternalMailbox = true | ||
alert.addAction(UIAlertAction(title: "Gmail", style: .default, handler: { action in | ||
UIApplication.shared.open(url) | ||
})) | ||
} | ||
|
||
if haveExternalMailbox { | ||
alert.message = "Would you like to open an external mailbox?" | ||
} else { | ||
alert.message = "Please add your mail to Settings before using the mail service." | ||
|
||
if let settingsUrl = URL(string: UIApplication.openSettingsURLString), | ||
UIApplication.shared.canOpenURL(settingsUrl) { | ||
alert.addAction(UIAlertAction(title: "Open Settings App", style: .default, handler: { action in | ||
UIApplication.shared.open(settingsUrl) | ||
})) | ||
} | ||
} | ||
|
||
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) | ||
viewController.present(alert, animated: true, completion: nil) | ||
return | ||
} | ||
|
||
let mailCompose = MFMailComposeViewController() | ||
mailCompose.setSubject(subject) | ||
mailCompose.setMessageBody(body, isHTML: false) | ||
mailCompose.setToRecipients(to) | ||
mailCompose.mailComposeDelegate = self | ||
|
||
if let data = NSData(contentsOf: file) { | ||
mailCompose.addAttachmentData(data as Data, mimeType: "application/csv", fileName: "export.csv") | ||
} | ||
|
||
viewController.present(mailCompose, animated: true, completion: nil) | ||
} | ||
|
||
private func createEmailUrl(to: String, subject: String, body: String) -> URL? { | ||
let subjectEncoded = subject.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! | ||
let bodyEncoded = body.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! | ||
|
||
let gmailUrl = URL(string: "googlegmail://co?to=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)") | ||
let outlookUrl = URL(string: "ms-outlook://compose?to=\(to)&subject=\(subjectEncoded)") | ||
let yahooMail = URL(string: "ymail://mail/compose?to=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)") | ||
let sparkUrl = URL(string: "readdle-spark://compose?recipient=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)") | ||
let defaultUrl = URL(string: "mailto:\(to)?subject=\(subjectEncoded)&body=\(bodyEncoded)") | ||
|
||
if let gmailUrl = gmailUrl, UIApplication.shared.canOpenURL(gmailUrl) { | ||
return gmailUrl | ||
} else if let outlookUrl = outlookUrl, UIApplication.shared.canOpenURL(outlookUrl) { | ||
return outlookUrl | ||
} else if let yahooMail = yahooMail, UIApplication.shared.canOpenURL(yahooMail) { | ||
return yahooMail | ||
} else if let sparkUrl = sparkUrl, UIApplication.shared.canOpenURL(sparkUrl) { | ||
return sparkUrl | ||
} | ||
|
||
return defaultUrl | ||
} | ||
} | ||
|
||
// MARK: - MFMailComposeViewControllerDelegate | ||
extension EmailHelper: MFMailComposeViewControllerDelegate { | ||
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { | ||
controller.dismiss(animated: true, completion: nil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,15 +25,15 @@ struct ProfileView: View { | |
@EnvironmentObject var noteStore: NoteStore | ||
@EnvironmentObject var drawStore: DrawStore | ||
@EnvironmentObject var coloringStore: ColoringStore | ||
#if DEMO | ||
#if DEMO | ||
@EnvironmentObject var logStore: ActivityLogStore | ||
#endif | ||
#endif | ||
@State private var displayName = "" | ||
@State private var avatar = "" | ||
@State private var email = "" | ||
@State private var patientID = "" | ||
@State private var showAlert = false | ||
|
||
var body: some View { | ||
ActivityLogContainer { | ||
ZStack { | ||
|
@@ -136,6 +136,19 @@ struct ProfileView: View { | |
} | ||
|
||
var shareOption: some View { | ||
Button(action: { | ||
EmailHelper.shared.send( | ||
subject: "Balance Export", | ||
body: "ParticipantID: " + self.patientID + " - Name: " + self.displayName + " - Email:" + self.email + "\n", | ||
file: convertToCSV(), | ||
to: ["[email protected]"] | ||
) | ||
}) { | ||
ProfileCellView(image: "directcurrent", text: "Share data") | ||
} | ||
} | ||
|
||
var shareLink: some View { | ||
ShareLink( | ||
item: convertToCSV(), | ||
subject: Text("Balance Export") | ||
|
@@ -175,10 +188,19 @@ struct ProfileView: View { | |
var logoutOption: some View { | ||
Button { | ||
print("Logout") | ||
// dismiss() | ||
#if DEMO | ||
UserImageCache.remove(key: self.patientID.appending("UploadedArray")) | ||
UserImageCache.remove(key: self.patientID.appending("RemovedArray")) | ||
UserImageCache.remove(key: self.patientID.appending("FavoritesArray")) | ||
logStore.removeStore() | ||
noteStore.removeStore() | ||
drawStore.removeStore() | ||
coloringStore.removeStore() | ||
#endif | ||
// dismiss() | ||
authModel.signOut() | ||
completedOnboardingFlow = false | ||
// account.signedIn = false | ||
// account.signedIn = false | ||
} label: { | ||
ProfileCellView(image: "figure.walk.motion", text: "Logout") | ||
} | ||
|
@@ -281,7 +303,7 @@ struct ProfileView: View { | |
} | ||
} | ||
|
||
func convertToCSV() -> String { | ||
func convertToCSV() -> URL { | ||
var noteAsCSV = "ParticipantID: " + self.patientID + " - " + self.displayName + " - " + self.email + "\n" | ||
noteAsCSV.append(contentsOf: "id, activeStartTime, activeEndTime, activeDuration, actionTime, actionDescription\n") | ||
|
||
|
@@ -291,6 +313,16 @@ struct ProfileView: View { | |
} | ||
} | ||
|
||
return noteAsCSV | ||
let fileManager = FileManager.default | ||
do { | ||
let path = try fileManager.url(for: .documentDirectory, in: .allDomainsMask, appropriateFor: nil, create: false) | ||
let fileURL = path.appendingPathComponent("export.csv") | ||
try noteAsCSV.write(to: fileURL, atomically: true, encoding: .utf8) | ||
|
||
return fileURL | ||
} catch { | ||
print("error creating file") | ||
} | ||
return URL(fileURLWithPath: "") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters