From d2126fb09a31d386d2e005b051f2d884ef354194 Mon Sep 17 00:00:00 2001 From: Jarvis Lin Date: Mon, 19 Feb 2024 17:14:28 +0800 Subject: [PATCH] Make refactors --- .../NotificationsListFragmentPage.kt | 14 ++--- .../ui/notifications/adapters/NotesAdapter.kt | 56 +++++++------------ 2 files changed, 26 insertions(+), 44 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/notifications/NotificationsListFragmentPage.kt b/WordPress/src/main/java/org/wordpress/android/ui/notifications/NotificationsListFragmentPage.kt index 3cbf5d3ba99c..7380176aa54f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/notifications/NotificationsListFragmentPage.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/notifications/NotificationsListFragmentPage.kt @@ -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 @@ -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 @@ -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) { @@ -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) @@ -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, diff --git a/WordPress/src/main/java/org/wordpress/android/ui/notifications/adapters/NotesAdapter.kt b/WordPress/src/main/java/org/wordpress/android/ui/notifications/adapters/NotesAdapter.kt index 21f0e6c9ac84..2011743d63d6 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/notifications/adapters/NotesAdapter.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/notifications/adapters/NotesAdapter.kt @@ -46,19 +46,15 @@ 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, -) : RecyclerView.Adapter() { - private val avatarSize: Int - private val textIndentSize: Int - private val dataLoadedListener: DataLoadedListener - private val onLoadMoreListener: OnLoadMoreListener? +class NotesAdapter(context: Context, private val inlineActionEvents: MutableSharedFlow) : + RecyclerView.Adapter() { private val coroutineScope = CoroutineScope(Dispatchers.IO) val filteredNotes = ArrayList() var onNoteClicked = { _: String -> } + var onNotesLoaded = { _: Int -> } + var onScrolledToBottom = { _:Long -> } @Inject lateinit var imageManager: ImageManager @@ -66,6 +62,16 @@ class NotesAdapter( @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, @@ -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 } @@ -117,7 +115,7 @@ class NotesAdapter( filteredNotes.addAll(newNotes) withContext(Dispatchers.Main) { differ.submitList(newNotes) - dataLoadedListener.onDataLoaded(itemCount) + onNotesLoaded(itemCount) } } @@ -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 @@ -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 @@ -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) } @@ -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.