Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Commit

Permalink
Added ignoreUntil and simplified slop
Browse files Browse the repository at this point in the history
Summary: Fixed #87 and #86

Reviewers: O2 Material Motion, O4 Material Apple platform reviewers, #material_motion, featherless

Reviewed By: O2 Material Motion, O4 Material Apple platform reviewers, #material_motion, featherless

Subscribers: featherless

Tags: #material_motion

Differential Revision: http://codereview.cc/D3170
  • Loading branch information
randcode-generator committed May 5, 2017
1 parent 27edf6b commit 86bf12b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
07EB59021EBCEF8F0045ABAE /* ignoreUntilTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07EB59001EBCEF3D0045ABAE /* ignoreUntilTest.swift */; };
19940C71B8C97EDA48552251 /* Pods_MaterialMotionCatalog.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 16DDCA39C49FF5C091B2AF6C /* Pods_MaterialMotionCatalog.framework */; };
6605044E1E83146C009EDB8A /* distanceFromTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6605044D1E83146C009EDB8A /* distanceFromTests.swift */; };
66090B841E03715F00B1D598 /* PropertyObservation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66090B831E03715F00B1D598 /* PropertyObservation.swift */; };
Expand Down Expand Up @@ -87,6 +88,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
07EB59001EBCEF3D0045ABAE /* ignoreUntilTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ignoreUntilTest.swift; sourceTree = "<group>"; };
1486CD16AAF554D1E8B5BBB3 /* Pods-UnitTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-UnitTests.debug.xcconfig"; path = "../../../Pods/Target Support Files/Pods-UnitTests/Pods-UnitTests.debug.xcconfig"; sourceTree = "<group>"; };
16DDCA39C49FF5C091B2AF6C /* Pods_MaterialMotionCatalog.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_MaterialMotionCatalog.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4F2DC52F25787C67CDBB6189 /* Pods-UnitTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-UnitTests.release.xcconfig"; path = "../../../Pods/Target Support Files/Pods-UnitTests/Pods-UnitTests.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -276,6 +278,7 @@
669512921E8301D100D8868D /* dedupeTests.swift */,
669512961E8305AC00D8868D /* delayTests.swift */,
6605044D1E83146C009EDB8A /* distanceFromTests.swift */,
07EB59001EBCEF3D0045ABAE /* ignoreUntilTest.swift */,
66F2C3C81E83245800DD9728 /* invertedTests.swift */,
6695129A1E830AE500D8868D /* lowerBoundTests.swift */,
6613A9DA1E832779004A3699 /* mergeTests.swift */,
Expand Down Expand Up @@ -653,6 +656,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
07EB59021EBCEF8F0045ABAE /* ignoreUntilTest.swift in Sources */,
6613A9F51E842FF5004A3699 /* yLockedToTests.swift in Sources */,
669512891E82E8CB00D8868D /* _rememberTests.swift in Sources */,
669512741E82E68900D8868D /* SubtractableTests.swift in Sources */,
Expand Down
33 changes: 33 additions & 0 deletions src/operators/ignoreUntil.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2016-present The Material Motion Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import UIKit

extension MotionObservableConvertible where T: Equatable {
/**
Ignores values from upstream until the expected value is received, at which point it emits
that value and all further values without modification.
*/
public func ignoreUntil(_ expected: T) -> MotionObservable<T> {
var shouldSend = false
return _filter() { value in
if value == expected {
shouldSend = true
}
return shouldSend
}
}
}
15 changes: 2 additions & 13 deletions src/operators/slop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,17 @@ extension MotionObservableConvertible where T == CGFloat {
size.
*/
public func slop(size: CGFloat) -> MotionObservable<SlopEvent> {
let didLeaveSlopRegion = createProperty("slop.didLeaveSlopRegion", withInitialValue: false)

let size = abs(size)

return MotionObservable(self.metadata.createChild(Metadata(#function, type: .constraint, args: [size]))) { observer in
let upstreamSubscription = self
.thresholdRange(min: -size, max: size)
.rewrite([.below: true, .above: true])
.dedupe()
.subscribeToValue { didLeaveSlopRegion.value = $0 }

let downstreamSubscription = self
.valve(openWhenTrue: didLeaveSlopRegion)
.thresholdRange(min: -size, max: size)
.rewrite([.below: .onExit, .within: .onReturn, .above: .onExit])
.dedupe()
.ignoreUntil(SlopEvent.onExit)
.subscribeAndForward(to: observer)

return {
upstreamSubscription.unsubscribe()
downstreamSubscription.unsubscribe()
}
return upstreamSubscription.unsubscribe
}
}
}
39 changes: 39 additions & 0 deletions tests/unit/operator/ignoreUntilTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2017-present The Material Motion Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import XCTest
import IndefiniteObservable
import MaterialMotion

class ignoreUntilTest: XCTestCase {
func testIgnoreUntil() {

let input = [20, 10, 60, 50, 10, 20, 80]
let expected = [50, 10, 20, 80]
let observable = MotionObservable<Int> { observer in
for i in input {
observer.next(i)
}
return noopDisconnect
}

var values: [Int] = []
observable.ignoreUntil(50).subscribeToValue {
values.append($0)
}
XCTAssertEqual(values, expected)
}
}

0 comments on commit 86bf12b

Please sign in to comment.