-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from natura-cosmeticos/short-cut
[DSY-864] Shortcut
- Loading branch information
Showing
28 changed files
with
851 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,3 +120,4 @@ And: | |
- Menu | ||
- Navigation View | ||
- Text Field | ||
- [Shortcut](doc/shortcut.md) |
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
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
6 changes: 6 additions & 0 deletions
6
designsystem/src/main/kotlin/com/natura/android/exceptions/MissingThemeException.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,6 @@ | ||
package com.natura.android.exceptions | ||
|
||
import java.lang.IllegalArgumentException | ||
|
||
class MissingThemeException : | ||
IllegalArgumentException("⚠️ ⚠️ Missing DS Theme. You are using a DS component without setting a DS Theme. You MUST set a DS theme at the component or in a parent view") |
12 changes: 12 additions & 0 deletions
12
designsystem/src/main/kotlin/com/natura/android/extensions/TextFieldExtensions.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,12 @@ | ||
package com.natura.android.extensions | ||
|
||
import android.os.Build | ||
import android.widget.TextView | ||
|
||
fun TextView.setAppearance(textAppearance: Int) { | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { | ||
this.setTextAppearance(context, textAppearance) | ||
} else { | ||
this.setTextAppearance(textAppearance) | ||
} | ||
} |
179 changes: 179 additions & 0 deletions
179
designsystem/src/main/kotlin/com/natura/android/shortcut/Shortcut.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,179 @@ | ||
package com.natura.android.shortcut | ||
|
||
import android.content.Context | ||
import android.content.res.TypedArray | ||
import android.graphics.drawable.GradientDrawable | ||
import android.util.AttributeSet | ||
import android.view.View | ||
import android.widget.ImageView | ||
import android.widget.LinearLayout | ||
import android.widget.TextView | ||
import androidx.constraintlayout.widget.ConstraintLayout | ||
import androidx.core.content.ContextCompat | ||
import androidx.core.content.res.getIntOrThrow | ||
import androidx.core.content.res.getResourceIdOrThrow | ||
import androidx.core.content.res.getStringOrThrow | ||
import androidx.core.graphics.drawable.DrawableCompat | ||
import com.natura.android.R | ||
import com.natura.android.exceptions.MissingThemeException | ||
import com.natura.android.extensions.setAppearance | ||
|
||
class Shortcut @JvmOverloads constructor( | ||
context: Context, | ||
private val attrs: AttributeSet? = null, | ||
defStyleAttr: Int = 0 | ||
) : ConstraintLayout(context, attrs, defStyleAttr) { | ||
|
||
private var labelAttribute: String? = null | ||
private var typeAttribute: Int? = null | ||
private var backgroundColorResourceAttribute = 0 | ||
private var iconColorResourceAttribute = 0 | ||
private var labelTextAppearanceResourceAttribute = 0 | ||
private var iconAttribute: Int? = null | ||
private var shortcutAttributesArray: TypedArray | ||
|
||
private val labelContainer by lazy { findViewById<TextView>(R.id.shortCutLabel) } | ||
private val backgroundContainer by lazy { findViewById<LinearLayout>(R.id.shortcutBackground) } | ||
private val iconContainer by lazy { findViewById<ImageView>(R.id.shortCutIcon) } | ||
|
||
init { | ||
try { | ||
View.inflate(context, R.layout.shortcut, this) | ||
} catch (e: Exception) { | ||
throw (MissingThemeException()) | ||
} | ||
|
||
shortcutAttributesArray = context.obtainStyledAttributes(attrs, R.styleable.Shortcut) | ||
|
||
getShortcutAttributes() | ||
getAttributesFromTheme() | ||
configureShortCutByType(typeAttribute) | ||
|
||
shortcutAttributesArray.recycle() | ||
} | ||
|
||
fun setLabel(text: String?) { | ||
labelContainer.text = text | ||
labelContainer.setAppearance(labelTextAppearanceResourceAttribute) | ||
} | ||
|
||
fun getLabel(): CharSequence? { | ||
return labelContainer.text | ||
} | ||
|
||
fun getType(): Int? = typeAttribute | ||
|
||
fun setIcon(icon: Int?) { | ||
icon?.apply { | ||
iconContainer.setImageResource(icon) | ||
iconContainer.setColorFilter(ContextCompat.getColor(context, iconColorResourceAttribute), android.graphics.PorterDuff.Mode.SRC_IN) | ||
} | ||
} | ||
|
||
fun getIcon(): ImageView { | ||
return iconContainer | ||
} | ||
|
||
private fun getAttributesFromTheme() { | ||
try { | ||
if (typeAttribute == CONTAINED) { | ||
setContainedTypeAttributes() | ||
} else { | ||
setOutlinedTypeAttributes() | ||
} | ||
} catch (e: Exception) { | ||
throw (MissingThemeException()) | ||
} | ||
} | ||
|
||
private fun setContainedTypeAttributes() { | ||
context | ||
.theme | ||
.obtainStyledAttributes( | ||
attrs, | ||
R.styleable.Shortcut, | ||
R.attr.shortcutContained, | ||
0 | ||
) | ||
.apply { | ||
backgroundColorResourceAttribute = this.getResourceIdOrThrow(R.styleable.Shortcut_colorBackground) | ||
iconColorResourceAttribute = this.getResourceIdOrThrow(R.styleable.Shortcut_colorIcon) | ||
labelTextAppearanceResourceAttribute = this.getResourceIdOrThrow(R.styleable.Shortcut_labelAppearance) | ||
} | ||
} | ||
|
||
private fun setOutlinedTypeAttributes() { | ||
context | ||
.theme | ||
.obtainStyledAttributes(attrs, R.styleable.Shortcut, R.attr.shortcutOutlined, 0) | ||
.apply { | ||
backgroundColorResourceAttribute = this.getResourceIdOrThrow(R.styleable.Shortcut_colorBackground) | ||
iconColorResourceAttribute = this.getResourceIdOrThrow(R.styleable.Shortcut_colorIcon) | ||
labelTextAppearanceResourceAttribute = this.getResourceIdOrThrow(R.styleable.Shortcut_labelAppearance) | ||
} | ||
} | ||
|
||
private fun getShortcutAttributes() { | ||
getLabelAttribute() | ||
getIconAttribute() | ||
getTypeAttribute() | ||
} | ||
|
||
private fun getTypeAttribute() { | ||
try { | ||
typeAttribute = shortcutAttributesArray.getIntOrThrow(R.styleable.Shortcut_type) | ||
} catch (e: Exception) { | ||
throw (IllegalArgumentException("⚠️ ⚠️ Missing shortcut required argument. You MUST set the shortcut type(contained or outlined).", e)) | ||
} | ||
} | ||
|
||
private fun getIconAttribute() { | ||
try { | ||
iconAttribute = shortcutAttributesArray.getResourceIdOrThrow(R.styleable.Shortcut_icon) | ||
} catch (e: Exception) { | ||
throw (IllegalArgumentException("⚠️ ⚠️ Missing shortcut required argument. You MUST set the shortcut icon(drawable).", e)) | ||
} | ||
} | ||
|
||
private fun getLabelAttribute() { | ||
try { | ||
labelAttribute = shortcutAttributesArray.getStringOrThrow(R.styleable.Shortcut_textLabel) | ||
} catch (e: Exception) { | ||
throw (IllegalArgumentException("⚠️ ⚠️ Missing shortcut required argument. You MUST set the shortcut label(string).", e)) | ||
} | ||
} | ||
|
||
private fun configureShortCutByType(type: Int?) { | ||
type?.apply { | ||
setLabel(labelAttribute) | ||
setIcon(iconAttribute) | ||
|
||
when (this) { | ||
CONTAINED -> setBackgroundContained() | ||
OUTLINED -> setBackgroundOutlined() | ||
} | ||
} | ||
} | ||
|
||
private fun setBackgroundContained() { | ||
val background = resources.getDrawable(R.drawable.shortcut_background, null) | ||
val backgroundWrap = DrawableCompat.wrap(background).mutate() | ||
DrawableCompat.setTint(backgroundWrap, ContextCompat.getColor(context, backgroundColorResourceAttribute)) | ||
|
||
backgroundContainer.background = background | ||
} | ||
|
||
private fun setBackgroundOutlined() { | ||
val background = resources.getDrawable(R.drawable.shortcut_background, null) as GradientDrawable | ||
background.setColor(ContextCompat.getColor(context, backgroundColorResourceAttribute)) | ||
background.setStroke(1, ContextCompat.getColor(context, iconColorResourceAttribute)) | ||
|
||
backgroundContainer.background = background | ||
backgroundContainer.elevation = 0F | ||
} | ||
|
||
companion object { | ||
const val OUTLINED = 0 | ||
const val CONTAINED = 1 | ||
} | ||
} |
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,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:shape="oval"> | ||
<solid android:color="#FFFFFF"/> | ||
<stroke android:color="#333333" android:width="1dp"/> | ||
</shape> |
12 changes: 12 additions & 0 deletions
12
designsystem/src/main/res/drawable/shortcut_ripple_background.xml
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,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<ripple | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:color="?attr/colorHighlightOpacity05"> | ||
<item | ||
android:id="@android:id/mask" | ||
android:gravity="center"> | ||
<shape android:shape="oval"> | ||
<solid android:color="@android:color/white"/> | ||
</shape> | ||
</item> | ||
</ripple> |
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,61 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/shortcutMainContainer" | ||
android:layout_width="64dp" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="?spacingSmall" | ||
android:layout_marginEnd="?spacingSmall" | ||
tools:theme="@style/Theme.Natura"> | ||
|
||
<LinearLayout | ||
android:id="@+id/shortcutRippleBackground" | ||
android:layout_width="?sizeMediumX" | ||
android:layout_height="?sizeMediumX" | ||
android:background="@drawable/shortcut_ripple_background" | ||
android:orientation="horizontal" | ||
android:elevation="?elevation04" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="@id/shortcutBackground" /> | ||
|
||
<LinearLayout | ||
android:id="@+id/shortcutBackground" | ||
android:layout_width="?sizeMediumX" | ||
android:layout_height="?sizeMediumX" | ||
android:background="@drawable/shortcut_background" | ||
android:gravity="center" | ||
android:orientation="vertical" | ||
android:elevation="?elevation02" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.5" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:layout_constraintVertical_bias="0.0" | ||
tools:backgroundTint="?colorPrimary"> | ||
|
||
<ImageView | ||
android:id="@+id/shortCutIcon" | ||
android:layout_width="?sizeSemi" | ||
android:layout_height="?sizeSemi" | ||
tools:srcCompat="@drawable/ds_ic_filled_brand_naturarosacea" /> | ||
</LinearLayout> | ||
|
||
<TextView | ||
android:id="@+id/shortCutLabel" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="?spacingTiny" | ||
android:textAlignment="center" | ||
tools:text="Shortcut big label to check the behavior" | ||
android:textAppearance="?textAppearanceCaption" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintHorizontal_bias="0.5" | ||
app:layout_constraintTop_toBottomOf="@+id/shortcutBackground" | ||
app:layout_constraintVertical_bias="0.0" /> | ||
</androidx.constraintlayout.widget.ConstraintLayout> |
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
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
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
Oops, something went wrong.