From 968208fafb61813bb40c644689fe21685b8d2cd5 Mon Sep 17 00:00:00 2001 From: Pantelis Stampoulis Date: Tue, 20 Feb 2024 16:28:02 +0200 Subject: [PATCH] wip --- .../wordpress/android/modules/PostModule.kt | 17 ++++++++++ .../android/ui/posts/EditPostActivity.java | 13 +++++--- .../android/ui/posts/IPostFreshnessChecker.kt | 18 +++++++++++ .../ui/posts/PostFreshnessCheckerImpl.kt | 32 +++++++++++++++++++ .../android/ui/posts/PostUtilsWrapper.kt | 10 +++++- .../res/layout/new_edit_post_activity.xml | 17 ++++++++++ 6 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 WordPress/src/main/java/org/wordpress/android/modules/PostModule.kt create mode 100644 WordPress/src/main/java/org/wordpress/android/ui/posts/IPostFreshnessChecker.kt create mode 100644 WordPress/src/main/java/org/wordpress/android/ui/posts/PostFreshnessCheckerImpl.kt diff --git a/WordPress/src/main/java/org/wordpress/android/modules/PostModule.kt b/WordPress/src/main/java/org/wordpress/android/modules/PostModule.kt new file mode 100644 index 000000000000..b92bd2f2d0ed --- /dev/null +++ b/WordPress/src/main/java/org/wordpress/android/modules/PostModule.kt @@ -0,0 +1,17 @@ +package org.wordpress.android.modules + +import dagger.Binds +import dagger.Module +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import org.wordpress.android.ui.posts.IPostFreshnessChecker +import org.wordpress.android.ui.posts.PostFreshnessCheckerImpl + +@InstallIn(SingletonComponent::class) +@Module +abstract class PostModule { + + @Binds + abstract fun bindPostFreshnessChecker(impl: PostFreshnessCheckerImpl): IPostFreshnessChecker + +} diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java index 95f985cd22c3..766dc1c10fbc 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java @@ -19,6 +19,7 @@ import android.view.ViewGroup; import android.webkit.MimeTypeMap; import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.Toast; import androidx.activity.OnBackPressedCallback; @@ -392,6 +393,8 @@ enum RestartEditorOptions { private boolean mHtmlModeMenuStateOn = false; + private LinearLayout mUpdatingPostArea; + @Inject Dispatcher mDispatcher; @Inject AccountStore mAccountStore; @Inject SiteStore mSiteStore; @@ -630,7 +633,6 @@ public void handleOnBackPressed() { mToolbar = findViewById(R.id.toolbar_main); setSupportActionBar(mToolbar); - final ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setDisplayHomeAsUpEnabled(false); @@ -827,12 +829,13 @@ public void handleOnBackPressed() { customizeToolbar(); // only for testing - if (mEditPostRepository.hasPost()) { + mUpdatingPostArea = findViewById(R.id.updating); + if (mEditPostRepository.hasPost() && mPostUtils.shouldRefreshPost(mEditPostRepository.getPost())) { + showOverlay(false); + mUpdatingPostArea.setVisibility(View.VISIBLE); RemotePostPayload payload = new RemotePostPayload(mEditPostRepository.getEditablePost(), mSite); mDispatcher.dispatch(PostActionBuilder.newFetchPostAction(payload)); } - - } private void customizeToolbar() { @@ -3756,6 +3759,8 @@ public void onPostChanged(OnPostChanged event) { String dateCreated = Objects.requireNonNull(mEditPostRepository.getPost()).getDateCreated(); Log.d("mytest","mEditPostRepository post getDbTimestamp = " + mEditPostRepository.getPost().getDbTimestamp()); refreshEditorContent(); + mUpdatingPostArea.setVisibility(View.GONE); + hideOverlay(); } private boolean isRemotePreviewingFromEditor() { diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/IPostFreshnessChecker.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/IPostFreshnessChecker.kt new file mode 100644 index 000000000000..e252643c3dca --- /dev/null +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/IPostFreshnessChecker.kt @@ -0,0 +1,18 @@ +package org.wordpress.android.ui.posts + +import org.wordpress.android.fluxc.model.PostImmutableModel +import org.wordpress.android.fluxc.model.SiteModel + +/** + * This interface is implemented by a component that takes a post id as input and determines if this post + * is "fresh" or we need to refetch it from the backend. + */ +interface IPostFreshnessChecker { + + fun shouldRefreshPost(localPostId: Int): Boolean + + fun shouldRefreshPost(remotePostId: Long, site: SiteModel): Boolean + + fun shouldRefreshPost(post: PostImmutableModel): Boolean + +} diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostFreshnessCheckerImpl.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostFreshnessCheckerImpl.kt new file mode 100644 index 000000000000..c432b93e1e6d --- /dev/null +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostFreshnessCheckerImpl.kt @@ -0,0 +1,32 @@ +package org.wordpress.android.ui.posts + +import org.wordpress.android.fluxc.model.PostImmutableModel +import org.wordpress.android.fluxc.model.PostModel +import org.wordpress.android.fluxc.model.SiteModel +import org.wordpress.android.fluxc.store.PostStore +import java.util.concurrent.TimeUnit +import javax.inject.Inject + +class PostFreshnessCheckerImpl +@Inject constructor( + private val postStore: PostStore +) : IPostFreshnessChecker { + override fun shouldRefreshPost(localPostId: Int): Boolean { + val post = postStore.getPostByLocalPostId(localPostId) + return postNeedsRefresh(post) + } + + override fun shouldRefreshPost(remotePostId: Long, site: SiteModel): Boolean { + val post = postStore.getPostByRemotePostId(remotePostId, site) + return postNeedsRefresh(post) + } + + override fun shouldRefreshPost(post: PostImmutableModel): Boolean { + return postNeedsRefresh(post) + } + + private fun postNeedsRefresh(post: PostImmutableModel) : Boolean { + val cacheValidity = TimeUnit.SECONDS.toMillis(20) // For example, cache is valid for 20 seconds + return System.currentTimeMillis() - post.dbTimestamp > cacheValidity + } +} diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostUtilsWrapper.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostUtilsWrapper.kt index 8c622c138a56..cf7495459e96 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostUtilsWrapper.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostUtilsWrapper.kt @@ -16,7 +16,11 @@ import javax.inject.Inject * */ @Reusable -class PostUtilsWrapper @Inject constructor(private val dateProvider: DateProvider) { +class PostUtilsWrapper +@Inject constructor( + private val dateProvider: DateProvider, + private val postFreshnessChecker: IPostFreshnessChecker +) { fun isPublishable(post: PostImmutableModel) = PostUtils.isPublishable(post) fun isPostInConflictWithRemote(post: PostImmutableModel) = @@ -51,4 +55,8 @@ class PostUtilsWrapper @Inject constructor(private val dateProvider: DateProvide fun shouldPublishImmediatelyOptionBeAvailable(status: PostStatus?) = PostUtils.shouldPublishImmediatelyOptionBeAvailable(status) + + fun shouldRefreshPost(post: PostImmutableModel) : Boolean = + postFreshnessChecker.shouldRefreshPost(post) + } diff --git a/WordPress/src/main/res/layout/new_edit_post_activity.xml b/WordPress/src/main/res/layout/new_edit_post_activity.xml index b619e8b65803..76bc5e0007a3 100644 --- a/WordPress/src/main/res/layout/new_edit_post_activity.xml +++ b/WordPress/src/main/res/layout/new_edit_post_activity.xml @@ -65,6 +65,23 @@ tools:context=".ui.photopicker.PhotoPickerFragment" tools:visibility="visible" /> + + + + + +