Add support to copy a send url to the clipboard (#508)

This commit is contained in:
David Perez
2024-01-05 23:10:06 -06:00
committed by Álison Fernandes
parent 0a3377d98a
commit 4a39f126dd
9 changed files with 230 additions and 40 deletions

View File

@@ -4,6 +4,39 @@ import com.x8bit.bitwarden.data.auth.datasource.disk.model.EnvironmentUrlDataJso
import com.x8bit.bitwarden.data.platform.repository.model.Environment
import java.net.URI
private const val DEFAULT_WEB_VAULT_URL: String = "https://vault.bitwarden.com"
private const val DEFAULT_WEB_SEND_URL: String = "https://send.bitwarden.com/#"
/**
* Returns the base web vault URL. This will check for a custom [EnvironmentUrlDataJson.webVault]
* before falling back to the [EnvironmentUrlDataJson.base]. This can still return null if both are
* null or blank.
*/
val EnvironmentUrlDataJson.baseWebVaultUrlOrNull: String?
get() =
this
.webVault
.takeIf { !it.isNullOrBlank() }
?: base.takeIf { it.isNotBlank() }
/**
* Returns the base web vault URL or the default value if one is not present.
*
* See [baseWebVaultUrlOrNull] for more details.
*/
val EnvironmentUrlDataJson.baseWebVaultUrlOrDefault: String
get() = this.baseWebVaultUrlOrNull ?: DEFAULT_WEB_VAULT_URL
/**
* Returns the base web send URL or the default value if one is not present.
*/
val EnvironmentUrlDataJson.baseWebSendUrl: String
get() =
this
.baseWebVaultUrlOrNull
?.let { "$it/#/send/" }
?: DEFAULT_WEB_SEND_URL
/**
* Returns the appropriate pre-defined labels for environments matching the known US/EU values.
* Otherwise returns the host of the custom base URL.

View File

@@ -6,7 +6,9 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.platform.manager.clipboard.BitwardenClipboardManager
import com.x8bit.bitwarden.data.platform.repository.EnvironmentRepository
import com.x8bit.bitwarden.data.platform.repository.model.DataState
import com.x8bit.bitwarden.data.platform.repository.util.baseWebSendUrl
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
import com.x8bit.bitwarden.data.vault.repository.model.SendData
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
@@ -34,6 +36,7 @@ private const val KEY_STATE = "state"
class SendViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val clipboardManager: BitwardenClipboardManager,
private val environmentRepo: EnvironmentRepository,
private val vaultRepo: VaultRepository,
) : BaseViewModel<SendState, SendEvent, SendAction>(
// We load the state from the savedStateHandle for testing purposes.
@@ -84,7 +87,14 @@ class SendViewModel @Inject constructor(
is DataState.Loaded -> {
mutableStateFlow.update {
it.copy(viewState = dataState.data.toViewState())
it.copy(
viewState = dataState.data.toViewState(
baseWebSendUrl = environmentRepo
.environment
.environmentUrlData
.baseWebSendUrl,
),
)
}
}
@@ -108,7 +118,14 @@ class SendViewModel @Inject constructor(
is DataState.Pending -> {
mutableStateFlow.update {
it.copy(viewState = dataState.data.toViewState())
it.copy(
viewState = dataState.data.toViewState(
baseWebSendUrl = environmentRepo
.environment
.environmentUrlData
.baseWebSendUrl,
),
)
}
}
}
@@ -142,8 +159,7 @@ class SendViewModel @Inject constructor(
}
private fun handleCopyClick(action: SendAction.CopyClick) {
// TODO: Create a link and copy it to the clipboard BIT-??
sendEvent(SendEvent.ShowToast("Not yet implemented".asText()))
clipboardManager.setText(text = action.sendItem.shareUrl)
}
private fun handleSendClick(action: SendAction.SendClick) {
@@ -205,6 +221,7 @@ data class SendState(
val deletionDate: String,
val type: Type,
val iconList: List<SendStatusIcon>,
val shareUrl: String,
) : Parcelable {
/**
* Indicates the type of send this, a text or file.

View File

@@ -13,14 +13,18 @@ private const val DELETION_DATE_PATTERN: String = "MMM d, uuuu, hh:mm a"
/**
* Transforms [SendData] into [SendState.ViewState].
*/
fun SendData.toViewState(): SendState.ViewState =
fun SendData.toViewState(
baseWebSendUrl: String,
): SendState.ViewState =
this
.sendViewList
.takeUnless { it.isEmpty() }
?.toSendContent()
?.toSendContent(baseWebSendUrl)
?: SendState.ViewState.Empty
private fun List<SendView>.toSendContent(): SendState.ViewState.Content {
private fun List<SendView>.toSendContent(
baseWebSendUrl: String,
): SendState.ViewState.Content {
return SendState.ViewState.Content(
textTypeCount = this.count { it.type == SendType.TEXT },
fileTypeCount = this.count { it.type == SendType.FILE },
@@ -49,6 +53,7 @@ private fun List<SendView>.toSendContent(): SendState.ViewState.Content {
sendView.deletionDate.isBefore(Instant.now())
},
),
shareUrl = sendView.toSendUrl(baseWebSendUrl),
)
}
.sortedBy { it.name },

View File

@@ -0,0 +1,11 @@
package com.x8bit.bitwarden.ui.tools.feature.send.util
import com.bitwarden.core.SendView
/**
* Creates a sharable url from a [SendView].
*/
fun SendView.toSendUrl(
baseWebSendUrl: String,
// TODO: The `key` being used here is not correct and should be updated (BIT-1386)
): String = "$baseWebSendUrl$accessId/$key"