Skip to content

Commit

Permalink
Merge pull request #158 from natura-cosmeticos/DSY-1485-menu-with-tag
Browse files Browse the repository at this point in the history
[DSY-1485] add tag to menu
  • Loading branch information
mlcsouza authored Sep 21, 2020
2 parents 06d14bd + 2311af2 commit 989035f
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 11 deletions.
18 changes: 18 additions & 0 deletions designsystem/src/main/kotlin/com/natura/android/menu/MenuView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.natura.android.R
import com.natura.android.ext.setVisibilityFromBoolean
import com.natura.android.tag.Tag

@SuppressLint("CustomViewStyleable")
class MenuView @JvmOverloads constructor(
Expand All @@ -24,6 +25,7 @@ class MenuView @JvmOverloads constructor(
private val labelContainer by lazy { findViewById<View>(R.id.ds_menu_view_background) }
private val iconMenu by lazy { findViewById<AppCompatImageView>(R.id.ds_menu_icon) }
private val iconArrowMenu by lazy { findViewById<AppCompatImageView>(R.id.ds_menu_arrow) }
private val menuTag by lazy { findViewById<Tag>(R.id.ds_menu_tag) }

private var selectedDrawable: Int
private var openedDrawable: Int
Expand All @@ -40,6 +42,12 @@ class MenuView @JvmOverloads constructor(
setMenuIconImage(value)
}

var tagLabel: String? = ""
set(value) {
field = value
menuTag.setLabel(value)
}

init {
View.inflate(context, R.layout.ds_menu_view, this)

Expand Down Expand Up @@ -69,6 +77,12 @@ class MenuView @JvmOverloads constructor(
val enabled = typedArray.getBoolean(R.styleable.ds_menu_menu_is_enabled, true)
val isOpened = typedArray.getBoolean(R.styleable.ds_menu_menu_is_opened, false)

val hasTag = typedArray.getBoolean(R.styleable.ds_menu_menu_has_tag, false)
val tagText = typedArray.getString(R.styleable.ds_menu_menu_tag_label)

showTag(hasTag)
tagLabel = tagText

typedArray.recycle()

configLabel(labelText, labelColor, labelSize)
Expand All @@ -83,6 +97,10 @@ class MenuView @JvmOverloads constructor(
isEnabled = enabled
}

fun showTag(hasTag: Boolean) {
menuTag.setVisibilityFromBoolean(hasTag, View.GONE)
}

override fun setSelected(isSelected: Boolean) {
super.setSelected(isSelected)
changeBackground(isSelected, selectedDrawable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class ExpandableNavigationAdapter(
convertView: View?,
parent: ViewGroup?
): View {
val parentView = convertView
?: LayoutInflater.from(context).inflate(R.layout.ds_menu_item, null)
val parentView = LayoutInflater.from(context).inflate(R.layout.ds_menu_item, null)

val groupView = parentView.findViewById<MenuView>(R.id.menu_item)
navigationItems[groupPosition].let { item ->
Expand All @@ -69,6 +68,8 @@ class ExpandableNavigationAdapter(
icon = item.iconText
showArrow(item.hasSubMenu)
configStateMenu(item.menuState)
showTag(item.showTag)
tagLabel = item.tagLabel
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ data class NavigationItem(
var hasSubMenu: Boolean = true,
var tagAnalytics: String = "",
var menuState: MenuView.MenuState = MenuView.MenuState.NONE,
val childItems: MutableList<NavigationItemChild> = mutableListOf()
val childItems: MutableList<NavigationItemChild> = mutableListOf(),
val tagLabel: String = "",
val showTag: Boolean = false
) : Navigation(id) {
fun indexOfChildItemId(childId: String) = childItems.indexOfFirst { it.id == childId }
}
Expand Down
29 changes: 21 additions & 8 deletions designsystem/src/main/res/layout/ds_menu_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@
android:id="@+id/ds_menu_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/ds_spacing_standard"
android:layout_marginRight="@dimen/ds_spacing_standard"
app:srcCompat="@drawable/outlined_default_mockup"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@id/guide_start"
app:layout_constraintEnd_toStartOf="@+id/ds_menu_label"
app:layout_constraintTop_toTopOf="parent"
app:tint="?colorOnSurface" />

Expand All @@ -59,20 +56,36 @@

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/ds_menu_label"
android:layout_width="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/ds_submenu_text_margin_top_bottom"
android:layout_marginEnd="@dimen/ds_spacin_tiny"
android:layout_marginRight="@dimen/ds_spacin_tiny"
android:layout_marginEnd="?spacingTiny"
android:layout_marginStart="?spacingTiny"
android:layout_marginBottom="@dimen/ds_submenu_text_margin_top_bottom"
app:layout_constrainedWidth="true"
android:gravity="center_vertical"
android:textColor="@color/colorBrdNatGray"
android:textSize="@dimen/ds_text_small"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintBottom_toBottomOf="@+id/ds_menu_icon"
app:layout_constraintEnd_toStartOf="@+id/ds_menu_arrow"
app:layout_constraintEnd_toStartOf="@+id/ds_menu_tag"
app:layout_constraintStart_toEndOf="@+id/ds_menu_icon"
app:layout_constraintTop_toTopOf="@+id/ds_menu_icon"
tools:text="Sub Menu item" />
app:layout_constraintHorizontal_chainStyle="packed"
tools:text="Sub Menu item with a big name to test what happens" />

<com.natura.android.tag.Tag
android:id="@+id/ds_menu_tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="?spacingTiny"
app:layout_constraintBottom_toBottomOf="@+id/ds_menu_icon"
app:layout_constraintEnd_toStartOf="@+id/ds_menu_arrow"
app:layout_constraintStart_toEndOf="@+id/ds_menu_label"
app:layout_constraintTop_toTopOf="@+id/ds_menu_icon"
app:tag_type="alert"
app:textLabel="NOVO"
tools:visibility="visible"/>

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/ds_menu_arrow"
Expand Down
2 changes: 2 additions & 0 deletions designsystem/src/main/res/values/ds_attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
<attr name="menu_is_selected" format="boolean" />
<attr name="menu_is_enabled" format="boolean" />
<attr name="menu_is_opened" format="boolean" />
<attr name="menu_has_tag" format="boolean" />
<attr name="menu_tag_label" format="string"/>
</declare-styleable>

<declare-styleable name="ds_submenu">
Expand Down
13 changes: 13 additions & 0 deletions doc/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,17 @@ You should use it to compose a catalog of options in applications
### How to use it?
WIP

Following there's an example of a menu with a tag code:

```android
<com.natura.android.menu.MenuView
android:id="@+id/menuTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:menu_label="Sample menu item"
app:menu_icon="outlined-action-add"
app:menu_label_color="?colorSurface"
app:menu_label_size="@dimen/ds_text_small"
app:menu_has_tag="true"
app:menu_tag_label="Sample tag" />
```
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class MenuActivityTest {
checkView(view)
}

@Test
fun verifyMenuWhenHasTag() {
val view: View = inflater.inflate(R.layout.component_menu_alert_tag, null, false)
checkView(view)
}

@UiThreadTest
@Test
fun verifyMenuWhenIsOpened() {
Expand Down
2 changes: 2 additions & 0 deletions sample/src/main/res/layout/activity_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@

<include layout="@layout/component_menu_opened" />

<include layout="@layout/component_menu_alert_tag" />

</LinearLayout>
18 changes: 18 additions & 0 deletions sample/src/main/res/layout/component_menu_alert_tag.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.natura.android.menu.MenuView
android:layout_width="match_parent"
android:layout_height="48dp"
app:menu_label="Menu item with alert"
app:menu_icon="outlined-action-add"
app:menu_label_color="?colorSurface"
app:menu_label_size="@dimen/ds_text_small"
app:menu_has_tag="true"
app:menu_tag_label="Novo"/>

</androidx.appcompat.widget.LinearLayoutCompat>

0 comments on commit 989035f

Please sign in to comment.