Skip to content

Commit

Permalink
Make refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarvis Lin committed Feb 19, 2024
1 parent 6d8aaa7 commit d2126fb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import org.wordpress.android.ui.notifications.NotificationsListFragment.Companio
import org.wordpress.android.ui.notifications.NotificationsListViewModel.InlineActionEvent
import org.wordpress.android.ui.notifications.NotificationsListViewModel.InlineActionEvent.SharePostButtonTapped
import org.wordpress.android.ui.notifications.adapters.NotesAdapter
import org.wordpress.android.ui.notifications.adapters.NotesAdapter.DataLoadedListener
import org.wordpress.android.ui.notifications.adapters.NotesAdapter.FILTERS
import org.wordpress.android.ui.notifications.services.NotificationsUpdateServiceStarter
import org.wordpress.android.ui.notifications.utils.NotificationsActions
Expand All @@ -75,8 +74,7 @@ import javax.inject.Inject

@AndroidEntryPoint
class NotificationsListFragmentPage : ViewPagerFragment(R.layout.notifications_list_fragment_page),
OnScrollToTopListener,
DataLoadedListener {
OnScrollToTopListener {
private lateinit var notesAdapter: NotesAdapter
private var swipeToRefreshHelper: SwipeToRefreshHelper? = null
private var isAnimatingOutNewNotificationsBar = false
Expand All @@ -100,7 +98,7 @@ class NotificationsListFragmentPage : ViewPagerFragment(R.layout.notifications_l

private var binding: NotificationsListFragmentPageBinding? = null

@Suppress("DEPRECATION", "OVERRIDE_DEPRECATION")
@Suppress("OVERRIDE_DEPRECATION")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == RequestCodes.NOTE_DETAIL) {
if (resultCode == Activity.RESULT_OK) {
Expand All @@ -123,11 +121,9 @@ class NotificationsListFragmentPage : ViewPagerFragment(R.layout.notifications_l
arguments?.let {
tabPosition = it.getInt(KEY_TAB_POSITION, All.ordinal)
}
notesAdapter = NotesAdapter(
requireActivity(), this, null,
inlineActionEvents = viewModel.inlineActionEvents
).apply {
notesAdapter = NotesAdapter(requireActivity(), inlineActionEvents = viewModel.inlineActionEvents).apply {
onNoteClicked = { noteId -> handleNoteClick(noteId) }
onNotesLoaded = { itemCount -> updateEmptyLayouts(itemCount) }
viewModel.inlineActionEvents.flowWithLifecycle(viewLifecycleOwner.lifecycle, Lifecycle.State.STARTED)
.onEach(::handleInlineActionEvent)
.launchIn(viewLifecycleOwner.lifecycleScope)
Expand Down Expand Up @@ -164,7 +160,7 @@ class NotificationsListFragmentPage : ViewPagerFragment(R.layout.notifications_l
binding = null
}

override fun onDataLoaded(itemsCount: Int) {
private fun updateEmptyLayouts(itemsCount: Int) {
if (!isAdded) {
AppLog.d(
T.NOTIFS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,32 @@ import org.wordpress.android.util.extensions.getColorFromAttribute
import org.wordpress.android.util.image.ImageManager
import org.wordpress.android.util.image.ImageType
import javax.inject.Inject
import kotlin.math.roundToInt

class NotesAdapter(
context: Context, dataLoadedListener: DataLoadedListener,
onLoadMoreListener: OnLoadMoreListener?,
private val inlineActionEvents: MutableSharedFlow<InlineActionEvent>,
) : RecyclerView.Adapter<NoteViewHolder>() {
private val avatarSize: Int
private val textIndentSize: Int
private val dataLoadedListener: DataLoadedListener
private val onLoadMoreListener: OnLoadMoreListener?
class NotesAdapter(context: Context, private val inlineActionEvents: MutableSharedFlow<InlineActionEvent>) :
RecyclerView.Adapter<NoteViewHolder>() {
private val coroutineScope = CoroutineScope(Dispatchers.IO)
val filteredNotes = ArrayList<Note>()
var onNoteClicked = { _: String -> }
var onNotesLoaded = { _: Int -> }
var onScrolledToBottom = { _:Long -> }

@Inject
lateinit var imageManager: ImageManager

@Inject
lateinit var notificationsUtilsWrapper: NotificationsUtilsWrapper

init {
(context.applicationContext as WordPress).component().inject(this)

// this is on purpose - we don't show more than a hundred or so notifications at a time so no need to set
// stable IDs. This helps prevent crashes in case a note comes with no ID (we've code checking for that
// elsewhere, but telling the RecyclerView.Adapter the notes have stable Ids and then failing to provide them
// will make things go south as in https://github.com/wordpress-mobile/WordPress-Android/issues/8741
setHasStableIds(false)
}

enum class FILTERS {
FILTER_ALL,
FILTER_COMMENT,
Expand All @@ -88,14 +94,6 @@ class NotesAdapter(
private set
private var reloadLocalNotesJob: Job? = null

interface DataLoadedListener {
fun onDataLoaded(itemsCount: Int)
}

interface OnLoadMoreListener {
fun onLoadMore(timestamp: Long)
}

fun setFilter(newFilter: FILTERS) {
currentFilter = newFilter
}
Expand All @@ -117,7 +115,7 @@ class NotesAdapter(
filteredNotes.addAll(newNotes)
withContext(Dispatchers.Main) {
differ.submitList(newNotes)
dataLoadedListener.onDataLoaded(itemCount)
onNotesLoaded(itemCount)
}
}

Expand Down Expand Up @@ -194,6 +192,8 @@ class NotesAdapter(
if (RtlUtils.isRtl(noteViewHolder.itemView.context)) {
noteViewHolder.binding.noteSubjectNoticon.scaleX = -1f
}
val textIndentSize = noteViewHolder.itemView.context.resources
.getDimensionPixelSize(R.dimen.notifications_text_indent_sz)
CommentUtils.indentTextViewFirstLine(noteViewHolder.binding.noteSubject, textIndentSize)
noteViewHolder.binding.noteSubjectNoticon.text = noteSubjectNoticon
noteViewHolder.binding.noteSubjectNoticon.visibility = View.VISIBLE
Expand All @@ -213,8 +213,8 @@ class NotesAdapter(
noteViewHolder.binding.notificationUnread.isVisible = note.isUnread

// request to load more comments when we near the end
if (onLoadMoreListener != null && position >= itemCount - 1) {
onLoadMoreListener.onLoadMore(note.timestamp)
if (position >= itemCount - 1) {
onScrolledToBottom(note.timestamp)
}
val headerMarginTop: Int
val context = noteViewHolder.itemView.context
Expand Down Expand Up @@ -256,6 +256,7 @@ class NotesAdapter(
}

private fun loadAvatar(imageView: ImageView, avatarUrl: String) {
val avatarSize = imageView.context.resources.getDimension(R.dimen.notifications_avatar_sz).roundToInt()
val url = GravatarUtils.fixGravatarUrl(avatarUrl, avatarSize)
imageManager.loadIntoCircle(imageView, ImageType.AVATAR_WITH_BACKGROUND, url)
}
Expand Down Expand Up @@ -365,21 +366,6 @@ class NotesAdapter(
}
}

init {
(context.applicationContext as WordPress).component().inject(this)
this.dataLoadedListener = dataLoadedListener
this.onLoadMoreListener = onLoadMoreListener

// this is on purpose - we don't show more than a hundred or so notifications at a time so no need to set
// stable IDs. This helps prevent crashes in case a note comes with no ID (we've code checking for that
// elsewhere, but telling the RecyclerView.Adapter the notes have stable Ids and then failing to provide them
// will make things go south as in https://github.com/wordpress-mobile/WordPress-Android/issues/8741
setHasStableIds(false)
avatarSize = context.resources.getDimension(R.dimen.notifications_avatar_sz).toInt()
textIndentSize =
context.resources.getDimensionPixelSize(R.dimen.notifications_text_indent_sz)
}

companion object {
// Instead of building the filtered notes list dynamically, create it once and re-use it.
// Otherwise it's re-created so many times during layout.
Expand Down

0 comments on commit d2126fb

Please sign in to comment.