Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. // Creating a CSV file to export Property (As a test)
  2. @objc private func createCSV() {
  3. let fileName = "\(Auth.auth().currentUser!.displayName!)-spreadsheet-info.csv"
  4. let path = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
  5. var csvText = "Name,Cost,Rent,Building Tax,Property Tax,Yearly Fees,Value Growth,Squared Feet\n"
  6. var totalTaxAndFees: Double = 0
  7. var totalRent: Double = 0
  8. var totalIncome: Double = 0
  9.  
  10.  
  11. for property in properties {
  12. // Creating line for each Property to add to spreadsheet
  13. csvText += "\(property.name),$ \(property.buyingPrice),$ \(property.rent),$ \(property.buildingTax),$ \(property.propertyTax),$ \(property.yearlyFees),\(property.valueGrowth),\(property.squaredFeet)\n"
  14.  
  15.  
  16. // Calculating Total tax and Fees yearly
  17. totalTaxAndFees += ((property.propertyTax + property.buildingTax) * 12) + property.yearlyFees
  18. // Calculating Total rent Yearly
  19. totalRent += property.rent * 12
  20.  
  21. }
  22. totalIncome = totalRent - totalTaxAndFees
  23. csvText += " \nYearly\nTax + Fees,$ \(totalTaxAndFees)\nRent,$ \(totalRent)\nTotal,$ \(totalIncome)\n"
  24. csvText += " \nMothly\nTax + Fees,$ \(totalTaxAndFees / 12)\nRent,$ \(totalRent / 12)\nTotal,$ \(totalIncome / 12)\n"
  25.  
  26. do {
  27. try csvText.write(to: path!, atomically: true, encoding: String.Encoding.utf8)
  28.  
  29. let vc = UIActivityViewController(activityItems: [path], applicationActivities: [])
  30. vc.excludedActivityTypes = [
  31. UIActivity.ActivityType.assignToContact,
  32. UIActivity.ActivityType.saveToCameraRoll,
  33. UIActivity.ActivityType.postToFlickr,
  34. UIActivity.ActivityType.postToVimeo,
  35. UIActivity.ActivityType.postToTencentWeibo,
  36. UIActivity.ActivityType.postToTwitter,
  37. UIActivity.ActivityType.postToFacebook,
  38. UIActivity.ActivityType.openInIBooks
  39. ]
  40. present(vc, animated: true, completion: nil)
  41.  
  42. } catch {
  43.  
  44. print("Failed to create file")
  45. print("\(error)")
  46. }
  47. }
  48. }
  49.  
  50.  
  51. First creates a file name using the user's username.
  52.  
  53. Create the first row of cells to sort Name, Cost, Rent, Building Tax, Property Tax, Yearly Fees, Value Growth, Squared Feet.
  54.  
  55. Create a for loop for the user's properties' data.
  56.  
  57. For each property, create a row with the same properties indicated in the first row (Name, Cost, Rent, Building Tax, Property Tax, Yearly Fees, Value Growth, Squared Feet.)
  58.  
  59. At the bottom of all the properties, fill overall user properties' data.
  60.  
  61. Lastly, provide the user with options to share the created file.
  62.  
  63. Add a throw error in case anything fails.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement