-
Notifications
You must be signed in to change notification settings - Fork 128
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
Sleep Timer: add the option to select the number of episodes #1640
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2989ef4
Add a stepper for integer value
leandroalonso c14fbce
Add a settings to store the number of episodes
leandroalonso 7a0a48f
Add i18n
leandroalonso 2e0fcfb
Add stepper to the sleep timer view
leandroalonso 4c91bb9
Rename sleepOnEpisodeEnd to numberOfEpisodesToSleepAfter
leandroalonso 6933b72
When restarting per episode end inform the number of episodes
leandroalonso 88352b2
Set the number of episodes from the sleep timer view
leandroalonso a6bea9d
Add number_of_episodes in Analytics
leandroalonso 61a217c
Update label to ensure correct number of episodes is displayed
leandroalonso ebac89c
Ensure sleep timer restarts only when the new episode starts playing
leandroalonso 6f70cda
Remove timer based solution in favor of a cleaner one
leandroalonso 236e54c
Merge branch 'trunk' into sleep-timer/change-number-of-episodes
leandroalonso d3a7eca
Update CHANGELOG.md
leandroalonso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
import UIKit | ||
|
||
class CustomStepper: UIControl { | ||
private static let buttonWidth: CGFloat = 44 | ||
private static let buttonHeight: CGFloat = 44 | ||
|
||
private static let initialHoldTime: TimeInterval = 1 | ||
private static let holdRepetition: TimeInterval = 0.15 | ||
|
||
let minusButton = UIButton(type: .custom) | ||
let plusButton = UIButton(type: .custom) | ||
|
||
override var tintColor: UIColor! { | ||
didSet { | ||
minusButton.tintColor = tintColor | ||
plusButton.tintColor = tintColor | ||
} | ||
} | ||
|
||
var minimumValue: Int = 0 { | ||
didSet { | ||
if currentValue < minimumValue { | ||
currentValue = minimumValue | ||
} | ||
} | ||
} | ||
|
||
var maximumValue: Int = 200 { | ||
didSet { | ||
if currentValue > maximumValue { | ||
currentValue = maximumValue | ||
} | ||
} | ||
} | ||
|
||
var bigIncrements: Int = 1 | ||
var smallIncrements: Int = 1 | ||
var smallIncrementThreshold: Int = 1 | ||
var currentValue: Int = 1 { | ||
didSet { | ||
accessibilityValue = "\(currentValue)" | ||
} | ||
} | ||
Comment on lines
+36
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional Suggestion: Instead of generics why not abstract this to a protocol and have two implementations one for Int and another for TimeInterval that you can set on |
||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
|
||
setup() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
super.init(coder: coder) | ||
|
||
setup() | ||
} | ||
|
||
private func setup() { | ||
isAccessibilityElement = true | ||
accessibilityTraits = [.adjustable] | ||
|
||
minusButton.setImage(UIImage(named: "player_effects_less"), for: .normal) | ||
minusButton.addTarget(self, action: #selector(lessTouchUp), for: .touchUpInside) | ||
minusButton.addTarget(self, action: #selector(touchCancelled), for: .touchCancel) | ||
minusButton.addTarget(self, action: #selector(touchCancelled), for: .touchUpOutside) | ||
minusButton.addTarget(self, action: #selector(lessTouchDown), for: .touchDown) | ||
addSubview(minusButton) | ||
|
||
minusButton.translatesAutoresizingMaskIntoConstraints = false | ||
NSLayoutConstraint.activate([ | ||
minusButton.leadingAnchor.constraint(equalTo: leadingAnchor), | ||
minusButton.centerYAnchor.constraint(equalTo: centerYAnchor), | ||
minusButton.widthAnchor.constraint(equalToConstant: Self.buttonWidth), | ||
minusButton.heightAnchor.constraint(equalToConstant: Self.buttonHeight) | ||
]) | ||
|
||
plusButton.setImage(UIImage(named: "player_effects_more"), for: .normal) | ||
plusButton.addTarget(self, action: #selector(moreTouchUp), for: .touchUpInside) | ||
plusButton.addTarget(self, action: #selector(touchCancelled), for: .touchCancel) | ||
plusButton.addTarget(self, action: #selector(touchCancelled), for: .touchUpOutside) | ||
plusButton.addTarget(self, action: #selector(moreTouchDown), for: .touchDown) | ||
addSubview(plusButton) | ||
|
||
plusButton.translatesAutoresizingMaskIntoConstraints = false | ||
NSLayoutConstraint.activate([ | ||
plusButton.trailingAnchor.constraint(equalTo: trailingAnchor), | ||
plusButton.centerYAnchor.constraint(equalTo: centerYAnchor), | ||
plusButton.widthAnchor.constraint(equalToConstant: Self.buttonWidth), | ||
plusButton.heightAnchor.constraint(equalToConstant: Self.buttonHeight) | ||
]) | ||
} | ||
|
||
// MARK: - Hold Support | ||
|
||
private var holdTimer: Timer? | ||
@objc private func lessTouchDown() { | ||
holdTimer?.invalidate() | ||
|
||
holdTimer = Timer.scheduledTimer(withTimeInterval: Self.initialHoldTime, repeats: false, block: { [weak self] _ in | ||
if self?.currentValue == self?.minimumValue { return } | ||
|
||
self?.holdTimer = Timer.scheduledTimer(withTimeInterval: Self.holdRepetition, repeats: true, block: { _ in | ||
self?.performHoldLessDown() | ||
}) | ||
}) | ||
} | ||
|
||
private func performHoldLessDown() { | ||
if currentValue == minimumValue { | ||
holdTimer?.invalidate() | ||
|
||
return | ||
} | ||
|
||
currentValue = max(minimumValue, currentValue - negativeIncrement()) | ||
sendActions(for: .valueChanged) | ||
} | ||
|
||
@objc private func moreTouchDown() { | ||
holdTimer?.invalidate() | ||
|
||
holdTimer = Timer.scheduledTimer(withTimeInterval: Self.initialHoldTime, repeats: false, block: { [weak self] _ in | ||
if self?.currentValue == self?.maximumValue { return } | ||
|
||
self?.holdTimer = Timer.scheduledTimer(withTimeInterval: Self.holdRepetition, repeats: true, block: { _ in | ||
self?.performHoldMoreDown() | ||
}) | ||
}) | ||
} | ||
|
||
private func performHoldMoreDown() { | ||
if currentValue == maximumValue { | ||
holdTimer?.invalidate() | ||
|
||
return | ||
} | ||
|
||
currentValue = min(maximumValue, currentValue + positiveIncrement()) | ||
sendActions(for: .valueChanged) | ||
} | ||
|
||
@objc private func touchCancelled() { | ||
holdTimer?.invalidate() | ||
} | ||
|
||
// MARK: - Button Taps | ||
|
||
@objc private func lessTouchUp() { | ||
performDecrementAction() | ||
} | ||
|
||
private func performDecrementAction() { | ||
holdTimer?.invalidate() | ||
if currentValue == minimumValue { return } | ||
|
||
currentValue = max(minimumValue, currentValue - negativeIncrement()) | ||
sendActions(for: .valueChanged) | ||
} | ||
|
||
@objc private func moreTouchUp() { | ||
performIncrementAction() | ||
} | ||
|
||
private func performIncrementAction() { | ||
holdTimer?.invalidate() | ||
if currentValue == maximumValue { return } | ||
|
||
currentValue = min(maximumValue, currentValue + positiveIncrement()) | ||
sendActions(for: .valueChanged) | ||
} | ||
|
||
private func positiveIncrement() -> Int { | ||
currentValue < smallIncrementThreshold ? smallIncrements : bigIncrements | ||
} | ||
|
||
private func negativeIncrement() -> Int { | ||
currentValue <= smallIncrementThreshold ? smallIncrements : bigIncrements | ||
} | ||
|
||
// MARK: - Accessibility actions | ||
|
||
override func accessibilityIncrement() { | ||
performIncrementAction() | ||
} | ||
|
||
override func accessibilityDecrement() { | ||
performDecrementAction() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not proud of copying/pasting
CustomTimeStepper
here. I tried playing with generics but forgot that Storyboard only plays nice with Obj-C. 🤡