Commit e7c3d53b by Demid Merzlyakov

IOS-157: fix SHORTS_CARD_BINGE_VIEW event is not getting fired.

parent 40a375a0
...@@ -17,6 +17,15 @@ private enum ScrollDirection { ...@@ -17,6 +17,15 @@ private enum ScrollDirection {
class ShortsViewController: UIViewController { class ShortsViewController: UIViewController {
//Private //Private
enum Source: String {
case swipeUp = "swipe_up"
case swipeDown = "swipe_down"
case cardClick = "card_click"
case deeplink = "deeplink"
case others = "others"
case weatherFacts = "weather_facts"
}
private let kAnimationKey = "com.oneWeather.scrollView.snappingAnimation" private let kAnimationKey = "com.oneWeather.scrollView.snappingAnimation"
private let coordinator:ShortsCoordinator private let coordinator:ShortsCoordinator
private let viewModel = ShortsViewModel(shortsManager: ShortsManager.shared) private let viewModel = ShortsViewModel(shortsManager: ShortsManager.shared)
...@@ -27,7 +36,7 @@ class ShortsViewController: UIViewController { ...@@ -27,7 +36,7 @@ class ShortsViewController: UIViewController {
private var lastOffset: CGFloat = 0 private var lastOffset: CGFloat = 0
private var itemIndexToScroll: Int? private var itemIndexToScroll: Int?
private var swipeUpCounter = 0 private var swipeUpCounter = 0
private var source: String? private var source: Source = .others
private var shortsViewTimeSpentSec = 0 private var shortsViewTimeSpentSec = 0
private lazy var viewScheduler: Scheduler = { private lazy var viewScheduler: Scheduler = {
let scheduler = Scheduler(with: "shorts_binge_view_time", let scheduler = Scheduler(with: "shorts_binge_view_time",
...@@ -41,6 +50,7 @@ class ShortsViewController: UIViewController { ...@@ -41,6 +50,7 @@ class ShortsViewController: UIViewController {
deinit { deinit {
coordinator.viewControllerDidEnd(controller: self) coordinator.viewControllerDidEnd(controller: self)
NotificationCenter.default.removeObserver(self)
} }
init(coordinator:ShortsCoordinator) { init(coordinator:ShortsCoordinator) {
...@@ -48,6 +58,7 @@ class ShortsViewController: UIViewController { ...@@ -48,6 +58,7 @@ class ShortsViewController: UIViewController {
super.init(nibName: nil, bundle: nil) super.init(nibName: nil, bundle: nil)
} }
@available(*, unavailable)
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
...@@ -68,7 +79,7 @@ class ShortsViewController: UIViewController { ...@@ -68,7 +79,7 @@ class ShortsViewController: UIViewController {
viewScheduler.start() viewScheduler.start()
if let indexToScroll = itemIndexToScroll { if let indexToScroll = itemIndexToScroll {
source = "card_click" source = .cardClick
tableView.scrollToRow(at: [0, indexToScroll], at: .top, animated: false) tableView.scrollToRow(at: [0, indexToScroll], at: .top, animated: false)
lastOffset = tableView.contentOffset.y lastOffset = tableView.contentOffset.y
updateView(basedOnDirection: .swipeUp, willShowRowAt: indexToScroll) updateView(basedOnDirection: .swipeUp, willShowRowAt: indexToScroll)
...@@ -77,10 +88,10 @@ class ShortsViewController: UIViewController { ...@@ -77,10 +88,10 @@ class ShortsViewController: UIViewController {
AppAnalytics.shared.log(event: .ANALYTICS_SHORTS_VIEW_SHORTS, AppAnalytics.shared.log(event: .ANALYTICS_SHORTS_VIEW_SHORTS,
params: [.ANALYTICS_KEY_SHORTS_CARD_ID : viewModel.shorts[indexToScroll].id, params: [.ANALYTICS_KEY_SHORTS_CARD_ID : viewModel.shorts[indexToScroll].id,
.ANALYTICS_KEY_SOURCE : "card_click"]) .ANALYTICS_KEY_SOURCE : source])
} }
else { else {
source = "others" source = .others
lastOffset = 0 lastOffset = 0
updateView(basedOnDirection: .swipeDown, willShowRowAt: 0) updateView(basedOnDirection: .swipeDown, willShowRowAt: 0)
tableView.scrollToRow(at: [0, 0], at: .top, animated: false) tableView.scrollToRow(at: [0, 0], at: .top, animated: false)
...@@ -88,20 +99,27 @@ class ShortsViewController: UIViewController { ...@@ -88,20 +99,27 @@ class ShortsViewController: UIViewController {
AppAnalytics.shared.log(event: .ANALYTICS_SHORTS_VIEW_SHORTS, AppAnalytics.shared.log(event: .ANALYTICS_SHORTS_VIEW_SHORTS,
params: [.ANALYTICS_KEY_SHORTS_CARD_ID : viewModel.shorts[0].id, params: [.ANALYTICS_KEY_SHORTS_CARD_ID : viewModel.shorts[0].id,
.ANALYTICS_KEY_SOURCE : "others"]) .ANALYTICS_KEY_SOURCE : source])
} }
NotificationCenter.default.addObserver(self, selector: #selector(didEnterBackgroundHandler), name: UIApplication.didEnterBackgroundNotification, object: nil)
} }
override func viewWillDisappear(_ animated: Bool) { override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated) super.viewWillDisappear(animated)
source = nil
viewScheduler.stop() viewScheduler.stop()
viewScheduler.resetTimer() viewScheduler.resetTimer()
shortsViewTimeSpentSec = 0 shortsViewTimeSpentSec = 0
swipeUpCounter = 0 swipeUpCounter = 0
ShortsManager.shared.reordering() ShortsManager.shared.reordering()
AppAnalytics.shared.log(event: .ANALYTICS_SHORTS_EXIT_SHORTS_VIEW, params: nil) if let currentCardIndex = self.currentCardIndex?.row {
AppAnalytics.shared.log(event: .ANALYTICS_SHORTS_EXIT_SHORTS_VIEW, params: [
.ANALYTICS_KEY_SHORTS_CARD_ID: currentCardIndex,
.ANALYTICS_KEY_SOURCE: source
])
logShortsAnalytics(source: source, forRowAtIndex: currentCardIndex)
}
source = .others
NotificationCenter.default.removeObserver(self)
} }
private func scrollTo(newOffset:CGPoint, velocity:CGPoint) { private func scrollTo(newOffset:CGPoint, velocity:CGPoint) {
...@@ -177,16 +195,7 @@ class ShortsViewController: UIViewController { ...@@ -177,16 +195,7 @@ class ShortsViewController: UIViewController {
} }
private func logShortsAnalytics(source: String, forRowAtIndex index: Int) { private func logShortsAnalytics(source: Source, forRowAtIndex index: Int) {
let shortsSource: String
if self.source != nil {
shortsSource = self.source!
self.source = nil
}
else {
shortsSource = source
}
var timeSpent: String var timeSpent: String
switch shortsViewTimeSpentSec { switch shortsViewTimeSpentSec {
case 0...2: case 0...2:
...@@ -205,7 +214,7 @@ class ShortsViewController: UIViewController { ...@@ -205,7 +214,7 @@ class ShortsViewController: UIViewController {
AppAnalytics.shared.log(event: .ANALYTICS_SHORTS_CARD_BINGE_VIEW, AppAnalytics.shared.log(event: .ANALYTICS_SHORTS_CARD_BINGE_VIEW,
params: [.ANALYTICS_KEY_SHORTS_CARD_ID : viewModel.shorts[index].id, params: [.ANALYTICS_KEY_SHORTS_CARD_ID : viewModel.shorts[index].id,
.ANALYTICS_KEY_SOURCE : shortsSource, .ANALYTICS_KEY_SOURCE : source.rawValue,
.ANALYTICS_KEY_SHORTS_TIME_SPENT : timeSpent]) .ANALYTICS_KEY_SHORTS_TIME_SPENT : timeSpent])
viewScheduler.resetTimer() viewScheduler.resetTimer()
shortsViewTimeSpentSec = 0 shortsViewTimeSpentSec = 0
...@@ -214,6 +223,14 @@ class ShortsViewController: UIViewController { ...@@ -214,6 +223,14 @@ class ShortsViewController: UIViewController {
func set(indexToScroll: Int) { func set(indexToScroll: Int) {
itemIndexToScroll = indexToScroll itemIndexToScroll = indexToScroll
} }
@objc
private func didEnterBackgroundHandler() {
if let currentCardIndex = currentCardIndex?.row {
logShortsAnalytics(source: source, forRowAtIndex: currentCardIndex)
}
source = .others
}
} }
//MARK:- Prepare //MARK:- Prepare
...@@ -278,6 +295,11 @@ extension ShortsViewController: UITableViewDelegate { ...@@ -278,6 +295,11 @@ extension ShortsViewController: UITableViewDelegate {
return tableView.bounds.height return tableView.bounds.height
} }
private var currentCardIndex: IndexPath? {
let visibleRows = tableView.indexPathsForVisibleRows?.sorted { $0.row < $1.row }
return visibleRows?.first
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
//Get direction //Get direction
let direction:ScrollDirection let direction:ScrollDirection
...@@ -295,11 +317,7 @@ extension ShortsViewController: UITableViewDelegate { ...@@ -295,11 +317,7 @@ extension ShortsViewController: UITableViewDelegate {
targetContentOffset.pointee = scrollView.contentOffset targetContentOffset.pointee = scrollView.contentOffset
//Get visible rows IndexPaths //Get visible rows IndexPaths
guard guard let currentCardIndex = currentCardIndex else {
let visiblePath = (tableView.indexPathsForVisibleRows?.sorted{$0.row < $1.row}),
visiblePath.count > 0,
let topRowIndexPath = visiblePath.first
else {
return return
} }
...@@ -307,9 +325,9 @@ extension ShortsViewController: UITableViewDelegate { ...@@ -307,9 +325,9 @@ extension ShortsViewController: UITableViewDelegate {
let nextRowIndexPath:IndexPath let nextRowIndexPath:IndexPath
switch direction { switch direction {
case .swipeUp: case .swipeUp:
let rowsCount = tableView.numberOfRows(inSection: topRowIndexPath.section) let rowsCount = tableView.numberOfRows(inSection: currentCardIndex.section)
let nextIndex = min(topRowIndexPath.row + 1, rowsCount - 1) let nextIndex = min(currentCardIndex.row + 1, rowsCount - 1)
nextRowIndexPath = IndexPath(row: nextIndex, section: topRowIndexPath.section) nextRowIndexPath = IndexPath(row: nextIndex, section: currentCardIndex.section)
//Check for the last row //Check for the last row
if nextRowIndexPath.row == rowsCount - 1 { if nextRowIndexPath.row == rowsCount - 1 {
...@@ -317,26 +335,29 @@ extension ShortsViewController: UITableViewDelegate { ...@@ -317,26 +335,29 @@ extension ShortsViewController: UITableViewDelegate {
updateView(basedOnDirection: direction, willShowRowAt: nextRowIndexPath.row) updateView(basedOnDirection: direction, willShowRowAt: nextRowIndexPath.row)
let offset = tableView.contentSize.height - tableView.frame.height let offset = tableView.contentSize.height - tableView.frame.height
self.scrollTo(newOffset: .init(x: tableView.contentOffset.x, y: offset), velocity: velocity) self.scrollTo(newOffset: .init(x: tableView.contentOffset.x, y: offset), velocity: velocity)
self.logShortsAnalytics(source: "swipe_up", forRowAtIndex: rowsCount - 1) self.logShortsAnalytics(source: source, forRowAtIndex: rowsCount - 1)
source = .swipeUp
return return
} }
case .swipeDown: case .swipeDown:
if topRowIndexPath.row == 0 { if currentCardIndex.row == 0 {
viewModel.markAsViewed(atIndex: 0) viewModel.markAsViewed(atIndex: 0)
updateView(basedOnDirection: direction, willShowRowAt: 0) updateView(basedOnDirection: direction, willShowRowAt: 0)
self.scrollTo(newOffset: .zero, velocity: velocity) self.scrollTo(newOffset: .zero, velocity: velocity)
self.logShortsAnalytics(source: "swipe_down", forRowAtIndex: 0) self.logShortsAnalytics(source: source, forRowAtIndex: 0)
source = .swipeDown
return return
} }
nextRowIndexPath = topRowIndexPath nextRowIndexPath = currentCardIndex
} }
let nextRowRect = tableView.rectForRow(at: nextRowIndexPath) let nextRowRect = tableView.rectForRow(at: nextRowIndexPath)
viewModel.markAsViewed(atIndex: nextRowIndexPath.row) viewModel.markAsViewed(atIndex: nextRowIndexPath.row)
updateView(basedOnDirection: direction, willShowRowAt: nextRowIndexPath.row) updateView(basedOnDirection: direction, willShowRowAt: nextRowIndexPath.row)
self.scrollTo(newOffset: nextRowRect.origin, velocity: velocity) self.scrollTo(newOffset: nextRowRect.origin, velocity: velocity)
self.logShortsAnalytics(source: direction == .swipeDown ? "swipe_down" : "swipe_up", forRowAtIndex: nextRowIndexPath.row - 1) self.logShortsAnalytics(source: source, forRowAtIndex: nextRowIndexPath.row - 1)
source = direction == .swipeDown ? .swipeDown : .swipeUp
} }
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment