This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #12061 - Add support to display AddressPicker
- Loading branch information
Alexandru2909
committed
May 31, 2022
1 parent
588c2c4
commit 56289a4
Showing
14 changed files
with
875 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...eature/prompts/src/main/java/mozilla/components/feature/prompts/address/AddressAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* 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/. */ | ||
|
||
package mozilla.components.feature.prompts.address | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.annotation.VisibleForTesting | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import mozilla.components.concept.storage.Address | ||
import mozilla.components.feature.prompts.R | ||
|
||
@VisibleForTesting | ||
internal object AddressDiffCallback : DiffUtil.ItemCallback<Address>() { | ||
override fun areItemsTheSame(oldItem: Address, newItem: Address) = | ||
oldItem.guid == newItem.guid | ||
|
||
override fun areContentsTheSame(oldItem: Address, newItem: Address) = | ||
oldItem == newItem | ||
} | ||
|
||
/** | ||
* RecyclerView adapter for displaying address items. | ||
*/ | ||
internal class AddressAdapter( | ||
private val onAddressSelected: (Address) -> Unit | ||
) : ListAdapter<Address, AddressViewHolder>(AddressDiffCallback) { | ||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AddressViewHolder { | ||
val view = LayoutInflater | ||
.from(parent.context) | ||
.inflate(R.layout.mozac_feature_prompts_address_list_item, parent, false) | ||
return AddressViewHolder(view, onAddressSelected) | ||
} | ||
|
||
override fun onBindViewHolder(holder: AddressViewHolder, position: Int) { | ||
holder.bind(getItem(position)) | ||
} | ||
} | ||
|
||
/** | ||
* View holder for a address item. | ||
*/ | ||
@VisibleForTesting | ||
internal class AddressViewHolder( | ||
itemView: View, | ||
private val onAddressSelected: (Address) -> Unit | ||
) : RecyclerView.ViewHolder(itemView), View.OnClickListener { | ||
@VisibleForTesting | ||
lateinit var address: Address | ||
|
||
init { | ||
itemView.setOnClickListener(this) | ||
} | ||
|
||
fun bind(address: Address) { | ||
this.address = address | ||
itemView.findViewById<TextView>(R.id.address_name)?.text = address.displayFormat() | ||
} | ||
|
||
override fun onClick(v: View?) { | ||
onAddressSelected(address) | ||
} | ||
} | ||
|
||
/** | ||
* Format the address details to be displayed to the user. | ||
*/ | ||
fun Address.displayFormat(): String = | ||
"${this.streetAddress}, ${this.addressLevel2}, ${this.addressLevel1}, ${this.postalCode}" |
33 changes: 33 additions & 0 deletions
33
...ature/prompts/src/main/java/mozilla/components/feature/prompts/address/AddressDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* 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/. */ | ||
|
||
package mozilla.components.feature.prompts.address | ||
|
||
import mozilla.components.concept.storage.Address | ||
import mozilla.components.feature.prompts.concept.SelectablePromptView | ||
|
||
/** | ||
* Delegate for address picker | ||
*/ | ||
interface AddressDelegate { | ||
/** | ||
* The [SelectablePromptView] used for [AddressPicker] to display a | ||
* selectable prompt list of address options. | ||
*/ | ||
val addressPickerView: SelectablePromptView<Address>? | ||
|
||
/** | ||
* Callback invoked when the user clicks "Manage addresses" from | ||
* select address prompt. | ||
*/ | ||
val onManageAddresses: () -> Unit | ||
} | ||
|
||
/** | ||
* Default implementation for address picker delegate | ||
*/ | ||
class DefaultAddressDelegate( | ||
override val addressPickerView: SelectablePromptView<Address>? = null, | ||
override val onManageAddresses: () -> Unit = {} | ||
) : AddressDelegate |
85 changes: 85 additions & 0 deletions
85
...feature/prompts/src/main/java/mozilla/components/feature/prompts/address/AddressPicker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* 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/. */ | ||
|
||
package mozilla.components.feature.prompts.address | ||
|
||
import mozilla.components.browser.state.action.ContentAction | ||
import mozilla.components.browser.state.store.BrowserStore | ||
import mozilla.components.concept.engine.prompt.PromptRequest | ||
import mozilla.components.concept.storage.Address | ||
import mozilla.components.feature.prompts.concept.SelectablePromptView | ||
import mozilla.components.feature.prompts.consumePromptFrom | ||
import mozilla.components.support.base.log.logger.Logger | ||
|
||
/** | ||
* Interactor that implements [SelectablePromptView.Listener] and notifies the feature about actions | ||
* the user performed in the address picker. | ||
* | ||
* @property store The [BrowserStore] this feature should subscribe to. | ||
* @property addressSelectBar The [SelectablePromptView] view into which the select address | ||
* prompt will be inflated. | ||
* @property onManageAddresses Callback invoked when user clicks on "Manage adresses" button from | ||
* select address prompt. | ||
* @property sessionId The session ID which requested the prompt. | ||
*/ | ||
class AddressPicker( | ||
private val store: BrowserStore, | ||
private val addressSelectBar: SelectablePromptView<Address>, | ||
private val onManageAddresses: () -> Unit = {}, | ||
private var sessionId: String? = null | ||
) : SelectablePromptView.Listener<Address> { | ||
|
||
init { | ||
addressSelectBar.listener = this | ||
} | ||
|
||
/** | ||
* Shows the select address prompt in response to the [PromptRequest] event. | ||
* | ||
* @param request The [PromptRequest] containing the the address request data to be shown. | ||
*/ | ||
internal fun handleSelectAddressRequest(request: PromptRequest.SelectAddress) { | ||
addressSelectBar.showPrompt(request.addresses) | ||
} | ||
|
||
/** | ||
* Dismisses the active [PromptRequest.SelectAddress] request. | ||
* | ||
* @param promptRequest The current active [PromptRequest.SelectAddress] or null | ||
* otherwise. | ||
*/ | ||
@Suppress("TooGenericExceptionCaught") | ||
fun dismissSelectAddressRequest(promptRequest: PromptRequest.SelectAddress? = null) { | ||
addressSelectBar.hidePrompt() | ||
|
||
try { | ||
if (promptRequest != null) { | ||
promptRequest.onDismiss() | ||
sessionId?.let { | ||
store.dispatch(ContentAction.ConsumePromptRequestAction(it, promptRequest)) | ||
} | ||
return | ||
} | ||
|
||
store.consumePromptFrom<PromptRequest.SelectAddress>(sessionId) { | ||
it.onDismiss() | ||
} | ||
} catch (e: RuntimeException) { | ||
Logger.error("Can't dismiss select address prompt", e) | ||
} | ||
} | ||
|
||
override fun onOptionSelect(option: Address) { | ||
store.consumePromptFrom<PromptRequest.SelectAddress>(sessionId) { | ||
it.onConfirm(option) | ||
} | ||
|
||
addressSelectBar.hidePrompt() | ||
} | ||
|
||
override fun onManageOptions() { | ||
onManageAddresses.invoke() | ||
dismissSelectAddressRequest() | ||
} | ||
} |
Oops, something went wrong.