From 849ef3e945cfd11cfb1f1fce119f9829d33520ea Mon Sep 17 00:00:00 2001 From: Felix Mau Date: Sat, 2 Oct 2021 13:43:52 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20::=20Directly=20access=20f?= =?UTF-8?q?irst=20row=20of=20color=20location=20matrix=20instead=20of=20ha?= =?UTF-8?q?ving=20a=20separate=20observable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This way we can prevent `colorLocationMatrix` and `colorLocationInitialRow` running out of sync. --- ...ntActivityIndicatorViewModelTestCase.swift | 34 ------------------- .../GradientActivityIndicatorViewModel.swift | 11 ------ .../Views/GradientActivityIndicatorView.swift | 8 ++--- 3 files changed, 4 insertions(+), 49 deletions(-) diff --git a/Example/Tests/ViewModel/GradientActivityIndicatorViewModelTestCase.swift b/Example/Tests/ViewModel/GradientActivityIndicatorViewModelTestCase.swift index 6013683..0328e33 100644 --- a/Example/Tests/ViewModel/GradientActivityIndicatorViewModelTestCase.swift +++ b/Example/Tests/ViewModel/GradientActivityIndicatorViewModelTestCase.swift @@ -38,18 +38,6 @@ class GradientActivityIndicatorViewModelTestCase: XCTestCase { XCTAssertEqual(viewModel.gradientLayerColors.value, expectedGradientLayerColors) } - func testInitializerShouldSetColorLocationInitialRowToCorrectValue() throws { - let receivedColorLocationInitialRow = try XCTUnwrap(viewModel.colorLocationInitialRow.value) - let expectedColorLocationInitialRow = makeColorLocationInitialRow() - - // Unfortunately there is no easier way comparing an array of type `NSNumber` / `Double` with a given accuracy. - XCTAssertEqual(receivedColorLocationInitialRow.count, expectedColorLocationInitialRow.count) - - for (receivedColorLocation, expectedColorLocation) in zip(receivedColorLocationInitialRow, expectedColorLocationInitialRow) { - XCTAssertEqual(receivedColorLocation.doubleValue, expectedColorLocation.doubleValue, accuracy: .ulpOfOne) - } - } - func testInitializerShouldSetColorLocationMatrixToCorrectValue() throws { let receivedColorLocationMatrix = try XCTUnwrap(viewModel.colorLocationMatrix.value) let expectedColorLocationMatrix = makeColorLocationMatrix() @@ -108,27 +96,6 @@ class GradientActivityIndicatorViewModelTestCase: XCTestCase { XCTAssertEqual(viewModel.gradientLayerColors.value, expectedGradientLayerColors) } - func testSettingGradientColorsShouldUpdateColorLocationInitialRowObservable() { - // Given - let gradientColors: [UIColor] = [.red, .yellow, .green] - - // When - viewModel.gradientColors = gradientColors - - // Then - // - // `gradientColors = [.red, .yellow, .green]` - // `gradientLayerColors = [.red, .yellow, .green, .yellow, .red, .yellow, .green]` - // - // gradientLayerColors | .red | .yellow | .green | .green | .yellow | .red | .yellow | .green - // initialLocations | 0 | 0 | 0 | 0 | 0 | 0 | 0.5 | 1 - // - let expectedGradientLocations = [0, 0, 0, 0, 0, 0.5, 1] - let expectedGradientLayerLocations = expectedGradientLocations.map { NSNumber(value: $0) } - - XCTAssertEqual(viewModel.colorLocationInitialRow.value, expectedGradientLayerLocations) - } - func testSettingGradientColorsShouldUpdateColorLocationMatrixObservable() { // Given let gradientColors: [UIColor] = [.red, .yellow, .green] @@ -255,7 +222,6 @@ extension GradientActivityIndicatorViewModelTestCase { // ... // 0 | 0.2 | 0.4 | 0.6 | 0.8 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 // - let colorLocationMatrix = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.2, 0.4, 0.6, 0.8, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.2, 0.4, 0.6, 0.8, 1, 1], diff --git a/GradientLoadingBar/Classes/ViewModel/GradientActivityIndicatorViewModel.swift b/GradientLoadingBar/Classes/ViewModel/GradientActivityIndicatorViewModel.swift index abeff37..1090ffd 100644 --- a/GradientLoadingBar/Classes/ViewModel/GradientActivityIndicatorViewModel.swift +++ b/GradientLoadingBar/Classes/ViewModel/GradientActivityIndicatorViewModel.swift @@ -49,14 +49,6 @@ final class GradientActivityIndicatorViewModel { gradientLayerColorsSubject } - /// Observable color locations for the gradient layer. - /// - /// - Note: In order to have a working animation we need to provide the initial gradient-locations, - /// which is the first row of our animation matrix. - var colorLocationInitialRow: Observable { - colorLocationInitialRowSubject - } - /// Observable color location matrix, used to position the gradient colors during the animation. /// - The outer array defines each animation step. /// - The inner array defines the location of each gradient-color during this animation step. @@ -83,7 +75,6 @@ final class GradientActivityIndicatorViewModel { let gradientLocationMatrix = ColorLocationMatrix.colorLocationMatrix(gradientColorsQuantity: gradientColors.count, gradientLayerColorsQuantity: gradientLayerColors.count) - colorLocationInitialRowSubject.value = gradientLocationMatrix[0] colorLocationMatrixSubject.value = gradientLocationMatrix } } @@ -108,7 +99,6 @@ final class GradientActivityIndicatorViewModel { // MARK: - Private properties private let gradientLayerColorsSubject: Variable<[CGColor]> - private let colorLocationInitialRowSubject: Variable private let colorLocationMatrixSubject: Variable private let animationDurationSubject = Variable(TimeInterval.GradientLoadingBar.progressDuration) @@ -122,7 +112,6 @@ final class GradientActivityIndicatorViewModel { gradientLayerColorsQuantity: gradientLayerColors.count) gradientLayerColorsSubject = Variable(gradientLayerColors) - colorLocationInitialRowSubject = Variable(gradientLocationMatrix[0]) colorLocationMatrixSubject = Variable(gradientLocationMatrix) } } diff --git a/GradientLoadingBar/Classes/Views/GradientActivityIndicatorView.swift b/GradientLoadingBar/Classes/Views/GradientActivityIndicatorView.swift index 99bff95..96682f1 100644 --- a/GradientLoadingBar/Classes/Views/GradientActivityIndicatorView.swift +++ b/GradientLoadingBar/Classes/Views/GradientActivityIndicatorView.swift @@ -103,12 +103,12 @@ open class GradientActivityIndicatorView: UIView { self?.gradientLayer?.colors = newGradientLayerColors }.disposed(by: &disposeBag) - viewModel.colorLocationInitialRow.subscribeDistinct { [weak self] newColorLocationInitialRow, _ in - self?.gradientLayer?.locations = newColorLocationInitialRow - }.disposed(by: &disposeBag) - viewModel.colorLocationMatrix.subscribeDistinct { [weak self] newColorLocationMatrix, _ in self?.progressAnimation.values = newColorLocationMatrix + + // In order to have a working animation we need to provide the initial gradient-locations, + // which is the first row of our animation matrix. + self?.gradientLayer?.locations = newColorLocationMatrix.first }.disposed(by: &disposeBag) viewModel.animationDuration.subscribeDistinct { [weak self] newAnimationDuration, _ in