Skip to content

Commit

Permalink
Merge pull request #10355 from wordpress-mobile/refactor-get-upload-a…
Browse files Browse the repository at this point in the history
…ction-methods

Refactor get upload action methods
  • Loading branch information
shiki authored Aug 8, 2019
2 parents 280e45a + ae42221 commit 1a3fb61
Show file tree
Hide file tree
Showing 17 changed files with 150 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import org.wordpress.android.ui.prefs.SiteSettingsFragment;
import org.wordpress.android.ui.reader.ReaderPostListFragment;
import org.wordpress.android.ui.reader.ReaderPostPagerActivity;
import org.wordpress.android.ui.uploads.UploadActionUseCase;
import org.wordpress.android.ui.uploads.UploadUtils;
import org.wordpress.android.util.AniUtils;
import org.wordpress.android.util.AppLog;
Expand Down Expand Up @@ -169,6 +170,7 @@ public class WPMainActivity extends AppCompatActivity implements
@Inject ShortcutUtils mShortcutUtils;
@Inject NewsManager mNewsManager;
@Inject QuickStartStore mQuickStartStore;
@Inject UploadActionUseCase mUploadActionUseCase;

/*
* fragments implement this if their contents can be scrolled, called when user
Expand Down Expand Up @@ -756,6 +758,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
data,
post,
site,
mUploadActionUseCase.getUploadAction(post),
new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import org.wordpress.android.ui.posts.PostListViewLayoutType.STANDARD
import org.wordpress.android.ui.posts.PostListViewLayoutTypeMenuUiState.CompactViewLayoutTypeMenuUiState
import org.wordpress.android.ui.posts.PostListViewLayoutTypeMenuUiState.StandardViewLayoutTypeMenuUiState
import org.wordpress.android.ui.prefs.AppPrefsWrapper
import org.wordpress.android.ui.uploads.UploadActionUseCase
import org.wordpress.android.ui.uploads.UploadStarter
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.NetworkUtilsWrapper
Expand Down Expand Up @@ -66,6 +67,7 @@ class PostListMainViewModel @Inject constructor(
private val dispatcher: Dispatcher,
private val postStore: PostStore,
private val accountStore: AccountStore,
uploadActionUseCase: UploadActionUseCase,
uploadStore: UploadStore,
mediaStore: MediaStore,
private val networkUtilsWrapper: NetworkUtilsWrapper,
Expand Down Expand Up @@ -130,7 +132,10 @@ class PostListMainViewModel @Inject constructor(
private val _searchQuery = MutableLiveData<String>()
val searchQuery: LiveData<String> = _searchQuery

private val uploadStatusTracker = PostListUploadStatusTracker(uploadStore = uploadStore)
private val uploadStatusTracker = PostListUploadStatusTracker(
uploadStore = uploadStore,
uploadActionUseCase = uploadActionUseCase
)
private val featuredImageTracker = PostListFeaturedImageTracker(dispatcher = dispatcher, mediaStore = mediaStore)

private val postFetcher by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@ package org.wordpress.android.ui.posts
import org.wordpress.android.fluxc.model.PostModel
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.store.UploadStore
import org.wordpress.android.ui.uploads.UploadActionUseCase
import org.wordpress.android.ui.uploads.UploadActionUseCase.UploadAction.DO_NOTHING
import org.wordpress.android.ui.uploads.UploadActionUseCase.UploadAction.REMOTE_AUTO_SAVE
import org.wordpress.android.ui.uploads.UploadActionUseCase.UploadAction.UPLOAD
import org.wordpress.android.ui.uploads.UploadActionUseCase.UploadAction.UPLOAD_AS_DRAFT
import org.wordpress.android.ui.uploads.UploadService
import org.wordpress.android.ui.uploads.UploadStarter
import org.wordpress.android.ui.uploads.UploadUtils.PostUploadAction.DO_NOTHING
import org.wordpress.android.ui.uploads.UploadUtils.PostUploadAction.REMOTE_AUTO_SAVE
import org.wordpress.android.ui.uploads.UploadUtils.PostUploadAction.UPLOAD
import org.wordpress.android.ui.uploads.UploadUtils.PostUploadAction.UPLOAD_AS_DRAFT
import org.wordpress.android.viewmodel.posts.PostListItemUploadStatus

/**
* This is a temporary class to make the PostListViewModel more manageable. Please feel free to refactor it any way
* you see fit.
*/
class PostListUploadStatusTracker(private val uploadStore: UploadStore) {
class PostListUploadStatusTracker(
private val uploadStore: UploadStore,
private val uploadActionUseCase: UploadActionUseCase
) {
private val uploadStatusMap = HashMap<Int, PostListItemUploadStatus>()

fun getUploadStatus(post: PostModel, uploadStarter: UploadStarter, siteModel: SiteModel): PostListItemUploadStatus {
fun getUploadStatus(post: PostModel, siteModel: SiteModel): PostListItemUploadStatus {
uploadStatusMap[post.id]?.let { return it }
val uploadError = uploadStore.getUploadErrorForPost(post)
val isUploadingOrQueued = UploadService.isPostUploadingOrQueued(post)
Expand All @@ -32,14 +35,14 @@ class PostListUploadStatusTracker(private val uploadStore: UploadStore) {
isUploadFailed = uploadStore.isFailedPost(post),
hasInProgressMediaUpload = hasInProgressMediaUpload,
hasPendingMediaUpload = UploadService.hasPendingMediaUploadsForPost(post),
isEligibleForAutoUpload = isEligibleForAutoUpload(uploadStarter, siteModel, post)
isEligibleForAutoUpload = isEligibleForAutoUpload(siteModel, post)
)
uploadStatusMap[post.id] = newStatus
return newStatus
}

private fun isEligibleForAutoUpload(uploadStarter: UploadStarter, site: SiteModel, post: PostModel): Boolean {
return when (uploadStarter.getAutoUploadAction(post, site)) {
private fun isEligibleForAutoUpload(site: SiteModel, post: PostModel): Boolean {
return when (uploadActionUseCase.getAutoUploadAction(post, site)) {
UPLOAD -> true
UPLOAD_AS_DRAFT, REMOTE_AUTO_SAVE, DO_NOTHING -> false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.wordpress.android.fluxc.Dispatcher
import org.wordpress.android.fluxc.model.MediaModel
import org.wordpress.android.fluxc.model.PostModel
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.ui.uploads.UploadActionUseCase
import org.wordpress.android.ui.uploads.UploadService
import org.wordpress.android.ui.uploads.UploadUtils

Expand Down Expand Up @@ -45,15 +46,21 @@ sealed class PostUploadAction {
class CancelPostAndMediaUpload(val post: PostModel) : PostUploadAction()
}

fun handleUploadAction(action: PostUploadAction, activity: Activity, snackbarAttachView: View) {
fun handleUploadAction(
action: PostUploadAction,
activity: Activity,
snackbarAttachView: View,
uploadActionUseCase: UploadActionUseCase
) {
when (action) {
is PostUploadAction.EditPostResult -> {
UploadUtils.handleEditPostResultSnackbars(
activity,
snackbarAttachView,
action.data,
action.post,
action.site
action.site,
uploadActionUseCase.getUploadAction(action.post)
) {
action.publishAction()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.wordpress.android.ui.posts.BasicFragmentDialog.BasicDialogOnDismissBy
import org.wordpress.android.ui.posts.BasicFragmentDialog.BasicDialogPositiveClickInterface
import org.wordpress.android.ui.posts.PostListType.SEARCH
import org.wordpress.android.ui.posts.adapters.AuthorSelectionAdapter
import org.wordpress.android.ui.uploads.UploadActionUseCase
import org.wordpress.android.ui.utils.UiHelpers
import org.wordpress.android.ui.utils.UiString
import org.wordpress.android.util.AppLog
Expand All @@ -58,6 +59,7 @@ class PostsListActivity : AppCompatActivity(),
@Inject internal lateinit var remotePreviewLogicHelper: RemotePreviewLogicHelper
@Inject internal lateinit var previewStateHelper: PreviewStateHelper
@Inject internal lateinit var progressDialogHelper: ProgressDialogHelper
@Inject internal lateinit var uploadActionUseCase: UploadActionUseCase

private lateinit var site: SiteModel
private lateinit var viewModel: PostListMainViewModel
Expand Down Expand Up @@ -256,7 +258,8 @@ class PostsListActivity : AppCompatActivity(),
handleUploadAction(
uploadAction,
this@PostsListActivity,
findViewById(R.id.coordinator)
findViewById(R.id.coordinator),
uploadActionUseCase
)
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package org.wordpress.android.ui.posts

import android.app.Activity
import dagger.Reusable
import org.wordpress.android.fluxc.model.PostModel
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.ui.ActivityLauncherWrapper
import org.wordpress.android.ui.WPWebViewUsageCategory
import org.wordpress.android.ui.uploads.UploadUtils
import org.wordpress.android.ui.uploads.UploadUtils.PostUploadAction
import org.wordpress.android.ui.uploads.UploadUtils.PostUploadAction.REMOTE_AUTO_SAVE
import org.wordpress.android.ui.uploads.UploadUtils.PostUploadAction.UPLOAD
import org.wordpress.android.ui.uploads.UploadUtils.PostUploadAction.UPLOAD_AS_DRAFT
import org.wordpress.android.ui.uploads.UploadActionUseCase
import org.wordpress.android.ui.uploads.UploadActionUseCase.UploadAction
import org.wordpress.android.ui.uploads.UploadActionUseCase.UploadAction.REMOTE_AUTO_SAVE
import org.wordpress.android.ui.uploads.UploadActionUseCase.UploadAction.UPLOAD
import org.wordpress.android.ui.uploads.UploadActionUseCase.UploadAction.UPLOAD_AS_DRAFT
import org.wordpress.android.util.NetworkUtilsWrapper
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
@Reusable
class RemotePreviewLogicHelper @Inject constructor(
private val networkUtilsWrapper: NetworkUtilsWrapper,
private val activityLauncherWrapper: ActivityLauncherWrapper,
private val postUtilsWrapper: PostUtilsWrapper
private val postUtilsWrapper: PostUtilsWrapper,
private val uploadActionUseCase: UploadActionUseCase
) {
enum class RemotePreviewType {
NOT_A_REMOTE_PREVIEW,
Expand Down Expand Up @@ -68,7 +69,7 @@ class RemotePreviewLogicHelper @Inject constructor(
// (eg. during and editing session)
val updatedPost = helperFunctions.updatePostIfNeeded() ?: post

val uploadAction = UploadUtils.getPostUploadAction(updatedPost)
val uploadAction = uploadActionUseCase.getUploadAction(updatedPost)

return when {
shouldUpload(updatedPost, uploadAction) -> {
Expand Down Expand Up @@ -124,11 +125,11 @@ class RemotePreviewLogicHelper @Inject constructor(
}
}

private fun shouldUpload(post: PostModel, action: PostUploadAction): Boolean {
private fun shouldUpload(post: PostModel, action: UploadAction): Boolean {
return (post.isLocallyChanged || post.isLocalDraft) && (action == UPLOAD_AS_DRAFT || action == UPLOAD)
}

private fun shouldRemoteAutoSave(post: PostModel, action: PostUploadAction): Boolean {
private fun shouldRemoteAutoSave(post: PostModel, action: UploadAction): Boolean {
return post.isLocallyChanged && action == REMOTE_AUTO_SAVE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class PostUploadHandler implements UploadHandler<PostModel> {
@Inject PostStore mPostStore;
@Inject MediaStore mMediaStore;
@Inject UiHelpers mUiHelpers;
@Inject UploadActionUseCase mUploadActionUseCase;

PostUploadHandler(PostUploadNotifier postUploadNotifier) {
((WordPress) WordPress.getContext().getApplicationContext()).component().inject(this);
Expand Down Expand Up @@ -266,7 +267,7 @@ protected Boolean doInBackground(PostModel... posts) {

RemotePostPayload payload = new RemotePostPayload(mPost, mSite);

switch (UploadUtils.getPostUploadAction(mPost)) {
switch (mUploadActionUseCase.getUploadAction(mPost)) {
case UPLOAD:
AppLog.d(T.POSTS,
"Invoking newPushPostAction. Post: " + mPost.getTitle() + " status: " + mPost.getStatus());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.wordpress.android.ui.uploads

import dagger.Reusable
import org.wordpress.android.fluxc.model.PostModel
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.store.UploadStore
import org.wordpress.android.ui.posts.PostUtilsWrapper
import org.wordpress.android.ui.uploads.UploadActionUseCase.UploadAction.DO_NOTHING
import org.wordpress.android.ui.uploads.UploadActionUseCase.UploadAction.REMOTE_AUTO_SAVE
import org.wordpress.android.ui.uploads.UploadActionUseCase.UploadAction.UPLOAD
import org.wordpress.android.ui.uploads.UploadActionUseCase.UploadAction.UPLOAD_AS_DRAFT
import org.wordpress.android.util.DateTimeUtils
import java.util.Date
import javax.inject.Inject

private const val MAXIMUM_AUTO_UPLOAD_RETRIES = 10
private const val TWO_DAYS_IN_MILLIS = 1000 * 60 * 60 * 24 * 2

@Reusable
class UploadActionUseCase @Inject constructor(
private val uploadStore: UploadStore,
private val postUtilsWrapper: PostUtilsWrapper,
private val uploadServiceFacade: UploadServiceFacade
) {
enum class UploadAction {
REMOTE_AUTO_SAVE, UPLOAD_AS_DRAFT, UPLOAD, DO_NOTHING
}

fun getAutoUploadAction(post: PostModel, site: SiteModel): UploadAction {
val twoDaysAgoTimestamp = Date().time - TWO_DAYS_IN_MILLIS
// Don't auto-upload/save changes which are older than 2 days
if (DateTimeUtils.timestampFromIso8601Millis(post.dateLocallyChanged) < twoDaysAgoTimestamp) {
return DO_NOTHING
}

// Do not auto-upload empty post
if (!postUtilsWrapper.isPublishable(post)) {
return DO_NOTHING
}

// Do not auto-upload post which is in conflict with remote
if (postUtilsWrapper.isPostInConflictWithRemote(post)) {
return DO_NOTHING
}

// Do not auto-upload post which we already tried to upload certain number of times
if (uploadStore.getNumberOfPostUploadErrorsOrCancellations(post) >= MAXIMUM_AUTO_UPLOAD_RETRIES) {
return DO_NOTHING
}

// Do not auto-upload post which is currently being uploaded
if (uploadServiceFacade.isPostUploadingOrQueued(post)) {
return DO_NOTHING
}

val action = getUploadAction(post)
// Don't remoteAutoSave changes which were already remoteAutoSaved or when on a self-hosted site
if (action == REMOTE_AUTO_SAVE &&
(UploadUtils.postLocalChangesAlreadyRemoteAutoSaved(post) || !site.isUsingWpComRestApi)) {
return DO_NOTHING
}

return action
}

fun getUploadAction(post: PostModel): UploadAction {
return when {
post.changesConfirmedContentHashcode == post.contentHashcode() ->
// We are sure we can push the post as the user has explicitly confirmed the changes
UPLOAD
post.isLocalDraft ->
// Local draft can always be uploaded as DRAFT as it doesn't exist on the server yet
UPLOAD_AS_DRAFT
else -> REMOTE_AUTO_SAVE
}
}
}
Loading

0 comments on commit 1a3fb61

Please sign in to comment.