From 5b2b554a7a8d0a34c6e3b546739b451e4ad5ab29 Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Wed, 3 Apr 2019 05:50:42 -0500 Subject: [PATCH] Dynamically select number of columns to display --- .../uhabits/backend/MainScreenDataSource.kt | 5 +- .../Frontend/MainScreenController.swift | 83 +++++++++++-------- 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/core/src/commonMain/kotlin/org/isoron/uhabits/backend/MainScreenDataSource.kt b/core/src/commonMain/kotlin/org/isoron/uhabits/backend/MainScreenDataSource.kt index 76a36e8b1..07f487932 100644 --- a/core/src/commonMain/kotlin/org/isoron/uhabits/backend/MainScreenDataSource.kt +++ b/core/src/commonMain/kotlin/org/isoron/uhabits/backend/MainScreenDataSource.kt @@ -29,6 +29,7 @@ class MainScreenDataSource(val preferences: Preferences, val checkmarks: MutableMap, val taskRunner: TaskRunner) { + val maxNumberOfButtons = 60 private val today = LocalDate(2019, 3, 30) /* TODO */ data class Data(val habits: List, @@ -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) { diff --git a/ios/Application/Frontend/MainScreenController.swift b/ios/Application/Frontend/MainScreenController.swift index 6a15df298..43ee64223 100644 --- a/ios/Application/Frontend/MainScreenController.swift +++ b/ios/Application/Frontend/MainScreenController.swift @@ -20,47 +20,49 @@ 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) - - 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) { 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) 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) }