-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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); | ||
} | ||
|
||
|
||
|
@@ -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( | ||
"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) { | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
||
/* | ||
|
@@ -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(); | ||
|
@@ -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); | ||
} | ||
|
||
/* | ||
|
@@ -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) { | ||
|
@@ -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); | ||
} | ||
} | ||
|
||
|
@@ -751,6 +776,7 @@ private static void setFollowStatusForPosts(long blogId, long feedId, boolean is | |
} | ||
|
||
db.setTransactionSuccessful(); | ||
EventBus.getDefault().post(ReaderPostTableActionEnded.INSTANCE); | ||
} finally { | ||
db.endTransaction(); | ||
} | ||
|
@@ -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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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, | ||
|
@@ -45,6 +51,7 @@ class ReaderDiscoverRepository constructor( | |
get() = bgDispatcher | ||
|
||
private var isStarted = false | ||
private val isDirty = AtomicBoolean() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() }) | ||
|
@@ -57,6 +64,7 @@ class ReaderDiscoverRepository constructor( | |
if (isStarted) return | ||
|
||
isStarted = true | ||
eventBusWrapper.register(this) | ||
readerUpdatePostsEndedHandler.start(readerTag, | ||
ReaderUpdatePostsEndedHandler.setUpdatePostsEndedListeners( | ||
this::onNewPosts, | ||
|
@@ -65,6 +73,7 @@ class ReaderDiscoverRepository constructor( | |
} | ||
|
||
fun stop() { | ||
eventBusWrapper.unregister(this) | ||
getPostsForTagUseCase.stop() | ||
getNumPostsForTagUseCase.stop() | ||
shouldAutoUpdateTagUseCase.stop() | ||
|
@@ -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)) { | ||
|
@@ -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)) | ||
|
@@ -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, | ||
|
@@ -159,6 +179,7 @@ class ReaderDiscoverRepository constructor( | |
|
||
return ReaderDiscoverRepository( | ||
bgDispatcher, | ||
eventBusWrapper, | ||
tag, | ||
getPostsForTagUseCase, | ||
getNumPostsForTagUseCase, | ||
|
There was a problem hiding this comment.
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.