-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20916 from wordpress-mobile/issue/comment-details…
…-approve-moderation Comment details approve moderation redesign
- Loading branch information
Showing
14 changed files
with
553 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
WordPress/src/main/java/org/wordpress/android/ui/comments/CommentDetailViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
@file:Suppress("DEPRECATION") | ||
|
||
package org.wordpress.android.ui.comments | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import org.wordpress.android.fluxc.generated.CommentActionBuilder | ||
import org.wordpress.android.fluxc.model.CommentModel | ||
import org.wordpress.android.fluxc.model.CommentStatus | ||
import org.wordpress.android.fluxc.model.SiteModel | ||
import org.wordpress.android.fluxc.model.comments.CommentsMapper | ||
import org.wordpress.android.fluxc.store.CommentStore | ||
import org.wordpress.android.fluxc.store.CommentStore.RemoteLikeCommentPayload | ||
import org.wordpress.android.fluxc.store.CommentsStore | ||
import org.wordpress.android.models.Note | ||
import org.wordpress.android.modules.BG_THREAD | ||
import org.wordpress.android.ui.comments.unified.CommentsStoreAdapter | ||
import org.wordpress.android.ui.notifications.NotificationEvents.OnNoteCommentLikeChanged | ||
import org.wordpress.android.util.EventBusWrapper | ||
import org.wordpress.android.viewmodel.ScopedViewModel | ||
import javax.inject.Inject | ||
import javax.inject.Named | ||
|
||
@HiltViewModel | ||
class CommentDetailViewModel @Inject constructor( | ||
@Named(BG_THREAD) bgDispatcher: CoroutineDispatcher, | ||
private val commentsStore: CommentsStore, | ||
private val commentsStoreAdapter: CommentsStoreAdapter, | ||
private val eventBusWrapper: EventBusWrapper, | ||
private val commentsMapper: CommentsMapper | ||
) : ScopedViewModel(bgDispatcher) { | ||
private val _updatedComment = MutableLiveData<CommentModel>() | ||
val updatedComment: LiveData<CommentModel> = _updatedComment | ||
|
||
/** | ||
* Like or unlike a comment | ||
* @param comment the comment to like or unlike | ||
* @param site the site the comment belongs to | ||
* @param note the note the comment belongs to, non-null if the comment is from a notification | ||
*/ | ||
fun likeComment(comment: CommentModel, site: SiteModel, note: Note? = null) = launch { | ||
val liked = comment.iLike.not() | ||
comment.apply { iLike = liked } | ||
.let { _updatedComment.postValue(it) } | ||
|
||
commentsStoreAdapter.dispatch( | ||
CommentActionBuilder.newLikeCommentAction( | ||
RemoteLikeCommentPayload(site, comment, liked) | ||
) | ||
) | ||
|
||
note?.let { | ||
eventBusWrapper.postSticky(OnNoteCommentLikeChanged(note, liked)) | ||
} | ||
} | ||
|
||
/** | ||
* Dispatch a moderation action to the server, it does not include [CommentStatus.DELETED] status | ||
*/ | ||
fun dispatchModerationAction(site: SiteModel, comment: CommentModel, status: CommentStatus) { | ||
commentsStoreAdapter.dispatch( | ||
CommentActionBuilder.newPushCommentAction(CommentStore.RemoteCommentPayload(site, comment)) | ||
) | ||
|
||
comment.apply { this.status = status.toString() } | ||
.let { _updatedComment.postValue(it) } | ||
} | ||
|
||
/** | ||
* Fetch the latest comment from the server | ||
* @param site the site the comment belongs to | ||
* @param remoteCommentId the remote ID of the comment to fetch | ||
*/ | ||
fun fetchComment(site: SiteModel, remoteCommentId: Long) = launch { | ||
val result = commentsStore.fetchComment(site, remoteCommentId, null) | ||
result.data?.comments?.firstOrNull()?.let { | ||
_updatedComment.postValue(commentsMapper.commentEntityToLegacyModel(it)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.