Skip to content

Commit

Permalink
Merge pull request #12454 from wordpress-mobile/issue/12039-reader-li…
Browse files Browse the repository at this point in the history
…ke-action

Issue/12039 reader like action
  • Loading branch information
malinajirka authored Jul 23, 2020
2 parents 01093e7 + f0a984a commit d0707b1
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
import org.wordpress.android.ui.reader.actions.ReaderActions.UpdateResultListener;
import org.wordpress.android.ui.reader.models.ReaderSimplePost;
import org.wordpress.android.ui.reader.models.ReaderSimplePostList;
import org.wordpress.android.ui.reader.repository.ReaderRepositoryEvent;
import org.wordpress.android.ui.reader.repository.ReaderRepositoryEvent.PostLikeEnded.PostLikeFailure;
import org.wordpress.android.ui.reader.repository.ReaderRepositoryEvent.PostLikeEnded.PostLikeSuccess;
import org.wordpress.android.ui.reader.repository.ReaderRepositoryEvent.PostLikeEnded.PostLikeUnChanged;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.AppLog.T;
import org.wordpress.android.util.DateTimeUtils;
Expand Down Expand Up @@ -67,6 +71,10 @@ public static boolean performLikeAction(final ReaderPost post,
boolean isCurrentlyLiked = ReaderPostTable.isPostLikedByCurrentUser(post);
if (isCurrentlyLiked == isAskingToLike) {
AppLog.w(T.READER, "post like unchanged");
final PostLikeUnChanged onPostLikeUnChanged =
new ReaderRepositoryEvent.PostLikeEnded.PostLikeUnChanged(
post.postId, post.blogId, isAskingToLike, wpComUserId);
EventBus.getDefault().post(onPostLikeUnChanged);
return false;
}

Expand All @@ -91,6 +99,10 @@ public static boolean performLikeAction(final ReaderPost post,
@Override
public void onResponse(JSONObject jsonObject) {
AppLog.d(T.READER, String.format("post %s succeeded", actionName));
final PostLikeSuccess onPostLikeSuccess =
new ReaderRepositoryEvent.PostLikeEnded.PostLikeSuccess(
post.postId, post.blogId, isAskingToLike, wpComUserId);
EventBus.getDefault().post(onPostLikeSuccess);
}
};

Expand All @@ -106,13 +118,18 @@ public void onErrorResponse(VolleyError volleyError) {
AppLog.e(T.READER, volleyError);
ReaderPostTable.setLikesForPost(post, post.numLikes, post.isLikedByCurrentUser);
ReaderLikeTable.setCurrentUserLikesPost(post, post.isLikedByCurrentUser, wpComUserId);
final PostLikeFailure onPostLikeFailure =
new ReaderRepositoryEvent.PostLikeEnded.PostLikeFailure(
post.postId, post.blogId, isAskingToLike, wpComUserId);
EventBus.getDefault().post(onPostLikeFailure);
}
};

WordPress.getRestClientUtilsV1_1().post(path, listener, errorListener);
return true;
}


/*
* get the latest version of this post - note that the post is only considered changed if the
* like/comment count has changed, or if the current user's like/follow status has changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ import androidx.lifecycle.MutableLiveData
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.wordpress.android.models.ReaderPost
import org.wordpress.android.models.ReaderPostList
import org.wordpress.android.models.ReaderTag
import org.wordpress.android.models.ReaderTagType.DEFAULT
import org.wordpress.android.modules.BG_THREAD
import org.wordpress.android.ui.reader.ReaderConstants
import org.wordpress.android.ui.reader.ReaderEvents.UpdatePostsEnded
import org.wordpress.android.ui.reader.repository.ReaderRepositoryCommunication.Failure
import org.wordpress.android.ui.reader.repository.ReaderRepositoryCommunication.Success
import org.wordpress.android.ui.reader.repository.ReaderRepositoryEvent.PostLikeEnded.PostLikeFailure
import org.wordpress.android.ui.reader.repository.ReaderRepositoryEvent.PostLikeEnded.PostLikeSuccess
import org.wordpress.android.ui.reader.repository.ReaderRepositoryEvent.PostLikeEnded.PostLikeUnChanged
import org.wordpress.android.ui.reader.repository.usecases.FetchPostsForTagUseCase
import org.wordpress.android.ui.reader.repository.usecases.GetNumPostsForTagUseCase
import org.wordpress.android.ui.reader.repository.usecases.GetPostsForTagUseCase
import org.wordpress.android.ui.reader.repository.usecases.GetPostsForTagWithCountUseCase
import org.wordpress.android.ui.reader.repository.usecases.PostLikeActionUseCase
import org.wordpress.android.ui.reader.repository.usecases.ShouldAutoUpdateTagUseCase
import org.wordpress.android.ui.reader.utils.ReaderUtilsWrapper
import org.wordpress.android.viewmodel.Event
Expand All @@ -32,7 +38,8 @@ class ReaderDiscoverRepository constructor(
private val shouldAutoUpdateTagUseCase: ShouldAutoUpdateTagUseCase,
private val getPostsForTagWithCountUseCase: GetPostsForTagWithCountUseCase,
private val fetchPostsForTagUseCase: FetchPostsForTagUseCase,
private val readerUpdatePostsEndedHandler: ReaderUpdatePostsEndedHandler
private val readerUpdatePostsEndedHandler: ReaderUpdatePostsEndedHandler,
private val postLikeActionUseCase: PostLikeActionUseCase
) : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = bgDispatcher
Expand Down Expand Up @@ -63,6 +70,23 @@ class ReaderDiscoverRepository constructor(
shouldAutoUpdateTagUseCase.stop()
getPostsForTagWithCountUseCase.stop()
readerUpdatePostsEndedHandler.stop()
postLikeActionUseCase.stop()
}

// todo - can change this to blogId, feedId, etc
fun performLikeAction(post: ReaderPost, isAskingToLike: Boolean, wpComUserId: Long) {
launch {
when (val event = postLikeActionUseCase.perform(post, isAskingToLike, wpComUserId)) {
is PostLikeSuccess -> {
reloadPosts()
}
is PostLikeFailure -> {
_communicationChannel.postValue(Event(Failure(event)))
reloadPosts()
}
is PostLikeUnChanged -> { }
}
}
}

fun getTag(): ReaderTag {
Expand Down Expand Up @@ -126,7 +150,8 @@ class ReaderDiscoverRepository constructor(
private val shouldAutoUpdateTagUseCase: ShouldAutoUpdateTagUseCase,
private val getPostsForTagWithCountUseCase: GetPostsForTagWithCountUseCase,
private val fetchPostsForTagUseCase: FetchPostsForTagUseCase,
private val readerUpdatePostsEndedHandler: ReaderUpdatePostsEndedHandler
private val readerUpdatePostsEndedHandler: ReaderUpdatePostsEndedHandler,
private val postLikeActionUseCase: PostLikeActionUseCase
) {
fun create(readerTag: ReaderTag? = null): ReaderDiscoverRepository {
val tag = readerTag
Expand All @@ -140,7 +165,8 @@ class ReaderDiscoverRepository constructor(
shouldAutoUpdateTagUseCase,
getPostsForTagWithCountUseCase,
fetchPostsForTagUseCase,
readerUpdatePostsEndedHandler
readerUpdatePostsEndedHandler,
postLikeActionUseCase
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import androidx.lifecycle.MutableLiveData
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.wordpress.android.models.ReaderPost
import org.wordpress.android.models.ReaderPostList
import org.wordpress.android.models.ReaderTag
import org.wordpress.android.modules.BG_THREAD
import org.wordpress.android.ui.reader.ReaderEvents.UpdatePostsEnded
import org.wordpress.android.ui.reader.repository.ReaderRepositoryCommunication.Failure
import org.wordpress.android.ui.reader.repository.ReaderRepositoryCommunication.Success
import org.wordpress.android.ui.reader.repository.ReaderRepositoryEvent.PostLikeEnded.PostLikeFailure
import org.wordpress.android.ui.reader.repository.ReaderRepositoryEvent.PostLikeEnded.PostLikeSuccess
import org.wordpress.android.ui.reader.repository.ReaderRepositoryEvent.PostLikeEnded.PostLikeUnChanged
import org.wordpress.android.ui.reader.repository.usecases.FetchPostsForTagUseCase
import org.wordpress.android.ui.reader.repository.usecases.GetNumPostsForTagUseCase
import org.wordpress.android.ui.reader.repository.usecases.GetPostsForTagUseCase
import org.wordpress.android.ui.reader.repository.usecases.GetPostsForTagWithCountUseCase
import org.wordpress.android.ui.reader.repository.usecases.PostLikeActionUseCase
import org.wordpress.android.ui.reader.repository.usecases.ShouldAutoUpdateTagUseCase
import org.wordpress.android.viewmodel.Event
import org.wordpress.android.viewmodel.ReactiveMutableLiveData
Expand All @@ -29,16 +35,18 @@ class ReaderPostRepository(
private val shouldAutoUpdateTagUseCase: ShouldAutoUpdateTagUseCase,
private val getPostsForTagWithCountUseCase: GetPostsForTagWithCountUseCase,
private val fetchPostsForTagUseCase: FetchPostsForTagUseCase,
private val readerUpdatePostsEndedHandler: ReaderUpdatePostsEndedHandler
private val readerUpdatePostsEndedHandler: ReaderUpdatePostsEndedHandler,
private val postLikeActionUseCase: PostLikeActionUseCase
) : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = bgDispatcher

private var isStarted = false
private var isDirty = false

private val _postsForTag = ReactiveMutableLiveData<ReaderPostList>(
onActive = { onActivePostsForTag() }, onInactive = { onInactivePostsForTag() })
val postsForTag: LiveData<ReaderPostList> = _postsForTag
private val _posts = ReactiveMutableLiveData<ReaderPostList>(
onActive = { onActivePosts() }, onInactive = { onInactivePosts() })
val posts: LiveData<ReaderPostList> = _posts

private val _communicationChannel = MutableLiveData<Event<ReaderRepositoryCommunication>>()
val communicationChannel: LiveData<Event<ReaderRepositoryCommunication>> = _communicationChannel
Expand All @@ -47,11 +55,13 @@ class ReaderPostRepository(
if (isStarted) return

isStarted = true
readerUpdatePostsEndedHandler.start(readerTag,
readerUpdatePostsEndedHandler.start(
readerTag,
ReaderUpdatePostsEndedHandler.setUpdatePostsEndedListeners(
this::onNewPosts,
this::onChangedPosts, this::onUnchanged, this::onFailed
))
)
)
}

fun stop() {
Expand All @@ -66,6 +76,24 @@ class ReaderPostRepository(
return readerTag
}

// todo: annmarie - Possibly implement a "LikeManager" that will encapsulate all the "UseCase".
fun performLikeAction(post: ReaderPost, isAskingToLike: Boolean, wpComUserId: Long) {
launch {
when (val event = postLikeActionUseCase.perform(post, isAskingToLike, wpComUserId)) {
is PostLikeSuccess -> {
reloadPosts()
}
is PostLikeFailure -> {
_communicationChannel.postValue(Event(Failure(event)))
reloadPosts()
}
is PostLikeUnChanged -> {
// Unused
}
}
}
}

private fun onNewPosts(event: UpdatePostsEnded) {
reloadPosts()
}
Expand All @@ -79,26 +107,27 @@ class ReaderPostRepository(

private fun onFailed(event: UpdatePostsEnded) {
_communicationChannel.postValue(
Event(ReaderRepositoryCommunication.Error.RemoteRequestFailure))
Event(ReaderRepositoryCommunication.Error.RemoteRequestFailure)
)
}

private fun onActivePostsForTag() {
private fun onActivePosts() {
loadPosts()
}

private fun onInactivePostsForTag() {
private fun onInactivePosts() {
}

private fun loadPosts() {
launch {
val existsInMemory = postsForTag.value?.let {
val existsInMemory = posts.value?.let {
!it.isEmpty()
} ?: false
val refresh = shouldAutoUpdateTagUseCase.get(readerTag)

if (!existsInMemory) {
val result = getPostsForTagUseCase.get(readerTag)
_postsForTag.postValue(result)
_posts.postValue(result)
}

if (refresh) {
Expand All @@ -111,7 +140,7 @@ class ReaderPostRepository(
private fun reloadPosts() {
launch {
val result = getPostsForTagUseCase.get(readerTag)
_postsForTag.postValue(result)
_posts.postValue(result)
}
}

Expand All @@ -123,7 +152,8 @@ class ReaderPostRepository(
private val shouldAutoUpdateTagUseCase: ShouldAutoUpdateTagUseCase,
private val getPostsForTagWithCountUseCase: GetPostsForTagWithCountUseCase,
private val fetchPostsForTagUseCase: FetchPostsForTagUseCase,
private val readerUpdatePostsEndedHandler: ReaderUpdatePostsEndedHandler
private val readerUpdatePostsEndedHandler: ReaderUpdatePostsEndedHandler,
private val postLikeActionUseCase: PostLikeActionUseCase
) {
fun create(readerTag: ReaderTag): ReaderPostRepository {
return ReaderPostRepository(
Expand All @@ -134,7 +164,9 @@ class ReaderPostRepository(
shouldAutoUpdateTagUseCase,
getPostsForTagWithCountUseCase,
fetchPostsForTagUseCase,
readerUpdatePostsEndedHandler
readerUpdatePostsEndedHandler,
postLikeActionUseCase

)
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.wordpress.android.ui.reader.repository

sealed class ReaderRepositoryEvent {
sealed class PostLikeEnded(
val postId: Long,
val blogId: Long,
val isAskingToLike: Boolean,
val wpComUserId: Long
) : ReaderRepositoryEvent() {
class PostLikeSuccess(postId: Long, blogId: Long, isAskingToLike: Boolean, wpComUserId: Long) :
PostLikeEnded(postId, blogId, isAskingToLike, wpComUserId)
class PostLikeFailure(postId: Long, blogId: Long, isAskingToLike: Boolean, wpComUserId: Long) :
PostLikeEnded(postId, blogId, isAskingToLike, wpComUserId)
class PostLikeUnChanged(postId: Long, blogId: Long, isAskingToLike: Boolean, wpComUserId: Long) :
PostLikeEnded(postId, blogId, isAskingToLike, wpComUserId)
}
}

sealed class ReaderRepositoryCommunication {
object Success : ReaderRepositoryCommunication()
class Failure(val event: ReaderRepositoryEvent) : ReaderRepositoryCommunication()
sealed class Error : ReaderRepositoryCommunication() {
object NetworkUnavailable : Error()
object RemoteRequestFailure : Error()
class ReaderRepositoryException(val exception: Exception) : Error()
}

override fun toString(): String {
return "${this.javaClass.simpleName})"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ import javax.inject.Named
class GetPostsForTagUseCase @Inject constructor(
@Named(IO_THREAD) private val ioDispatcher: CoroutineDispatcher
) : ReaderRepositoryDispatchingUseCase(ioDispatcher) {
suspend fun get(readerTag: ReaderTag): ReaderPostList =
suspend fun get(
readerTag: ReaderTag,
maxRows: Int = 0,
excludeTextColumns: Boolean = true
): ReaderPostList =
withContext(coroutineContext) {
ReaderPostTable.getPostsWithTag(
readerTag,
MAX_ROWS,
EXCLUDE_TEXT_COLUMN
maxRows,
excludeTextColumns
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import javax.inject.Named
class GetPostsForTagWithCountUseCase @Inject constructor(
@Named(IO_THREAD) private val ioDispatcher: CoroutineDispatcher
) : ReaderRepositoryDispatchingUseCase(ioDispatcher) {
suspend fun get(readerTag: ReaderTag): Pair<ReaderPostList, Int> =
suspend fun get(
readerTag: ReaderTag,
maxRows: Int = 0,
excludeTextColumns: Boolean = true
): Pair<ReaderPostList, Int> =
withContext(coroutineContext) {
val postsForTagFromLocalDeferred = async {
ReaderPostTable.getPostsWithTag(
readerTag,
MAX_ROWS,
EXCLUDE_TEXT_COLUMN
maxRows,
excludeTextColumns
)
}

Expand Down
Loading

0 comments on commit d0707b1

Please sign in to comment.