Skip to content

Commit

Permalink
BAL-155-GP-ShareFix
Browse files Browse the repository at this point in the history
  • Loading branch information
gperissetcelteeka committed Jul 19, 2023
1 parent 96d0069 commit 4d50852
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Balance.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
6CF6BEE92A31103A00A337F3 /* CodableArray+RawRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CF6BEE42A31103A00A337F3 /* CodableArray+RawRepresentable.swift */; };
6CF6BEEA2A31103A00A337F3 /* Binding+Negate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CF6BEE52A31103A00A337F3 /* Binding+Negate.swift */; };
6CF6BEF52A37B55A00A337F3 /* TrackCellView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CF6BEF42A37B55900A337F3 /* TrackCellView.swift */; };
6CF978992A65EC970046E90A /* EmailHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CF978982A65EC970046E90A /* EmailHelper.swift */; };
6CFB02412A438AFA000045E1 /* Photo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CFB02402A438AFA000045E1 /* Photo.swift */; };
6CFB02432A438CC6000045E1 /* PhotoArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CFB02422A438CC6000045E1 /* PhotoArray.swift */; };
6CFB02452A438D4E000045E1 /* Category.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CFB02442A438D4E000045E1 /* Category.swift */; };
Expand Down Expand Up @@ -338,6 +339,7 @@
6CF6BEE42A31103A00A337F3 /* CodableArray+RawRepresentable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CodableArray+RawRepresentable.swift"; sourceTree = "<group>"; };
6CF6BEE52A31103A00A337F3 /* Binding+Negate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Binding+Negate.swift"; sourceTree = "<group>"; };
6CF6BEF42A37B55900A337F3 /* TrackCellView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrackCellView.swift; sourceTree = "<group>"; };
6CF978982A65EC970046E90A /* EmailHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmailHelper.swift; sourceTree = "<group>"; };
6CFB02402A438AFA000045E1 /* Photo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Photo.swift; sourceTree = "<group>"; };
6CFB02422A438CC6000045E1 /* PhotoArray.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhotoArray.swift; sourceTree = "<group>"; };
6CFB02442A438D4E000045E1 /* Category.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Category.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -579,6 +581,7 @@
6C5523F629F80E8800F802D9 /* ShowHideSecureField.swift */,
6C5523F829F8126000F802D9 /* ProfileCellView.swift */,
6C47339029FC5C7D00DC72D0 /* PersonalDataView.swift */,
6CF978982A65EC970046E90A /* EmailHelper.swift */,
);
path = Profile;
sourceTree = "<group>";
Expand Down Expand Up @@ -1119,6 +1122,7 @@
2720083D29A7E5830052908D /* PastDiaryEntry.swift in Sources */,
5EF9D78429B5AFF1006C3B22 /* ActivityStorageManager.swift in Sources */,
6CFDD58A2A0D9ECE009D7E56 /* ColoringHomeView.swift in Sources */,
6CF978992A65EC970046E90A /* EmailHelper.swift in Sources */,
6C0A4BEE29E9B053003007C7 /* AvatarSelectionView.swift in Sources */,
6CF510C029C8FD25008A2F55 /* LocationView.swift in Sources */,
6CF6BEE72A31103A00A337F3 /* FeatureFlags.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Balance/Distraction/Gallery/LookImages/ImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct ImageView: View {
ZStack {
backgroundColor.edgesIgnoringSafeArea(.all)
VStack {
HeaderMenu(title: "Distraction")
HeaderMenu(title: "Look at Images")

Check warning on line 56 in Balance/Distraction/Gallery/LookImages/ImageView.swift

View check run for this annotation

Codecov / codecov/patch

Balance/Distraction/Gallery/LookImages/ImageView.swift#L56

Added line #L56 was not covered by tests
Spacer()
tabImages
.tabViewStyle(.page(indexDisplayMode: .never))
Expand Down
105 changes: 105 additions & 0 deletions Balance/Profile/EmailHelper.swift
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() {}

Check warning on line 17 in Balance/Profile/EmailHelper.swift

View check run for this annotation

Codecov / codecov/patch

Balance/Profile/EmailHelper.swift#L17

Added line #L17 was not covered by tests
}

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)
}

Check warning on line 74 in Balance/Profile/EmailHelper.swift

View check run for this annotation

Codecov / codecov/patch

Balance/Profile/EmailHelper.swift#L21-L74

Added lines #L21 - L74 were not covered by tests

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
}

Check warning on line 97 in Balance/Profile/EmailHelper.swift

View check run for this annotation

Codecov / codecov/patch

Balance/Profile/EmailHelper.swift#L76-L97

Added lines #L76 - L97 were not covered by tests
}

// MARK: - MFMailComposeViewControllerDelegate
extension EmailHelper: MFMailComposeViewControllerDelegate {
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}

Check warning on line 104 in Balance/Profile/EmailHelper.swift

View check run for this annotation

Codecov / codecov/patch

Balance/Profile/EmailHelper.swift#L102-L104

Added lines #L102 - L104 were not covered by tests
}
46 changes: 39 additions & 7 deletions Balance/Profile/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
}

Check warning on line 149 in Balance/Profile/ProfileView.swift

View check run for this annotation

Codecov / codecov/patch

Balance/Profile/ProfileView.swift#L139-L149

Added lines #L139 - L149 were not covered by tests

var shareLink: some View {

Check warning on line 151 in Balance/Profile/ProfileView.swift

View check run for this annotation

Codecov / codecov/patch

Balance/Profile/ProfileView.swift#L151

Added line #L151 was not covered by tests
ShareLink(
item: convertToCSV(),
subject: Text("Balance Export")
Expand Down Expand Up @@ -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()

Check warning on line 200 in Balance/Profile/ProfileView.swift

View check run for this annotation

Codecov / codecov/patch

Balance/Profile/ProfileView.swift#L191-L200

Added lines #L191 - L200 were not covered by tests
authModel.signOut()
completedOnboardingFlow = false
// account.signedIn = false
// account.signedIn = false

Check warning on line 203 in Balance/Profile/ProfileView.swift

View check run for this annotation

Codecov / codecov/patch

Balance/Profile/ProfileView.swift#L203

Added line #L203 was not covered by tests
} label: {
ProfileCellView(image: "figure.walk.motion", text: "Logout")
}
Expand Down Expand Up @@ -281,7 +303,7 @@ struct ProfileView: View {
}
}

func convertToCSV() -> String {
func convertToCSV() -> URL {

Check warning on line 306 in Balance/Profile/ProfileView.swift

View check run for this annotation

Codecov / codecov/patch

Balance/Profile/ProfileView.swift#L306

Added line #L306 was not covered by tests
var noteAsCSV = "ParticipantID: " + self.patientID + " - " + self.displayName + " - " + self.email + "\n"
noteAsCSV.append(contentsOf: "id, activeStartTime, activeEndTime, activeDuration, actionTime, actionDescription\n")

Expand All @@ -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: "")

Check warning on line 326 in Balance/Profile/ProfileView.swift

View check run for this annotation

Codecov / codecov/patch

Balance/Profile/ProfileView.swift#L316-L326

Added lines #L316 - L326 were not covered by tests
}
}
7 changes: 7 additions & 0 deletions Balance/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,12 @@
<string>6897d8e8934a48c19a9f2129c7349a0c</string>
<key>SpotifySecretID</key>
<string>d389fb0d126f4960ba79b031145f6f49</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlegmail</string>
<string>ms-outlook</string>
<string>readdle-spark</string>
<string>ymail</string>
</array>
</dict>
</plist>

0 comments on commit 4d50852

Please sign in to comment.