mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Dynamically select number of columns to display
This commit is contained in:
@@ -29,6 +29,7 @@ class MainScreenDataSource(val preferences: Preferences,
|
|||||||
val checkmarks: MutableMap<Habit, CheckmarkList>,
|
val checkmarks: MutableMap<Habit, CheckmarkList>,
|
||||||
val taskRunner: TaskRunner) {
|
val taskRunner: TaskRunner) {
|
||||||
|
|
||||||
|
val maxNumberOfButtons = 60
|
||||||
private val today = LocalDate(2019, 3, 30) /* TODO */
|
private val today = LocalDate(2019, 3, 30) /* TODO */
|
||||||
|
|
||||||
data class Data(val habits: List<Habit>,
|
data class Data(val habits: List<Habit>,
|
||||||
@@ -51,8 +52,8 @@ class MainScreenDataSource(val preferences: Preferences,
|
|||||||
|
|
||||||
val recentCheckmarks = filtered.associate { habit ->
|
val recentCheckmarks = filtered.associate { habit ->
|
||||||
val allValues = checkmarks.getValue(habit).getValuesUntil(today)
|
val allValues = checkmarks.getValue(habit).getValuesUntil(today)
|
||||||
if (allValues.size <= 7) habit to allValues
|
if (allValues.size <= maxNumberOfButtons) habit to allValues
|
||||||
else habit to allValues.subList(0, 7)
|
else habit to allValues.subList(0, maxNumberOfButtons)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!preferences.showCompleted) {
|
if (!preferences.showCompleted) {
|
||||||
|
|||||||
@@ -20,47 +20,49 @@
|
|||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
class MainScreenCell : UITableViewCell {
|
class MainScreenCell : UITableViewCell {
|
||||||
var ring: ComponentView
|
var ring = ComponentView(frame: CGRect(), component: nil)
|
||||||
var label = UILabel()
|
var label = UILabel()
|
||||||
var buttons: [ComponentView] = []
|
var buttons: [ComponentView] = []
|
||||||
var theme = LightTheme()
|
|
||||||
|
|
||||||
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
||||||
ring = ComponentView(frame: CGRect(), component: nil)
|
|
||||||
|
|
||||||
super.init(style: .default, reuseIdentifier: reuseIdentifier)
|
super.init(style: .default, reuseIdentifier: reuseIdentifier)
|
||||||
let size = CGFloat(theme.checkmarkButtonSize)
|
|
||||||
|
|
||||||
let stack = UIStackView(frame: contentView.frame)
|
|
||||||
stack.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
||||||
stack.axis = .horizontal
|
|
||||||
stack.distribution = .fill
|
|
||||||
stack.alignment = .center
|
|
||||||
contentView.addSubview(stack)
|
|
||||||
|
|
||||||
ring.backgroundColor = .white
|
|
||||||
ring.widthAnchor.constraint(equalToConstant: size * 0.75).isActive = true
|
|
||||||
ring.heightAnchor.constraint(equalToConstant: size).isActive = true
|
|
||||||
stack.addArrangedSubview(ring)
|
|
||||||
|
|
||||||
label.backgroundColor = .white
|
|
||||||
label.heightAnchor.constraint(equalToConstant: size).isActive = true
|
|
||||||
stack.addArrangedSubview(label)
|
|
||||||
|
|
||||||
for _ in 1...4 {
|
|
||||||
let btn = ComponentView(frame: frame, component: nil)
|
|
||||||
btn.backgroundColor = .white
|
|
||||||
btn.widthAnchor.constraint(equalToConstant: size).isActive = true
|
|
||||||
btn.heightAnchor.constraint(equalToConstant: size).isActive = true
|
|
||||||
buttons.append(btn)
|
|
||||||
stack.addArrangedSubview(btn)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
required init?(coder aDecoder: NSCoder) {
|
required init?(coder aDecoder: NSCoder) {
|
||||||
fatalError()
|
fatalError()
|
||||||
}
|
}
|
||||||
|
|
||||||
func update(habit: Habit, values: [KotlinInt], theme: Theme) {
|
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
|
||||||
|
stack.distribution = .fill
|
||||||
|
stack.alignment = .center
|
||||||
|
contentView.addSubview(stack)
|
||||||
|
|
||||||
|
ring.backgroundColor = .white
|
||||||
|
ring.widthAnchor.constraint(equalToConstant: size * 0.75).isActive = true
|
||||||
|
ring.heightAnchor.constraint(equalToConstant: size).isActive = true
|
||||||
|
stack.addArrangedSubview(ring)
|
||||||
|
|
||||||
|
label.backgroundColor = .white
|
||||||
|
label.heightAnchor.constraint(equalToConstant: size).isActive = true
|
||||||
|
stack.addArrangedSubview(label)
|
||||||
|
|
||||||
|
for _ in 1...nButtons {
|
||||||
|
let btn = ComponentView(frame: frame, component: nil)
|
||||||
|
btn.backgroundColor = .white
|
||||||
|
btn.widthAnchor.constraint(equalToConstant: size).isActive = true
|
||||||
|
btn.heightAnchor.constraint(equalToConstant: size).isActive = true
|
||||||
|
buttons.append(btn)
|
||||||
|
stack.addArrangedSubview(btn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var color = theme.color(paletteIndex: habit.color.index)
|
var color = theme.color(paletteIndex: habit.color.index)
|
||||||
if habit.isArchived { color = theme.mediumContrastTextColor }
|
if habit.isArchived { color = theme.mediumContrastTextColor }
|
||||||
label.text = habit.name
|
label.text = habit.name
|
||||||
@@ -129,6 +131,7 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
|
|||||||
]
|
]
|
||||||
tableView.register(MainScreenCell.self, forCellReuseIdentifier: "cell")
|
tableView.register(MainScreenCell.self, forCellReuseIdentifier: "cell")
|
||||||
tableView.backgroundColor = theme.headerBackgroundColor.uicolor
|
tableView.backgroundColor = theme.headerBackgroundColor.uicolor
|
||||||
|
computeNumberOfButtons(Double(view.frame.width))
|
||||||
}
|
}
|
||||||
|
|
||||||
override func viewDidAppear(_ animated: Bool) {
|
override func viewDidAppear(_ animated: Bool) {
|
||||||
@@ -145,7 +148,7 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
|
|||||||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||||
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MainScreenCell
|
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MainScreenCell
|
||||||
let habit = data!.habits[indexPath.row]
|
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
|
return cell
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,7 +166,7 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
|
|||||||
}
|
}
|
||||||
|
|
||||||
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
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) {
|
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)
|
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() {
|
@objc func onCreateHabitClicked() {
|
||||||
self.navigationController?.pushViewController(EditHabitController(), animated: true)
|
self.navigationController?.pushViewController(EditHabitController(), animated: true)
|
||||||
}
|
}
|
||||||
@@ -237,6 +245,15 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
|
|||||||
|
|
||||||
func onDataChanged(newData: MainScreenDataSource.Data) {
|
func onDataChanged(newData: MainScreenDataSource.Data) {
|
||||||
data = newData
|
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))
|
let sections = NSIndexSet(indexesIn: NSMakeRange(0, self.tableView.numberOfSections))
|
||||||
tableView.reloadSections(sections as IndexSet, with: .automatic)
|
tableView.reloadSections(sections as IndexSet, with: .automatic)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user