git.iko.soy/dashared/hookah-tau
on develop help sign in
Добавила экспорт в csv клиентской базы
be45a28Darya Rednikina
Hookah Tau/Controllers/Admin/Clients/ClientsViewController.swift+44−3
@@ -8,7 +8,7 @@
8
8
9import UIKit
9import UIKit
10
10
11−class ClientsViewController: UITableViewController {
11+class ClientsViewController: BaseTableViewController {
12
12
13 // MARK: - Properties
13 // MARK: - Properties
14
14
@@ -27,7 +27,7 @@ class ClientsViewController: UITableViewController {
27 override func viewDidLoad() {
27 override func viewDidLoad() {
28 super.viewDidLoad()
28 super.viewDidLoad()
29
29
30− navigationItem.title = "Клиенты"
30+ setupNavigationBar()
31 clientsService = ClientsService(apiClient: APIClient.shared)
31 clientsService = ClientsService(apiClient: APIClient.shared)
32 }
32 }
33
33
@@ -40,6 +40,16 @@ class ClientsViewController: UITableViewController {
40 super.viewDidAppear(animated)
40 super.viewDidAppear(animated)
41 loadClients()
41 loadClients()
42 }
42 }
43+
44+ // MARK: - Setup
45+
46+ func setupNavigationBar() {
47+
48+ navigationItem.title = "Клиенты"
49+ navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareCSVFile))
50+ }
51+
52+ // MARK: - Api calls
43
53
44 /// Загружаем клиентов кальянки
54 /// Загружаем клиентов кальянки
45 func loadClients() {
55 func loadClients() {
@@ -53,8 +63,39 @@ class ClientsViewController: UITableViewController {
53 })
63 })
54 }
64 }
55
65
66+ // MARK: - Button handlers
56
67
68+ /// https://developer.apple.com/design/human-interface-guidelines/ios/views/activity-views/
69+ @objc func shareCSVFile() {
70+ //Your CSV text
71+ let str = HFunc.main.exportClientsToCSV(dataSource)
72+ let filename = getDocumentsDirectory().appendingPathComponent("clients.csv")
73+
74+ do {
75+ try str.write(toFile: filename, atomically: true, encoding: String.Encoding.utf8)
76+
77+ let fileData = NSURL(fileURLWithPath: filename)
78+
79+ let objectsToShare = [fileData]
80+ let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
81+
82+ self.present(activityVC, animated: true, completion: nil)
83+
84+ } catch {
85+ self.displayAlert(with: "По какой-то причине не могу сохранить файл!")
86+ }
87+ }
88+
89+ func getDocumentsDirectory() -> NSString {
90+ let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
91+ let documentsDirectory = paths[0]
92+ return documentsDirectory as NSString
93+ }
94+}
95+
57− // MARK: - Tableview
96+// MARK: - Tableview
97+
98+extension ClientsViewController {
58
99
59 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
100 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
60 let cell = tableView.dequeueReusableCell(withIdentifier: clientTableViewCellId, for: indexPath) as! ClientTableViewCell
101 let cell = tableView.dequeueReusableCell(withIdentifier: clientTableViewCellId, for: indexPath) as! ClientTableViewCell
Hookah Tau/HFunc.swift+7
@@ -15,6 +15,9 @@ class HFunc {
15
15
16 static let main = HFunc()
16 static let main = HFunc()
17
17
18+ func exportClientsToCSV(_ arr: [Client]) -> String {
19+ return arr.reduce(Client.csvHeading) { return funcToMapClientsIntoString($0, $1) }
20+ }
18
21
19 /// Filtering function to get every periods which intersects with given date
22 /// Filtering function to get every periods which intersects with given date
20 /// - Parameter periods: all periods of bookings (for a particular table)
23 /// - Parameter periods: all periods of bookings (for a particular table)
@@ -58,4 +61,8 @@ class HFunc {
58
61
59 return { (period: ReservationPeriod) in intervalChecking(period.startTime, period.endTime) }
62 return { (period: ReservationPeriod) in intervalChecking(period.startTime, period.endTime) }
60 }
63 }
64+
65+ private func funcToMapClientsIntoString(_ acc: String, _ el: Client) -> String {
66+ return acc + el.toString + "\n"
67+ }
61}
68}
Hookah Tau/Models/User.swift+9
@@ -30,4 +30,13 @@ struct Client: MyCodable {
30 var phoneNumber: String
30 var phoneNumber: String
31 var isAdmin: Bool
31 var isAdmin: Bool
32 var reservationCount: Int
32 var reservationCount: Int
33+ var isBlocked: Bool
34+
35+ static var csvHeading: String {
36+ return "uuid, name, phone, isAdmin, reservationCount, isBlocked\n"
37+ }
38+
39+ var toString: String {
40+ return "\(uuid), \(name ?? "❔"), \(phoneNumber), \(isAdmin), \(reservationCount), \(isBlocked)"
41+ }
33}
42}