mirror of
https://github.com/bitwarden/android.git
synced 2026-04-27 03:18:30 -05:00
[PM-22831] Migrate IconData and BitwardenIcon to ui module (#5385)
This commit is contained in:
@@ -58,6 +58,7 @@ dependencies {
|
||||
implementation(libs.androidx.compose.ui.tooling.preview)
|
||||
implementation(libs.androidx.core.ktx)
|
||||
implementation(libs.androidx.navigation.compose)
|
||||
implementation(libs.bumptech.glide)
|
||||
implementation(libs.kotlinx.serialization)
|
||||
implementation(libs.kotlinx.coroutines.core)
|
||||
implementation(libs.kotlinx.collections.immutable)
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.bitwarden.ui.platform.components.icon
|
||||
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import com.bitwarden.ui.platform.base.util.nullableTestTag
|
||||
import com.bitwarden.ui.platform.components.icon.model.IconData
|
||||
import com.bitwarden.ui.platform.components.util.rememberVectorPainter
|
||||
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
|
||||
import com.bumptech.glide.integration.compose.GlideImage
|
||||
import com.bumptech.glide.integration.compose.placeholder
|
||||
|
||||
/**
|
||||
* Represents a Bitwarden icon that is either locally loaded or loaded using glide.
|
||||
*
|
||||
* @param iconData Label for the text field.
|
||||
* @param modifier A [Modifier] for the composable.
|
||||
* @param tint the color to be applied as the tint for the icon.
|
||||
*/
|
||||
@OptIn(ExperimentalGlideComposeApi::class)
|
||||
@Composable
|
||||
fun BitwardenIcon(
|
||||
iconData: IconData,
|
||||
modifier: Modifier = Modifier,
|
||||
tint: Color = Color.Unspecified,
|
||||
) {
|
||||
when (iconData) {
|
||||
is IconData.Network -> {
|
||||
GlideImage(
|
||||
model = iconData.uri,
|
||||
failure = placeholder(iconData.fallbackIconRes),
|
||||
contentDescription = iconData.contentDescription?.invoke(),
|
||||
modifier = modifier.nullableTestTag(tag = iconData.testTag),
|
||||
) {
|
||||
it.placeholder(iconData.fallbackIconRes)
|
||||
}
|
||||
}
|
||||
|
||||
is IconData.Local -> {
|
||||
Icon(
|
||||
painter = rememberVectorPainter(id = iconData.iconRes),
|
||||
contentDescription = iconData.contentDescription?.invoke(),
|
||||
tint = tint,
|
||||
modifier = modifier.nullableTestTag(tag = iconData.testTag),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.bitwarden.ui.platform.components.icon.model
|
||||
|
||||
import android.os.Parcelable
|
||||
import androidx.annotation.DrawableRes
|
||||
import com.bitwarden.ui.util.Text
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
/**
|
||||
* A class to denote the type of icon being passed.
|
||||
*/
|
||||
@Parcelize
|
||||
sealed class IconData : Parcelable {
|
||||
|
||||
/**
|
||||
* The icon content description.
|
||||
*/
|
||||
abstract val contentDescription: Text?
|
||||
|
||||
/**
|
||||
* The icon test tag.
|
||||
*/
|
||||
abstract val testTag: String?
|
||||
|
||||
/**
|
||||
* Data class representing the resources required for an icon.
|
||||
*
|
||||
* @property iconRes the resource for the local icon.
|
||||
*/
|
||||
@Parcelize
|
||||
data class Local(
|
||||
@DrawableRes val iconRes: Int,
|
||||
override val contentDescription: Text? = null,
|
||||
override val testTag: String? = null,
|
||||
) : IconData()
|
||||
|
||||
/**
|
||||
* Data class representing the resources required for a network-based icon.
|
||||
*
|
||||
* @property uri the link for the icon.
|
||||
* @property fallbackIconRes fallback resource if the image cannot be loaded.
|
||||
*/
|
||||
@Parcelize
|
||||
data class Network(
|
||||
val uri: String,
|
||||
@DrawableRes val fallbackIconRes: Int,
|
||||
override val contentDescription: Text? = null,
|
||||
override val testTag: String? = null,
|
||||
) : IconData()
|
||||
}
|
||||
Reference in New Issue
Block a user