I am trying to filter only .portrait and .landscape orientation and then apply distinctUntilChanged i.e. removeDuplicates()
But removeDuplicates() seems to be never called as my debugger never stops inside comparator
and onReceive callback is called.
// MARK: - Publishers
private var orientationChange = NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)
.map { toOrientation($0) }
.filter { filterOrientation($0) }
//.removeDuplicates()
.removeDuplicates(by: { o1, o2 in
_ = filterOrientation(o1)
_ = filterOrientation(o2)
return false
})
.receive(on: DispatchQueue.main)
How to allow only .portrait, .landscape, .portrait, .landscape sequences and not .portrait, .portrait, .landscape etc
Update
I need to use this, but it adds unnecessary boilerplate code:
.onReceive(orientationChange) { orientation in
guard orientation != self.lastOrientation else { return }
self.lastOrientation = orientation
}
Here are internal implementations of helper functions
fileprivate func toOrientation(_ notification: UIKit.Notification) -> UIDeviceOrientation {
(notification.object as? UIDevice)?.orientation ?? .unknown
}
fileprivate func filterOrientation(_ orientation: UIDeviceOrientation, orientations: [UIDeviceOrientation] = [.landscapeLeft, .landscapeRight, .portrait, .portraitUpsideDown]) -> Bool {
return orientations.contains(orientation)
}