Skip to content

Commit

Permalink
Merge pull request #10 from daangn/feature/david/tracking-delegate-im…
Browse files Browse the repository at this point in the history
…provement

willBeginTracking and didTrack redefinition
  • Loading branch information
GeekTree0101 authored Oct 28, 2022
2 parents de0f3e7 + 97cf427 commit bca0d56
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 26 deletions.
5 changes: 5 additions & 0 deletions Example/SushiBelt/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ extension ViewController: SushiBeltTrackerDelegate {

print("🚀 begin tracking: \(item.debugDescription)")
}

func didTrack(_ tracker: SushiBeltTracker, item: SushiBeltTrackerItem) {

print("🎯 tracked: \(item.debugDescription)")
}

func didEndTracking(_ tracker: SushiBeltTracker, item: SushiBeltTrackerItem) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public final class DefaultSushiBeltTrackerItemDiffChecker: SushiBeltTrackerItemD
)

let endedItems = updatedItems.subtracting(newItems)
let newItems = updatedItems.subtracting(oldItems)

return SushiBeltTrackerItemDiffResult(
calculationTargetedItems: updatedItems.subtracting(endedItems),
newItems: newItems,
endedItems: endedItems
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import Foundation

public struct SushiBeltTrackerItemDiffResult {
public let calculationTargetedItems: Set<SushiBeltTrackerItem>
public let newItems: Set<SushiBeltTrackerItem>
public let endedItems: Set<SushiBeltTrackerItem>

public init(
calculationTargetedItems: Set<SushiBeltTrackerItem>,
newItems: Set<SushiBeltTrackerItem>,
endedItems: Set<SushiBeltTrackerItem>
) {
self.calculationTargetedItems = calculationTargetedItems
self.newItems = newItems
self.endedItems = endedItems
}
}
12 changes: 9 additions & 3 deletions Sources/SushiBelt/SushiBeltTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class SushiBeltTracker {
)

self.cachedItems = result.calculationTargetedItems

self.willBeginTracking(items: result.newItems)
self.checkBeginTrackingItems(items: self.cachedItems)
self.didEndTracking(items: result.endedItems)

Expand Down Expand Up @@ -97,7 +97,7 @@ extension SushiBeltTracker {
/// delegate willBeginTracking
if shouldSendWillBeginTracking {
mutableItem.isTracked = true
self.delegate?.willBeginTracking(self, item: mutableItem)
self.delegate?.didTrack(self, item: mutableItem)
}

/// update cached item
Expand Down Expand Up @@ -139,7 +139,13 @@ extension SushiBeltTracker {
scrollDirection: self.recentScrollDirection
)
}


private func willBeginTracking(items: Set<SushiBeltTrackerItem>) {
items.forEach {
self.delegate?.willBeginTracking(self, item: $0)
}
}

private func didEndTracking(items: Set<SushiBeltTrackerItem>) {
items.forEach {
self.delegate?.didEndTracking(self, item: $0)
Expand Down
1 change: 1 addition & 0 deletions Sources/SushiBelt/SushiBeltTrackerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ import UIKit

public protocol SushiBeltTrackerDelegate: AnyObject {
func willBeginTracking(_ tracker: SushiBeltTracker, item: SushiBeltTrackerItem)
func didTrack(_ tracker: SushiBeltTracker, item: SushiBeltTrackerItem)
func didEndTracking(_ tracker: SushiBeltTracker, item: SushiBeltTrackerItem)
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extension SushiBeltTrackerItemDiffCheckerTests {
}
}

// MARK: - diff calculationTargeted & ended items
// MARK: - diff calculationTargeted & new/ended items

extension SushiBeltTrackerItemDiffCheckerTests {

Expand All @@ -84,7 +84,10 @@ extension SushiBeltTrackerItemDiffCheckerTests {

// then
XCTAssertEqual(result.calculationTargetedItems.count, 1)


XCTAssertEqual(result.newItems.count, 1)
XCTAssertEqual(result.newItems.first?.id, .index(1))

XCTAssertEqual(result.endedItems.count, 0)
}

Expand All @@ -111,9 +114,12 @@ extension SushiBeltTrackerItemDiffCheckerTests {
// then
XCTAssertEqual(result.calculationTargetedItems.count, 1)
XCTAssertEqual(result.calculationTargetedItems.first, newItems.first)


XCTAssertEqual(result.newItems.count, 1)
XCTAssertEqual(result.newItems.first?.id, .index(200))

XCTAssertEqual(result.endedItems.count, 1)
XCTAssertEqual(result.endedItems.first, oldItems.first)
XCTAssertEqual(result.endedItems.first?.id, .index(1))
}

func test_diff_overlaped_items_should_return_as_calculationTargetedItems() {
Expand Down Expand Up @@ -146,7 +152,10 @@ extension SushiBeltTrackerItemDiffCheckerTests {
guard case let .index(index) = item.id else { return false }
return index == 1
}))


XCTAssertEqual(result.newItems.count, 1)
XCTAssertEqual(result.newItems.first?.id, .index(200))

XCTAssertEqual(result.endedItems.count, 0)
}

Expand Down Expand Up @@ -181,11 +190,11 @@ extension SushiBeltTrackerItemDiffCheckerTests {
// then
XCTAssertEqual(result.calculationTargetedItems.count, 2)
XCTAssertEqual(result.calculationTargetedItems, newItems)


XCTAssertEqual(result.newItems.count, 1)
XCTAssertEqual(result.newItems.first?.id, .index(200))

XCTAssertEqual(result.endedItems.count, 1)
XCTAssertTrue(result.endedItems.contains(where: { item in
guard case let .index(index) = item.id else { return false }
return index == 1
}))
XCTAssertEqual(result.endedItems.first?.id, .index(1))
}
}
68 changes: 60 additions & 8 deletions Tests/SushiBeltTests/SushiBeltTrackerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ extension SushiBeltTrackerTests {
}
}

// MARK: - checkBeginTrackingItems
// MARK: - tracked

extension SushiBeltTrackerTests {

Expand Down Expand Up @@ -221,7 +221,7 @@ extension SushiBeltTrackerTests {
XCTAssertEqual(trackedItems.count, 1)
}

func test_checkBeginTrackingItems_should_call_willBeginTracking() {
func test_checkBeginTrackingItems_should_call_didTrack() {
// given
let tracker = self.createTracker()
let items = [
Expand Down Expand Up @@ -256,13 +256,14 @@ extension SushiBeltTrackerTests {
// then
let trackedItems = tracker.cachedItems.filter({ $0.isTracked })
XCTAssertEqual(trackedItems.count, 1)
XCTAssertEqual(self.sushiBeltTrackerDelegate.willBeginTrackingItem?.currentVisibleRatio, 0.8)
XCTAssertEqual(self.sushiBeltTrackerDelegate.willBeginTrackingItem?.objectiveVisibleRatio, 0.5)
XCTAssertEqual(self.sushiBeltTrackerDelegate.willBeginTrackingItem?.isTracked, true)
XCTAssertEqual(self.sushiBeltTrackerDelegate.willBeginTrackingItem?.id, .index(2))
XCTAssertEqual(self.sushiBeltTrackerDelegate.didTrackItems.count, 1)
XCTAssertEqual(self.sushiBeltTrackerDelegate.didTrackItems.first?.currentVisibleRatio, 0.8)
XCTAssertEqual(self.sushiBeltTrackerDelegate.didTrackItems.first?.objectiveVisibleRatio, 0.5)
XCTAssertEqual(self.sushiBeltTrackerDelegate.didTrackItems.first?.isTracked, true)
XCTAssertEqual(self.sushiBeltTrackerDelegate.didTrackItems.first?.id, .index(2))
}

func test_checkBeginTrackingItems_should_not_call_willBeginTracking_on_already_tracked() {
func test_checkBeginTrackingItems_should_not_call_didTrack_on_already_tracked() {
// given
let tracker = self.createTracker()
let items = [
Expand Down Expand Up @@ -297,6 +298,57 @@ extension SushiBeltTrackerTests {
// then
let trackedItems = tracker.cachedItems.filter({ $0.isTracked })
XCTAssertEqual(trackedItems.count, 1)
XCTAssertEqual(self.sushiBeltTrackerDelegate.willBeginTrackingItem, nil)
XCTAssertEqual(self.sushiBeltTrackerDelegate.didTrackItems.isEmpty, true)
}
}

// MARK: - willBeginTracking

extension SushiBeltTrackerTests {

func test_checkBeginTrackingItems_willBeginTracking() {
// given
let tracker = self.createTracker()
let items = [
SushiBeltTrackerItem(
id: .index(1),
view: UIView(frame: .zero)
).frameInWindow(CGRect(x: 0.0, y: 100.0, width: 100.0, height: 100.0))
]

tracker.cachedItems = .init()

// when
tracker.calculateItemsIfNeeded(items: items)

// then
XCTAssertEqual(self.sushiBeltTrackerDelegate.willBeginTrackingItems.count, 1)
XCTAssertEqual(self.sushiBeltTrackerDelegate.didEndTrackingItems.count, 0)
}
}


// MARK: - didEndTracking

extension SushiBeltTrackerTests {

func test_checkBeginTrackingItems_didEndTracking() {
// given
let tracker = self.createTracker()
let items: [SushiBeltTrackerItem] = []

tracker.cachedItems = .init([
SushiBeltTrackerItem(
id: .index(1),
view: UIView(frame: .zero)
).frameInWindow(CGRect(x: 0.0, y: 100.0, width: 100.0, height: 100.0))
])

// when
tracker.calculateItemsIfNeeded(items: items)

// then
XCTAssertEqual(self.sushiBeltTrackerDelegate.willBeginTrackingItems.count, 0)
XCTAssertEqual(self.sushiBeltTrackerDelegate.didEndTrackingItems.count, 1)
}
}
16 changes: 11 additions & 5 deletions Tests/SushiBeltTests/TestDoubles/SushiBeltTrackerDelegateSpy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ import Foundation

final class SushiBeltTrackerDelegateSpy: SushiBeltTrackerDelegate {

var willBeginTrackingItem: SushiBeltTrackerItem?
var willBeginTrackingItems: [SushiBeltTrackerItem] = []

func willBeginTracking(_ tracker: SushiBeltTracker, item: SushiBeltTrackerItem) {
self.willBeginTrackingItem = item
self.willBeginTrackingItems.append(item)
}

var didEndTrackingItem: SushiBeltTrackerItem?

var didTrackItems: [SushiBeltTrackerItem] = []

func didTrack(_ tracker: SushiBeltTracker, item: SushiBeltTrackerItem) {
self.didTrackItems.append(item)
}

var didEndTrackingItems: [SushiBeltTrackerItem] = []

func didEndTracking(_ tracker: SushiBeltTracker, item: SushiBeltTrackerItem) {
self.didEndTrackingItem = item
self.didEndTrackingItems.append(item)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class SushiBeltTrackerItemDiffCheckerSpy: SushiBeltTrackerItemDiffChecker
var diffNewItems: Set<SushiBeltTrackerItem>?
var diffStub: SushiBeltTrackerItemDiffResult = .init(
calculationTargetedItems: .init(),
newItems: .init(),
endedItems: .init()
)

Expand Down

0 comments on commit bca0d56

Please sign in to comment.