Refactored for improved table flexibility

master
Laurent 6 years ago
parent 9fca41a8db
commit 3e3651827c
  1. 5
      TournamentStats/UI/components/DataSourceWrapper.swift
  2. 2
      TournamentStats/report/structures/CountryCounter.swift
  3. 13
      TournamentStats/report/structures/CumulatedResults.swift
  4. 4
      TournamentStats/report/structures/CumulatedWins.swift
  5. 4
      TournamentStats/report/structures/PlayerResult.swift
  6. 4
      TournamentStats/report/structures/TournamentCounter.swift
  7. 4
      TournamentStats/report/structures/TournamentRepresentable.swift
  8. 4
      TournamentStats/report/structures/TournamentStats.swift
  9. 4
      TournamentStats/report/structures/TournamentWinner.swift
  10. 8
      TournamentStats/utils/ColumnRepresentable.swift

@ -12,12 +12,13 @@ import UIKit
class DataSourceWrapper<T : ColumnRepresentable> : NSObject, UITableViewDataSource, UITableViewDelegate { class DataSourceWrapper<T : ColumnRepresentable> : NSObject, UITableViewDataSource, UITableViewDelegate {
var columnRepresentables: [T] var columnRepresentables: [T]
let columnDescriptors = T.columnDescriptors() var columnDescriptors: [ColumnDescriptor] // = T.columnDescriptors()
var totalWidthWeigth: CGFloat = 0.0 var totalWidthWeigth: CGFloat = 0.0
fileprivate var _maxRows: Int? fileprivate var _maxRows: Int?
init(array: [T], maxRows: Int? = nil) { init(array: [T], maxRows: Int? = nil) {
self.columnRepresentables = array self.columnRepresentables = array
self.columnDescriptors = array.first?.columnDescriptors() ?? []
self.totalWidthWeigth = self.columnDescriptors.map { $0.widthWeight }.reduce(0, +) self.totalWidthWeigth = self.columnDescriptors.map { $0.widthWeight }.reduce(0, +)
if let max = maxRows, max > columnRepresentables.count { if let max = maxRows, max > columnRepresentables.count {
self._maxRows = nil self._maxRows = nil
@ -27,7 +28,7 @@ class DataSourceWrapper<T : ColumnRepresentable> : NSObject, UITableViewDataSour
} }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("numberOfRowsInSection = \(self.columnRepresentables.count)") // print("numberOfRowsInSection = \(self.columnRepresentables.count)")
if let max = self._maxRows { if let max = self._maxRows {
return 1 + max return 1 + max
} else { } else {

@ -37,7 +37,7 @@ final class CountryCounter : HTMLRepresentable, Aggregeable {
self.counter += 1 self.counter += 1
} }
static func columnDescriptors() -> [ColumnDescriptor] { func columnDescriptors() -> [ColumnDescriptor] {
return [ColumnDescriptor(header: "Country", number: false, widthWeight: 1.0), return [ColumnDescriptor(header: "Country", number: false, widthWeight: 1.0),
ColumnDescriptor(header: "Cashes", number: true, widthWeight: 1.0)] ColumnDescriptor(header: "Cashes", number: true, widthWeight: 1.0)]
} }

@ -10,7 +10,9 @@ import Foundation
class CumulatedResults : HTMLRepresentable, ColumnRepresentable { class CumulatedResults : HTMLRepresentable, ColumnRepresentable {
var player: Player let player: Player
let average: Bool
var total: Double = 0.0 var total: Double = 0.0
var numberOfCashes: Int = 0 var numberOfCashes: Int = 0
var results: [Result] = [] var results: [Result] = []
@ -19,6 +21,7 @@ class CumulatedResults : HTMLRepresentable, ColumnRepresentable {
init(player: Player, average: Bool = false) { init(player: Player, average: Bool = false) {
self.player = player self.player = player
self.average = average
for result in player.results { for result in player.results {
self.add(result: result) self.add(result: result)
@ -40,9 +43,11 @@ class CumulatedResults : HTMLRepresentable, ColumnRepresentable {
} }
} }
static func columnDescriptors() -> [ColumnDescriptor] { func columnDescriptors() -> [ColumnDescriptor] {
let totalColumn = self.average ? "Average Earnings" : "Total Earnings"
return [ColumnDescriptor(header: "Name", number: false, widthWeight: 2.0), return [ColumnDescriptor(header: "Name", number: false, widthWeight: 2.0),
ColumnDescriptor(header: "Total Earnings", number: true, widthWeight: 1.2), ColumnDescriptor(header: totalColumn, number: true, widthWeight: 1.2),
ColumnDescriptor(header: "Cashes", number: true, widthWeight: 0.8)] ColumnDescriptor(header: "Cashes", number: true, widthWeight: 0.8)]
} }
@ -54,7 +59,7 @@ class CumulatedResults : HTMLRepresentable, ColumnRepresentable {
return [name, self.total.currencyFormatted, "\(self.numberOfCashes)"] return [name, self.total.currencyFormatted, "\(self.numberOfCashes)"]
} }
// static func htmlHeaders() -> String { // func htmlHeaders() -> String {
// var strings: [String] = [] // var strings: [String] = []
// strings.append("Name") // strings.append("Name")
//// strings.append("Places (Year)") //// strings.append("Places (Year)")

@ -38,7 +38,7 @@ class CumulatedWins : HTMLRepresentable, ColumnRepresentable {
} }
} }
static func columnDescriptors() -> [ColumnDescriptor] { func columnDescriptors() -> [ColumnDescriptor] {
return [ColumnDescriptor(header: "Name", number: false, widthWeight: 2.0), return [ColumnDescriptor(header: "Name", number: false, widthWeight: 2.0),
ColumnDescriptor(header: "Wins", number: true, widthWeight: 1.0), ColumnDescriptor(header: "Wins", number: true, widthWeight: 1.0),
ColumnDescriptor(header: "Total Earnings", number: true, widthWeight: 1.5)] ColumnDescriptor(header: "Total Earnings", number: true, widthWeight: 1.5)]
@ -48,7 +48,7 @@ class CumulatedWins : HTMLRepresentable, ColumnRepresentable {
return [self.player.formattedName, "\(self.winsCount)", self.total.currencyFormatted] return [self.player.formattedName, "\(self.winsCount)", self.total.currencyFormatted]
} }
// static func htmlHeaders() -> String { // func htmlHeaders() -> String {
// var strings: [String] = [] // var strings: [String] = []
// strings.append("Name") // strings.append("Name")
//// strings.append("Places (Year)") //// strings.append("Places (Year)")

@ -13,7 +13,7 @@ struct PlayerResult : HTMLRepresentable {
var tournament: Tournament var tournament: Tournament
var result: Result var result: Result
static func columnDescriptors() -> [ColumnDescriptor] { func columnDescriptors() -> [ColumnDescriptor] {
return [ColumnDescriptor(header: "Player", number: false, widthWeight: 1.0), return [ColumnDescriptor(header: "Player", number: false, widthWeight: 1.0),
ColumnDescriptor(header: "Place", number: true, widthWeight: 1.0), ColumnDescriptor(header: "Place", number: true, widthWeight: 1.0),
ColumnDescriptor(header: "Earnings", number: true, widthWeight: 1.0), ColumnDescriptor(header: "Earnings", number: true, widthWeight: 1.0),
@ -31,7 +31,7 @@ struct PlayerResult : HTMLRepresentable {
] ]
} }
static func htmlHeaders() -> String { func htmlHeaders() -> String {
var strings: String = "" var strings: String = ""
strings.append("<td width=\"30%\">Player</td>") strings.append("<td width=\"30%\">Player</td>")
strings.append("<td width=\"80\">Place</td>") strings.append("<td width=\"80\">Place</td>")

@ -22,7 +22,7 @@ class DistributionCounter : HTMLRepresentable {
self.counter += 1 self.counter += 1
} }
static func columnDescriptors() -> [ColumnDescriptor] { func columnDescriptors() -> [ColumnDescriptor] {
return [ColumnDescriptor(header: "Tournament", number: false, widthWeight: 2.0), return [ColumnDescriptor(header: "Tournament", number: false, widthWeight: 2.0),
ColumnDescriptor(header: "Counter", number: true, widthWeight: 1.0)] ColumnDescriptor(header: "Counter", number: true, widthWeight: 1.0)]
} }
@ -38,7 +38,7 @@ class DistributionCounter : HTMLRepresentable {
return PieChartDataEntry(value: Double(self.counter), label: name) return PieChartDataEntry(value: Double(self.counter), label: name)
} }
// static func htmlHeaders() -> String { // func htmlHeaders() -> String {
// var strings: [String] = [] // var strings: [String] = []
// strings.append("Tournament") // strings.append("Tournament")
// strings.append("Counter") // strings.append("Counter")

@ -12,7 +12,7 @@ struct TournamentRepresentable : HTMLRepresentable {
var tournament: Tournament var tournament: Tournament
static func columnDescriptors() -> [ColumnDescriptor] { func columnDescriptors() -> [ColumnDescriptor] {
return [ColumnDescriptor(header: "#", number: true, widthWeight: 0.3), return [ColumnDescriptor(header: "#", number: true, widthWeight: 0.3),
ColumnDescriptor(header: "Buy-in", number: true, widthWeight: 0.5), ColumnDescriptor(header: "Buy-in", number: true, widthWeight: 0.5),
ColumnDescriptor(header: "Event", number: false, widthWeight: 2.6), ColumnDescriptor(header: "Event", number: false, widthWeight: 2.6),
@ -30,7 +30,7 @@ struct TournamentRepresentable : HTMLRepresentable {
] ]
} }
static func htmlHeaders() -> String { func htmlHeaders() -> String {
var strings: [String] = [] var strings: [String] = []
// strings.append("Date") // strings.append("Date")
strings.append("#") strings.append("#")

@ -27,7 +27,7 @@ class TournamentStats : NSObject, HTMLRepresentable, UITableViewDelegate, UITabl
} }
static func columnDescriptors() -> [ColumnDescriptor] { func columnDescriptors() -> [ColumnDescriptor] {
return [] return []
} }
@ -35,7 +35,7 @@ class TournamentStats : NSObject, HTMLRepresentable, UITableViewDelegate, UITabl
return [] return []
} }
static func htmlHeaders() -> String { func htmlHeaders() -> String {
return "" return ""
} }

@ -13,7 +13,7 @@ struct TournamentWinner : HTMLRepresentable {
var tournament: Tournament var tournament: Tournament
var result: Result var result: Result
static func columnDescriptors() -> [ColumnDescriptor] { func columnDescriptors() -> [ColumnDescriptor] {
return [ColumnDescriptor(header: "#", number: true, widthWeight: 0.2), return [ColumnDescriptor(header: "#", number: true, widthWeight: 0.2),
ColumnDescriptor(header: "Buy-in", number: true, widthWeight: 1.0), ColumnDescriptor(header: "Buy-in", number: true, widthWeight: 1.0),
ColumnDescriptor(header: "Event", number: false, widthWeight: 1.0), ColumnDescriptor(header: "Event", number: false, widthWeight: 1.0),
@ -30,7 +30,7 @@ struct TournamentWinner : HTMLRepresentable {
] ]
} }
static func htmlHeaders() -> String { func htmlHeaders() -> String {
var header = "" var header = ""
header.append("<td width=\"50\">#</td>") header.append("<td width=\"50\">#</td>")
header.append("<td width=\"100\">Buy-in</td>") header.append("<td width=\"100\">Buy-in</td>")

@ -34,7 +34,7 @@ struct ColumnDescriptor {
} }
protocol ColumnRepresentable { protocol ColumnRepresentable {
static func columnDescriptors() -> [ColumnDescriptor] func columnDescriptors() -> [ColumnDescriptor]
func cellValues() -> [String] func cellValues() -> [String]
var chartDataEntry: ChartDataEntry { get } var chartDataEntry: ChartDataEntry { get }
var pieChartDataEntry: PieChartDataEntry { get } var pieChartDataEntry: PieChartDataEntry { get }
@ -107,13 +107,13 @@ protocol Aggregeable {
} }
protocol HTMLRepresentable : ColumnRepresentable { protocol HTMLRepresentable : ColumnRepresentable {
static func htmlHeaders() -> String func htmlHeaders() -> String
func html() -> String func html() -> String
} }
extension HTMLRepresentable { extension HTMLRepresentable {
static func htmlHeaders() -> String { func htmlHeaders() -> String {
let all = self.columnDescriptors().map { $0.header }.joined(separator: "</td><td>") let all = self.columnDescriptors().map { $0.header }.joined(separator: "</td><td>")
return "<tr class=\"table-header\"><td>\(all)</td></tr>" return "<tr class=\"table-header\"><td>\(all)</td></tr>"
} }
@ -131,7 +131,7 @@ extension Array where Element : HTMLRepresentable {
var html = "<table class=\"wp-block-table alignwide\">\n" var html = "<table class=\"wp-block-table alignwide\">\n"
html.append("<thead>") html.append("<thead>")
html.append(Element.htmlHeaders()) html.append(self.first?.htmlHeaders() ?? "")
html.append("</thead>\n<tbody>") html.append("</thead>\n<tbody>")
let max = limit ?? Int.max let max = limit ?? Int.max
for (index, rep) in self.enumerated() { for (index, rep) in self.enumerated() {

Loading…
Cancel
Save