feat: clear all data dialog with local and network only options

This commit is contained in:
jubb
2021-06-22 17:01:27 +10:00
parent 05b0e5f308
commit 1df6fa46a4
6 changed files with 107 additions and 104 deletions

View File

@@ -31,10 +31,8 @@ import org.session.libsession.utilities.ProfilePictureUtilities
import org.session.libsession.utilities.SSKEnvironment.ProfileManagerProtocol
import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.ProfileKeyUtil
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
import org.thoughtcrime.securesms.avatar.AvatarSelection
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.loki.dialogs.ChangeUiModeDialog
import org.thoughtcrime.securesms.loki.dialogs.ClearAllDataDialog
import org.thoughtcrime.securesms.loki.dialogs.SeedDialog
@@ -92,7 +90,6 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
helpTranslateButton.setOnClickListener { helpTranslate() }
seedButton.setOnClickListener { showSeed() }
clearAllDataButton.setOnClickListener { clearAllData() }
clearAllDataAndNetworkButton.setOnClickListener { clearAllDataIncludingNetwork() }
versionTextView.text = String.format(getString(R.string.version_s), "${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})")
}
@@ -303,11 +300,7 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
}
private fun clearAllData() {
ClearAllDataDialog(deleteNetworkMessages = false).show(supportFragmentManager, "Clear All Data Dialog")
}
private fun clearAllDataIncludingNetwork() {
ClearAllDataDialog(deleteNetworkMessages = true).show(supportFragmentManager, "Clear All Data Dialog")
ClearAllDataDialog().show(supportFragmentManager, "Clear All Data Dialog")
}
// endregion

View File

@@ -23,18 +23,44 @@ import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.loki.protocol.MultiDeviceProtocol
import java.util.concurrent.Executors
class ClearAllDataDialog(val deleteNetworkMessages: Boolean) : DialogFragment() {
class ClearAllDataDialog : DialogFragment() {
enum class Steps {
INFO_PROMPT,
NETWORK_PROMPT,
DELETING
}
var clearJob: Job? = null
set(value) {
field = value
}
var step = Steps.INFO_PROMPT
set(value) {
field = value
updateUI()
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(requireContext())
val contentView = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_clear_all_data, null)
contentView.cancelButton.setOnClickListener { dismiss() }
contentView.clearAllDataButton.setOnClickListener { clearAllData() }
contentView.cancelButton.setOnClickListener {
if (step == Steps.NETWORK_PROMPT) {
clearAllData(false)
} else {
dismiss()
}
}
contentView.clearAllDataButton.setOnClickListener {
when(step) {
Steps.INFO_PROMPT -> step = Steps.NETWORK_PROMPT
Steps.NETWORK_PROMPT -> {
clearAllData(true)
}
Steps.DELETING -> { /* do nothing intentionally */ }
}
}
builder.setView(contentView)
builder.setCancelable(false)
val result = builder.create()
@@ -42,71 +68,70 @@ class ClearAllDataDialog(val deleteNetworkMessages: Boolean) : DialogFragment()
return result
}
override fun onDismiss(dialog: DialogInterface) {
super.onDismiss(dialog)
}
private fun updateUI() {
override fun onStart() {
super.onStart()
isCancelable = false
dialog?.setCanceledOnTouchOutside(false)
}
private fun updateUI(isLoading: Boolean) {
dialog?.let { view ->
val isLoading = step == Steps.DELETING
when (step) {
Steps.INFO_PROMPT -> {
view.dialogDescriptionText.setText(R.string.dialog_clear_all_data_explanation)
view.cancelButton.setText(R.string.cancel)
}
else -> {
view.dialogDescriptionText.setText(R.string.dialog_clear_all_data_network_explanation)
view.cancelButton.setText(R.string.dialog_clear_all_data_local_only)
}
}
view.cancelButton.isVisible = !isLoading
view.clearAllDataButton.isVisible = !isLoading
view.progressBar.isVisible = isLoading
view.setCanceledOnTouchOutside(!isLoading)
isCancelable = !isLoading
}
}
private fun clearAllData() {
private fun clearAllData(deleteNetworkMessages: Boolean) {
if (KeyPairUtilities.hasV2KeyPair(requireContext())) {
clearJob = lifecycleScope.launch(Dispatchers.IO) {
val previousStep = step
withContext(Dispatchers.Main) {
updateUI(true)
step = Steps.DELETING
}
if (!deleteNetworkMessages) {
try {
MultiDeviceProtocol.forceSyncConfigurationNowIfNeeded(requireContext()).get()
ApplicationContext.getInstance(context).clearAllData(false)
withContext(Dispatchers.Main) {
dismiss()
}
} catch (e: Exception) {
Log.e("Loki", "Failed to force sync", e)
withContext(Dispatchers.Main) {
updateUI(false)
}
}
ApplicationContext.getInstance(context).clearAllData(false)
withContext(Dispatchers.Main) {
dismiss()
}
} else {
// finish
val promises = try {
val result = try {
SnodeAPI.deleteAllMessages(requireContext()).get()
} catch (e: Exception) {
null
}
val rawResponses = promises?.map {
try {
it.get()
} catch (e: Exception) {
null
if (result == null || result.values.any { !it } || result.isEmpty()) {
// didn't succeed (at least one)
withContext(Dispatchers.Main) {
step = previousStep
}
} ?: listOf(null)
// TODO: process the responses here
if (rawResponses.all { it != null }) {
} else if (result.values.all { it }) {
// don't force sync because all the messages are deleted?
ApplicationContext.getInstance(context).clearAllData(false)
withContext(Dispatchers.Main) {
dismiss()
}
} else if (rawResponses.any { it == null || it["failed"] as? Boolean == true }) {
// didn't succeed (at least one)
withContext(Dispatchers.Main) {
updateUI(false)
}
}
}
}

View File

@@ -185,16 +185,6 @@
android:textStyle="bold"
android:gravity="center"
android:text="@string/activity_settings_clear_all_data_button_title" />
<TextView
android:id="@+id/clearAllDataAndNetworkButton"
android:layout_width="match_parent"
android:layout_height="@dimen/setting_button_height"
android:background="@drawable/setting_button_background"
android:textColor="@color/destructive"
android:textSize="@dimen/medium_font_size"
android:textStyle="bold"
android:gravity="center"
android:text="@string/activity_settings_clear_all_data_and_network_button_title" />
<View
android:layout_width="match_parent"

View File

@@ -21,7 +21,7 @@
android:textSize="@dimen/medium_font_size" />
<TextView
android:id="@+id/seedTextView"
android:id="@+id/dialogDescriptionText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/large_spacing"
@@ -37,7 +37,6 @@
android:orientation="horizontal">
<Button
tools:visibility="gone"
style="@style/Widget.Session.Button.Dialog.Unimportant"
android:id="@+id/cancelButton"
android:layout_width="0dp"
@@ -46,7 +45,6 @@
android:text="@string/cancel" />
<Button
tools:visibility="gone"
style="@style/Widget.Session.Button.Dialog.Destructive"
android:id="@+id/clearAllDataButton"
android:layout_width="0dp"

View File

@@ -778,6 +778,8 @@
<string name="dialog_clear_all_data_title">Clear All Data</string>
<string name="dialog_clear_all_data_explanation">This will permanently delete your messages, sessions, and contacts.</string>
<string name="dialog_clear_all_data_network_explanation">Do you also want to clear all your data from the network?</string>
<string name="dialog_clear_all_data_local_only">Delete Local Only</string>
<string name="activity_qr_code_title">QR Code</string>
<string name="activity_qr_code_view_my_qr_code_tab_title">View My QR Code</string>