-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
WordPress/src/main/java/org/wordpress/android/modules/PostModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
WordPress/src/main/java/org/wordpress/android/ui/posts/IPostFreshnessChecker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
WordPress/src/main/java/org/wordpress/android/ui/posts/PostFreshnessCheckerImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters