mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 17:48:52 -06:00
Reorganize packages; implement checkmarks
This commit is contained in:
87
ios/Application/Frontend/EditHabitController.swift
Normal file
87
ios/Application/Frontend/EditHabitController.swift
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import UIKit
|
||||
|
||||
class EditHabitTableViewController: NSObject, UITableViewDataSource, UITableViewDelegate {
|
||||
func disclosure(title: String, subtitle: String) -> UITableViewCell {
|
||||
let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
|
||||
cell.textLabel?.text = title
|
||||
cell.detailTextLabel?.text = subtitle
|
||||
cell.accessoryType = .disclosureIndicator
|
||||
return cell
|
||||
}
|
||||
|
||||
func input(title: String) -> UITableViewCell {
|
||||
let cell = UITableViewCell()
|
||||
let field = UITextField(frame: cell.bounds.insetBy(dx: 20, dy: 0))
|
||||
field.placeholder = title
|
||||
field.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth,
|
||||
UIView.AutoresizingMask.flexibleHeight]
|
||||
cell.contentView.addSubview(field)
|
||||
return cell
|
||||
}
|
||||
|
||||
var primary = [UITableViewCell]()
|
||||
var secondary = [UITableViewCell]()
|
||||
var parentController: EditHabitController
|
||||
|
||||
init(withParentController parentController: EditHabitController) {
|
||||
self.parentController = parentController
|
||||
super.init()
|
||||
primary.append(input(title: "Name"))
|
||||
primary.append(input(title: "Question (e.g. Did you wake up early today?)"))
|
||||
secondary.append(disclosure(title: "Color", subtitle: "Blue"))
|
||||
secondary.append(disclosure(title: "Repeat", subtitle: "Daily"))
|
||||
secondary.append(disclosure(title: "Reminder", subtitle: "Disabled"))
|
||||
}
|
||||
|
||||
func numberOfSections(in tableView: UITableView) -> Int {
|
||||
return 2
|
||||
}
|
||||
|
||||
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
return section == 0 ? primary.count : secondary.count
|
||||
}
|
||||
|
||||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||
return indexPath.section == 0 ? primary[indexPath.item] : secondary[indexPath.item]
|
||||
}
|
||||
|
||||
// func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||
// let alert = UIAlertController(title: "Hello", message: "You selected something", preferredStyle: .alert)
|
||||
// parentController.present(alert, animated: true)
|
||||
// }
|
||||
}
|
||||
|
||||
class EditHabitController: UIViewController {
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
let bounds = UIScreen.main.bounds
|
||||
let tableController = EditHabitTableViewController(withParentController: self)
|
||||
let table = UITableView(frame: bounds, style: .grouped)
|
||||
table.dataSource = tableController
|
||||
table.delegate = tableController
|
||||
self.view = table
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
self.title = "Edit Habit"
|
||||
}
|
||||
}
|
||||
172
ios/Application/Frontend/ListHabitsController.swift
Normal file
172
ios/Application/Frontend/ListHabitsController.swift
Normal file
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import UIKit
|
||||
|
||||
class ListHabitsCell : UITableViewCell {
|
||||
var ring: ComponentView
|
||||
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.backgroundColor = .red
|
||||
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 setColor(_ color: Color) {
|
||||
label.textColor = color.uicolor
|
||||
ring.component = Ring(color: color,
|
||||
percentage: Double.random(in: 0...1),
|
||||
thickness: 2.5,
|
||||
radius: 7,
|
||||
theme: theme,
|
||||
label: false)
|
||||
ring.setNeedsDisplay()
|
||||
let isNumerical = Int.random(in: 1...4) == 1
|
||||
for btn in buttons {
|
||||
if isNumerical {
|
||||
btn.component = NumberButton(color: color,
|
||||
value: Double.random(in: 0...5000),
|
||||
threshold: 2000,
|
||||
units: "steps",
|
||||
theme: theme)
|
||||
} else {
|
||||
btn.component = CheckmarkButton(value: Int32.random(in: 0...2),
|
||||
color: color,
|
||||
theme: theme)
|
||||
}
|
||||
btn.setNeedsDisplay()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ListHabitsController: UITableViewController, MainScreenDataSourceListener {
|
||||
var backend: Backend
|
||||
var dataSource: MainScreenDataSource
|
||||
var data: MainScreenDataSource.Data?
|
||||
var theme: Theme
|
||||
|
||||
required init?(coder aDecoder: NSCoder) {
|
||||
fatalError()
|
||||
}
|
||||
|
||||
init(withBackend backend:Backend) {
|
||||
self.backend = backend
|
||||
self.dataSource = backend.mainScreenDataSource
|
||||
self.theme = backend.theme
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
self.dataSource.addListener(listener: self)
|
||||
self.dataSource.requestData()
|
||||
}
|
||||
|
||||
func onDataChanged(newData: MainScreenDataSource.Data) {
|
||||
self.data = newData
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
self.title = "Habits"
|
||||
|
||||
self.navigationItem.rightBarButtonItems = [
|
||||
UIBarButtonItem(barButtonSystemItem: .add,
|
||||
target: self,
|
||||
action: #selector(self.onCreateHabitClicked))
|
||||
]
|
||||
tableView.register(ListHabitsCell.self, forCellReuseIdentifier: "cell")
|
||||
tableView.backgroundColor = theme.headerBackgroundColor.uicolor
|
||||
}
|
||||
|
||||
override func viewDidAppear(_ animated: Bool) {
|
||||
self.navigationController?.navigationBar.barStyle = .default
|
||||
self.navigationController?.navigationBar.tintColor = theme.highContrastTextColor.uicolor
|
||||
self.navigationController?.navigationBar.barTintColor = .white
|
||||
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
|
||||
}
|
||||
|
||||
@objc func onCreateHabitClicked() {
|
||||
self.navigationController?.pushViewController(EditHabitController(), animated: true)
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
return data?.names.count ?? 0
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||
let row = indexPath.row
|
||||
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ListHabitsCell
|
||||
let color = theme.color(paletteIndex: data!.colors[row].index)
|
||||
cell.label.text = data!.names[row]
|
||||
cell.setColor(color)
|
||||
return cell
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
||||
let component = HabitListHeader(today: LocalDate(year: 2019, month: 3, day: 24),
|
||||
nButtons: 4,
|
||||
theme: theme,
|
||||
fmt: IosLocalDateFormatter(),
|
||||
calc: IosLocalDateCalculator())
|
||||
return ComponentView(frame: CGRect(x: 0, y: 0, width: 100, height: CGFloat(theme.checkmarkButtonSize)),
|
||||
component: component)
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
||||
return CGFloat(theme.checkmarkButtonSize)
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
||||
return CGFloat(theme.checkmarkButtonSize) + 1
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||
let color = theme.color(paletteIndex: data!.colors[indexPath.row].index)
|
||||
self.navigationController?.pushViewController(ShowHabitController(theme: theme, color: color), animated: true)
|
||||
}
|
||||
}
|
||||
91
ios/Application/Frontend/ShowHabitController.swift
Normal file
91
ios/Application/Frontend/ShowHabitController.swift
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import UIKit
|
||||
|
||||
class ShowHabitController : UITableViewController {
|
||||
|
||||
let theme: Theme
|
||||
let color: Color
|
||||
var cells = [UITableViewCell]()
|
||||
|
||||
required init?(coder aDecoder: NSCoder) {
|
||||
fatalError()
|
||||
}
|
||||
|
||||
init(theme: Theme, color: Color) {
|
||||
self.theme = theme
|
||||
self.color = color
|
||||
super.init(style: .grouped)
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
self.title = "Exercise"
|
||||
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit,
|
||||
target: self,
|
||||
action: #selector(self.onEditHabitClicked))
|
||||
cells.append(buildHistoryChartCell())
|
||||
}
|
||||
|
||||
func buildHistoryChartCell() -> UITableViewCell {
|
||||
let component = CalendarChart(today: LocalDate(year: 2019, month: 3, day: 15),
|
||||
color: color,
|
||||
theme: theme,
|
||||
dateCalculator: IosLocalDateCalculator(),
|
||||
dateFormatter: IosLocalDateFormatter())
|
||||
let cell = UITableViewCell()
|
||||
let view = ComponentView(frame: cell.frame, component: component)
|
||||
var series = [KotlinDouble]()
|
||||
for _ in 1...365 {
|
||||
series.append(KotlinDouble(value: Double.random(in: 0...1)))
|
||||
}
|
||||
component.series = series
|
||||
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
||||
cell.contentView.addSubview(view)
|
||||
return cell
|
||||
}
|
||||
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
self.navigationController?.navigationBar.barStyle = .blackOpaque
|
||||
self.navigationController?.navigationBar.barTintColor = color.uicolor
|
||||
self.navigationController?.navigationBar.tintColor = .white
|
||||
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
return 1
|
||||
}
|
||||
|
||||
override func numberOfSections(in tableView: UITableView) -> Int {
|
||||
return cells.count
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||
return cells[indexPath.section]
|
||||
}
|
||||
|
||||
@objc func onEditHabitClicked() {
|
||||
self.navigationController?.pushViewController(EditHabitController(), animated: true)
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
||||
return 200
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user