Skip to content

Commit

Permalink
Add more information to the filter list view
Browse files Browse the repository at this point in the history
  • Loading branch information
Tak committed Feb 10, 2023
1 parent 569564f commit 056efda
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,61 +1,55 @@
package com.keylesspalace.tusky.components.filters

import android.content.Context
import android.text.format.DateUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.databinding.ItemRemovableBinding
import com.keylesspalace.tusky.entity.Filter
import com.keylesspalace.tusky.util.getRelativeTimeSpanString

class FiltersAdapter(context: Context, val listener: FiltersListener, val filters: List<Filter>) :
ArrayAdapter<String>(
context,
R.layout.item_removable,
filters.map { filter ->
if (filter.expiresAt == null) {
class FiltersAdapter(context: Context, val listener: FiltersListener, filters: List<Filter>) :
ArrayAdapter<Filter>(context, R.layout.item_removable, filters) {

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val binding = if (convertView == null) {
ItemRemovableBinding.inflate(LayoutInflater.from(context), parent, false)
} else {
ItemRemovableBinding.bind(convertView)
}

val resources = binding.root.resources
val actions = resources.getStringArray(R.array.filter_actions)
val contexts = resources.getStringArray(R.array.filter_contexts)

getItem(position)?.let { filter ->
val context = binding.root.context
binding.textPrimary.text = if (filter.expiresAt == null) {
filter.title
} else {
context.getString(
R.string.filter_expiration_format,
filter.title,
DateUtils.getRelativeTimeSpanString(
filter.expiresAt.time,
System.currentTimeMillis(),
DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE
)
getRelativeTimeSpanString(binding.root.context, filter.expiresAt.time, System.currentTimeMillis())
)
}
binding.textSecondary.text = context.getString(
R.string.filter_description_format,
actions.getOrNull(filter.action.ordinal - 1),
filter.context.map { contexts.getOrNull(Filter.Kind.from(it).ordinal) }.joinToString("/")
)
}
) {

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val binding = if (convertView == null) {
ItemRemovableBinding.inflate(LayoutInflater.from(context), parent, false)
} else {
ItemRemovableBinding.bind(convertView)
}
binding.text.text = filters[position].title

binding.delete.setOnClickListener {
deleteFilter(position)
getItem(position)?.let { listener.deleteFilter(it) }
}

binding.root.setOnClickListener {
editFilter(position)
getItem(position)?.let { listener.updateFilter(it) }
}

return binding.root
}

private fun deleteFilter(position: Int) {
listener.deleteFilter(filters[position])
}

private fun editFilter(position: Int) {
listener.updateFilter(filters[position])
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_edit_filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="@string/filter_action_warn"/>
android:text="@string/filter_description_warn"/>
<RadioButton
android:id="@+id/filter_action_hide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="@string/filter_action_hide"/>
android:text="@string/filter_description_hide"/>
</RadioGroup>

<TextView
Expand Down
30 changes: 24 additions & 6 deletions app/src/main/res/layout/item_removable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,34 @@
android:orientation="horizontal">

<TextView
android:id="@+id/text"
android:id="@+id/textPrimary"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.91"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/delete"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/delete"
app:layout_constraintTop_toTopOf="parent"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:paddingTop="8dp"
android:textSize="?attr/status_text_medium"
android:textColor="@color/textColorPrimary"
/>

<TextView
android:id="@+id/textSecondary"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.91"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textPrimary"
app:layout_constraintBottom_toBottomOf="parent"
android:padding="8dp"
android:textSize="?attr/status_text_medium" />
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:paddingBottom="8dp"
android:textSize="?attr/status_text_small"
android:textColor="@color/textColorTertiary"
/>

<ImageButton
android:id="@+id/delete"
Expand All @@ -25,7 +43,7 @@
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:layout_margin="12dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="?attr/selectableItemBackgroundBorderless"
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/string-arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@
<item>@string/pref_title_thread_filter_keywords</item>
<item>@string/pref_title_account_filter_keywords</item>
</string-array>

<string-array name="filter_actions">
<item>@string/filter_action_warn</item>
<item>@string/filter_action_hide</item>
</string-array>
</resources>
7 changes: 5 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -743,13 +743,16 @@
<string name="a11y_label_loading_thread">Loading thread</string>
<string name="hint_filter_title">My filter</string>
<string name="label_filter_title">Title</string>
<string name="filter_action_warn">Hide with a warning</string>
<string name="filter_action_hide">Hide completely</string>
<string name="filter_action_warn">Warn</string>
<string name="filter_action_hide">Hide</string>
<string name="filter_description_warn">Hide with a warning</string>
<string name="filter_description_hide">Hide completely</string>
<string name="label_filter_action">Filter action</string>
<string name="label_filter_context">Filter contexts</string>
<string name="label_filter_keywords">Keywords or phrases to filter</string>
<string name="action_add">Add</string>
<string name="filter_keyword_display_format">%s (whole word)</string>
<string name="filter_keyword_addition_title">Add keyword</string>
<string name="filter_edit_keyword_title">Edit keyword</string>
<string name="filter_description_format">%s: %s</string>
</resources>

0 comments on commit 056efda

Please sign in to comment.