From 7a1304e11e9636245f104a98a036a916ab057f4f Mon Sep 17 00:00:00 2001 From: Warner Wilson <63309006+WarnerBros144@users.noreply.github.com> Date: Tue, 4 Apr 2023 20:15:43 -0600 Subject: [PATCH] Update Volumer.swift to fix detection bug --- Source/Volumer.swift | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/Source/Volumer.swift b/Source/Volumer.swift index 4659e79..18b1145 100644 --- a/Source/Volumer.swift +++ b/Source/Volumer.swift @@ -39,7 +39,7 @@ public enum Volume private static var observer: Observer! - private static func setup() + public static func setup() { if self.hasSetup { return @@ -60,7 +60,7 @@ public enum Volume for: .valueChanged) } - private static func unsetup() + public static func unsetup() { if !self.hasSetup { return @@ -88,12 +88,7 @@ public enum Volume @objc private class Observer: NSObject { - private var initialValue: Float! - /* - Hacky way of preventing Volumer from firing its blocks when the MPVolumeView starts (and, for some reason, fires a .ValueChanged event twice) - Also, when moving from the background, MPVolumeView fires once more (for some reason) - */ - private var activationCount = 2 + private var currentValue: Float? private var notify = true deinit { @@ -104,42 +99,36 @@ public enum Volume super.init() NotificationCenter.default.addObserver(self, selector: #selector(Observer.willBecomeInactive(_:)), - name: .UIApplicationWillResignActive, + name: UIApplication.willResignActiveNotification, object: nil) } @objc fileprivate func sliderDidChange(_ sender: UISlider) { - activationCount -= 1 - if activationCount > 0 { - if self.initialValue == nil { - self.initialValue = sender.value - } - return - } - if self.initialValue == nil { + if currentValue == nil { + currentValue = sender.value return } if !notify { notify = true return } - if sender.value == self.initialValue { - Volume.didChange(up: sender.value == 1.0) - } else { - Volume.didChange(up: sender.value > self.initialValue) + if sender.value == currentValue { + return } + Volume.didChange(up: sender.value > currentValue!) + currentValue = sender.value if Volume.keepIntact { notify = false - sender.value = self.initialValue + sender.value = currentValue! } } @objc fileprivate func willBecomeInactive(_ notification: NSNotification) { - activationCount = 1 - self.initialValue = nil + currentValue = nil } } + private extension MPVolumeView { var slider: UISlider? { @@ -153,4 +142,3 @@ private extension Bool return self ? .up : .down } } -