forked from mozilla-mobile/android-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For mozilla-mobile#12061 - Add support to display AddressPicker
- Loading branch information
Alexandru2909
committed
May 11, 2022
1 parent
1366f8e
commit d83bef3
Showing
8 changed files
with
466 additions
and
13 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}" |
83 changes: 83 additions & 0 deletions
83
...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,83 @@ | ||
/* 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 androidx.annotation.VisibleForTesting | ||
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 selectAddressCallback A callback invoked when a user selects an address option | ||
* from the 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 selectAddressCallback: () -> Unit = {}, | ||
private var sessionId: String? = null | ||
) : SelectablePromptView.Listener<Address> { | ||
|
||
init { | ||
addressSelectBar.listener = this | ||
} | ||
|
||
// The selected address option to confirm. | ||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) | ||
internal var selectedAddress: Address? = null | ||
|
||
override fun onManageOptions() { | ||
dismissSelectAddressRequest() | ||
} | ||
|
||
override fun onOptionSelect(option: Address) { | ||
selectedAddress = option | ||
addressSelectBar.hidePrompt() | ||
selectAddressCallback.invoke() | ||
} | ||
|
||
/** | ||
* 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() | ||
return | ||
} | ||
|
||
store.consumePromptFrom<PromptRequest.SelectAddress>(sessionId) { | ||
it.onDismiss() | ||
} | ||
} catch (e: RuntimeException) { | ||
Logger.error("Can't dismiss select address prompt", e) | ||
} | ||
} | ||
|
||
/** | ||
* 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) | ||
} | ||
} |
Oops, something went wrong.