Skip to content

Commit 7af03ae

Browse files
committed
Fix list menu item content description and icon not update issue
1 parent 460125b commit 7af03ae

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/toolbar/AztecToolbar.kt

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import android.widget.PopupMenu
2222
import android.widget.PopupMenu.OnMenuItemClickListener
2323
import android.widget.Toast
2424
import android.widget.ToggleButton
25+
import org.wordpress.android.util.AppLog
2526
import org.wordpress.aztec.AztecText
2627
import org.wordpress.aztec.AztecTextFormat
2728
import org.wordpress.aztec.ITextFormat
@@ -626,10 +627,8 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
626627
val headingButton = findViewById<ToggleButton>(R.id.format_bar_button_heading)
627628
// Use unnumbered heading selector by default.
628629
updateHeadingMenuItem(AztecTextFormat.FORMAT_PARAGRAPH, headingButton)
629-
if (textFormats.size == 0) {
630-
// Select paragraph by default.
631-
headingMenu?.menu?.findItem(R.id.paragraph)?.isChecked = true
632-
} else {
630+
headingMenu?.menu?.findItem(R.id.paragraph)?.isChecked = true
631+
if (textFormats.size != 0) {
633632
textFormats.forEach {
634633
when (it) {
635634
AztecTextFormat.FORMAT_HEADING_1 -> headingMenu?.menu?.findItem(R.id.heading_1)?.isChecked = true
@@ -640,10 +639,6 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
640639
AztecTextFormat.FORMAT_HEADING_6 -> headingMenu?.menu?.findItem(R.id.heading_6)?.isChecked = true
641640
// TODO: Uncomment when Preformat is to be added back as a feature
642641
// AztecTextFormat.FORMAT_PREFORMAT -> headingMenu?.menu?.findItem(R.id.preformat)?.isChecked = true
643-
else -> {
644-
// Select paragraph by default.
645-
headingMenu?.menu?.findItem(R.id.paragraph)?.isChecked = true
646-
}
647642
}
648643

649644
updateHeadingMenuItem(it, headingButton)
@@ -652,25 +647,16 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
652647
}
653648

654649
private fun selectListMenuItem(textFormats: ArrayList<ITextFormat>) {
655-
if (textFormats.size == 0) {
656-
// Select no list by default.
657-
listMenu?.menu?.findItem(R.id.list_none)?.isChecked = true
658-
// Use unordered list selector by default.
659-
setListMenuSelector(AztecTextFormat.FORMAT_UNORDERED_LIST)
660-
} else {
650+
val listButton = findViewById<ToggleButton>(R.id.format_bar_button_list)
651+
updateListMenuItem(AztecTextFormat.FORMAT_NONE, listButton)
652+
listMenu?.menu?.findItem(R.id.list_none)?.isChecked = true
653+
if (textFormats.size != 0) {
661654
textFormats.forEach {
662655
when (it) {
663656
AztecTextFormat.FORMAT_UNORDERED_LIST -> listMenu?.menu?.findItem(R.id.list_unordered)?.isChecked = true
664657
AztecTextFormat.FORMAT_ORDERED_LIST -> listMenu?.menu?.findItem(R.id.list_ordered)?.isChecked = true
665-
else -> {
666-
// Select no list by default.
667-
listMenu?.menu?.findItem(R.id.list_none)?.isChecked = true
668-
}
669658
}
670-
671-
setListMenuSelector(it)
672-
673-
return
659+
updateListMenuItem(it, listButton)
674660
}
675661
}
676662
}
@@ -869,15 +855,31 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
869855
listMenu?.inflate(R.menu.list)
870856
}
871857

872-
private fun setListMenuSelector(textFormat: ITextFormat) {
858+
private fun updateListMenuItem(textFormat: ITextFormat, listButton: ToggleButton) {
859+
var backgroundRes = R.drawable.format_bar_button_ul_selector
860+
var contentDescriptionRes = R.string.format_bar_description_list
861+
var check = true
873862
when (textFormat) {
874-
AztecTextFormat.FORMAT_UNORDERED_LIST -> findViewById<ToggleButton>(R.id.format_bar_button_list).setBackgroundResource(R.drawable.format_bar_button_ul_selector)
875-
AztecTextFormat.FORMAT_ORDERED_LIST -> findViewById<ToggleButton>(R.id.format_bar_button_list).setBackgroundResource(R.drawable.format_bar_button_ol_selector)
863+
AztecTextFormat.FORMAT_ORDERED_LIST -> {
864+
backgroundRes = R.drawable.format_bar_button_ol_selector
865+
contentDescriptionRes = R.string.item_format_list_ordered
866+
}
867+
AztecTextFormat.FORMAT_UNORDERED_LIST -> {
868+
contentDescriptionRes = R.string.item_format_list_unordered
869+
// keep default background
870+
}
871+
AztecTextFormat.FORMAT_NONE -> {
872+
check = false
873+
// keep default background and content description
874+
}
876875
else -> {
877-
// Use unordered list selector by default.
878-
findViewById<ToggleButton>(R.id.format_bar_button_list).setBackgroundResource(R.drawable.format_bar_button_ul_selector)
876+
// ignore for unknown formats
877+
return
879878
}
880879
}
880+
listButton.setBackgroundResource(backgroundRes)
881+
listButton.contentDescription = context.getString(contentDescriptionRes)
882+
listButton.isChecked = check
881883
}
882884

883885
private fun updateHeadingMenuItem(textFormat: ITextFormat, headingButton: ToggleButton) {
@@ -950,19 +952,22 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
950952
}
951953

952954
private fun toggleListMenuSelection(listMenuItemId: Int, isChecked: Boolean) {
955+
val listButton = findViewById<ToggleButton>(R.id.format_bar_button_list)
953956
if (isChecked) {
954957
listMenu?.menu?.findItem(listMenuItemId)?.isChecked = true
955958

956959
when (listMenuItemId) {
957-
R.id.list_ordered -> setListMenuSelector(AztecTextFormat.FORMAT_ORDERED_LIST)
958-
R.id.list_unordered -> setListMenuSelector(AztecTextFormat.FORMAT_UNORDERED_LIST)
959-
else -> setListMenuSelector(AztecTextFormat.FORMAT_UNORDERED_LIST) // Use unordered list selector by default.
960+
R.id.list_ordered -> updateListMenuItem(AztecTextFormat.FORMAT_ORDERED_LIST, listButton)
961+
R.id.list_unordered -> updateListMenuItem(AztecTextFormat.FORMAT_UNORDERED_LIST, listButton)
962+
else -> {
963+
AppLog.w(AppLog.T.EDITOR, "Unknown list menu item")
964+
updateListMenuItem(AztecTextFormat.FORMAT_UNORDERED_LIST, listButton) // Use unordered list selector by default.
965+
}
960966
}
961967
} else {
962968
listMenu?.menu?.findItem(R.id.list_none)?.isChecked = true
963969

964-
// Use unordered list selector by default.
965-
setListMenuSelector(AztecTextFormat.FORMAT_UNORDERED_LIST)
970+
updateListMenuItem(AztecTextFormat.FORMAT_NONE, listButton)
966971
}
967972
}
968973

0 commit comments

Comments
 (0)