Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
For #24854: Add ability to delete an existing address.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarare committed May 5, 2022
1 parent 7ebceea commit d847ec2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ interface AddressEditorController {
*/
fun handleSaveAddress(addressFields: UpdatableAddressFields)

/**
* @see [AddressEditorInteractor.onDeleteAddress]
*/
fun handleDeleteAddress(guid: String)

/**
* @see [AddressEditorInteractor.onUpdateAddress]
*/
Expand Down Expand Up @@ -63,6 +68,15 @@ class DefaultAddressEditorController(
}
}

override fun handleDeleteAddress(guid: String) {
lifecycleScope.launch {
storage.deleteAddress(guid)
lifecycleScope.launch(Dispatchers.Main) {
navController.popBackStack()
}
}
}

override fun handleUpdateAddress(guid: String, addressFields: UpdatableAddressFields) {
lifecycleScope.launch {
storage.updateAddress(guid, addressFields)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package org.mozilla.fenix.settings.address.interactor

import mozilla.components.concept.storage.Address
import mozilla.components.concept.storage.UpdatableAddressFields
import org.mozilla.fenix.settings.address.controller.AddressEditorController

Expand All @@ -26,6 +27,14 @@ interface AddressEditorInteractor {
*/
fun onSaveAddress(addressFields: UpdatableAddressFields)

/**
* Deletes the provided address from the autofill storage. Called when a user
* taps on the save menu item or "Save" button.
*
* @param address An [Address] record to delete.
*/
fun onDeleteAddress(guid: String)

/**
* Updates the provided address in the autofill storage. Called when a user
* taps on the update menu item or "Update" button.
Expand Down Expand Up @@ -53,6 +62,10 @@ class DefaultAddressEditorInteractor(
controller.handleSaveAddress(addressFields)
}

override fun onDeleteAddress(guid: String) {
controller.handleDeleteAddress(guid)
}

override fun onUpdateAddress(guid: String, addressFields: UpdatableAddressFields) {
controller.handleUpdateAddress(guid, addressFields)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,17 @@ class AddressEditorView(
interactor.onSaveAddress(addressFields)
}
}

internal fun showConfirmDeleteAddressDialog(context: Context, guid: String) {
AlertDialog.Builder(context).apply {
setMessage(R.string.addressess_confirm_dialog_message)
setNegativeButton(R.string.addressess_confirm_dialog_cancel_button) { dialog: DialogInterface, _ ->
dialog.cancel()
}
setPositiveButton(R.string.addressess_confirm_dialog_ok_button) { _, _ ->
interactor.onDeleteAddress(guid)
}
create()
}.show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ package org.mozilla.fenix.settings.address

import android.view.LayoutInflater
import android.view.View
import io.mockk.every
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import mozilla.components.concept.storage.Address
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -26,12 +30,15 @@ class AddressEditorViewTest {
private lateinit var interactor: AddressEditorInteractor
private lateinit var addressEditorView: AddressEditorView
private lateinit var binding: FragmentAddressEditorBinding
private lateinit var address: Address

@Before
fun setup() {
view = LayoutInflater.from(testContext).inflate(R.layout.fragment_address_editor, null)
binding = FragmentAddressEditorBinding.bind(view)
interactor = mockk(relaxed = true)
address = mockk(relaxed = true)
every { address.guid } returns "123"

addressEditorView = spyk(AddressEditorView(binding, interactor))
}
Expand All @@ -44,4 +51,22 @@ class AddressEditorViewTest {

verify { interactor.onCancelButtonClicked() }
}

@Test
fun `GIVEN an existing address WHEN editor is opened THEN the delete address button is visible`() = runBlocking {
val addressEditorView = spyk(AddressEditorView(binding, interactor, address))
addressEditorView.bind()

assertEquals(View.VISIBLE, binding.deleteButton.visibility)
}

@Test
fun `GIVEN an existing address WHEN the delete address button is clicked THEN confirm delete dialog is shown`() = runBlocking {
val addressEditorView = spyk(AddressEditorView(binding, interactor, address))
addressEditorView.bind()

binding.deleteButton.performClick()

verify { addressEditorView.showConfirmDeleteAddressDialog(view.context, "123") }
}
}

0 comments on commit d847ec2

Please sign in to comment.