Skip to content

Commit

Permalink
AddIndicator: add content description and tooltip for all states
Browse files Browse the repository at this point in the history
  • Loading branch information
UweTrottmann committed Oct 10, 2024
1 parent 8792317 commit 3e1158c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import android.widget.ArrayAdapter
import android.widget.GridView
import androidx.appcompat.widget.TooltipCompat
import androidx.core.view.ViewCompat
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import com.battlelancer.seriesguide.R
import com.battlelancer.seriesguide.databinding.ItemAddshowBinding
import com.battlelancer.seriesguide.enums.NetworkResult
import com.battlelancer.seriesguide.shows.tools.AddShowTask.OnShowAddedEvent
Expand Down Expand Up @@ -258,18 +259,18 @@ abstract class AddFragment : Fragment() {
if (enableMoreOptions) View.VISIBLE else View.GONE

if (item == null) {
binding.addIndicatorAddShow.setState(SearchResult.STATE_ADD)
binding.addIndicatorAddShow.setContentDescriptionAdded(null)
binding.addIndicatorAddShow.isGone = true
binding.textViewAddTitle.text = null
binding.textViewAddDescription.text = null
binding.imageViewAddPoster.setImageDrawable(null)
} else {
// display added indicator instead of add button if already added that show
binding.addIndicatorAddShow.setState(item.state)
// add indicator
val showTitle = item.title
binding.addIndicatorAddShow.setContentDescriptionAdded(
context.getString(R.string.add_already_exists, showTitle)
)
binding.addIndicatorAddShow.apply {
setState(item.state)
setNameOfAssociatedItem(showTitle)
isVisible = true
}

// set text properties immediately
binding.textViewAddTitle.text = showTitle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,43 @@ import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.appcompat.widget.TooltipCompat
import com.battlelancer.seriesguide.R
import com.battlelancer.seriesguide.databinding.LayoutAddIndicatorBinding

/**
* A three state visual indicator with states add, adding and added from [SearchResult].
*
* Make sure to call [setNameOfAssociatedItem] to provide content description and tooltips for all
* states.
*/
class AddIndicator(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {

private val binding: LayoutAddIndicatorBinding

init {
binding = LayoutAddIndicatorBinding.inflate(LayoutInflater.from(context), this)
}
private val binding = LayoutAddIndicatorBinding.inflate(LayoutInflater.from(context), this)

override fun onFinishInflate() {
super.onFinishInflate()
binding.imageViewAddIndicator.also {
TooltipCompat.setTooltipText(it, it.contentDescription)
}
binding.imageViewAddIndicatorAdded.visibility = GONE
binding.progressBarAddIndicator.visibility = GONE
}

fun setContentDescriptionAdded(contentDescription: CharSequence?) {
binding.imageViewAddIndicatorAdded.contentDescription = contentDescription
/**
* Sets content descriptions and tooltips of added and adding states.
*/
fun setNameOfAssociatedItem(name: String) {
binding.apply {
imageViewAddIndicatorAdded.also {
it.contentDescription = context.getString(R.string.add_already_exists, name)
TooltipCompat.setTooltipText(it, it.contentDescription)
}
progressBarAddIndicator.also {
it.contentDescription = context.getString(R.string.add_started, name)
TooltipCompat.setTooltipText(it, it.contentDescription)
}
}
}

fun setOnAddClickListener(onClickListener: OnClickListener?) {
Expand All @@ -41,11 +57,13 @@ class AddIndicator(context: Context, attrs: AttributeSet?) : FrameLayout(context
binding.progressBarAddIndicator.visibility = GONE
binding.imageViewAddIndicatorAdded.visibility = GONE
}

SearchResult.STATE_ADDING -> {
binding.imageViewAddIndicator.visibility = GONE
binding.progressBarAddIndicator.visibility = VISIBLE
binding.imageViewAddIndicatorAdded.visibility = GONE
}

SearchResult.STATE_ADDED -> {
binding.imageViewAddIndicator.visibility = GONE
binding.progressBarAddIndicator.visibility = GONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,16 @@ class SearchResultViewHolder(
// hide more options button
moreOptionsButton.visibility = View.GONE

// display added indicator instead of add button if already added that show
val showTitle = searchResult?.title
// add indicator
if (searchResult != null) {
addIndicator.setState(searchResult.state)
addIndicator.setContentDescriptionAdded(
itemView.context.getString(R.string.add_already_exists, showTitle)
)
addIndicator.setNameOfAssociatedItem(searchResult.title)
addIndicator.visibility = View.VISIBLE
} else {
addIndicator.visibility = View.GONE
}

title.text = showTitle
title.text = searchResult?.title
description.text = searchResult?.overview

// only local shows will have a poster path set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,7 @@ class ShowsDiscoverAdapter(
// display added indicator instead of add button if already added that show
binding.addIndicatorAddShow.setState(item.state)
val showTitle = item.title
binding.addIndicatorAddShow.setContentDescriptionAdded(
context.getString(R.string.add_already_exists, showTitle)
)
binding.addIndicatorAddShow.setNameOfAssociatedItem(showTitle)

// set text properties immediately
binding.textViewAddTitle.text = showTitle
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/layout_add_indicator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
android:scaleType="centerInside"
app:srcCompat="@drawable/ic_add_white_24dp" />

<!-- Content description is set in code. -->
<!-- Content description is set in code -->
<ImageView
android:id="@+id/imageViewAddIndicatorAdded"
android:layout_width="match_parent"
Expand All @@ -21,6 +21,7 @@
android:scaleType="centerInside"
app:srcCompat="@drawable/ic_watched_24dp" />

<!-- Content description is set in code -->
<ProgressBar
android:id="@+id/progressBarAddIndicator"
android:layout_width="match_parent"
Expand Down

0 comments on commit 3e1158c

Please sign in to comment.