From 262b68edb7712718289d071218b48f8591430bd3 Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 5 Aug 2019 11:35:34 +0200 Subject: [PATCH] added ColumnRepresentable to serve as basis for various exports --- TournamentStats.xcodeproj/project.pbxproj | 20 ++++-- .../UI/reports/InfographyView.swift | 22 ++++++ .../report/structures/CountryCounter.swift | 38 ++++++++--- .../report/structures/CumulatedResults.swift | 49 +++++++++----- .../report/structures/PlayerResult.swift | 25 ++++--- .../report/structures/TournamentCounter.swift | 31 ++++++--- .../structures/TournamentRepresentable.swift | 36 +++++++--- .../report/structures/TournamentStats.swift | 8 +++ .../report/structures/TournamentWinner.swift | 34 +++++++--- ...VUtils.swift => ColumnRepresentable.swift} | 67 ++++++++++++------- 10 files changed, 232 insertions(+), 98 deletions(-) create mode 100644 TournamentStats/UI/reports/InfographyView.swift rename TournamentStats/utils/{CSVUtils.swift => ColumnRepresentable.swift} (76%) diff --git a/TournamentStats.xcodeproj/project.pbxproj b/TournamentStats.xcodeproj/project.pbxproj index 8c3c9c8..976220e 100644 --- a/TournamentStats.xcodeproj/project.pbxproj +++ b/TournamentStats.xcodeproj/project.pbxproj @@ -36,6 +36,7 @@ 4D2F1C5B22CC92D1007C639E /* event84 in Resources */ = {isa = PBXBuildFile; fileRef = 4D2F1C4322CC92D1007C639E /* event84 */; }; 4D30463322F4232600DA86C4 /* event57 in Resources */ = {isa = PBXBuildFile; fileRef = 4D30463122F4232600DA86C4 /* event57 */; }; 4D30463422F4232600DA86C4 /* event78 in Resources */ = {isa = PBXBuildFile; fileRef = 4D30463222F4232600DA86C4 /* event78 */; }; + 4D39B6F122F829A500625E31 /* InfographyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D39B6F022F829A500625E31 /* InfographyView.swift */; }; 4DA5CA1E22AD078A00AC628E /* CountryCounter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DA5CA1D22AD078A00AC628E /* CountryCounter.swift */; }; 4DDEF11422AE4FB900F4D7C1 /* TournamentStats.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DDEF11322AE4FB900F4D7C1 /* TournamentStats.swift */; }; 4DF7608422A3FB96004B0EF1 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DF7608322A3FB96004B0EF1 /* AppDelegate.swift */; }; @@ -55,7 +56,7 @@ 4DF760BA22A524F4004B0EF1 /* Formatters.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DF760B922A524F4004B0EF1 /* Formatters.swift */; }; 4DF760BC22A5270E004B0EF1 /* Queries.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DF760BB22A5270E004B0EF1 /* Queries.swift */; }; 4DF760BF22A560AA004B0EF1 /* FileWriter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DF760BE22A560AA004B0EF1 /* FileWriter.swift */; }; - 4DF760C222A561FF004B0EF1 /* CSVUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DF760C122A561FF004B0EF1 /* CSVUtils.swift */; }; + 4DF760C222A561FF004B0EF1 /* ColumnRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DF760C122A561FF004B0EF1 /* ColumnRepresentable.swift */; }; 4DF760C522A56451004B0EF1 /* event1 in Resources */ = {isa = PBXBuildFile; fileRef = 4DF760C422A56451004B0EF1 /* event1 */; }; 4DF760C922A56497004B0EF1 /* Seed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DF760C822A56497004B0EF1 /* Seed.swift */; }; 4DF760CB22A56765004B0EF1 /* event4 in Resources */ = {isa = PBXBuildFile; fileRef = 4DF760CA22A56765004B0EF1 /* event4 */; }; @@ -167,6 +168,7 @@ 4D2F1C4322CC92D1007C639E /* event84 */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = event84; sourceTree = ""; }; 4D30463122F4232600DA86C4 /* event57 */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = event57; sourceTree = ""; }; 4D30463222F4232600DA86C4 /* event78 */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = event78; sourceTree = ""; }; + 4D39B6F022F829A500625E31 /* InfographyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfographyView.swift; sourceTree = ""; }; 4DA5CA1D22AD078A00AC628E /* CountryCounter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CountryCounter.swift; sourceTree = ""; }; 4DDEF11322AE4FB900F4D7C1 /* TournamentStats.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TournamentStats.swift; sourceTree = ""; }; 4DF7608022A3FB96004B0EF1 /* TournamentStats.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TournamentStats.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -190,7 +192,7 @@ 4DF760B922A524F4004B0EF1 /* Formatters.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Formatters.swift; sourceTree = ""; }; 4DF760BB22A5270E004B0EF1 /* Queries.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Queries.swift; sourceTree = ""; }; 4DF760BE22A560AA004B0EF1 /* FileWriter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileWriter.swift; sourceTree = ""; }; - 4DF760C122A561FF004B0EF1 /* CSVUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CSVUtils.swift; sourceTree = ""; }; + 4DF760C122A561FF004B0EF1 /* ColumnRepresentable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColumnRepresentable.swift; sourceTree = ""; }; 4DF760C422A56451004B0EF1 /* event1 */ = {isa = PBXFileReference; lastKnownFileType = text; path = event1; sourceTree = ""; }; 4DF760C822A56497004B0EF1 /* Seed.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Seed.swift; sourceTree = ""; }; 4DF760CA22A56765004B0EF1 /* event4 */ = {isa = PBXFileReference; lastKnownFileType = text; path = event4; sourceTree = ""; }; @@ -282,6 +284,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 4D97941222F827A5004A2D7F /* reports */ = { + isa = PBXGroup; + children = ( + 4D39B6F022F829A500625E31 /* InfographyView.swift */, + ); + path = reports; + sourceTree = ""; + }; 4DF7607722A3FB96004B0EF1 = { isa = PBXGroup; children = ( @@ -458,6 +468,7 @@ 4DF760C022A561A8004B0EF1 /* UI */ = { isa = PBXGroup; children = ( + 4D97941222F827A5004A2D7F /* reports */, 4DF7608722A3FB96004B0EF1 /* DetailViewController.swift */, 4DF7608522A3FB96004B0EF1 /* MasterViewController.swift */, ); @@ -467,7 +478,7 @@ 4DF760C322A56207004B0EF1 /* utils */ = { isa = PBXGroup; children = ( - 4DF760C122A561FF004B0EF1 /* CSVUtils.swift */, + 4DF760C122A561FF004B0EF1 /* ColumnRepresentable.swift */, 4DF760BE22A560AA004B0EF1 /* FileWriter.swift */, ); path = utils; @@ -713,10 +724,11 @@ buildActionMask = 2147483647; files = ( 4DF7614822A59660004B0EF1 /* ReportGenerator.swift in Sources */, - 4DF760C222A561FF004B0EF1 /* CSVUtils.swift in Sources */, + 4DF760C222A561FF004B0EF1 /* ColumnRepresentable.swift in Sources */, 4DF760B022A47C74004B0EF1 /* Importer.swift in Sources */, 4DF7614A22A66675004B0EF1 /* TournamentWinner.swift in Sources */, 4D0F103722C4C08F005F797A /* ChipCount.swift in Sources */, + 4D39B6F122F829A500625E31 /* InfographyView.swift in Sources */, 4DF760B322A47CAE004B0EF1 /* Realm+Extensions.swift in Sources */, 4DF7608822A3FB96004B0EF1 /* DetailViewController.swift in Sources */, 4DF7615122A7AECA004B0EF1 /* TournamentRepresentable.swift in Sources */, diff --git a/TournamentStats/UI/reports/InfographyView.swift b/TournamentStats/UI/reports/InfographyView.swift new file mode 100644 index 0000000..e05edae --- /dev/null +++ b/TournamentStats/UI/reports/InfographyView.swift @@ -0,0 +1,22 @@ +// +// InfographyView.swift +// TournamentStats +// +// Created by Laurent Morvillier on 05/08/2019. +// Copyright © 2019 Stax River. All rights reserved. +// + +import Foundation +import UIKit + +class InfographyView : UIView { + + override init(frame: CGRect) { + super.init(frame: frame) + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + +} diff --git a/TournamentStats/report/structures/CountryCounter.swift b/TournamentStats/report/structures/CountryCounter.swift index a61768b..fce2de6 100644 --- a/TournamentStats/report/structures/CountryCounter.swift +++ b/TournamentStats/report/structures/CountryCounter.swift @@ -22,16 +22,11 @@ class CountryCounter : HTMLRepresentable { self.counter += 1 } - static func htmlHeaders() -> String { - var strings: [String] = [] - strings.append("Country") - strings.append("Cashes") - let all = strings.joined(separator: "") - return "\(all)" + static func headers() -> [String] { + return ["Country", "Cashes"] } - func html() -> String { - + func colums() -> [String] { let formattedCountry: String if let flagoji = Locale.current.flagoji(from: self.country) { formattedCountry = "\(flagoji) \(self.country)" @@ -42,8 +37,31 @@ class CountryCounter : HTMLRepresentable { var strings: [String] = [] strings.append(formattedCountry) strings.append("\(counter)") - let all = strings.joined(separator: "") - return "\(all)" + return strings } +// static func htmlHeaders() -> String { +// var strings: [String] = [] +// strings.append("Country") +// strings.append("Cashes") +// let all = strings.joined(separator: "") +// return "\(all)" +// } +// +// func html() -> String { +// +// let formattedCountry: String +// if let flagoji = Locale.current.flagoji(from: self.country) { +// formattedCountry = "\(flagoji) \(self.country)" +// } else { +// formattedCountry = self.country +// } +// +// var strings: [String] = [] +// strings.append(formattedCountry) +// strings.append("\(counter)") +// let all = strings.joined(separator: "") +// return "\(all)" +// } + } diff --git a/TournamentStats/report/structures/CumulatedResults.swift b/TournamentStats/report/structures/CumulatedResults.swift index 4afb3da..82db2be 100644 --- a/TournamentStats/report/structures/CumulatedResults.swift +++ b/TournamentStats/report/structures/CumulatedResults.swift @@ -8,8 +8,13 @@ import Foundation - -class CumulatedResults : HTMLRepresentable { +class CumulatedResults : HTMLRepresentable, ColumnRepresentable { + + enum Fields : String, CaseIterable { + case name = "Name" + case earnings = "Total Earnings" + case cashes = "Cashes" + } var player: Player var total: Double = 0.0 @@ -33,24 +38,32 @@ class CumulatedResults : HTMLRepresentable { self.results.append(result) } - static func htmlHeaders() -> String { - var strings: [String] = [] - strings.append("Name") -// strings.append("Places (Year)") - strings.append("Total Earnings") - strings.append("Cashes") - let all = strings.joined(separator: "") - return "\(all)" + static func headers() -> [String] { + return Fields.allCases.map { $0.rawValue } } - func html() -> String { - var strings: [String] = [] - strings.append(self.player.formattedName) -// strings.append(self.results.map { "\($0.rank.rankFormatted) (\($0.tournaments.first?.date.year ?? ""))" }.joined(separator: ", ")) - strings.append(self.total.currencyFormatted) - strings.append("\(self.numberOfCashes)") - let all = strings.joined(separator: "") - return "\(all)" + func colums() -> [String] { + return [self.player.formattedName, self.total.currencyFormatted, "\(self.numberOfCashes)"] } +// static func htmlHeaders() -> String { +// var strings: [String] = [] +// strings.append("Name") +//// strings.append("Places (Year)") +// strings.append("Total Earnings") +// strings.append("Cashes") +// let all = strings.joined(separator: "") +// return "\(all)" +// } +// +// func html() -> String { +// var strings: [String] = [] +// strings.append(self.player.formattedName) +//// strings.append(self.results.map { "\($0.rank.rankFormatted) (\($0.tournaments.first?.date.year ?? ""))" }.joined(separator: ", ")) +// strings.append(self.total.currencyFormatted) +// strings.append("\(self.numberOfCashes)") +// let all = strings.joined(separator: "") +// return "\(all)" +// } + } diff --git a/TournamentStats/report/structures/PlayerResult.swift b/TournamentStats/report/structures/PlayerResult.swift index a93e994..28d98ff 100644 --- a/TournamentStats/report/structures/PlayerResult.swift +++ b/TournamentStats/report/structures/PlayerResult.swift @@ -13,6 +13,20 @@ struct PlayerResult : HTMLRepresentable { var tournament: Tournament var result: Result + static func headers() -> [String] { + return ["Player", "Place", "Earnings", "#", "Event"] + } + + func colums() -> [String] { + return [ + self.result.player?.formattedName ?? "", + self.result.rank.rankFormatted, + result.earnings.currencyFormatted, + "\(tournament.number)", + "\(tournament.buyin.currencyFormatted) \(tournament.name)" + ] + } + static func htmlHeaders() -> String { var strings: String = "" strings.append("Player") @@ -23,15 +37,4 @@ struct PlayerResult : HTMLRepresentable { return "\(strings)" } - func html() -> String { - var strings: [String] = [] - strings.append(self.result.player?.formattedName ?? "") - strings.append(self.result.rank.rankFormatted) - strings.append(result.earnings.currencyFormatted) - strings.append("\(tournament.number)") - strings.append("\(tournament.buyin.currencyFormatted) \(tournament.name)") - let all = strings.joined(separator: "") - return "\(all)" - } - } diff --git a/TournamentStats/report/structures/TournamentCounter.swift b/TournamentStats/report/structures/TournamentCounter.swift index a31fcf3..d2316ee 100644 --- a/TournamentStats/report/structures/TournamentCounter.swift +++ b/TournamentStats/report/structures/TournamentCounter.swift @@ -21,21 +21,32 @@ class DistributionCounter : HTMLRepresentable { self.counter += 1 } - static func htmlHeaders() -> String { - var strings: [String] = [] - strings.append("Tournament") - strings.append("Counter") - let all = strings.joined(separator: "") - return "\(all)" + static func headers() -> [String] { + return ["Tournament", "Counter"] } - func html() -> String { - + func colums() -> [String] { var strings: [String] = [] strings.append(self.name) strings.append("\(self.counter)") - let all = strings.joined(separator: "") - return "\(all)" + return strings } +// static func htmlHeaders() -> String { +// var strings: [String] = [] +// strings.append("Tournament") +// strings.append("Counter") +// let all = strings.joined(separator: "") +// return "\(all)" +// } +// +// func html() -> String { +// +// var strings: [String] = [] +// strings.append(self.name) +// strings.append("\(self.counter)") +// let all = strings.joined(separator: "") +// return "\(all)" +// } + } diff --git a/TournamentStats/report/structures/TournamentRepresentable.swift b/TournamentStats/report/structures/TournamentRepresentable.swift index 94f89f5..99c6519 100644 --- a/TournamentStats/report/structures/TournamentRepresentable.swift +++ b/TournamentStats/report/structures/TournamentRepresentable.swift @@ -12,6 +12,20 @@ struct TournamentRepresentable : HTMLRepresentable { var tournament: Tournament + static func headers() -> [String] { + return ["#", "Buy-in", "Event", "Prizepool", "Entries"] + } + + func colums() -> [String] { + return [ + "\(tournament.number)", + tournament.buyin.currencyFormatted, + tournament.name, + tournament.prizepool.currencyFormatted, + "\(tournament.entries)" + ] + } + static func htmlHeaders() -> String { var strings: [String] = [] // strings.append("Date") @@ -24,16 +38,16 @@ struct TournamentRepresentable : HTMLRepresentable { return "\(all)" } - func html() -> String { - var strings: [String] = [] -// strings.append(tournament.date.year) - strings.append("\(tournament.number)") - strings.append(tournament.buyin.currencyFormatted) - strings.append(tournament.name) - strings.append(tournament.prizepool.currencyFormatted) - strings.append("\(tournament.entries)") - let all = strings.joined(separator: "") - return "\(all)" - } +// func html() -> String { +// var strings: [String] = [] +//// strings.append(tournament.date.year) +// strings.append("\(tournament.number)") +// strings.append(tournament.buyin.currencyFormatted) +// strings.append(tournament.name) +// strings.append(tournament.prizepool.currencyFormatted) +// strings.append("\(tournament.entries)") +// let all = strings.joined(separator: "") +// return "\(all)" +// } } diff --git a/TournamentStats/report/structures/TournamentStats.swift b/TournamentStats/report/structures/TournamentStats.swift index 06f570d..786b88b 100644 --- a/TournamentStats/report/structures/TournamentStats.swift +++ b/TournamentStats/report/structures/TournamentStats.swift @@ -15,6 +15,14 @@ struct TournamentStats : HTMLRepresentable { var prizepool: Double var itmValue: Double + static func headers() -> [String] { + return [] + } + + func colums() -> [String] { + return [] + } + static func htmlHeaders() -> String { return "" } diff --git a/TournamentStats/report/structures/TournamentWinner.swift b/TournamentStats/report/structures/TournamentWinner.swift index 123a789..8884fee 100644 --- a/TournamentStats/report/structures/TournamentWinner.swift +++ b/TournamentStats/report/structures/TournamentWinner.swift @@ -13,6 +13,19 @@ struct TournamentWinner : HTMLRepresentable { var tournament: Tournament var result: Result + static func headers() -> [String] { + return ["#", "Buy-in", "Event", "Player", "Prize"] + } + + func colums() -> [String] { + return ["\(tournament.number)", + tournament.buyin.currencyFormatted, + tournament.name, + result.player?.formattedName ?? "", + result.earnings.currencyFormatted + ] + } + static func htmlHeaders() -> String { var header = "" header.append("#") @@ -23,14 +36,15 @@ struct TournamentWinner : HTMLRepresentable { return "\(header)" } - func html() -> String { - var strings: [String] = [] - strings.append("\(tournament.number)") - strings.append(tournament.buyin.currencyFormatted) - strings.append(tournament.name) - strings.append(result.player?.formattedName ?? "") - strings.append(result.earnings.currencyFormatted) - let all = strings.joined(separator: "") - return "\(all)" - } +// func html() -> String { +// var strings: [String] = [] +// strings.append("\(tournament.number)") +// strings.append(tournament.buyin.currencyFormatted) +// strings.append(tournament.name) +// strings.append(result.player?.formattedName ?? "") +// strings.append(result.earnings.currencyFormatted) +// let all = strings.joined(separator: "") +// return "\(all)" +// } + } diff --git a/TournamentStats/utils/CSVUtils.swift b/TournamentStats/utils/ColumnRepresentable.swift similarity index 76% rename from TournamentStats/utils/CSVUtils.swift rename to TournamentStats/utils/ColumnRepresentable.swift index 5f3e636..3803364 100644 --- a/TournamentStats/utils/CSVUtils.swift +++ b/TournamentStats/utils/ColumnRepresentable.swift @@ -8,23 +8,51 @@ import Foundation -extension Array where Element : CSVRepresentable { +public protocol ColumnRepresentable { + static func headers() -> [String] + func colums() -> [String] +} + +public protocol HTMLRepresentable : ColumnRepresentable { + static func htmlHeaders() -> String + func html() -> String +} + +extension HTMLRepresentable { - func writeCSV(file: String, limit: Int? = 10) { + static func htmlHeaders() -> String { + let all = self.headers().joined(separator: "") + return "\(all)" + } + + func html() -> String { + let all = self.colums().joined(separator: "") + return "\(all)" + } + +} + +extension Array where Element : HTMLRepresentable { + + func writeHTML(fileName: String, limit: Int? = 10) { - var string = Element.csvHeaders() + var html = "\n" + html.append("") + html.append(Element.htmlHeaders()) + html.append("\n") let max = limit ?? Int.max for (index, rep) in self.enumerated() { - string.append("/n") - string.append(rep.csv()) + html.append("\n") + html.append(rep.html()) - if index >= max { + if index + 1 >= max { break } } + html.append("\n
") do { - try FileWriter.writeToDocumentDirectory(content: string, fileName: file) + try FileWriter.writeToDocumentDirectory(content: html, fileName: fileName) } catch { print(error) } @@ -33,27 +61,24 @@ extension Array where Element : CSVRepresentable { } -extension Array where Element : HTMLRepresentable { + +extension Array where Element : CSVRepresentable { - func writeHTML(fileName: String, limit: Int? = 10) { + func writeCSV(file: String, limit: Int? = 10) { - var html = "\n" - html.append("") - html.append(Element.htmlHeaders()) - html.append("\n") + var string = Element.csvHeaders() let max = limit ?? Int.max for (index, rep) in self.enumerated() { - html.append("\n") - html.append(rep.html()) + string.append("/n") + string.append(rep.csv()) - if index + 1 >= max { + if index >= max { break } } - html.append("\n
") do { - try FileWriter.writeToDocumentDirectory(content: html, fileName: fileName) + try FileWriter.writeToDocumentDirectory(content: string, fileName: file) } catch { print(error) } @@ -66,9 +91,3 @@ public protocol CSVRepresentable { static func csvHeaders() -> String func csv() -> String } - -public protocol HTMLRepresentable { - static func htmlHeaders() -> String - func html() -> String -} -