Skip to content
This repository has been archived by the owner on Dec 15, 2020. It is now read-only.

Enable or disable the “Add Sensors” button #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion ScienceJournal/ActionArea/ActionAreaBarButtonItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,24 @@ extension ActionArea {
private(set) var title: String
private(set) var image: UIImage?
private(set) var action: () -> Void // TODO: Consider passing the AA controller here.
private(set) var enabler: FeatureEnabler?

/// Designated initializer.
///
/// - Parameters:
/// - title: The title for the item.
/// - accessibilityHint: The accessibility hint for the item.
/// - image: The image for the item.
/// - enabler: A feature enabler to handle changes in an observed Bool value.
/// - action: A block to execute when the item is tapped.
init(title: String, accessibilityHint: String?, image: UIImage?, action: @escaping () -> Void) {
init(title: String,
accessibilityHint: String?,
image: UIImage?,
enabler: FeatureEnabler? = nil,
action: @escaping () -> Void) {
self.title = title
self.image = image
self.enabler = enabler
self.action = action
super.init()
self.accessibilityHint = accessibilityHint
Expand Down
26 changes: 24 additions & 2 deletions ScienceJournal/ActionArea/ActionAreaBarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ extension ActionArea {
(stackView.arrangedSubviews.count ..< 4).forEach { _ in
stackView.addArrangedSubview(UIView())
}

items.forEach { (item) in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no parens around block args (item and enabled)

item.enabler?.observe({ (enabled) in
self.setButtonFor(item: item, enabled: enabled)
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do all the items need to be re-enabled here? It seems like if one were previously disabled, it may not get turned back on after a button change.

}
}
}

Expand All @@ -448,8 +454,12 @@ extension ActionArea {
fatalError("init(coder:) has not been implemented")
}

func setButtonFor(item: BarButtonItem, enabled: Bool, animated: Bool = true) {
if let index = items.index(of: item) {
buttons[index].setEnabled(enabled, animated: animated)
}
}
}

}

// MARK: - ActionArea.BarButton
Expand Down Expand Up @@ -527,8 +537,20 @@ extension ActionArea {
button.backgroundColor = customTint.secondary
}

func setEnabled(_ enabled: Bool, animated: Bool = true) {
button.isEnabled = enabled
let updatedAlpha: CGFloat = enabled ? 1.0 : BarViewController.Metrics.Bar.disabledAlpha
if animated {
UIView.animate(withDuration: 0.5) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move the animation block up to the VC. It currently controls timings for animations, so it'll be easier to reason about with those in the same place. There's also some logic up there related to enabling/disabling the entire bar/detail era, and the alpha values there and here are likely related.

self.button.alpha = updatedAlpha
self.label.alpha = updatedAlpha
}
} else {
self.button.alpha = updatedAlpha
self.label.alpha = updatedAlpha
}
}
}

}

// MARK: - Debugging
Expand Down
12 changes: 10 additions & 2 deletions ScienceJournal/UI/ObserveViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ open class ObserveViewController: ScienceJournalCollectionViewController, ChartC
return self.observeDataSource.availableSensorIDs
}

@objc dynamic private(set) var unusedSensors: Bool = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasUnusedSensors would be more consistent with the property naming guidelines for boolean values.


// MARK: - Public

/// Designated initializer.
Expand All @@ -197,6 +199,7 @@ open class ObserveViewController: ScienceJournalCollectionViewController, ChartC

let flowLayout = MDCCollectionViewFlowLayout()
flowLayout.minimumLineSpacing = SensorCardCell.cardInsets.bottom

super.init(collectionViewLayout: flowLayout, analyticsReporter: analyticsReporter)

observeDataSource.delegate = self
Expand Down Expand Up @@ -757,6 +760,10 @@ open class ObserveViewController: ScienceJournalCollectionViewController, ChartC
sensorCard.toneGenerator.stop()
}

private func updateUnusedSensors() {
unusedSensors = observeDataSource.items.count < sensorController.availableSensors.count
}

private func removeSensorCardCell(_ cell: SensorCardCell) {
guard let indexPath = collectionView?.indexPath(for: cell) else {
return
Expand All @@ -780,7 +787,7 @@ open class ObserveViewController: ScienceJournalCollectionViewController, ChartC
}
}, completion: { (_) in
self.updateSensorPickersIfNeeded()
// TODO: Show/Enable the "Add Sensor" button if needed
self.updateUnusedSensors()
})
}

Expand Down Expand Up @@ -809,7 +816,7 @@ open class ObserveViewController: ScienceJournalCollectionViewController, ChartC
self.collectionView?.deleteSections(IndexSet(integer: previousFooterIndexPath.section))
}
}, completion: { (_) in
// TODO: Hide/Disable the "Add Sensor" button.
self.updateUnusedSensors()
})
collectionView?.scrollToItem(at: IndexPath(item: newItemIndexPath.item, section: 0),
at: .top,
Expand Down Expand Up @@ -1028,6 +1035,7 @@ open class ObserveViewController: ScienceJournalCollectionViewController, ChartC
addInitialSensorCardIfNeeded(andAddListener: andAddListeners)

collectionView?.reloadData()
updateUnusedSensors()
}

/// Sets the available sensors for the current experiment.
Expand Down
4 changes: 3 additions & 1 deletion ScienceJournal/UI/UserFlowViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,9 @@ class UserFlowViewController: UIViewController, ExperimentsListViewControllerDel
let addSensorItem = ActionArea.BarButtonItem(
title: String.actionAreaButtonAddSensor,
accessibilityHint: String.actionAreaButtonAddSensorContentDescription,
image: UIImage(named: "ic_action_area_add_sensor")
image: UIImage(named: "ic_action_area_add_sensor"),
enabler: FeatureEnabler(target: experimentCoordinator.observeViewController,
keyPath: \.unusedSensors)
) {
experimentCoordinator.observeViewController.observeFooterAddButtonPressed()
}
Expand Down