mirror of
https://github.com/fork-maintainers/iceraven-browser
synced 2024-11-19 09:25:34 +00:00
[fenix] For https://github.com/mozilla-mobile/fenix/issues/225 - Deletes data on button press
This commit is contained in:
parent
c412afec23
commit
1fd2b24bc9
@ -4,20 +4,28 @@
|
||||
|
||||
package org.mozilla.fenix.settings
|
||||
|
||||
import android.content.DialogInterface
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.ContextThemeWrapper
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.fragment.app.Fragment
|
||||
import kotlinx.android.synthetic.main.fragment_delete_browsing_data.*
|
||||
import kotlinx.android.synthetic.main.fragment_delete_browsing_data.view.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.launch
|
||||
import mozilla.components.browser.session.Session
|
||||
import mozilla.components.browser.session.SessionManager
|
||||
import mozilla.components.concept.engine.Engine
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.ext.requireComponents
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
@ -66,6 +74,12 @@ class DeleteBrowsingDataFragment : Fragment(), CoroutineScope {
|
||||
requireComponents.core.sessionManager.register(sessionObserver, owner = this)
|
||||
|
||||
view?.open_tabs_item?.onCheckListener = { _ -> updateDeleteButton() }
|
||||
view?.browsing_data_item?.onCheckListener = { _ -> updateDeleteButton() }
|
||||
view?.collections_item?.onCheckListener = { _ -> updateDeleteButton() }
|
||||
|
||||
view?.delete_data?.setOnClickListener {
|
||||
askToDelete()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
@ -86,13 +100,76 @@ class DeleteBrowsingDataFragment : Fragment(), CoroutineScope {
|
||||
updateDeleteButton()
|
||||
}
|
||||
|
||||
private fun updateDeleteButton() {
|
||||
view?.delete_data?.isEnabled =
|
||||
view!!.open_tabs_item!!.isChecked
|
||||
|| view!!.browsing_data_item!!.isChecked
|
||||
|| view!!.collections_item!!.isChecked
|
||||
private fun askToDelete() {
|
||||
AlertDialog.Builder(
|
||||
ContextThemeWrapper(
|
||||
activity,
|
||||
R.style.DialogStyle
|
||||
)
|
||||
).apply {
|
||||
val appName = context.getString(R.string.app_name)
|
||||
val message = context.getString(R.string.preferences_delete_browsing_data_prompt_message, appName)
|
||||
setMessage(message)
|
||||
|
||||
Log.e("wat", view?.delete_data?.isEnabled.toString())
|
||||
setNegativeButton(R.string.preferences_delete_browsing_data_prompt_cancel) { dialog: DialogInterface, _ ->
|
||||
dialog.cancel()
|
||||
}
|
||||
|
||||
setPositiveButton(R.string.preferences_delete_browsing_data_prompt_allow) { dialog: DialogInterface, _ ->
|
||||
dialog.dismiss()
|
||||
deleteSelected()
|
||||
}
|
||||
create()
|
||||
}.show()
|
||||
}
|
||||
|
||||
private fun deleteSelected() {
|
||||
val open_tabs = view!!.open_tabs_item!!.isChecked
|
||||
val browsing_data = view!!.browsing_data_item!!.isChecked
|
||||
val collections = view!!.collections_item!!.isChecked
|
||||
|
||||
startDeletion()
|
||||
launch(Dispatchers.IO) {
|
||||
var jobs = mutableListOf<Deferred<Unit>>()
|
||||
if (open_tabs) jobs.add(deleteTabsAsync())
|
||||
if (browsing_data) jobs.add(deleteBrowsingDataAsync())
|
||||
if (collections) jobs.add(deleteCollectionsAsync())
|
||||
|
||||
jobs.awaitAll()
|
||||
|
||||
launch(Dispatchers.Main) {
|
||||
finishDeletion()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun startDeletion() {
|
||||
progress_bar.visibility = View.VISIBLE
|
||||
delete_browsing_data_wrapper.isEnabled = false
|
||||
delete_browsing_data_wrapper.isClickable = false
|
||||
delete_browsing_data_wrapper.alpha = 0.6f
|
||||
}
|
||||
|
||||
fun finishDeletion() {
|
||||
progress_bar.visibility = View.GONE
|
||||
delete_browsing_data_wrapper.isEnabled = true
|
||||
delete_browsing_data_wrapper.isClickable = true
|
||||
delete_browsing_data_wrapper.alpha = 1f
|
||||
|
||||
updateTabCount()
|
||||
updateHistoryCount()
|
||||
updateCollectionsCount()
|
||||
}
|
||||
|
||||
private fun updateDeleteButton() {
|
||||
val open_tabs = view!!.open_tabs_item!!.isChecked
|
||||
val browsing_data = view!!.browsing_data_item!!.isChecked
|
||||
val collections = view!!.collections_item!!.isChecked
|
||||
val enabled = open_tabs || browsing_data || collections
|
||||
|
||||
view?.delete_data?.isEnabled = enabled
|
||||
view?.delete_data?.alpha = if (enabled) 1f else 0.6f
|
||||
}
|
||||
|
||||
private fun updateTabCount() {
|
||||
@ -135,4 +212,18 @@ class DeleteBrowsingDataFragment : Fragment(), CoroutineScope {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun deleteTabsAsync() = async(Dispatchers.IO) { requireComponents.core.sessionManager.removeSessions() }
|
||||
|
||||
private fun deleteBrowsingDataAsync() = async(Dispatchers.IO) {
|
||||
requireComponents.core.engine.clearData(Engine.BrowsingData.all())
|
||||
requireComponents.core.historyStorage.deleteEverything()
|
||||
}
|
||||
|
||||
private fun deleteCollectionsAsync() = async(Dispatchers.IO) {
|
||||
val count = requireComponents.core.tabCollectionStorage.getTabCollectionsCount()
|
||||
val data = requireComponents.core.tabCollectionStorage.getCollections(count).value?.forEach {
|
||||
requireComponents.core.tabCollectionStorage.removeCollection(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,8 @@
|
||||
app:titleMarginEnd="16dp"
|
||||
app:titleTextAppearance="@style/ToolbarTitleTextStyle"
|
||||
android:background="?foundation"
|
||||
android:elevation="8dp"/>
|
||||
android:elevation="8dp">
|
||||
</androidx.appcompat.widget.Toolbar>
|
||||
|
||||
<fragment
|
||||
android:id="@+id/container"
|
||||
|
@ -2,12 +2,29 @@
|
||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||
<ScrollView
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_width="match_parent">
|
||||
<ProgressBar
|
||||
android:id="@+id/progress_bar"
|
||||
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
|
||||
android:indeterminate="true"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="8dp"
|
||||
android:translationY="-3dp"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
<ScrollView
|
||||
android:id="@+id/delete_browsing_data_wrapper"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent">
|
||||
|
||||
android:layout_height="fill_parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintBottom_toBottomOf="parent">
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
@ -59,4 +76,5 @@
|
||||
android:textSize="16sp"
|
||||
android:textStyle="bold" />
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
</ScrollView>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
@ -3,7 +3,7 @@
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/nav_graph"
|
||||
app:startDestination="@id/deleteBrowsingDataFragment">
|
||||
app:startDestination="@id/homeFragment">
|
||||
|
||||
<action
|
||||
android:id="@+id/action_global_browser"
|
||||
|
@ -581,4 +581,11 @@
|
||||
<string name="preferences_delete_browsing_data_collections_subtitle">%d collections</string>
|
||||
<!-- Text for the button to delete browsing data -->
|
||||
<string name="preferences_delete_browsing_data_button">Delete browsing data</string>
|
||||
|
||||
<!-- Dialog message to the user asking to delete browsing data. Parameter is the name of the app (e.g. Fenix) -->
|
||||
<string name="preferences_delete_browsing_data_prompt_message">Allow %s to delete your browsing data?</string>
|
||||
<!-- Text for the cancel button for the data deletion dialog -->
|
||||
<string name="preferences_delete_browsing_data_prompt_cancel">Don’t allow</string>
|
||||
<!-- Text for the allow button for the data deletion dialog -->
|
||||
<string name="preferences_delete_browsing_data_prompt_allow">Allow</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user