Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redesign the edit datapoint view #413

Merged
merged 7 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion BeeSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 53;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -80,6 +80,7 @@
E42CB452291727B200A35AB9 /* HealthKitError.swift in Sources */ = {isa = PBXBuildFile; fileRef = E42CB451291727B200A35AB9 /* HealthKitError.swift */; };
E42CB453291727B200A35AB9 /* HealthKitError.swift in Sources */ = {isa = PBXBuildFile; fileRef = E42CB451291727B200A35AB9 /* HealthKitError.swift */; };
E42CB454291727B200A35AB9 /* HealthKitError.swift in Sources */ = {isa = PBXBuildFile; fileRef = E42CB451291727B200A35AB9 /* HealthKitError.swift */; };
E43833942AC1473E0098A38F /* InlineDatePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = E43833932AC1473E0098A38F /* InlineDatePicker.swift */; };
E43BEA842A036A9C00FC3A38 /* LogReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = E43BEA832A036A9C00FC3A38 /* LogReader.swift */; };
E43BEA862A036D4300FC3A38 /* LogReaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E43BEA852A036D4300FC3A38 /* LogReaderTests.swift */; };
E43D9AFB2929C37D00FC1578 /* DatapointValueAccessory.swift in Sources */ = {isa = PBXBuildFile; fileRef = E43D9AFA2929C37D00FC1578 /* DatapointValueAccessory.swift */; };
Expand Down Expand Up @@ -348,6 +349,7 @@
E4040D732A7B5F0E008E7D0E /* WorkoutMinutesHealthKitMetric.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorkoutMinutesHealthKitMetric.swift; sourceTree = "<group>"; };
E417572C2A6446FE0029CDDA /* CurrentUserManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrentUserManagerTests.swift; sourceTree = "<group>"; };
E42CB451291727B200A35AB9 /* HealthKitError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HealthKitError.swift; sourceTree = "<group>"; };
E43833932AC1473E0098A38F /* InlineDatePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InlineDatePicker.swift; sourceTree = "<group>"; };
E43BEA832A036A9C00FC3A38 /* LogReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogReader.swift; sourceTree = "<group>"; };
E43BEA852A036D4300FC3A38 /* LogReaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogReaderTests.swift; sourceTree = "<group>"; };
E43D9AFA2929C37D00FC1578 /* DatapointValueAccessory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatapointValueAccessory.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -517,6 +519,7 @@
A1C170DF1BEEB3110018D32B /* UIFontExtension.swift */,
E43D9AFA2929C37D00FC1578 /* DatapointValueAccessory.swift */,
E46DC80E2AA58DF20059FDFE /* PullToRefreshHint.swift */,
E43833932AC1473E0098A38F /* InlineDatePicker.swift */,
);
name = UI;
sourceTree = "<group>";
Expand Down Expand Up @@ -1143,6 +1146,7 @@
A1BE73AA1E8B45BF00DEC4DB /* ChooseHKMetricViewController.swift in Sources */,
A149147B1BE79FD50060600A /* EditNotificationsViewController.swift in Sources */,
A1BE73AC1E8B47E700DEC4DB /* HealthKitMetricTableViewCell.swift in Sources */,
E43833942AC1473E0098A38F /* InlineDatePicker.swift in Sources */,
A1C11B481B06F5D100D22871 /* Constants.swift in Sources */,
E4B0833D293810EB00A71564 /* DataPoint.swift in Sources */,
E44CE7732993317B00394E87 /* ServiceLocator.swift in Sources */,
Expand Down
5 changes: 5 additions & 0 deletions BeeSwift/DatapointsTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
import Foundation

class DatapointsTableView : UITableView {
override var contentSize:CGSize {
didSet {
invalidateIntrinsicContentSize()
}
}

override var intrinsicContentSize : CGSize {
self.layoutIfNeeded()
Expand Down
224 changes: 122 additions & 102 deletions BeeSwift/EditDatapointViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import OSLog

class EditDatapointViewController: UIViewController, UITextFieldDelegate {
private let logger = Logger(subsystem: "com.beeminder.beeminder", category: "EditDatapointViewController")

private let margin = 10

var datapoint : ExistingDataPoint
var goalSlug : String
fileprivate var datePicker = UIDatePicker()
fileprivate var scrollView = UIScrollView()
fileprivate var dateLabel = BSLabel()
fileprivate var valueField = BSTextField()
fileprivate var commentField = BSTextField()
fileprivate var datePicker = InlineDatePicker()
fileprivate var valueField = UITextField()
fileprivate var commentField = UITextField()

init(goalSlug: String, datapoint: ExistingDataPoint) {
self.goalSlug = goalSlug
Expand All @@ -38,87 +38,134 @@ class EditDatapointViewController: UIViewController, UITextFieldDelegate {
self.title = "Edit Datapoint"

self.view.backgroundColor = .systemBackground
self.view.addSubview(self.scrollView)
self.scrollView.snp.makeConstraints { (make) -> Void in
make.top.equalTo(self.view.safeAreaLayoutGuide.snp.topMargin)
make.left.equalTo(0)
make.right.equalTo(0)
make.bottom.equalTo(0)

let formView = UIView()
self.view.addSubview(formView)
formView.layer.cornerRadius = 10
formView.backgroundColor = UIColor(white: 0.5, alpha: 0.2)
formView.snp.makeConstraints{ (make) in
make.top.left.right.equalTo(self.view.safeAreaLayoutGuide).inset(10)
}

self.scrollView.addSubview(self.dateLabel)
self.dateLabel.snp.makeConstraints { (make) in
make.top.left.equalTo(self.scrollView).offset(10)
make.right.equalTo(self.scrollView).offset(-10)
make.width.equalTo(self.scrollView).offset(-20)
make.height.equalTo(42)
}
self.dateLabel.textAlignment = .center
self.dateLabel.isUserInteractionEnabled = true
let tapGR = UITapGestureRecognizer(target: self, action: #selector(self.dateLabelTapped))
self.dateLabel.addGestureRecognizer(tapGR)

self.scrollView.addSubview(self.datePicker)
self.datePicker.snp.makeConstraints { (make) in
make.left.right.equalTo(self.scrollView)
make.width.equalTo(self.scrollView)
make.height.equalTo(0)
make.top.equalTo(self.dateLabel.snp.bottom)

let dateLabel = UILabel()
formView.addSubview(dateLabel)
dateLabel.text = "Date"
dateLabel.layer.opacity = 0.5
dateLabel.font = UIFont.systemFont(ofSize: 12)
dateLabel.snp.makeConstraints { (make) in
make.top.left.right.equalTo(formView).inset(10)
}

formView.addSubview(self.datePicker)
self.datePicker.datePickerMode = .date
self.datePicker.isHidden = true
self.datePicker.addTarget(self, action: #selector(self.datePickerValueChanged), for: .valueChanged)

self.scrollView.addSubview(self.valueField)
self.valueField.snp.makeConstraints { (make) in
make.left.right.height.equalTo(self.dateLabel)
self.datePicker.contentHorizontalAlignment = .left

let daystamp = self.datapoint.daystamp
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
self.datePicker.date = dateFormatter.date(from: daystamp)!

self.datePicker.snp.makeConstraints { (make) in
make.left.right.equalTo(formView).inset(10)
make.top.equalTo(dateLabel.snp.bottom).offset(3)
}

let divider1 = formDivider()
formView.addSubview(divider1)
divider1.snp.makeConstraints { (make) in
make.left.right.equalTo(formView).inset(10)
make.top.equalTo(self.datePicker.snp.bottom).offset(10)
}

let valueLabel = UILabel()
formView.addSubview(valueLabel)
valueLabel.text = "Value"
valueLabel.layer.opacity = 0.5
valueLabel.font = UIFont.systemFont(ofSize: 12)
valueLabel.snp.makeConstraints { (make) in
make.left.right.equalTo(formView).inset(10)
make.top.equalTo(divider1.snp.bottom).offset(10)
}

formView.addSubview(self.valueField)
self.valueField.delegate = self
self.valueField.placeholder = "Value"
self.valueField.textAlignment = .center
self.valueField.keyboardType = .decimalPad
self.valueField.returnKeyType = .done
self.valueField.text = "\(self.datapoint.value)"
self.valueField.addTarget(self, action: #selector(self.textFieldEditingDidBegin), for: .editingDidBegin)


let accessory = DatapointValueAccessory()
accessory.valueField = self.valueField
self.valueField.inputAccessoryView = accessory

self.scrollView.addSubview(self.commentField)
self.commentField.snp.makeConstraints { (make) in
make.left.right.height.equalTo(self.dateLabel)

self.valueField.snp.makeConstraints { (make) in
make.left.right.equalTo(formView).inset(10)
make.top.equalTo(valueLabel.snp.bottom).offset(3)
}

let divider2 = formDivider()
formView.addSubview(divider2)
divider2.snp.makeConstraints { (make) in
make.left.right.equalTo(formView).inset(10)
make.top.equalTo(self.valueField.snp.bottom).offset(10)
}
self.commentField.placeholder = "Comment"
self.commentField.textAlignment = .center

let commentLabel = UILabel()
formView.addSubview(commentLabel)
commentLabel.text = "Comment"
commentLabel.layer.opacity = 0.5
commentLabel.font = UIFont.systemFont(ofSize: 12)
commentLabel.snp.makeConstraints { (make) in
make.left.right.equalTo(formView).inset(10)
make.top.equalTo(divider2).offset(10)
}

formView.addSubview(self.commentField)
self.commentField.text = self.datapoint.comment
self.commentField.addTarget(self, action: #selector(self.textFieldEditingDidBegin), for: .editingDidBegin)

let updateButton = BSButton()
self.scrollView.addSubview(updateButton)
updateButton.snp.makeConstraints { (make) in
make.left.right.height.equalTo(self.dateLabel)
make.top.equalTo(self.commentField.snp.bottom).offset(10)
self.commentField.delegate = self
self.commentField.returnKeyType = .done
self.commentField.snp.makeConstraints { (make) in
make.left.right.bottom.equalTo(formView).inset(10)
make.top.equalTo(commentLabel.snp.bottom).offset(3)
}

let updateButton = UIButton(type: .system)
self.view.addSubview(updateButton)
updateButton.setTitle("Update", for: .normal)
if #available(iOS 15.0, *) {
updateButton.configuration = .filled()
}
updateButton.addTarget(self, action: #selector(self.updateButtonPressed), for: .touchUpInside)

let deleteButton = BSButton()
self.scrollView.addSubview(deleteButton)
deleteButton.snp.makeConstraints { (make) in
make.left.right.height.equalTo(self.dateLabel)
make.top.equalTo(updateButton.snp.bottom).offset(10)
}
deleteButton.setTitle("Delete", for: .normal)
deleteButton.addTarget(self, action: #selector(self.deleteButtonPressed), for: .touchUpInside)

let daystamp = self.datapoint.daystamp
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
self.datePicker.date = dateFormatter.date(from: daystamp)!
self.updateDateLabel()
updateButton.snp.makeConstraints { (make) in
make.left.right.equalTo(formView)
make.top.equalTo(formView.snp.bottom).offset(20)
}

let deleteButton = UIBarButtonItem(barButtonSystemItem: .trash, target: self, action: #selector(self.deleteButtonPressed))
deleteButton.tintColor = .red
self.navigationItem.rightBarButtonItem = deleteButton
}

func formDivider() -> UIView {
let divider = UIView()
divider.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
divider.snp.makeConstraints { (make) in
make.height.equalTo(1)
}
return divider
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if let bgView = self.datePicker.subviews.first?.subviews.first?.subviews.first {
bgView.backgroundColor = nil
}
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let bgView = self.datePicker.subviews.first?.subviews.first?.subviews.first {
bgView.backgroundColor = nil
}
}

func urtext() -> String {
Expand All @@ -127,23 +174,6 @@ class EditDatapointViewController: UIViewController, UITextFieldDelegate {
return "\(dateFormatter.string(from: self.datePicker.date)) \(self.valueField.text!) \"\(self.commentField.text!)\""
}

func updateDateLabel() {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
self.dateLabel.text = dateFormatter.string(from: self.datePicker.date)
}

@objc func datePickerValueChanged() {
self.updateDateLabel()
}

@objc func textFieldEditingDidBegin() {
self.datePicker.snp.updateConstraints { (make) in
make.height.equalTo(0)
}
self.datePicker.isHidden = true
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if (textField.isEqual(self.valueField)) {
// Only allow a single decimal separator (, or .)
Expand All @@ -164,18 +194,13 @@ class EditDatapointViewController: UIViewController, UITextFieldDelegate {
}
return true
}

@objc func dateLabelTapped() {
self.datePicker.snp.updateConstraints { (make) in
if self.datePicker.isHidden {
make.height.equalTo(200)
} else {
make.height.equalTo(0)
}
}
self.datePicker.isHidden = !self.datePicker.isHidden
self.view.endEditing(true)


func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}


@objc func updateButtonPressed() {
Task { @MainActor in
Expand Down Expand Up @@ -233,9 +258,4 @@ class EditDatapointViewController: UIViewController, UITextFieldDelegate {

self.present(alert, animated: true, completion: nil)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

}
4 changes: 3 additions & 1 deletion BeeSwift/GoalViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,9 @@ class GoalViewController: UIViewController, UIScrollViewDelegate, DatapointTabl
guard let existingDatapoint = datapoint as? ExistingDataPoint else { return }

let editDatapointViewController = EditDatapointViewController(goalSlug: goal.slug, datapoint: existingDatapoint)
self.navigationController?.pushViewController(editDatapointViewController, animated: true)
let navigationController = UINavigationController(rootViewController: editDatapointViewController)
navigationController.modalPresentationStyle = .formSheet
self.present(navigationController, animated: true, completion: nil)
}

@objc func dateStepperValueChanged() {
Expand Down
Loading