Skip to content

Commit

Permalink
Merge branch 'master' into Adorkable-forkable-feature/ShareFromDashboard
Browse files Browse the repository at this point in the history
* master:
  + Used fileURL for sharing a GPX file Share from Files Table
  • Loading branch information
merlos committed Sep 22, 2018
2 parents 291611f + 4347974 commit acd7555
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 68 deletions.
4 changes: 0 additions & 4 deletions OpenGpxTracker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
3C0C335821535C6400C1AF4B /* GPXActivityItemProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C0C335721535C6400C1AF4B /* GPXActivityItemProvider.swift */; };
8913CD8C1BDB0A9A009EC729 /* PreferencesTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8913CD8B1BDB0A9A009EC729 /* PreferencesTableViewController.swift */; };
8913CD8E1BDB2477009EC729 /* PreferencesTableViewControllerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8913CD8D1BDB2477009EC729 /* PreferencesTableViewControllerDelegate.swift */; };
893BEC8619C7E62200C77354 /* GPXWaypoint+MKAnnotation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 893BEC8519C7E62200C77354 /* GPXWaypoint+MKAnnotation.swift */; };
Expand Down Expand Up @@ -60,7 +59,6 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
3C0C335721535C6400C1AF4B /* GPXActivityItemProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GPXActivityItemProvider.swift; sourceTree = "<group>"; };
44E786790A477838358F948C /* Pods-OpenGpxTracker.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OpenGpxTracker.debug.xcconfig"; path = "Pods/Target Support Files/Pods-OpenGpxTracker/Pods-OpenGpxTracker.debug.xcconfig"; sourceTree = "<group>"; };
8913CD8B1BDB0A9A009EC729 /* PreferencesTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferencesTableViewController.swift; sourceTree = "<group>"; };
8913CD8D1BDB2477009EC729 /* PreferencesTableViewControllerDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferencesTableViewControllerDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -149,7 +147,6 @@
895F04B31A7143CE004BEE8A /* GPXExtentCoordinates.swift */,
89F8749A1BBB7362004EF41A /* GPXTrack+length.swift */,
89F8749E1BBCB97F004EF41A /* GPXRoot+length.swift */,
3C0C335721535C6400C1AF4B /* GPXActivityItemProvider.swift */,
);
name = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -473,7 +470,6 @@
899D677219CE8B3600C88A0A /* StopWatch.swift in Sources */,
893BEC8619C7E62200C77354 /* GPXWaypoint+MKAnnotation.swift in Sources */,
DC4908A31C4A332E009484AE /* MapViewDelegate.swift in Sources */,
3C0C335821535C6400C1AF4B /* GPXActivityItemProvider.swift in Sources */,
898EED3619C6402900B4B207 /* GPXFilesTableViewController.swift in Sources */,
DC4908A11C4A136F009484AE /* TrackerButton.swift in Sources */,
89F8749F1BBCB97F004EF41A /* GPXRoot+length.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "OpenGpxTracker/ViewController.swift"
timestampString = "558404755.490386"
timestampString = "559270362.465259"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "993"
endingLineNumber = "993"
startingLineNumber = "998"
endingLineNumber = "998"
landmarkName = "locationManager(_:didUpdateLocations:)"
landmarkType = "7">
</BreakpointContent>
Expand All @@ -26,11 +26,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "OpenGpxTracker/ViewController.swift"
timestampString = "558404755.49068"
timestampString = "559270362.465775"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "993"
endingLineNumber = "993"
startingLineNumber = "998"
endingLineNumber = "998"
landmarkName = "locationManager(_:didUpdateLocations:)"
landmarkType = "7">
</BreakpointContent>
Expand All @@ -42,11 +42,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "OpenGpxTracker/ViewController.swift"
timestampString = "558404755.490948"
timestampString = "559270362.46622"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "993"
endingLineNumber = "993"
startingLineNumber = "998"
endingLineNumber = "998"
landmarkName = "locationManager(_:didUpdateLocations:)"
landmarkType = "7">
</BreakpointContent>
Expand All @@ -58,11 +58,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "OpenGpxTracker/ViewController.swift"
timestampString = "558404755.49121"
timestampString = "559270362.466681"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "993"
endingLineNumber = "993"
startingLineNumber = "998"
endingLineNumber = "998"
landmarkName = "locationManager(_:didUpdateLocations:)"
landmarkType = "7">
</BreakpointContent>
Expand Down
29 changes: 0 additions & 29 deletions OpenGpxTracker/GPXActivityItemProvider.swift

This file was deleted.

17 changes: 12 additions & 5 deletions OpenGpxTracker/GPXFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,15 @@ class GPXFileManager: NSObject {
return FileManager.default.fileExists(atPath: fileURL.path)
}

class func save(_ filename: String, gpxContents: String) {
//check if name exists
let finalFileURL: URL = self.URLForFilename(filename)
//Saves the GPX contents to the specified URL
class func saveToURL(_ fileURL: URL, gpxContents: String) {
//save file
print("Saving file at path: \(finalFileURL)")
print("Saving file at path: \(fileURL)")
// write gpx to file
var writeError: NSError?
let saved: Bool
do {
try gpxContents.write(toFile: finalFileURL.path, atomically: true, encoding: String.Encoding.utf8)
try gpxContents.write(toFile: fileURL.path, atomically: true, encoding: String.Encoding.utf8)
saved = true
} catch let error as NSError {
writeError = error
Expand All @@ -86,6 +85,14 @@ class GPXFileManager: NSObject {
print("[ERROR] GPXFileManager:save: \(error.localizedDescription)")
}
}

}

//Saves in the default folder the filename with the gpxContents
class func save(_ filename: String, gpxContents: String) {
//check if name exists
let fileURL: URL = self.URLForFilename(filename)
GPXFileManager.saveToURL(fileURL, gpxContents: gpxContents)
}

class func removeFile(_ filename: String) {
Expand Down
27 changes: 23 additions & 4 deletions OpenGpxTracker/GPXFilesTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,17 @@ class GPXFilesTableViewController: UITableViewController, UINavigationBarDelegat

/// Displays an action sheet with the actions for that file (Send it by email, Load in map and Delete)
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let sheet = UIActionSheet()
sheet.title = "Select option"
sheet.addButton(withTitle: "Send by email")
sheet.addButton(withTitle: "Load in Map")
sheet.addButton(withTitle: "Share")
sheet.addButton(withTitle: "Cancel")
sheet.addButton(withTitle: "Delete")
sheet.cancelButtonIndex = 2
sheet.destructiveButtonIndex = 3
sheet.cancelButtonIndex = 3
sheet.destructiveButtonIndex = 4

sheet.delegate = self
sheet.show(in: self.view)
self.selectedRowIndex = (indexPath as NSIndexPath).row
Expand All @@ -156,6 +158,10 @@ class GPXFilesTableViewController: UITableViewController, UINavigationBarDelegat
return gpxFilesFound
}

internal func fileListObjectTitle(_ rowIndex: Int) -> String {
return fileList.object(at: rowIndex) as? NSString as String? ?? ""
}

// MARK: Action Sheet - Actions

internal func actionSheetCancel(_ actionSheet: UIActionSheet) {
Expand Down Expand Up @@ -190,6 +196,17 @@ class GPXFilesTableViewController: UITableViewController, UINavigationBarDelegat
self.dismiss(animated: true, completion: nil)

}
/// Shares file at `rowIndex`
internal func actionShareFileAtIndex(_ rowIndex: Int) {
guard let filename: String = fileList.object(at: rowIndex) as? String else {
print("Unable to get filename at row \(rowIndex), cannot respond to \(type(of: self))didSelectRowAt")
return
}
let fileURL: URL = GPXFileManager.URLForFilename(filename)
let activityViewController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
}

/// Sends the file at `rowIndex` by email
internal func actionSendEmailWithAttachment(_ rowIndex: Int) {
guard let filename: String = fileList.object(at: rowIndex) as? String else {
Expand Down Expand Up @@ -226,8 +243,10 @@ extension GPXFilesTableViewController: UIActionSheetDelegate{
case 1:
self.actionLoadFileAtIndex(self.selectedRowIndex)
case 2:
self.actionShareFileAtIndex(self.selectedRowIndex)
case 3:
print("ActionSheet: Cancel")
case 3: //Delete
case 4: //Delete
self.actionDeleteFileAtIndex(self.selectedRowIndex)
default: //cancel
print("action Sheet do nothing")
Expand Down
35 changes: 21 additions & 14 deletions OpenGpxTracker/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,15 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
NotificationCenter.default.removeObserver(self)
}

/// returns a string with the format of current date dd-MMM-yyyy-HHmm' (20-Jun-2018-1133)
///

func defaultFilename() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyyy-HHmm"
print("fileName:" + dateFormatter.string(from: Date()))
return dateFormatter.string(from: Date())
}

///
/// Called when the application Becomes active (background -> foreground) this function verifies if
Expand Down Expand Up @@ -630,22 +639,20 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
self.present(navController, animated: true) { () -> Void in }
}


/// Opens an Activity View Controller to share the file
func openShare() {
print("share")

//Create a temporary file
let filename = lastGpxFilename.isEmpty ? defaultFilename() : lastGpxFilename
let gpxString: String = self.map.exportToGPXString()
guard let gpxData: Data = gpxString.data(using: .utf8) else {
print("Unable to prepare GPX data for sharing")
return
}

let activityItems: [AnyObject] = [
GPXActivityItemProvider(gpxFileData: gpxData)
]

let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
let tmpFile = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(filename).gpx")
GPXFileManager.saveToURL(tmpFile, gpxContents: gpxString)

//Call Share activity View controller
let activityViewController = UIActivityViewController(activityItems: [tmpFile], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)

This comment has been minimized.

Copy link
@yoiang

yoiang Sep 22, 2018

Contributor

Should we delete the temporary file on completion? If I'm reading correctly will it create a different one each unique minute you share, each that will be left over?


}

///
Expand Down Expand Up @@ -752,13 +759,13 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
return
}

let alert = UIAlertView(title: "Save as", message: "Enter GPX session name", delegate: self, cancelButtonTitle: "Continue tracking")
let alert = UIAlertView(title: "Save as", message: "Enter GPX session name", delegate: self, cancelButtonTitle: "Cancel")

alert.addButton(withTitle: "Save")
alert.alertViewStyle = .plainTextInput
alert.tag = kSaveSessionAlertViewTag
alert.show()
alert.textField(at: 0)?.text = lastGpxFilename
alert.textField(at: 0)?.text = lastGpxFilename.isEmpty ? defaultFilename() : lastGpxFilename
//alert.textFieldAtIndex(0)?.selectAll(self)
}

Expand Down Expand Up @@ -838,7 +845,7 @@ extension ViewController: UIAlertViewDelegate {
print("Save canceled")

case 1: //Save
let filename = (alertView.textField(at: 0)?.text!.utf16.count == 0) ? " " : alertView.textField(at: 0)?.text
let filename = (alertView.textField(at: 0)?.text!.utf16.count == 0) ? defaultFilename() : alertView.textField(at: 0)?.text
print("Save File \(String(describing: filename))")
//export to a file
let gpxString = self.map.exportToGPXString()
Expand Down

0 comments on commit acd7555

Please sign in to comment.