Skip to content

Commit

Permalink
Revert "Updated AppTrackingProtectionStateView to receive events and …
Browse files Browse the repository at this point in the history
…update only when it is visible."

This reverts commit e667ac5.
  • Loading branch information
anikiki committed Jan 28, 2025
1 parent 82f5c6d commit d915f13
Showing 1 changed file with 5 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ package com.duckduckgo.mobile.android.vpn.ui.newtab
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewTreeObserver
import android.widget.FrameLayout
import androidx.core.text.HtmlCompat
import androidx.core.view.isVisible
import androidx.lifecycle.*
import com.duckduckgo.anvil.annotations.ContributesActivePlugin
import com.duckduckgo.anvil.annotations.InjectWith
Expand All @@ -49,6 +47,7 @@ import com.duckduckgo.newtabpage.api.NewTabPageSection
import com.duckduckgo.newtabpage.api.NewTabPageSectionPlugin
import dagger.android.support.AndroidSupportInjection
import javax.inject.Inject
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

Expand Down Expand Up @@ -80,47 +79,24 @@ class AppTrackingProtectionStateView @JvmOverloads constructor(

private val binding: FragmentDeviceShieldCtaBinding by viewBinding()

private var isViewVisible = false

private val preDrawListener = ViewTreeObserver.OnPreDrawListener {
val isCurrentlyVisible = isVisible && isShown
if (isCurrentlyVisible != isViewVisible) {
isViewVisible = isCurrentlyVisible
if (isViewVisible) {
startCollecting()
} else {
stopCollecting()
}
}
true
}

override fun onAttachedToWindow() {
AndroidSupportInjection.inject(this)
super.onAttachedToWindow()

viewTreeObserver.addOnPreDrawListener(preDrawListener)
conflatedJob += viewModel.viewStateFlow
.onEach { viewState -> renderViewState(viewState) }
.launchIn(findViewTreeLifecycleOwner()?.lifecycleScope!!)

deviceShieldPixels.didShowNewTabSummary()

configureViewReferences()
}

override fun onDetachedFromWindow() {
viewTreeObserver.removeOnPreDrawListener(preDrawListener)
conflatedJob.cancel()
super.onDetachedFromWindow()
}

private fun startCollecting() {
conflatedJob += viewModel.viewStateFlow
.onEach { viewState -> renderViewState(viewState) }
.launchIn(findViewTreeLifecycleOwner()?.lifecycleScope!!)
}

private fun stopCollecting() {
conflatedJob.cancel()
}

private fun configureViewReferences() {
binding.deviceShieldCtaLayout.setOnClickListener {
deviceShieldPixels.didPressNewTabSummary()
Expand Down

0 comments on commit d915f13

Please sign in to comment.