Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pantstamp committed Feb 20, 2024
1 parent e1800ed commit 968208f
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -392,6 +393,8 @@ enum RestartEditorOptions {

private boolean mHtmlModeMenuStateOn = false;

private LinearLayout mUpdatingPostArea;

@Inject Dispatcher mDispatcher;
@Inject AccountStore mAccountStore;
@Inject SiteStore mSiteStore;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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

}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down Expand Up @@ -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)

}
17 changes: 17 additions & 0 deletions WordPress/src/main/res/layout/new_edit_post_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@
tools:context=".ui.photopicker.PhotoPickerFragment"
tools:visibility="visible" />

<LinearLayout
android:id="@+id/updating"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:visibility="gone">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Updating post content"
tools:ignore="HardcodedText" />

</LinearLayout>

</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

0 comments on commit 968208f

Please sign in to comment.