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 {
class ShortsViewController: UIViewController {
//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 coordinator:ShortsCoordinator
private let viewModel = ShortsViewModel(shortsManager: ShortsManager.shared)
......@@ -27,7 +36,7 @@ class ShortsViewController: UIViewController {
private var lastOffset: CGFloat = 0
private var itemIndexToScroll: Int?
private var swipeUpCounter = 0
private var source: String?
private var source: Source = .others
private var shortsViewTimeSpentSec = 0
private lazy var viewScheduler: Scheduler = {
let scheduler = Scheduler(with: "shorts_binge_view_time",
......@@ -41,6 +50,7 @@ class ShortsViewController: UIViewController {
deinit {
coordinator.viewControllerDidEnd(controller: self)
NotificationCenter.default.removeObserver(self)
}
init(coordinator:ShortsCoordinator) {
......@@ -48,6 +58,7 @@ class ShortsViewController: UIViewController {
super.init(nibName: nil, bundle: nil)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
......@@ -68,7 +79,7 @@ class ShortsViewController: UIViewController {
viewScheduler.start()
if let indexToScroll = itemIndexToScroll {
source = "card_click"
source = .cardClick
tableView.scrollToRow(at: [0, indexToScroll], at: .top, animated: false)
lastOffset = tableView.contentOffset.y
updateView(basedOnDirection: .swipeUp, willShowRowAt: indexToScroll)
......@@ -77,10 +88,10 @@ class ShortsViewController: UIViewController {
AppAnalytics.shared.log(event: .ANALYTICS_SHORTS_VIEW_SHORTS,
params: [.ANALYTICS_KEY_SHORTS_CARD_ID : viewModel.shorts[indexToScroll].id,
.ANALYTICS_KEY_SOURCE : "card_click"])
.ANALYTICS_KEY_SOURCE : source])
}
else {
source = "others"
source = .others
lastOffset = 0
updateView(basedOnDirection: .swipeDown, willShowRowAt: 0)
tableView.scrollToRow(at: [0, 0], at: .top, animated: false)
......@@ -88,20 +99,27 @@ class ShortsViewController: UIViewController {
AppAnalytics.shared.log(event: .ANALYTICS_SHORTS_VIEW_SHORTS,
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) {
super.viewWillDisappear(animated)
source = nil
viewScheduler.stop()
viewScheduler.resetTimer()
shortsViewTimeSpentSec = 0
swipeUpCounter = 0
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) {
......@@ -177,16 +195,7 @@ class ShortsViewController: UIViewController {
}
private func logShortsAnalytics(source: String, forRowAtIndex index: Int) {
let shortsSource: String
if self.source != nil {
shortsSource = self.source!
self.source = nil
}
else {
shortsSource = source
}
private func logShortsAnalytics(source: Source, forRowAtIndex index: Int) {
var timeSpent: String
switch shortsViewTimeSpentSec {
case 0...2:
......@@ -205,7 +214,7 @@ class ShortsViewController: UIViewController {
AppAnalytics.shared.log(event: .ANALYTICS_SHORTS_CARD_BINGE_VIEW,
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])
viewScheduler.resetTimer()
shortsViewTimeSpentSec = 0
......@@ -214,6 +223,14 @@ class ShortsViewController: UIViewController {
func set(indexToScroll: Int) {
itemIndexToScroll = indexToScroll
}
@objc
private func didEnterBackgroundHandler() {
if let currentCardIndex = currentCardIndex?.row {
logShortsAnalytics(source: source, forRowAtIndex: currentCardIndex)
}
source = .others
}
}
//MARK:- Prepare
......@@ -278,6 +295,11 @@ extension ShortsViewController: UITableViewDelegate {
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>) {
//Get direction
let direction:ScrollDirection
......@@ -295,11 +317,7 @@ extension ShortsViewController: UITableViewDelegate {
targetContentOffset.pointee = scrollView.contentOffset
//Get visible rows IndexPaths
guard
let visiblePath = (tableView.indexPathsForVisibleRows?.sorted{$0.row < $1.row}),
visiblePath.count > 0,
let topRowIndexPath = visiblePath.first
else {
guard let currentCardIndex = currentCardIndex else {
return
}
......@@ -307,9 +325,9 @@ extension ShortsViewController: UITableViewDelegate {
let nextRowIndexPath:IndexPath
switch direction {
case .swipeUp:
let rowsCount = tableView.numberOfRows(inSection: topRowIndexPath.section)
let nextIndex = min(topRowIndexPath.row + 1, rowsCount - 1)
nextRowIndexPath = IndexPath(row: nextIndex, section: topRowIndexPath.section)
let rowsCount = tableView.numberOfRows(inSection: currentCardIndex.section)
let nextIndex = min(currentCardIndex.row + 1, rowsCount - 1)
nextRowIndexPath = IndexPath(row: nextIndex, section: currentCardIndex.section)
//Check for the last row
if nextRowIndexPath.row == rowsCount - 1 {
......@@ -317,26 +335,29 @@ extension ShortsViewController: UITableViewDelegate {
updateView(basedOnDirection: direction, willShowRowAt: nextRowIndexPath.row)
let offset = tableView.contentSize.height - tableView.frame.height
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
}
case .swipeDown:
if topRowIndexPath.row == 0 {
if currentCardIndex.row == 0 {
viewModel.markAsViewed(atIndex: 0)
updateView(basedOnDirection: direction, willShowRowAt: 0)
self.scrollTo(newOffset: .zero, velocity: velocity)
self.logShortsAnalytics(source: "swipe_down", forRowAtIndex: 0)
self.logShortsAnalytics(source: source, forRowAtIndex: 0)
source = .swipeDown
return
}
nextRowIndexPath = topRowIndexPath
nextRowIndexPath = currentCardIndex
}
let nextRowRect = tableView.rectForRow(at: nextRowIndexPath)
viewModel.markAsViewed(atIndex: nextRowIndexPath.row)
updateView(basedOnDirection: direction, willShowRowAt: nextRowIndexPath.row)
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) {
......
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