Skip to content

Commit

Permalink
refactor: Pass pachliAccountId to more functions (#1008)
Browse files Browse the repository at this point in the history
While this isn't used in many places yet, it pushes the places where the
active account is referenced further up the call stack.
  • Loading branch information
nikclayton authored Oct 17, 2024
1 parent ab186d4 commit 1b3b78a
Show file tree
Hide file tree
Showing 31 changed files with 308 additions and 263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,24 @@ import app.pachli.interfaces.StatusActionListener
import app.pachli.viewdata.IStatusViewData

open class FilterableStatusViewHolder<T : IStatusViewData>(
private val pachliAccountId: Long,
private val binding: ItemStatusWrapperBinding,
) : StatusViewHolder<T>(pachliAccountId, binding.statusContainer, binding.root) {
) : StatusViewHolder<T>(binding.statusContainer, binding.root) {
/** The filter that matched the status, null if the status is not being filtered. */
var matchedFilter: Filter? = null

override fun setupWithStatus(
pachliAccountId: Long,
viewData: T,
listener: StatusActionListener<T>,
statusDisplayOptions: StatusDisplayOptions,
payloads: Any?,
) {
super.setupWithStatus(viewData, listener, statusDisplayOptions, payloads)
setupFilterPlaceholder(viewData, listener)
super.setupWithStatus(pachliAccountId, viewData, listener, statusDisplayOptions, payloads)
setupFilterPlaceholder(pachliAccountId, viewData, listener)
}

private fun setupFilterPlaceholder(
pachliAccountId: Long,
status: T,
listener: StatusActionListener<T>,
) {
Expand All @@ -70,7 +71,7 @@ open class FilterableStatusViewHolder<T : IStatusViewData>(
binding.statusFilteredPlaceholder.statusFilterLabel.text = label

binding.statusFilteredPlaceholder.statusFilterShowAnyway.setOnClickListener {
listener.clearWarningAction(status)
listener.clearWarningAction(pachliAccountId, status)
}
binding.statusFilteredPlaceholder.statusFilterEditFilter.setOnClickListener {
listener.onEditFilterById(pachliAccountId, result.filter.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class FollowRequestViewHolder(
) : NotificationsPagingAdapter.ViewHolder, RecyclerView.ViewHolder(binding.root) {

override fun bind(
pachliAccountId: Long,
viewData: NotificationViewData,
payloads: List<*>?,
statusDisplayOptions: StatusDisplayOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ReportNotificationViewHolder(
) : NotificationsPagingAdapter.ViewHolder, RecyclerView.ViewHolder(binding.root) {

override fun bind(
pachliAccountId: Long,
viewData: NotificationViewData,
payloads: List<*>?,
statusDisplayOptions: StatusDisplayOptions,
Expand Down
28 changes: 19 additions & 9 deletions app/src/main/java/app/pachli/adapter/StatusBaseViewHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ import java.text.NumberFormat
import java.util.Date

abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
private val pachliAccountId: Long,
itemView: View,
) :
RecyclerView.ViewHolder(itemView) {
Expand Down Expand Up @@ -146,6 +145,7 @@ abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
}

protected fun setSpoilerAndContent(
pachliAccountId: Long,
viewData: T,
statusDisplayOptions: StatusDisplayOptions,
listener: StatusActionListener<T>,
Expand All @@ -165,6 +165,7 @@ abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
setContentWarningButtonText(expanded)
contentWarningButton.setOnClickListener {
toggleExpandedState(
pachliAccountId,
viewData,
true,
!expanded,
Expand Down Expand Up @@ -196,17 +197,19 @@ abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
}

protected open fun toggleExpandedState(
pachliAccountId: Long,
viewData: T,
sensitive: Boolean,
expanded: Boolean,
statusDisplayOptions: StatusDisplayOptions,
listener: StatusActionListener<T>,
) {
contentWarningDescription.invalidate()
listener.onExpandedChange(viewData, expanded)
listener.onExpandedChange(pachliAccountId, viewData, expanded)
setContentWarningButtonText(expanded)
setTextVisible(sensitive, expanded, viewData, statusDisplayOptions, listener)
setupCard(
pachliAccountId,
viewData,
expanded,
statusDisplayOptions.cardViewMode,
Expand Down Expand Up @@ -463,6 +466,7 @@ abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
}

protected fun setMediaPreviews(
pachliAccountId: Long,
viewData: T,
attachments: List<Attachment>,
sensitive: Boolean,
Expand Down Expand Up @@ -494,7 +498,7 @@ abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
} else {
imageView.foreground = null
}
setAttachmentClickListener(viewData, imageView, listener, i, attachment, true)
setAttachmentClickListener(pachliAccountId, viewData, imageView, listener, i, attachment, true)
if (sensitive) {
sensitiveMediaWarning.setText(R.string.post_sensitive_media_title)
} else {
Expand All @@ -505,13 +509,13 @@ abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
descriptionIndicator.visibility =
if (hasDescription && showingContent) View.VISIBLE else View.GONE
sensitiveMediaShow.setOnClickListener { v: View ->
listener.onContentHiddenChange(viewData, false)
listener.onContentHiddenChange(pachliAccountId, viewData, false)
v.visibility = View.GONE
sensitiveMediaWarning.visibility = View.VISIBLE
descriptionIndicator.visibility = View.GONE
}
sensitiveMediaWarning.setOnClickListener { v: View ->
listener.onContentHiddenChange(viewData, true)
listener.onContentHiddenChange(pachliAccountId, viewData, true)
v.visibility = View.GONE
sensitiveMediaShow.visibility = View.VISIBLE
descriptionIndicator.visibility = if (hasDescription) View.VISIBLE else View.GONE
Expand All @@ -526,6 +530,7 @@ abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
}

protected fun setMediaLabel(
pachliAccountId: Long,
viewData: T,
attachments: List<Attachment>,
sensitive: Boolean,
Expand All @@ -543,14 +548,15 @@ abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
// Set the icon next to the label.
val drawableId = attachments[0].iconResource()
mediaLabel.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableId, 0, 0, 0)
setAttachmentClickListener(viewData, mediaLabel, listener, i, attachment, false)
setAttachmentClickListener(pachliAccountId, viewData, mediaLabel, listener, i, attachment, false)
} else {
mediaLabel.visibility = View.GONE
}
}
}

private fun setAttachmentClickListener(
pachliAccountId: Long,
viewData: T,
view: View,
listener: StatusActionListener<T>,
Expand All @@ -560,7 +566,7 @@ abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
) {
view.setOnClickListener { v: View? ->
if (sensitiveMediaWarning.visibility == View.VISIBLE) {
listener.onContentHiddenChange(viewData, true)
listener.onContentHiddenChange(pachliAccountId, viewData, true)
} else {
listener.onViewMedia(viewData, index, if (animateTransition) v else null)
}
Expand Down Expand Up @@ -676,6 +682,7 @@ abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
}

open fun setupWithStatus(
pachliAccountId: Long,
viewData: T,
listener: StatusActionListener<T>,
statusDisplayOptions: StatusDisplayOptions,
Expand Down Expand Up @@ -707,6 +714,7 @@ abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
val sensitive = actionable.sensitive
if (statusDisplayOptions.mediaPreviewEnabled && hasPreviewableAttachment(attachments)) {
setMediaPreviews(
pachliAccountId,
viewData,
attachments,
sensitive,
Expand All @@ -722,12 +730,13 @@ abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
mediaLabel.visibility = View.GONE
}
} else {
setMediaLabel(viewData, attachments, sensitive, listener, viewData.isShowingContent)
setMediaLabel(pachliAccountId, viewData, attachments, sensitive, listener, viewData.isShowingContent)
// Hide all unused views.
mediaPreview.visibility = View.GONE
hideSensitiveMediaWarning()
}
setupCard(
pachliAccountId,
viewData,
viewData.isExpanded,
statusDisplayOptions.cardViewMode,
Expand All @@ -741,7 +750,7 @@ abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
statusDisplayOptions,
)
setRebloggingEnabled(actionable.rebloggingAllowed(), actionable.visibility)
setSpoilerAndContent(viewData, statusDisplayOptions, listener)
setSpoilerAndContent(pachliAccountId, viewData, statusDisplayOptions, listener)
setContentDescriptionForStatus(viewData, statusDisplayOptions)

// Workaround for RecyclerView 1.0.0 / androidx.core 1.0.0
Expand Down Expand Up @@ -865,6 +874,7 @@ abstract class StatusBaseViewHolder<T : IStatusViewData> protected constructor(
}

protected fun setupCard(
pachliAccountId: Long,
viewData: T,
expanded: Boolean,
cardViewMode: CardViewMode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ import java.text.DateFormat
import java.util.Locale

class StatusDetailedViewHolder(
pachliAccountId: Long,
private val binding: ItemStatusDetailedBinding,
) : StatusBaseViewHolder<StatusViewData>(pachliAccountId, binding.root) {
) : StatusBaseViewHolder<StatusViewData>(binding.root) {

override fun setMetaData(
viewData: StatusViewData,
Expand Down Expand Up @@ -107,6 +106,7 @@ class StatusDetailedViewHolder(
}

override fun setupWithStatus(
pachliAccountId: Long,
viewData: StatusViewData,
listener: StatusActionListener<StatusViewData>,
statusDisplayOptions: StatusDisplayOptions,
Expand All @@ -115,8 +115,9 @@ class StatusDetailedViewHolder(
// We never collapse statuses in the detail view
val uncollapsedStatus =
if (viewData.isCollapsible && viewData.isCollapsed) viewData.copy(isCollapsed = false) else viewData
super.setupWithStatus(uncollapsedStatus, listener, statusDisplayOptions, payloads)
super.setupWithStatus(pachliAccountId, uncollapsedStatus, listener, statusDisplayOptions, payloads)
setupCard(
pachliAccountId,
uncollapsedStatus,
viewData.isExpanded,
CardViewMode.FULL_WIDTH,
Expand Down
16 changes: 9 additions & 7 deletions app/src/main/java/app/pachli/adapter/StatusViewHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ import app.pachli.viewdata.IStatusViewData
import at.connyduck.sparkbutton.helpers.Utils

open class StatusViewHolder<T : IStatusViewData>(
pachliAccountId: Long,
private val binding: ItemStatusBinding,
root: View? = null,
) : StatusBaseViewHolder<T>(pachliAccountId, root ?: binding.root) {
) : StatusBaseViewHolder<T>(root ?: binding.root) {

override fun setupWithStatus(
pachliAccountId: Long,
viewData: T,
listener: StatusActionListener<T>,
statusDisplayOptions: StatusDisplayOptions,
Expand All @@ -50,7 +50,7 @@ open class StatusViewHolder<T : IStatusViewData>(
if (payloads == null) {
val sensitive = !TextUtils.isEmpty(viewData.actionable.spoilerText)
val expanded = viewData.isExpanded
setupCollapsedState(viewData, sensitive, expanded, listener)
setupCollapsedState(pachliAccountId, viewData, sensitive, expanded, listener)
val reblogging = viewData.rebloggingStatus
if (reblogging == null || viewData.filterAction === FilterAction.WARN) {
statusInfo.hide()
Expand All @@ -70,7 +70,7 @@ open class StatusViewHolder<T : IStatusViewData>(
statusFavouritesCount.visible(statusDisplayOptions.showStatsInline)
setFavouritedCount(viewData.actionable.favouritesCount)
setReblogsCount(viewData.actionable.reblogsCount)
super.setupWithStatus(viewData, listener, statusDisplayOptions, payloads)
super.setupWithStatus(pachliAccountId, viewData, listener, statusDisplayOptions, payloads)
}

private fun setRebloggedByDisplayName(
Expand Down Expand Up @@ -109,6 +109,7 @@ open class StatusViewHolder<T : IStatusViewData>(
}

private fun setupCollapsedState(
pachliAccountId: Long,
viewData: T,
sensitive: Boolean,
expanded: Boolean,
Expand All @@ -117,7 +118,7 @@ open class StatusViewHolder<T : IStatusViewData>(
/* input filter for TextViews have to be set before text */
if (viewData.isCollapsible && (!sensitive || expanded)) {
buttonToggleContent.setOnClickListener {
listener.onContentCollapsedChange(viewData, !viewData.isCollapsed)
listener.onContentCollapsedChange(pachliAccountId, viewData, !viewData.isCollapsed)
}
buttonToggleContent.show()
if (viewData.isCollapsed) {
Expand All @@ -139,14 +140,15 @@ open class StatusViewHolder<T : IStatusViewData>(
}

override fun toggleExpandedState(
pachliAccountId: Long,
viewData: T,
sensitive: Boolean,
expanded: Boolean,
statusDisplayOptions: StatusDisplayOptions,
listener: StatusActionListener<T>,
) {
setupCollapsedState(viewData, sensitive, expanded, listener)
super.toggleExpandedState(viewData, sensitive, expanded, statusDisplayOptions, listener)
setupCollapsedState(pachliAccountId, viewData, sensitive, expanded, listener)
super.toggleExpandedState(pachliAccountId, viewData, sensitive, expanded, statusDisplayOptions, listener)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ConversationAdapter(

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ConversationViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_conversation, parent, false)
return ConversationViewHolder(pachliAccountId, view, statusDisplayOptions, listener)
return ConversationViewHolder(view, statusDisplayOptions, listener)
}

override fun onBindViewHolder(holder: ConversationViewHolder, position: Int) {
Expand All @@ -54,7 +54,7 @@ class ConversationAdapter(
payloads: List<Any>,
) {
getItem(position)?.let { conversationViewData ->
holder.setupWithConversation(conversationViewData, payloads.firstOrNull())
holder.setupWithConversation(pachliAccountId, conversationViewData, payloads.firstOrNull())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ import app.pachli.interfaces.StatusActionListener
import app.pachli.util.SmartLengthInputFilter

class ConversationViewHolder internal constructor(
pachliAccountId: Long,
itemView: View,
private val statusDisplayOptions: StatusDisplayOptions,
private val listener: StatusActionListener<ConversationViewData>,
) : StatusBaseViewHolder<ConversationViewData>(pachliAccountId, itemView) {
) : StatusBaseViewHolder<ConversationViewData>(itemView) {
private val conversationNameTextView: TextView = itemView.findViewById(R.id.conversation_name)
private val contentCollapseButton: Button = itemView.findViewById(R.id.button_toggle_content)
private val avatars: Array<ImageView> = arrayOf(
Expand All @@ -47,12 +46,13 @@ class ConversationViewHolder internal constructor(
)

fun setupWithConversation(
pachliAccountId: Long,
viewData: ConversationViewData,
payloads: Any?,
) {
val (_, _, account, inReplyToId, _, _, _, _, _, _, _, _, _, _, favourited, bookmarked, sensitive, _, _, attachments) = viewData.status
if (payloads == null) {
setupCollapsedState(viewData, listener)
setupCollapsedState(pachliAccountId, viewData, listener)
setDisplayName(account.name, account.emojis, statusDisplayOptions)
setUsername(account.username)
setMetaData(viewData, statusDisplayOptions, listener)
Expand All @@ -61,6 +61,7 @@ class ConversationViewHolder internal constructor(
setBookmarked(bookmarked)
if (statusDisplayOptions.mediaPreviewEnabled && hasPreviewableAttachment(attachments)) {
setMediaPreviews(
pachliAccountId,
viewData,
attachments,
sensitive,
Expand All @@ -76,7 +77,7 @@ class ConversationViewHolder internal constructor(
mediaLabel.visibility = View.GONE
}
} else {
setMediaLabel(viewData, attachments, sensitive, listener, viewData.isShowingContent)
setMediaLabel(pachliAccountId, viewData, attachments, sensitive, listener, viewData.isShowingContent)
// Hide all unused views.
mediaPreview.visibility = View.GONE
hideSensitiveMediaWarning()
Expand All @@ -87,7 +88,7 @@ class ConversationViewHolder internal constructor(
account.id,
statusDisplayOptions,
)
setSpoilerAndContent(viewData, statusDisplayOptions, listener)
setSpoilerAndContent(pachliAccountId, viewData, statusDisplayOptions, listener)
setConversationName(viewData.accounts)
setAvatars(viewData.accounts)
} else {
Expand Down Expand Up @@ -137,13 +138,14 @@ class ConversationViewHolder internal constructor(
}

private fun setupCollapsedState(
pachliAccountId: Long,
viewData: ConversationViewData,
listener: StatusActionListener<ConversationViewData>,
) {
/* input filter for TextViews have to be set before text */
if (viewData.isCollapsible && (viewData.isExpanded || TextUtils.isEmpty(viewData.spoilerText))) {
contentCollapseButton.setOnClickListener {
listener.onContentCollapsedChange(viewData, !viewData.isCollapsed)
listener.onContentCollapsedChange(pachliAccountId, viewData, !viewData.isCollapsed)
}
contentCollapseButton.show()
if (viewData.isCollapsed) {
Expand Down
Loading

0 comments on commit 1b3b78a

Please sign in to comment.