From c69e7b8be446c4b9385116c8b6d23ce40c480414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matya=CC=81s=CC=8C=20Kr=CC=8Ci=CC=81z=CC=8C?= Date: Fri, 13 Jul 2018 09:02:42 +0200 Subject: [PATCH 1/2] Replace `Variable` with `BehaviorRelay`. --- Reactant.xcodeproj/project.pbxproj | 4 +++ .../ActivityIndicator/ActivityIndicator.swift | 28 ++++++++++++------- .../Extensions/BehaviorRelay+mutate.swift | 17 +++++++++++ 3 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 Source/Core/Styling/Extensions/BehaviorRelay+mutate.swift diff --git a/Reactant.xcodeproj/project.pbxproj b/Reactant.xcodeproj/project.pbxproj index bed8b70..0c6a559 100644 --- a/Reactant.xcodeproj/project.pbxproj +++ b/Reactant.xcodeproj/project.pbxproj @@ -76,6 +76,7 @@ 22F48FE71FAB0EE900E736C8 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22F48FE61FAB0EE900E736C8 /* AppDelegate.swift */; }; 22F48FEE1FAB0EE900E736C8 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 22F48FED1FAB0EE900E736C8 /* Assets.xcassets */; }; 677138D6038E38CF1E2AB7C6 /* Pods_ReactantPrototyping.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7B0CE169634834E74544F2A0 /* Pods_ReactantPrototyping.framework */; }; + 687B48BD20F88597002BB4A4 /* BehaviorRelay+mutate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 687B48BC20F88597002BB4A4 /* BehaviorRelay+mutate.swift */; }; 71E49563208F37DE00CA3044 /* SubConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71E49562208F37DE00CA3044 /* SubConfiguration.swift */; }; 71E49567208F38FA00CA3044 /* BaseSubConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71E49566208F38FA00CA3044 /* BaseSubConfiguration.swift */; }; 71E49569208F398600CA3044 /* Configuration+TableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71E49568208F398600CA3044 /* Configuration+TableView.swift */; }; @@ -283,6 +284,7 @@ 22F48FF41FAB0EE900E736C8 /* TVPrototypingTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TVPrototypingTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 50895ED3801A06D4D45EE033 /* Pods_Reactant.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Reactant.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 5A648916F85EF94ED93EB6F6 /* Pods-Reactant.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Reactant.release.xcconfig"; path = "Pods/Target Support Files/Pods-Reactant/Pods-Reactant.release.xcconfig"; sourceTree = ""; }; + 687B48BC20F88597002BB4A4 /* BehaviorRelay+mutate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BehaviorRelay+mutate.swift"; sourceTree = ""; }; 711603479E5441514316A7F3 /* Pods_ReactantTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ReactantTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 71E49562208F37DE00CA3044 /* SubConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubConfiguration.swift; sourceTree = ""; }; 71E49566208F38FA00CA3044 /* BaseSubConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseSubConfiguration.swift; sourceTree = ""; }; @@ -530,6 +532,7 @@ 183E0CC21EB88E9C006CF52A /* UILabel+Init.swift */, 183E0CC31EB88E9C006CF52A /* UIOffset+Init.swift */, 183E0CC41EB88E9C006CF52A /* UITableView+Init.swift */, + 687B48BC20F88597002BB4A4 /* BehaviorRelay+mutate.swift */, ); path = Extensions; sourceTree = ""; @@ -1613,6 +1616,7 @@ 183E0CCF1EB88E9C006CF52A /* UILabel+Init.swift in Sources */, 183457891EB16B8B00C972A4 /* ReactantUI.swift in Sources */, DC69407F1E51AE9A00C99380 /* ReactantCollectionView+Delegates.swift in Sources */, + 687B48BD20F88597002BB4A4 /* BehaviorRelay+mutate.swift in Sources */, DC1C67F21E5C39F30081E7BD /* Sequence+Utils.swift in Sources */, DC1C68011E5F574B0081E7BD /* ActivityIndicator.swift in Sources */, DC6940851E51FC8400C99380 /* FlowCollectionViewBase+Delegates.swift in Sources */, diff --git a/Source/ActivityIndicator/ActivityIndicator.swift b/Source/ActivityIndicator/ActivityIndicator.swift index 0575c57..c1cd94e 100644 --- a/Source/ActivityIndicator/ActivityIndicator.swift +++ b/Source/ActivityIndicator/ActivityIndicator.swift @@ -15,7 +15,7 @@ public final class ActivityIndicator: ObservableConvertibleType { public let disposeBag = DisposeBag() - private let variable: Variable<[(id: UUID, associatedValue: T?)]> = Variable([]) + private let behaviorRelay: BehaviorRelay<[(id: UUID, associatedValue: T?)]> = BehaviorRelay(value: []) private let driver: Driver private let equalFunction: (T?, T?) -> Bool fileprivate let defaultAssociatedValue: T? @@ -32,10 +32,9 @@ public final class ActivityIndicator: ObservableConvertibleType { // `self` cannot be captured before all fields are initialized. let equal = self.equalFunction - driver = variable.asDriver() + driver = behaviorRelay.asDriver() .map { (loading: !$0.isEmpty, associatedValue: $0.compactMap { $0.associatedValue }.first) } - .distinctUntilChanged { lhs, rhs in lhs.loading == rhs.loading && - equal(lhs.associatedValue, rhs.associatedValue) } + .distinctUntilChanged { lhs, rhs in lhs.loading == rhs.loading && equal(lhs.associatedValue, rhs.associatedValue) } } public func trackActivity(of source: O, initialAssociatedValue: T?, @@ -50,11 +49,15 @@ public final class ActivityIndicator: ObservableConvertibleType { let messageChangeDisposable = observable.map(associatedValueProvider) .startWith(initialAssociatedValue) .distinctUntilChanged(self.equalFunction) - .subscribe(onNext: { - if let index = self.variable.value.index(where: { $0.id == id }) { - self.variable.value[index].associatedValue = $0 + .subscribe(onNext: { associatedValue in + if let index = self.behaviorRelay.value.index(where: { $0.id == id }) { + self.behaviorRelay.mutate(using: { + $0[index].associatedValue = associatedValue + }) } else { - self.variable.value.append((id: id, associatedValue: $0)) + self.behaviorRelay.mutate(using: { + $0.append((id: id, associatedValue: associatedValue)) + }) } }) @@ -62,8 +65,13 @@ public final class ActivityIndicator: ObservableConvertibleType { subscriptionDisposable.dispose() messageChangeDisposable.dispose() - if let index = self.variable.value.index(where: { $0.id == id }) { - self.variable.value.remove(at: index) + if let index = self.behaviorRelay.value.index(where: { $0.id == id }) { + self.behaviorRelay.mutate(using: { + $0.remove(at: index) + }) + var mutableArray = self.behaviorRelay.value + mutableArray.remove(at: index) + self.behaviorRelay.accept(mutableArray) } } } diff --git a/Source/Core/Styling/Extensions/BehaviorRelay+mutate.swift b/Source/Core/Styling/Extensions/BehaviorRelay+mutate.swift new file mode 100644 index 0000000..06febb3 --- /dev/null +++ b/Source/Core/Styling/Extensions/BehaviorRelay+mutate.swift @@ -0,0 +1,17 @@ +// +// BehaviorRelay+mutate.swift +// Reactant +// +// Created by Matyáš Kříž on 13/07/2018. +// Copyright © 2018 Brightify. All rights reserved. +// + +import RxCocoa + +extension BehaviorRelay { + internal func mutate(using mutator: (inout Element) -> Void) { + var mutableValue = value + mutator(&mutableValue) + accept(mutableValue) + } +} From f04b2b26b63e05a1e86669b001bf0f12d86c58d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matya=CC=81s=CC=8C=20Kr=CC=8Ci=CC=81z=CC=8C?= Date: Fri, 13 Jul 2018 09:17:07 +0200 Subject: [PATCH 2/2] Remove overlooked old way. --- Source/ActivityIndicator/ActivityIndicator.swift | 3 --- 1 file changed, 3 deletions(-) diff --git a/Source/ActivityIndicator/ActivityIndicator.swift b/Source/ActivityIndicator/ActivityIndicator.swift index c1cd94e..bcdf38a 100644 --- a/Source/ActivityIndicator/ActivityIndicator.swift +++ b/Source/ActivityIndicator/ActivityIndicator.swift @@ -69,9 +69,6 @@ public final class ActivityIndicator: ObservableConvertibleType { self.behaviorRelay.mutate(using: { $0.remove(at: index) }) - var mutableArray = self.behaviorRelay.value - mutableArray.remove(at: index) - self.behaviorRelay.accept(mutableArray) } } }