Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/12039 reader table events #12502

Merged
merged 4 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import org.greenrobot.eventbus.EventBus;
import org.wordpress.android.WordPress;
import org.wordpress.android.models.ReaderPostList;
import org.wordpress.android.models.ReaderTagList;
import org.wordpress.android.ui.reader.repository.ReaderRepositoryEvent.ReaderPostTableActionEnded;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.AppLog.T;

Expand Down Expand Up @@ -270,6 +272,9 @@ private static void purge() {
}
}
db.setTransactionSuccessful();
if (numPostsDeleted > 0) {
EventBus.getDefault().post(ReaderPostTableActionEnded.INSTANCE);
}
} finally {
db.endTransaction();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import androidx.annotation.NonNull;

import org.greenrobot.eventbus.EventBus;
import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.models.ReaderCardType;
Expand All @@ -20,6 +21,7 @@
import org.wordpress.android.ui.reader.actions.ReaderActions;
import org.wordpress.android.ui.reader.models.ReaderBlogIdPostId;
import org.wordpress.android.ui.reader.models.ReaderBlogIdPostIdList;
import org.wordpress.android.ui.reader.repository.ReaderRepositoryEvent.ReaderPostTableActionEnded;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.SqlUtils;

Expand Down Expand Up @@ -233,6 +235,9 @@ public static int purgeUnbookmarkedPostsWithBookmarkTag() {
.delete("tbl_posts", "tag_name=? AND tag_type=? AND is_bookmarked=0", args);
}
}
if (numDeleted > 0) {
EventBus.getDefault().post(ReaderPostTableActionEnded.INSTANCE);
}
return numDeleted;
}

Expand Down Expand Up @@ -507,6 +512,7 @@ private static void update(long blogId, long postId, ContentValues values) {
values,
"blog_id=? AND post_id=?",
args);
EventBus.getDefault().post(ReaderPostTableActionEnded.INSTANCE);
}


Expand All @@ -526,10 +532,15 @@ public static int deletePostsWithTag(final ReaderTag tag) {
}

String[] args = {tag.getTagSlug(), Integer.toString(tag.tagType.toInt())};
return ReaderDatabase.getWritableDb().delete(
int rowsDeleted = ReaderDatabase.getWritableDb().delete(
Copy link
Contributor Author

@zwarm zwarm Jul 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method was directly returning the int value. I changed to keep the value local and will only post the new event if rows have actually been deleted.

"tbl_posts",
"tag_name=? AND tag_type=?",
args);

if (rowsDeleted > 0) {
EventBus.getDefault().post(ReaderPostTableActionEnded.INSTANCE);
}
return rowsDeleted;
}

public static int removeTagsFromPost(long blogId, long postId, final ReaderTagType tagType) {
Expand All @@ -538,20 +549,30 @@ public static int removeTagsFromPost(long blogId, long postId, final ReaderTagTy
}

String[] args = {Integer.toString(tagType.toInt()), Long.toString(blogId), Long.toString(postId)};
return ReaderDatabase.getWritableDb().delete(
int rowsDeleted = ReaderDatabase.getWritableDb().delete(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another instance of the method directly returning the int value. I changed to keep the value local and will only post the new event is rows have actually been deleted.

"tbl_posts",
"tag_type=? AND blog_id=? AND post_id=?",
args);

if (rowsDeleted > 0) {
EventBus.getDefault().post(ReaderPostTableActionEnded.INSTANCE);
}
return rowsDeleted;
}

public static int deletePostsInBlog(long blogId) {
String[] args = {Long.toString(blogId)};
return ReaderDatabase.getWritableDb().delete("tbl_posts", "blog_id = ?", args);
int rowsDeleted = ReaderDatabase.getWritableDb().delete("tbl_posts", "blog_id = ?", args);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another instance of the method directly returning the int value. I changed to keep the value local and will only post the new event is rows have actually been deleted.

if (rowsDeleted > 0) {
EventBus.getDefault().post(ReaderPostTableActionEnded.INSTANCE);
}
return rowsDeleted;
}

public static void deletePost(long blogId, long postId) {
String[] args = new String[]{Long.toString(blogId), Long.toString(postId)};
ReaderDatabase.getWritableDb().delete("tbl_posts", "blog_id=? AND post_id=?", args);
EventBus.getDefault().post(ReaderPostTableActionEnded.INSTANCE);
}

/*
Expand All @@ -568,6 +589,7 @@ public static void updateFollowedStatus() {
if (count > 0) {
AppLog.d(AppLog.T.READER, String.format(Locale.ENGLISH,
"reader post table > marked %d posts unfollowed", count));
EventBus.getDefault().post(ReaderPostTableActionEnded.INSTANCE);
}
} finally {
statement.close();
Expand Down Expand Up @@ -616,6 +638,7 @@ public static void removeGapMarkerForTag(final ReaderTag tag) {
String[] args = {tag.getTagSlug(), Integer.toString(tag.tagType.toInt())};
String sql = "UPDATE tbl_posts SET has_gap_marker=0 WHERE has_gap_marker!=0 AND tag_name=? AND tag_type=?";
ReaderDatabase.getWritableDb().execSQL(sql, args);
EventBus.getDefault().post(ReaderPostTableActionEnded.INSTANCE);
}

/*
Expand Down Expand Up @@ -656,6 +679,7 @@ public static void setGapMarkerForTag(long blogId, long postId, ReaderTag tag) {
String sql =
"UPDATE tbl_posts SET has_gap_marker=1 WHERE blog_id=? AND post_id=? AND tag_name=? AND tag_type=?";
ReaderDatabase.getWritableDb().execSQL(sql, args);
EventBus.getDefault().post(ReaderPostTableActionEnded.INSTANCE);
}

public static String getGapMarkerDateForTag(ReaderTag tag) {
Expand Down Expand Up @@ -709,6 +733,7 @@ public static void deletePostsBeforeGapMarkerForTag(ReaderTag tag) {
int numDeleted = ReaderDatabase.getWritableDb().delete("tbl_posts", where, args);
if (numDeleted > 0) {
AppLog.d(AppLog.T.READER, "removed " + numDeleted + " posts older than gap marker");
EventBus.getDefault().post(ReaderPostTableActionEnded.INSTANCE);
}
}

Expand Down Expand Up @@ -751,6 +776,7 @@ private static void setFollowStatusForPosts(long blogId, long feedId, boolean is
}

db.setTransactionSuccessful();
EventBus.getDefault().post(ReaderPostTableActionEnded.INSTANCE);
} finally {
db.endTransaction();
}
Expand Down Expand Up @@ -860,6 +886,7 @@ public static void addOrUpdatePosts(final ReaderTag tag, ReaderPostList posts) {
}

db.setTransactionSuccessful();
EventBus.getDefault().post(ReaderPostTableActionEnded.INSTANCE);
} finally {
db.endTransaction();
SqlUtils.closeStatement(stmtPosts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.lifecycle.MutableLiveData
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode.BACKGROUND
import org.wordpress.android.models.ReaderPost
import org.wordpress.android.models.ReaderPostList
import org.wordpress.android.models.ReaderTag
Expand All @@ -17,21 +19,25 @@ import org.wordpress.android.ui.reader.repository.ReaderRepositoryCommunication.
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.ReaderRepositoryEvent.ReaderPostTableActionEnded
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.util.EventBusWrapper
import org.wordpress.android.viewmodel.Event
import org.wordpress.android.viewmodel.ReactiveMutableLiveData
import java.util.concurrent.atomic.AtomicBoolean
import javax.inject.Inject
import javax.inject.Named
import kotlin.coroutines.CoroutineContext

class ReaderDiscoverRepository constructor(
private val bgDispatcher: CoroutineDispatcher,
private val eventBusWrapper: EventBusWrapper,
private val readerTag: ReaderTag,
private val getPostsForTagUseCase: GetPostsForTagUseCase,
private val getNumPostsForTagUseCase: GetNumPostsForTagUseCase,
Expand All @@ -45,6 +51,7 @@ class ReaderDiscoverRepository constructor(
get() = bgDispatcher

private var isStarted = false
private val isDirty = AtomicBoolean()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AtomicBoolean may be overkill, but considering there may be multiple event posts felt it was safer. Open to thoughts.


private val _discoverFeed = ReactiveMutableLiveData<ReaderPostList>(
onActive = { onActiveDiscoverFeed() }, onInactive = { onInactiveDiscoverFeed() })
Expand All @@ -57,6 +64,7 @@ class ReaderDiscoverRepository constructor(
if (isStarted) return

isStarted = true
eventBusWrapper.register(this)
readerUpdatePostsEndedHandler.start(readerTag,
ReaderUpdatePostsEndedHandler.setUpdatePostsEndedListeners(
this::onNewPosts,
Expand All @@ -65,6 +73,7 @@ class ReaderDiscoverRepository constructor(
}

fun stop() {
eventBusWrapper.unregister(this)
getPostsForTagUseCase.stop()
getNumPostsForTagUseCase.stop()
shouldAutoUpdateTagUseCase.stop()
Expand All @@ -73,7 +82,6 @@ class ReaderDiscoverRepository constructor(
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)) {
Expand Down Expand Up @@ -113,20 +121,20 @@ class ReaderDiscoverRepository constructor(
loadPosts()
}

private fun onInactiveDiscoverFeed() {
}
private fun onInactiveDiscoverFeed() {}

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

if (!existsInMemory) {
val result = getPostsForTagUseCase.get(readerTag)
_discoverFeed.postValue(result)
reloadPosts()
}

if (refresh) {
val response = fetchPostsForTagUseCase.fetch(readerTag)
if (response != Success) _communicationChannel.postValue(Event(response))
Expand All @@ -141,9 +149,21 @@ class ReaderDiscoverRepository constructor(
}
}

@Subscribe(threadMode = BACKGROUND)
@SuppressWarnings("unused")
fun onReaderPostTableAction(event: ReaderPostTableActionEnded) {
if (_discoverFeed.hasObservers()) {
isDirty.compareAndSet(true, false)
reloadPosts()
} else {
isDirty.compareAndSet(false, true)
}
}

class Factory
@Inject constructor(
@Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher,
private val eventBusWrapper: EventBusWrapper,
private val readerUtilsWrapper: ReaderUtilsWrapper,
private val getPostsForTagUseCase: GetPostsForTagUseCase,
private val getNumPostsForTagUseCase: GetNumPostsForTagUseCase,
Expand All @@ -159,6 +179,7 @@ class ReaderDiscoverRepository constructor(

return ReaderDiscoverRepository(
bgDispatcher,
eventBusWrapper,
tag,
getPostsForTagUseCase,
getNumPostsForTagUseCase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.lifecycle.MutableLiveData
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode.BACKGROUND
import org.wordpress.android.models.ReaderPost
import org.wordpress.android.models.ReaderPostList
import org.wordpress.android.models.ReaderTag
Expand All @@ -15,20 +17,24 @@ import org.wordpress.android.ui.reader.repository.ReaderRepositoryCommunication.
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.ReaderRepositoryEvent.ReaderPostTableActionEnded
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.util.EventBusWrapper
import org.wordpress.android.viewmodel.Event
import org.wordpress.android.viewmodel.ReactiveMutableLiveData
import java.util.concurrent.atomic.AtomicBoolean
import javax.inject.Inject
import javax.inject.Named
import kotlin.coroutines.CoroutineContext

class ReaderPostRepository(
private val bgDispatcher: CoroutineDispatcher,
private val eventBusWrapper: EventBusWrapper,
private val readerTag: ReaderTag,
private val getPostsForTagUseCase: GetPostsForTagUseCase,
private val getNumPostsForTagUseCase: GetNumPostsForTagUseCase,
Expand All @@ -42,7 +48,7 @@ class ReaderPostRepository(
get() = bgDispatcher

private var isStarted = false
private var isDirty = false
private val isDirty = AtomicBoolean()

private val _posts = ReactiveMutableLiveData<ReaderPostList>(
onActive = { onActivePosts() }, onInactive = { onInactivePosts() })
Expand All @@ -55,6 +61,7 @@ class ReaderPostRepository(
if (isStarted) return

isStarted = true
eventBusWrapper.register(this)
readerUpdatePostsEndedHandler.start(
readerTag,
ReaderUpdatePostsEndedHandler.setUpdatePostsEndedListeners(
Expand All @@ -65,6 +72,7 @@ class ReaderPostRepository(
}

fun stop() {
eventBusWrapper.unregister(this)
getPostsForTagUseCase.stop()
getNumPostsForTagUseCase.stop()
shouldAutoUpdateTagUseCase.stop()
Expand Down Expand Up @@ -123,16 +131,17 @@ class ReaderPostRepository(
val existsInMemory = posts.value?.let {
!it.isEmpty()
} ?: false
val refresh = shouldAutoUpdateTagUseCase.get(readerTag)
val refresh =
shouldAutoUpdateTagUseCase.get(readerTag) || isDirty.getAndSet(false)

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

if (refresh) {
val response = fetchPostsForTagUseCase.fetch(readerTag)
if (response != Success) _communicationChannel.postValue(Event(response))
reloadPosts()
}
}
}
Expand All @@ -144,9 +153,21 @@ class ReaderPostRepository(
}
}

@Subscribe(threadMode = BACKGROUND)
@SuppressWarnings("unused")
fun onReaderPostTableAction(event: ReaderPostTableActionEnded) {
if (_posts.hasObservers()) {
isDirty.compareAndSet(true, false)
reloadPosts()
} else {
isDirty.compareAndSet(false, true)
}
}

class Factory
@Inject constructor(
@Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher,
private val eventBusWrapper: EventBusWrapper,
private val getPostsForTagUseCase: GetPostsForTagUseCase,
private val getNumPostsForTagUseCase: GetNumPostsForTagUseCase,
private val shouldAutoUpdateTagUseCase: ShouldAutoUpdateTagUseCase,
Expand All @@ -158,6 +179,7 @@ class ReaderPostRepository(
fun create(readerTag: ReaderTag): ReaderPostRepository {
return ReaderPostRepository(
bgDispatcher,
eventBusWrapper,
readerTag,
getPostsForTagUseCase,
getNumPostsForTagUseCase,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.wordpress.android.ui.reader.repository

sealed class ReaderRepositoryEvent {
object ReaderPostTableActionEnded : ReaderRepositoryEvent()
sealed class PostLikeEnded(
val postId: Long,
val blogId: Long,
Expand Down