-
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.
Merge pull request #10355 from wordpress-mobile/refactor-get-upload-a…
…ction-methods Refactor get upload action methods
- Loading branch information
Showing
17 changed files
with
150 additions
and
109 deletions.
There are no files selected for viewing
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
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
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
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
77 changes: 77 additions & 0 deletions
77
WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadActionUseCase.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,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 | ||
} | ||
} | ||
} |
Oops, something went wrong.