diff --git a/app/src/main/java/org/mozilla/fenix/settings/DeleteBrowsingDataFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/DeleteBrowsingDataFragment.kt
index 04b427441e..58f6130ed1 100644
--- a/app/src/main/java/org/mozilla/fenix/settings/DeleteBrowsingDataFragment.kt
+++ b/app/src/main/java/org/mozilla/fenix/settings/DeleteBrowsingDataFragment.kt
@@ -10,12 +10,66 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
+import kotlinx.android.synthetic.main.fragment_delete_browsing_data.view.*
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.Job
+import kotlinx.coroutines.launch
+import mozilla.components.browser.session.Session
+import mozilla.components.browser.session.SessionManager
import org.mozilla.fenix.R
+import org.mozilla.fenix.ext.requireComponents
+import kotlin.coroutines.CoroutineContext
+class DeleteBrowsingDataFragment : Fragment(), CoroutineScope {
+ private lateinit var sessionObserver: SessionManager.Observer
-class DeleteBrowsingDataFragment : Fragment() {
- override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
- return inflater.inflate(R.layout.fragment_delete_browsing_data, container, false)
+ private lateinit var job: Job
+ override val coroutineContext: CoroutineContext
+ get() = Dispatchers.Main + job
+
+ override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
+ inflater.inflate(R.layout.fragment_delete_browsing_data, container, false)
+
+ override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+ super.onViewCreated(view, savedInstanceState)
+ job = Job()
+
+ sessionObserver = object : SessionManager.Observer {
+ override fun onSessionAdded(session: Session) {
+ super.onSessionAdded(session)
+ updateTabCount()
+ }
+
+ override fun onSessionRemoved(session: Session) {
+ super.onSessionRemoved(session)
+ updateTabCount()
+ }
+
+ override fun onSessionSelected(session: Session) {
+ super.onSessionSelected(session)
+ updateTabCount()
+ }
+
+ override fun onSessionsRestored() {
+ super.onSessionsRestored()
+ updateTabCount()
+ }
+
+ override fun onAllSessionsRemoved() {
+ super.onAllSessionsRemoved()
+ updateTabCount()
+ }
+ }
+
+ requireComponents.core.sessionManager.register(sessionObserver, owner = this)
+
+ view?.open_tabs_item?.onCheckListener = { _ -> updateDeleteButton() }
+ }
+
+ override fun onDestroyView() {
+ super.onDestroyView()
+ job.cancel()
}
override fun onResume() {
@@ -24,5 +78,54 @@ class DeleteBrowsingDataFragment : Fragment() {
title = getString(R.string.preferences_delete_browsing_data)
supportActionBar?.show()
}
+
+ updateTabCount()
+ updateHistoryCount()
+ updateCollectionsCount()
+ }
+
+ private fun updateDeleteButton() {
+
+ }
+
+ private fun updateTabCount() {
+ view?.open_tabs_item?.apply {
+ val openTabs = requireComponents.core.sessionManager.size
+ subtitleView.text = resources.getString(R.string.preferences_delete_browsing_data_tabs_subtitle, openTabs)
+ }
+ }
+
+ private fun updateHistoryCount() {
+ view?.browsing_data_item?.subtitleView?.text = ""
+
+ launch(Dispatchers.IO) {
+ val historyCount = requireComponents.core.historyStorage.getVisited().size
+ launch(Dispatchers.Main) {
+ view?.browsing_data_item?.apply {
+ subtitleView.text =
+ resources.getString(
+ R.string.preferences_delete_browsing_data_browsing_data_subtitle,
+ historyCount
+ )
+ }
+ }
+ }
+ }
+
+ private fun updateCollectionsCount() {
+ view?.browsing_data_item?.subtitleView?.text = ""
+
+ launch(Dispatchers.IO) {
+ val collectionsCount = requireComponents.core.tabCollectionStorage.getTabCollectionsCount()
+ launch(Dispatchers.Main) {
+ view?.collections_item?.apply {
+ subtitleView.text =
+ resources.getString(
+ R.string.preferences_delete_browsing_data_collections_subtitle,
+ collectionsCount
+ )
+ }
+ }
+ }
}
}
diff --git a/app/src/main/java/org/mozilla/fenix/settings/DeleteBrowsingDataItem.kt b/app/src/main/java/org/mozilla/fenix/settings/DeleteBrowsingDataItem.kt
index 3ddc2bf07d..5774c172d6 100644
--- a/app/src/main/java/org/mozilla/fenix/settings/DeleteBrowsingDataItem.kt
+++ b/app/src/main/java/org/mozilla/fenix/settings/DeleteBrowsingDataItem.kt
@@ -7,6 +7,7 @@ package org.mozilla.fenix.settings
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
+import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import kotlinx.android.synthetic.main.delete_browsing_data_item.view.*
import org.mozilla.fenix.R
@@ -16,6 +17,18 @@ class DeleteBrowsingDataItem @JvmOverloads constructor(
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
+
+ val titleView: TextView
+ get() = title
+
+ val subtitleView: TextView
+ get() = subtitle
+
+ val isChecked: Boolean
+ get() = checkbox.isChecked
+
+ var onCheckListener: ((Boolean) -> Unit)? = null
+
init {
LayoutInflater.from(context).inflate(R.layout.delete_browsing_data_item, this, true)
@@ -23,6 +36,10 @@ class DeleteBrowsingDataItem @JvmOverloads constructor(
checkbox.isChecked = !checkbox.isChecked
}
+ checkbox.setOnCheckedChangeListener { _, isChecked ->
+ onCheckListener?.invoke(isChecked)
+ }
+
attrs.let {
context.theme.obtainStyledAttributes(
it,
diff --git a/app/src/main/res/layout/fragment_delete_browsing_data.xml b/app/src/main/res/layout/fragment_delete_browsing_data.xml
index 3625492f40..7969281b8c 100644
--- a/app/src/main/res/layout/fragment_delete_browsing_data.xml
+++ b/app/src/main/res/layout/fragment_delete_browsing_data.xml
@@ -13,7 +13,7 @@
android:layout_height="wrap_content"
android:orientation="vertical">
Open Tabs
- %s tabs
+ %d tabs
Browsing history and site data
- %s addresses
+ %d addresses
Collections
- %s collections
+ %d collections