Skip to content

Commit

Permalink
Merge pull request #20478 from wordpress-mobile/experimental/hack-wee…
Browse files Browse the repository at this point in the history
…k-2024-03-11/refactor-notifications--remove-async-task

Refactor notifications: remove async task
  • Loading branch information
Antonis Lilis authored Apr 5, 2024
2 parents de5c120 + 7cd7ede commit b24bac6
Showing 1 changed file with 65 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*/
package org.wordpress.android.ui.notifications

import android.annotation.SuppressLint
import android.os.AsyncTask
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
Expand All @@ -16,7 +14,11 @@ import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.ListView
import androidx.fragment.app.ListFragment
import androidx.lifecycle.lifecycleScope
import com.airbnb.lottie.LottieAnimationView
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONArray
import org.json.JSONException
import org.wordpress.android.R
Expand All @@ -36,6 +38,8 @@ import org.wordpress.android.fluxc.tools.FormattableRangeType.SITE
import org.wordpress.android.fluxc.tools.FormattableRangeType.STAT
import org.wordpress.android.fluxc.tools.FormattableRangeType.USER
import org.wordpress.android.models.Note
import org.wordpress.android.modules.IO_THREAD
import org.wordpress.android.modules.UI_THREAD
import org.wordpress.android.ui.ScrollableViewInitializedListener
import org.wordpress.android.ui.ViewPagerFragment.Companion.restoreOriginalViewId
import org.wordpress.android.ui.ViewPagerFragment.Companion.setUniqueIdToView
Expand Down Expand Up @@ -66,6 +70,7 @@ import org.wordpress.android.util.image.ImageManager
import org.wordpress.android.util.image.ImageType.AVATAR_WITH_BACKGROUND
import org.wordpress.android.util.image.ImageType.BLAVATAR
import javax.inject.Inject
import javax.inject.Named

class NotificationsDetailListFragment : ListFragment(), NotificationFragment {
private var restoredListPosition = 0
Expand All @@ -86,6 +91,14 @@ class NotificationsDetailListFragment : ListFragment(), NotificationFragment {
@Inject
lateinit var listScenarioUtils: ListScenarioUtils

@Inject
@Named(IO_THREAD)
lateinit var ioDispatcher: CoroutineDispatcher

@Inject
@Named(UI_THREAD)
lateinit var mainDispatcher: CoroutineDispatcher

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(requireActivity().application as WordPress).component().inject(this)
Expand Down Expand Up @@ -188,9 +201,15 @@ class NotificationsDetailListFragment : ListFragment(), NotificationFragment {
super.onSaveInstanceState(outState)
}

@Suppress("DEPRECATION")
private fun reloadNoteBlocks() {
LoadNoteBlocksTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
lifecycleScope.launch(ioDispatcher) {
notification?.let { note ->
val noteBlocks = noteBlocksLoader.loadNoteBlocks(note)
withContext(mainDispatcher) {
noteBlocksLoader.handleNoteBlocks(noteBlocks)
}
}
}
}

fun setFooterView(footerView: ViewGroup?) {
Expand Down Expand Up @@ -317,10 +336,7 @@ class NotificationsDetailListFragment : ListFragment(), NotificationFragment {
private data class ManageUserBlockResults(val index: Int, val noteBlock: NoteBlock, val pingbackUrl: String?)

// Loop through the 'body' items in this note, and create blocks for each.
// TODO replace this inner async task with a coroutine
@Suppress("DEPRECATION")
@SuppressLint("StaticFieldLeak")
private inner class LoadNoteBlocksTask : AsyncTask<Void, Void, List<NoteBlock>?>() {
private val noteBlocksLoader = object {
private var mIsBadgeView = false

private fun addHeaderNoteBlock(note: Note, noteList: MutableList<NoteBlock>) {
Expand Down Expand Up @@ -460,37 +476,31 @@ class NotificationsDetailListFragment : ListFragment(), NotificationFragment {
}
}

@Suppress("OVERRIDE_DEPRECATION")
override fun doInBackground(vararg params: Void): List<NoteBlock>? {
if (notification == null) {
return null
}
requestReaderContentForNote()
suspend fun loadNoteBlocks(note: Note): List<NoteBlock> {
requestReaderContentForNote(note)

requireNotNull(notification).let { note ->
val bodyArray = note.body
val noteList: MutableList<NoteBlock> = ArrayList()
val bodyArray = note.body
val noteList: MutableList<NoteBlock> = ArrayList()

// Add the note header if one was provided
if (note.header != null) {
addHeaderNoteBlock(note, noteList)
}
var pingbackUrl: String? = null
val isPingback = isPingback(note)
if (bodyArray.length() > 0) {
pingbackUrl = addNotesBlock(note, noteList, bodyArray, isPingback)
}
if (isPingback) {
// Remove this when we start receiving "Read the source post block" from the backend
val generatedBlock = buildGeneratedLinkBlock(
mOnNoteBlockTextClickListener, pingbackUrl,
activity!!.getString(R.string.comment_read_source_post)
)
generatedBlock.setIsPingback()
noteList.add(generatedBlock)
}
return noteList
// Add the note header if one was provided
if (note.header != null) {
addHeaderNoteBlock(note, noteList)
}
var pingbackUrl: String? = null
val isPingback = isPingback(note)
if (bodyArray.length() > 0) {
pingbackUrl = addNotesBlock(note, noteList, bodyArray, isPingback)
}
if (isPingback) {
// Remove this when we start receiving "Read the source post block" from the backend
val generatedBlock = buildGeneratedLinkBlock(
mOnNoteBlockTextClickListener, pingbackUrl,
activity!!.getString(R.string.comment_read_source_post)
)
generatedBlock.setIsPingback()
noteList.add(generatedBlock)
}
return noteList
}

private fun isPingback(note: Note): Boolean {
Expand Down Expand Up @@ -525,8 +535,7 @@ class NotificationsDetailListFragment : ListFragment(), NotificationFragment {
)
}

@Suppress("OVERRIDE_DEPRECATION")
override fun onPostExecute(noteList: List<NoteBlock>?) {
fun handleNoteBlocks(noteList: List<NoteBlock>?) {
if (!isAdded || noteList == null) {
return
}
Expand Down Expand Up @@ -595,37 +604,35 @@ class NotificationsDetailListFragment : ListFragment(), NotificationFragment {
}

// Requests Reader content for certain notification types
private fun requestReaderContentForNote() {
if (notification == null || !isAdded) {
return
private suspend fun requestReaderContentForNote(note: Note) = withContext(ioDispatcher) {
if (!isAdded) {
return@withContext
}

// Request the reader post so that loading reader activities will work.
if (notification!!.isUserList && !ReaderPostTable.postExists(
notification!!.siteId.toLong(),
notification!!.postId.toLong()
if (note.isUserList && !ReaderPostTable.postExists(
note.siteId.toLong(),
note.postId.toLong()
)
) {
ReaderPostActions.requestBlogPost(notification!!.siteId.toLong(), notification!!.postId.toLong(), null)
ReaderPostActions.requestBlogPost(note.siteId.toLong(), note.postId.toLong(), null)
}

requireNotNull(notification).let { note ->
// Request reader comments until we retrieve the comment for this note
val isReplyOrCommentLike = note.isCommentLikeType || note.isCommentReplyType || note.isCommentWithUserReply
val commentNotExists = !ReaderCommentTable.commentExists(
// Request reader comments until we retrieve the comment for this note
val isReplyOrCommentLike = note.isCommentLikeType || note.isCommentReplyType || note.isCommentWithUserReply
val commentNotExists = !ReaderCommentTable.commentExists(
note.siteId.toLong(),
note.postId.toLong(),
note.commentId
)

if (isReplyOrCommentLike && commentNotExists) {
ReaderCommentService.startServiceForComment(
activity,
note.siteId.toLong(),
note.postId.toLong(),
note.commentId
)

if (isReplyOrCommentLike && commentNotExists) {
ReaderCommentService.startServiceForComment(
activity,
note.siteId.toLong(),
note.postId.toLong(),
note.commentId
)
}
}
}

Expand Down

0 comments on commit b24bac6

Please sign in to comment.