Dynamically select number of columns to display

pull/498/head
Alinson S. Xavier 7 years ago
parent 33bae657a3
commit 5b2b554a7a

@ -29,6 +29,7 @@ class MainScreenDataSource(val preferences: Preferences,
val checkmarks: MutableMap<Habit, CheckmarkList>,
val taskRunner: TaskRunner) {
val maxNumberOfButtons = 60
private val today = LocalDate(2019, 3, 30) /* TODO */
data class Data(val habits: List<Habit>,
@ -51,8 +52,8 @@ class MainScreenDataSource(val preferences: Preferences,
val recentCheckmarks = filtered.associate { habit ->
val allValues = checkmarks.getValue(habit).getValuesUntil(today)
if (allValues.size <= 7) habit to allValues
else habit to allValues.subList(0, 7)
if (allValues.size <= maxNumberOfButtons) habit to allValues
else habit to allValues.subList(0, maxNumberOfButtons)
}
if (!preferences.showCompleted) {

@ -20,17 +20,23 @@
import UIKit
class MainScreenCell : UITableViewCell {
var ring: ComponentView
var ring = ComponentView(frame: CGRect(), component: nil)
var label = UILabel()
var buttons: [ComponentView] = []
var theme = LightTheme()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
ring = ComponentView(frame: CGRect(), component: nil)
super.init(style: .default, reuseIdentifier: reuseIdentifier)
let size = CGFloat(theme.checkmarkButtonSize)
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
func update(habit: Habit, values: [KotlinInt], theme: Theme, nButtons: Int) {
if buttons.count != nButtons {
buttons.removeAll()
for v in contentView.subviews { v.removeFromSuperview() }
let size = CGFloat(theme.checkmarkButtonSize)
let stack = UIStackView(frame: contentView.frame)
stack.autoresizingMask = [.flexibleWidth, .flexibleHeight]
stack.axis = .horizontal
@ -47,7 +53,7 @@ class MainScreenCell : UITableViewCell {
label.heightAnchor.constraint(equalToConstant: size).isActive = true
stack.addArrangedSubview(label)
for _ in 1...4 {
for _ in 1...nButtons {
let btn = ComponentView(frame: frame, component: nil)
btn.backgroundColor = .white
btn.widthAnchor.constraint(equalToConstant: size).isActive = true
@ -56,11 +62,7 @@ class MainScreenCell : UITableViewCell {
stack.addArrangedSubview(btn)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
func update(habit: Habit, values: [KotlinInt], theme: Theme) {
var color = theme.color(paletteIndex: habit.color.index)
if habit.isArchived { color = theme.mediumContrastTextColor }
label.text = habit.name
@ -129,6 +131,7 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
]
tableView.register(MainScreenCell.self, forCellReuseIdentifier: "cell")
tableView.backgroundColor = theme.headerBackgroundColor.uicolor
computeNumberOfButtons(Double(view.frame.width))
}
override func viewDidAppear(_ animated: Bool) {
@ -145,7 +148,7 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MainScreenCell
let habit = data!.habits[indexPath.row]
cell.update(habit: habit, values: data!.checkmarkValues[habit]!, theme: theme)
cell.update(habit: habit, values: data!.checkmarkValues[habit]!, theme: theme, nButtons: nButtons)
return cell
}
@ -163,7 +166,7 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return CGFloat(theme.checkmarkButtonSize) + 1
return CGFloat(theme.checkmarkButtonSize) + 3
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
@ -171,6 +174,11 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
self.navigationController?.pushViewController(DetailScreenController(habit: habit, backend: backend), animated: true)
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
computeNumberOfButtons(Double(size.width))
reload()
}
@objc func onCreateHabitClicked() {
self.navigationController?.pushViewController(EditHabitController(), animated: true)
}
@ -237,6 +245,15 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
func onDataChanged(newData: MainScreenDataSource.Data) {
data = newData
reload()
}
func computeNumberOfButtons(_ width: Double) {
nButtons = Int((width - 220) / theme.checkmarkButtonSize)
nButtons = min(nButtons, Int(dataSource.maxNumberOfButtons))
}
func reload() {
let sections = NSIndexSet(indexesIn: NSMakeRange(0, self.tableView.numberOfSections))
tableView.reloadSections(sections as IndexSet, with: .automatic)
}

Loading…
Cancel
Save