From f6f93759b05bd0b1086cdeb7f3ec4042307ddcc4 Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Wed, 1 Nov 2023 13:42:21 +0200 Subject: [PATCH 01/15] Build: Update fluxc version to pr hash (#2886) This 'FluxC' PR hash updates the library to that branch version where the 'MediaModel' is updated to its new null proof version. FluxC PR: https://github.com/wordpress-mobile/ WordPress-FluxC-Android/pull/2886 This step is required in order to check that these 'FluxC' related changes work as expected for WPAndroid. --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 8a5408c9dc01..61f558252735 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,7 @@ ext { automatticTracksVersion = '3.3.0' gutenbergMobileVersion = 'v1.107.0' wordPressAztecVersion = 'v1.8.0' - wordPressFluxCVersion = 'trunk-7fd7840a9bd900fde8e32ad8bd5632ad8f05dff3' + wordPressFluxCVersion = '2886-e47338a4c9cc68b15d2a0618e4ef4d9e120bd34d' wordPressLoginVersion = 'trunk-9963d78096edf65f8704b803e5b93c08fc9174cd' wordPressPersistentEditTextVersion = '1.0.2' wordPressUtilsVersion = '3.10.0' From 1e8aa5a78b33b50211c970afe88df7433db4cfa0 Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Wed, 1 Nov 2023 15:11:29 +0200 Subject: [PATCH 02/15] Analysis: Resolve media model constructor deprecation warnings Warnings: - Kotlin: "'constructor MediaModel()' is deprecated. Deprecated in Java" - Java: "'MediaModel()' is deprecated" --- .../ui/media/MediaSettingsActivity.java | 28 ++++---- .../ui/posts/PostListFeaturedImageTracker.kt | 7 +- .../wordpress/android/util/FluxCUtils.java | 65 +++++++++++-------- .../viewmodel/pages/PageListViewModel.kt | 8 +-- .../insert/GifMediaInsertUseCaseTest.kt | 2 +- .../insert/StockMediaInsertUseCaseTest.kt | 6 +- .../loader/MediaLibraryDataSourceTest.kt | 3 +- .../ui/posts/FeaturedImageHelperTest.kt | 2 +- .../AddExistingMediaToPostUseCaseTest.kt | 8 +-- .../media/AppendMediaToEditorUseCaseTest.kt | 4 +- ...leanUpMediaToPostAssociationUseCaseTest.kt | 2 +- .../ui/posts/editor/media/EditorMediaTest.kt | 5 +- .../editor/media/GetMediaModelUseCaseTest.kt | 2 +- .../ReattachUploadingMediaUseCaseTest.kt | 4 +- .../editor/media/RemoveMediaUseCaseTest.kt | 2 +- .../RetryFailedMediaUploadUseCaseTest.kt | 2 +- .../media/UpdateMediaModelUseCaseTest.kt | 2 +- .../editor/media/UploadMediaUseCaseTest.kt | 2 +- .../ui/uploads/PostMediaHandlerTest.kt | 4 +- 19 files changed, 84 insertions(+), 74 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/media/MediaSettingsActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/media/MediaSettingsActivity.java index 97d1878eb7d3..15672b09cd48 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/media/MediaSettingsActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/media/MediaSettingsActivity.java @@ -409,21 +409,23 @@ private boolean loadMedia(MediaModel media) { return true; } + @NonNull private MediaModel getMediaModelFromEditorImageMetaData() { - MediaModel mediaModel = new MediaModel(); - mediaModel.setUrl(mEditorImageMetaData.getSrc()); - mediaModel.setTitle(mEditorImageMetaData.getTitle()); - mediaModel.setCaption(mEditorImageMetaData.getCaption()); - mediaModel.setAlt(mEditorImageMetaData.getAlt()); - if (!TextUtils.isEmpty(mEditorImageMetaData.getSrc())) { - mediaModel.setFileName( - mEditorImageMetaData.getSrc().substring(mEditorImageMetaData.getSrc().lastIndexOf("/") + 1)); + String source = mEditorImageMetaData.getSrc(); + String filename = null; + if (!TextUtils.isEmpty(source)) { + filename = source.substring(source.lastIndexOf("/") + 1); } - mediaModel.setFileExtension( - org.wordpress.android.fluxc.utils.MediaUtils.getExtension(mEditorImageMetaData.getSrc())); - mediaModel.setWidth(mEditorImageMetaData.getWidthInt()); - mediaModel.setHeight(mEditorImageMetaData.getHeightInt()); - return mediaModel; + return new MediaModel( + source, + filename, + org.wordpress.android.fluxc.utils.MediaUtils.getExtension(source), + mEditorImageMetaData.getTitle(), + mEditorImageMetaData.getCaption(), + mEditorImageMetaData.getAlt(), + mEditorImageMetaData.getWidthInt(), + mEditorImageMetaData.getHeightInt() + ); } @Override diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListFeaturedImageTracker.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListFeaturedImageTracker.kt index 669ff3a1e3f9..e04428ab59ad 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListFeaturedImageTracker.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListFeaturedImageTracker.kt @@ -37,9 +37,10 @@ class PostListFeaturedImageTracker(private val dispatcher: Dispatcher, private v } else null } // Media is not in the Store, we need to download it - val mediaToDownload = MediaModel() - mediaToDownload.mediaId = featuredImageId - mediaToDownload.localSiteId = site.id + val mediaToDownload = MediaModel( + site.id, + featuredImageId + ) val payload = MediaPayload(site, mediaToDownload) dispatcher.dispatch(MediaActionBuilder.newFetchMediaAction(payload)) return null diff --git a/WordPress/src/main/java/org/wordpress/android/util/FluxCUtils.java b/WordPress/src/main/java/org/wordpress/android/util/FluxCUtils.java index 621b4a7a4f90..b454187f5ed7 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/FluxCUtils.java +++ b/WordPress/src/main/java/org/wordpress/android/util/FluxCUtils.java @@ -9,6 +9,7 @@ import androidx.annotation.Nullable; import org.wordpress.android.fluxc.model.MediaModel; +import org.wordpress.android.fluxc.model.MediaModel.MediaUploadState; import org.wordpress.android.fluxc.store.AccountStore; import org.wordpress.android.fluxc.store.SiteStore; import org.wordpress.android.util.AppLog.T; @@ -40,23 +41,24 @@ public static MediaModel mediaModelFromMediaFile(MediaFile file) { return null; } - MediaModel mediaModel = new MediaModel(); - mediaModel.setFileName(file.getFileName()); - mediaModel.setFilePath(file.getFilePath()); - mediaModel.setFileExtension(org.wordpress.android.fluxc.utils.MediaUtils.getExtension(file.getFilePath())); - mediaModel.setMimeType(file.getMimeType()); - mediaModel.setThumbnailUrl(file.getThumbnailURL()); - mediaModel.setUrl(file.getFileURL()); - mediaModel.setTitle(file.getTitle()); - mediaModel.setDescription(file.getDescription()); - mediaModel.setCaption(file.getCaption()); - mediaModel.setAlt(file.getAlt()); - mediaModel.setMediaId(file.getMediaId() != null ? Long.valueOf(file.getMediaId()) : 0); - mediaModel.setId(file.getId()); - mediaModel.setUploadState(file.getUploadState()); - mediaModel.setLocalSiteId(Integer.valueOf(file.getBlogId())); - mediaModel.setVideoPressGuid(file.getVideoPressGuid()); - return mediaModel; + String filePath = file.getFilePath(); + return new MediaModel( + file.getId(), + Integer.parseInt(file.getBlogId()), + file.getMediaId() != null ? Long.parseLong(file.getMediaId()) : 0, + file.getFileURL(), + file.getThumbnailURL(), + file.getFileName(), + filePath, + org.wordpress.android.fluxc.utils.MediaUtils.getExtension(filePath), + file.getMimeType(), + file.getTitle(), + file.getCaption(), + file.getDescription(), + file.getAlt(), + file.getVideoPressGuid(), + MediaUploadState.fromString(file.getUploadState()) + ); } public static MediaFile mediaFileFromMediaModel(MediaModel media) { @@ -93,6 +95,7 @@ public static MediaFile mediaFileFromMediaModel(MediaModel media) { * * @return MediaModel or null in case of problems reading the URI */ + @Nullable public static MediaModel mediaModelFromLocalUri(@NonNull Context context, @NonNull Uri uri, @Nullable String mimeType, @@ -112,7 +115,6 @@ public static MediaModel mediaModelFromLocalUri(@NonNull Context context, return null; } - MediaModel media = mediaStore.instantiateMediaModel(); String filename = org.wordpress.android.fluxc.utils.MediaUtils.getFileName(path); if (filename == null) filename = ""; String fileExtension = org.wordpress.android.fluxc.utils.MediaUtils.getExtension(path); @@ -140,15 +142,22 @@ public static MediaModel mediaModelFromLocalUri(@NonNull Context context, filename += "." + fileExtension; } - media.setFileName(filename); - media.setTitle(title); - media.setFilePath(path); - media.setLocalSiteId(localSiteId); - media.setFileExtension(fileExtension); - media.setMimeType(mimeType); - media.setUploadState(MediaModel.MediaUploadState.QUEUED); - media.setUploadDate(DateTimeUtils.iso8601UTCFromTimestamp(System.currentTimeMillis() / 1000)); - - return media; + MediaModel media = new MediaModel( + localSiteId, + DateTimeUtils.iso8601UTCFromTimestamp(System.currentTimeMillis() / 1000), + filename, + path, + fileExtension, + mimeType, + title, + MediaModel.MediaUploadState.QUEUED + ); + MediaModel instantiatedMedia = mediaStore.instantiateMediaModel(media); + if (instantiatedMedia != null) { + return instantiatedMedia; + } else { + AppLog.w(T.MEDIA, "The media couldn't instantiate."); + return null; + } } } diff --git a/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt b/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt index ebf052a9aee4..bf415799ec48 100644 --- a/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt @@ -286,10 +286,10 @@ class PageListViewModel @Inject constructor( } // Media is not in the Store, we need to download it - val mediaToDownload = MediaModel() - mediaToDownload.mediaId = featuredImageId - mediaToDownload.localSiteId = pagesViewModel.site.id - + val mediaToDownload = MediaModel( + pagesViewModel.site.id, + featuredImageId + ) val payload = MediaPayload(pagesViewModel.site, mediaToDownload) dispatcher.dispatch(MediaActionBuilder.newFetchMediaAction(payload)) diff --git a/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/insert/GifMediaInsertUseCaseTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/insert/GifMediaInsertUseCaseTest.kt index 479606fca6cc..c1ccaca56581 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/insert/GifMediaInsertUseCaseTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/insert/GifMediaInsertUseCaseTest.kt @@ -69,7 +69,7 @@ class GifMediaInsertUseCaseTest : BaseUnitTest() { whenever(uriWrapper.toString()).thenReturn("https://sampleutl.org") val itemToInsert = GifMediaIdentifier(uriWrapper, null) - val insertedMediaModel = MediaModel().apply { id = 100 } + val insertedMediaModel = MediaModel(0, 0).apply { id = 100 } whenever(wpMediaUtilsWrapper.fetchMediaToUriWrapper(any())).thenReturn(mock()) whenever(mimeTypeMapUtilsWrapper.getFileExtensionFromUrl(any())).thenReturn("png") diff --git a/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/insert/StockMediaInsertUseCaseTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/insert/StockMediaInsertUseCaseTest.kt index 1ddade5950b1..3aab9bc83467 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/insert/StockMediaInsertUseCaseTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/insert/StockMediaInsertUseCaseTest.kt @@ -39,9 +39,11 @@ class StockMediaInsertUseCaseTest : BaseUnitTest() { @Test fun `uploads media on insert`() = test { val itemToInsert = Identifier.StockMediaIdentifier(url, name, title) - val insertedMediaModel = MediaModel() val mediaId: Long = 10 - insertedMediaModel.mediaId = mediaId + val insertedMediaModel = MediaModel( + site.id, + mediaId + ) whenever(stockMediaStore.performUploadStockMedia(any(), any())).thenReturn( OnStockMediaUploaded( site, listOf( diff --git a/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSourceTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSourceTest.kt index 951f0e2e563e..eace03cf2281 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSourceTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSourceTest.kt @@ -305,8 +305,7 @@ class MediaLibraryDataSourceTest : BaseUnitTest() { } private fun buildMediaModel(date: Long): MediaModel { - val mediaModel = MediaModel() - mediaModel.mediaId = mediaIdCounter + val mediaModel = MediaModel(siteModel.id, mediaIdCounter) mediaIdCounter += 1 mediaModel.url = "http://media.jpg" mediaModel.title = "media" diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/FeaturedImageHelperTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/FeaturedImageHelperTest.kt index 0d49488789c1..5cb236479216 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/FeaturedImageHelperTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/FeaturedImageHelperTest.kt @@ -374,7 +374,7 @@ class FeaturedImageHelperTest { companion object Fixtures { fun createMediaModel(markedLocallyAsFeatured: Boolean): MediaModel { - return MediaModel().apply { + return MediaModel(0, 0).apply { this.markedLocallyAsFeatured = markedLocallyAsFeatured } } diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/AddExistingMediaToPostUseCaseTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/AddExistingMediaToPostUseCaseTest.kt index 1edf0e963b82..630fe7cbab42 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/AddExistingMediaToPostUseCaseTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/AddExistingMediaToPostUseCaseTest.kt @@ -108,9 +108,9 @@ class AddExistingMediaToPostUseCaseTest : BaseUnitTest() { } } - fun createMediaModel(mediaModelId: Long) = - MediaModel().apply { - this.mediaId = mediaModelId - } + fun createMediaModel(mediaModelId: Long) = MediaModel( + 0, + mediaModelId + ) } } diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/AppendMediaToEditorUseCaseTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/AppendMediaToEditorUseCaseTest.kt index ef62df58269a..384ed36d9ce9 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/AppendMediaToEditorUseCaseTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/AppendMediaToEditorUseCaseTest.kt @@ -105,8 +105,8 @@ class AppendMediaToEditorUseCaseTest { } fun createMediaModel(url: String? = "", filePath: String? = ""): MediaModel { - return MediaModel().apply { - this.url = url + return MediaModel(0, 0).apply { + this.url = url ?: "" this.filePath = filePath } } diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/CleanUpMediaToPostAssociationUseCaseTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/CleanUpMediaToPostAssociationUseCaseTest.kt index 8576be031b17..2eaa9b23384e 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/CleanUpMediaToPostAssociationUseCaseTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/CleanUpMediaToPostAssociationUseCaseTest.kt @@ -161,7 +161,7 @@ class CleanUpMediaToPostAssociationUseCaseTest(private val containsGutenbergBloc fun createMediaList(): List { val list = mutableListOf() for (i in 1..10) { - list.add(MediaModel().apply { id = i }) + list.add(MediaModel(0, 0).apply { id = i }) } return list } diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/EditorMediaTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/EditorMediaTest.kt index 54497d980a00..c081fceead0b 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/EditorMediaTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/EditorMediaTest.kt @@ -415,9 +415,6 @@ class EditorMediaTest : BaseUnitTest() { on { isNetworkAvailable() }.thenReturn(isOnline) } - fun createMediaModel(localId: Int = 1, mediaId: Long) = MediaModel().apply { - this.mediaId = mediaId - this.id = localId - } + fun createMediaModel(localId: Int = 1, mediaId: Long) = MediaModel(0, mediaId).apply { id = localId } } } diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/GetMediaModelUseCaseTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/GetMediaModelUseCaseTest.kt index eb0e74a2c20a..7f0f0592e10b 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/GetMediaModelUseCaseTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/GetMediaModelUseCaseTest.kt @@ -225,7 +225,7 @@ class GetMediaModelUseCaseTest : BaseUnitTest() { private fun createFluxCUtilsWrapper() = mock { on { mediaModelFromLocalUri(any(), anyOrNull(), eq(LOCAL_SITE_ID)) }.thenReturn( - MediaModel() + MediaModel(0, 0) ) } diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/ReattachUploadingMediaUseCaseTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/ReattachUploadingMediaUseCaseTest.kt index 42617d617549..7f84a380208e 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/ReattachUploadingMediaUseCaseTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/ReattachUploadingMediaUseCaseTest.kt @@ -20,8 +20,8 @@ class ReattachUploadingMediaUseCaseTest { private val uploadServiceFacade: UploadServiceFacade = mock() private val editorMediaUploadListener: EditorMediaUploadListener = mock() - private val mediaModel1 = MediaModel().apply { id = 1 } - private val mediaModel2 = MediaModel().apply { id = 2 } + private val mediaModel1 = MediaModel(0, 0).apply { id = 1 } + private val mediaModel2 = MediaModel(0, 0).apply { id = 2 } @Before fun setUp() { diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/RemoveMediaUseCaseTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/RemoveMediaUseCaseTest.kt index 3ebf1470523e..de23cc6e08d8 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/RemoveMediaUseCaseTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/RemoveMediaUseCaseTest.kt @@ -42,7 +42,7 @@ class RemoveMediaUseCaseTest : BaseUnitTest() { uploadServiceFacade, testDispatcher() ) - whenever(mediaStore.getMediaWithLocalId(anyInt())).thenReturn(MediaModel().apply { + whenever(mediaStore.getMediaWithLocalId(anyInt())).thenReturn(MediaModel(0, 0).apply { uploadState = "non-empty-state" }) whenever(mediaUtilsWrapper.isLocalFile(anyString())).thenReturn(true) diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/RetryFailedMediaUploadUseCaseTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/RetryFailedMediaUploadUseCaseTest.kt index aa1fa24fdcad..366303e9fe00 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/RetryFailedMediaUploadUseCaseTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/RetryFailedMediaUploadUseCaseTest.kt @@ -132,7 +132,7 @@ class RetryFailedMediaUploadUseCaseTest : BaseUnitTest() { } fun createMediaModel(mediaModelId: Int) = - MediaModel().apply { + MediaModel(0, 0).apply { this.id = mediaModelId this.uploadState = MediaUploadState.FAILED.name } diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/UpdateMediaModelUseCaseTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/UpdateMediaModelUseCaseTest.kt index 2416b3921acc..f39bcc34b092 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/UpdateMediaModelUseCaseTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/UpdateMediaModelUseCaseTest.kt @@ -87,7 +87,7 @@ class UpdateMediaModelUseCaseTest { } fun createMediaModel(uploadState: MediaUploadState = MediaUploadState.QUEUED) = - MediaModel().apply { + MediaModel(0, 0).apply { this.uploadState = uploadState.name this.id = 1 this.postId = 2 diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/UploadMediaUseCaseTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/UploadMediaUseCaseTest.kt index 5d64a2428d4d..c77cfbfad37a 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/UploadMediaUseCaseTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/editor/media/UploadMediaUseCaseTest.kt @@ -88,6 +88,6 @@ class UploadMediaUseCaseTest { } private fun createMediaModel(uploadState: MediaUploadState = MediaUploadState.QUEUED) = - MediaModel().apply { this.uploadState = uploadState.name } + MediaModel(0, 0).apply { this.uploadState = uploadState.name } } } diff --git a/WordPress/src/test/java/org/wordpress/android/ui/uploads/PostMediaHandlerTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/uploads/PostMediaHandlerTest.kt index 9ea1eb5a53b6..268bd1c1f39c 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/uploads/PostMediaHandlerTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/uploads/PostMediaHandlerTest.kt @@ -38,8 +38,8 @@ class PostMediaHandlerTest { fun setUp() { postMediaHandler = PostMediaHandler(mediaStore, dispatcher) post = PostModel() - firstMediaItem = MediaModel() - secondMediaItem = MediaModel() + firstMediaItem = MediaModel(0, 0) + secondMediaItem = MediaModel(0, 0) actions.clear() doAnswer { actions.add(it.getArgument(0)) From fbcfd003a44c33e1560f3c59c8a07d5810ca01ab Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Wed, 1 Nov 2023 15:23:17 +0200 Subject: [PATCH 03/15] Analysis: Un-guard usages of non-null media model url FYI: This change changes the logic to only checking for not blank instead. --- .../android/ui/mediapicker/loader/MediaLibraryDataSource.kt | 2 +- .../java/org/wordpress/android/ui/posts/EditPostActivity.java | 4 ++-- .../android/ui/posts/PostListFeaturedImageTracker.kt | 2 +- .../wordpress/android/ui/posts/editor/StoriesEventListener.kt | 2 +- .../ui/posts/editor/media/AppendMediaToEditorUseCase.kt | 2 +- .../wordpress/android/viewmodel/pages/PageListViewModel.kt | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSource.kt b/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSource.kt index a2b991c26237..89c7ee3c618f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSource.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSource.kt @@ -125,7 +125,7 @@ class MediaLibraryDataSource( } private fun List.toMediaItems(mediaType: MediaType): List { - return this.filter { it.url != null }.map { mediaModel -> + return this.filter { it.url.isNotBlank() }.map { mediaModel -> MediaItem( RemoteId(mediaModel.mediaId), mediaModel.url, 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 22bdc8e4e0b3..8b78f89c69d1 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 @@ -3312,7 +3312,7 @@ public boolean onMediaRetryClicked(final String mediaId) { return false; } - if (media.getUrl() != null && media.getUploadState().equals(MediaUploadState.UPLOADED.toString())) { + if (!TextUtils.isEmpty(media.getUrl()) && media.getUploadState().equals(MediaUploadState.UPLOADED.toString())) { // Note: we should actually do this when the editor fragment starts instead of waiting for user input. // Notify the editor fragment upload was successful and it should replace the local url by the remote url. if (mEditorMediaUploadListener != null) { @@ -3672,7 +3672,7 @@ public void onMediaUploaded(OnMediaUploaded event) { mEditorMedia.onMediaUploadError(mEditorMediaUploadListener, event.media, event.error); } else if (event.completed) { // if the remote url on completed is null, we consider this upload wasn't successful - if (event.media.getUrl() == null) { + if (TextUtils.isEmpty(event.media.getUrl())) { MediaError error = new MediaError(MediaErrorType.GENERIC_ERROR); mEditorMedia.onMediaUploadError(mEditorMediaUploadListener, event.media, error); } else { diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListFeaturedImageTracker.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListFeaturedImageTracker.kt index e04428ab59ad..d94dd6e0a582 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListFeaturedImageTracker.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListFeaturedImageTracker.kt @@ -31,7 +31,7 @@ class PostListFeaturedImageTracker(private val dispatcher: Dispatcher, private v featuredImageMap[featuredImageId]?.let { return it } mediaStore.getSiteMediaWithId(site, featuredImageId)?.let { media -> // This should be a pretty rare case, but some media seems to be missing url - return if (media.url != null) { + return if (media.url.isNotBlank()) { featuredImageMap[featuredImageId] = media.url media.url } else null diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/editor/StoriesEventListener.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/editor/StoriesEventListener.kt index 5ffc2c1f8045..38d07bf8ee6b 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/editor/StoriesEventListener.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/editor/StoriesEventListener.kt @@ -353,7 +353,7 @@ class StoriesEventListener @Inject constructor( dialog.show() return } - if (media.url != null && media.uploadState == UPLOADED.toString()) { + if (media.url.isNotBlank() && media.uploadState == UPLOADED.toString()) { // Note: we should actually do this when the editor fragment starts instead of waiting for user // input. // Notify the editor fragment upload was successful and it should replace the local url by the diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/editor/media/AppendMediaToEditorUseCase.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/editor/media/AppendMediaToEditorUseCase.kt index 6719542817e5..1b4ce917c1e1 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/editor/media/AppendMediaToEditorUseCase.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/editor/media/AppendMediaToEditorUseCase.kt @@ -35,4 +35,4 @@ class AppendMediaToEditorUseCase @Inject constructor(private val fluxCUtilsWrapp } private val MediaModel.urlToUse - get() = if (url.isNullOrBlank()) filePath else url + get() = url.ifBlank { filePath } diff --git a/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt b/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt index bf415799ec48..ef1ff1ba255d 100644 --- a/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt @@ -279,7 +279,7 @@ class PageListViewModel @Inject constructor( mediaStore.getSiteMediaWithId(pagesViewModel.site, featuredImageId)?.let { media -> // This should be a pretty rare case, but some media seems to be missing url - return if (media.url != null) { + return if (media.url.isNotBlank()) { featuredImageMap[featuredImageId] = media.url media.url } else null From 4d3b22ed0c3d7a882e49eca811cbcbe714779d99 Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Wed, 1 Nov 2023 15:28:11 +0200 Subject: [PATCH 04/15] Analysis: Guard usages of nullable media model upload date --- .../android/ui/mediapicker/loader/MediaLibraryDataSource.kt | 2 +- .../android/ui/mediapicker/loader/MediaLibraryDataSourceTest.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSource.kt b/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSource.kt index 89c7ee3c618f..7009bda1d726 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSource.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSource.kt @@ -132,7 +132,7 @@ class MediaLibraryDataSource( mediaModel.title, mediaType, mediaModel.mimeType, - dateTimeUtilsWrapper.dateFromIso8601(mediaModel.uploadDate).time + mediaModel.uploadDate?.let { dateTimeUtilsWrapper.dateFromIso8601(it).time } ?: 0L ) } } diff --git a/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSourceTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSourceTest.kt index eace03cf2281..28f977afb915 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSourceTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/mediapicker/loader/MediaLibraryDataSourceTest.kt @@ -300,7 +300,7 @@ class MediaLibraryDataSourceTest : BaseUnitTest() { assertThat(mediaItem.url).isEqualTo(mediaModel.url) assertThat(mediaItem.name).isEqualTo(mediaModel.title) assertThat(mediaItem.mimeType).isEqualTo(mediaModel.mimeType) - assertThat(mediaItem.dataModified).isEqualTo(mediaModel.uploadDate.toLong()) + assertThat(mediaItem.dataModified).isEqualTo(mediaModel.uploadDate?.toLong()) } } From eff85644fd08806b6cfb561ab193fda79fefbb4a Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Wed, 1 Nov 2023 15:29:17 +0200 Subject: [PATCH 05/15] Analysis: Guard usages of nullable media model upload state --- .../android/ui/posts/editor/media/RemoveMediaUseCase.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/editor/media/RemoveMediaUseCase.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/editor/media/RemoveMediaUseCase.kt index 07aa98390574..546424e24e04 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/editor/media/RemoveMediaUseCase.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/editor/media/RemoveMediaUseCase.kt @@ -35,8 +35,9 @@ class RemoveMediaUseCase @Inject constructor( // also make sure it's not being uploaded anywhere else (maybe on some other Post, // simultaneously) - if (mediaModel.uploadState != null && - mediaUtils.isLocalFile(mediaModel.uploadState.lowercase(Locale.ROOT)) && + val uploadState = mediaModel.uploadState + if (uploadState != null && + mediaUtils.isLocalFile(uploadState.lowercase(Locale.ROOT)) && !uploadService.isPendingOrInProgressMediaUpload(mediaModel) ) { dispatcher.dispatch(MediaActionBuilder.newRemoveMediaAction(mediaModel)) From d2ccda4114c8b9cb67ace356b75aa7602bc88239 Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Wed, 1 Nov 2023 15:37:36 +0200 Subject: [PATCH 06/15] Analysis: Un-guard usages of non-null on media changed media list --- .../android/ui/media/MediaBrowserActivity.java | 12 +++++------- .../ui/media/services/MediaDeleteService.java | 2 +- .../android/ui/posts/PostListEventListener.kt | 2 +- .../android/viewmodel/pages/PageListEventListener.kt | 4 +--- .../android/viewmodel/pages/PageListViewModel.kt | 2 +- 5 files changed, 9 insertions(+), 13 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/media/MediaBrowserActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/media/MediaBrowserActivity.java index dede611f1f95..022523def4dd 100755 --- a/WordPress/src/main/java/org/wordpress/android/ui/media/MediaBrowserActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/media/MediaBrowserActivity.java @@ -804,17 +804,15 @@ public void onMediaChanged(OnMediaChanged event) { switch (event.cause) { case DELETE_MEDIA: - if (event.mediaList != null) { - // If the media was deleted, remove it from multi select if it was selected - for (MediaModel mediaModel : event.mediaList) { - int localMediaId = mediaModel.getId(); - mMediaGridFragment.removeFromMultiSelect(localMediaId); - } + // If the media was deleted, remove it from multi select if it was selected + for (MediaModel mediaModel : event.mediaList) { + int localMediaId = mediaModel.getId(); + mMediaGridFragment.removeFromMultiSelect(localMediaId); } break; } - if (event.mediaList != null && event.mediaList.size() == 1) { + if (event.mediaList.size() == 1) { updateMediaGridItem(event.mediaList.get(0), true); } else { reloadMediaGrid(); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/media/services/MediaDeleteService.java b/WordPress/src/main/java/org/wordpress/android/ui/media/services/MediaDeleteService.java index d2ec601503c1..00ea8471bf6f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/media/services/MediaDeleteService.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/media/services/MediaDeleteService.java @@ -102,7 +102,7 @@ public int onStartCommand(Intent intent, int flags, int startId) { @Subscribe(threadMode = ThreadMode.MAIN) public void onMediaChanged(OnMediaChanged event) { // event for unknown media, ignoring - if (event.mediaList == null || event.mediaList.isEmpty() || !matchesInProgressMedia(event.mediaList.get(0))) { + if (event.mediaList.isEmpty() || !matchesInProgressMedia(event.mediaList.get(0))) { AppLog.w(T.MEDIA, "Media event not recognized: " + event.mediaList); return; } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListEventListener.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListEventListener.kt index 0c9e9b3b6190..89032b7472eb 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListEventListener.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListEventListener.kt @@ -174,7 +174,7 @@ class PostListEventListener( @Suppress("unused") @Subscribe(threadMode = BACKGROUND) fun onMediaChanged(event: OnMediaChanged) { - if (!event.isError && event.mediaList != null) { + if (!event.isError) { featuredMediaChanged(*event.mediaList.map { it.mediaId }.toLongArray()) uploadStatusChanged(*event.mediaList.map { it.localPostId }.toIntArray()) } diff --git a/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListEventListener.kt b/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListEventListener.kt index 4a2531690077..d7fb9833d072 100644 --- a/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListEventListener.kt +++ b/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListEventListener.kt @@ -117,9 +117,7 @@ class PageListEventListener( @Suppress("unused") @Subscribe(threadMode = BACKGROUND) fun onMediaChanged(event: OnMediaChanged) { - if (event.mediaList != null) { - uploadStatusChanged(*event.mediaList.map { LocalId(it.localPostId) }.toTypedArray()) - } + uploadStatusChanged(*event.mediaList.map { LocalId(it.localPostId) }.toTypedArray()) } @Suppress("unused") diff --git a/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt b/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt index ef1ff1ba255d..394a57f64d79 100644 --- a/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt @@ -498,7 +498,7 @@ class PageListViewModel @Inject constructor( @Suppress("unused") @Subscribe(threadMode = ThreadMode.BACKGROUND) fun onMediaChanged(event: OnMediaChanged) { - if (!event.isError && event.mediaList != null) { + if (!event.isError) { invalidateFeaturedMedia(*event.mediaList.map { it.mediaId }.toLongArray()) } } From bc63a428c06b4d21f9cd6f9d9ba53c9078efcd95 Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Wed, 1 Nov 2023 15:44:19 +0200 Subject: [PATCH 07/15] Analysis: Suppress spread operator on page list event listener Detekt Warning: "SpreadOperator: In most cases using a spread operator causes a full copy of the array to be created before calling a method. This may result in a performance penalty." Now that this warning is suppressed close to source, it is no longer needed to be in the baseline. --- .../org/wordpress/android/ui/posts/PostListEventListener.kt | 4 ++-- .../android/viewmodel/pages/PageListEventListener.kt | 4 ++-- .../wordpress/android/viewmodel/pages/PageListViewModel.kt | 2 +- config/detekt/baseline.xml | 6 ------ 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListEventListener.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListEventListener.kt index 89032b7472eb..3cb62e1617ba 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListEventListener.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListEventListener.kt @@ -171,7 +171,7 @@ class PostListEventListener( } } - @Suppress("unused") + @Suppress("unused", "SpreadOperator") @Subscribe(threadMode = BACKGROUND) fun onMediaChanged(event: OnMediaChanged) { if (!event.isError) { @@ -283,7 +283,7 @@ class PostListEventListener( uploadStatusChanged(event.media.localPostId) } - @Suppress("unused") + @Suppress("unused", "SpreadOperator") @Subscribe(threadMode = BACKGROUND) fun onEventBackgroundThread(event: UploadService.UploadMediaRetryEvent) { if (event.mediaModelList != null && !event.mediaModelList.isEmpty()) { diff --git a/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListEventListener.kt b/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListEventListener.kt index d7fb9833d072..79c50b243293 100644 --- a/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListEventListener.kt +++ b/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListEventListener.kt @@ -114,7 +114,7 @@ class PageListEventListener( } } - @Suppress("unused") + @Suppress("unused", "SpreadOperator") @Subscribe(threadMode = BACKGROUND) fun onMediaChanged(event: OnMediaChanged) { uploadStatusChanged(*event.mediaList.map { LocalId(it.localPostId) }.toTypedArray()) @@ -167,7 +167,7 @@ class PageListEventListener( } } - @Suppress("unused") + @Suppress("unused", "SpreadOperator") @Subscribe(threadMode = BACKGROUND) fun onEventBackgroundThread(event: UploadService.UploadMediaRetryEvent) { if (event.mediaModelList != null && event.mediaModelList.isNotEmpty()) { diff --git a/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt b/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt index 394a57f64d79..447ba53e12c5 100644 --- a/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListViewModel.kt @@ -495,7 +495,7 @@ class PageListViewModel @Inject constructor( pagesViewModel.onImagesChanged() } - @Suppress("unused") + @Suppress("unused", "SpreadOperator") @Subscribe(threadMode = ThreadMode.BACKGROUND) fun onMediaChanged(event: OnMediaChanged) { if (!event.isError) { diff --git a/config/detekt/baseline.xml b/config/detekt/baseline.xml index 907f2b6ecacf..0176b2e33763 100644 --- a/config/detekt/baseline.xml +++ b/config/detekt/baseline.xml @@ -187,12 +187,6 @@ ReturnCount:UploadActionUseCase.kt$UploadActionUseCase$fun getAutoUploadAction(post: PostImmutableModel, site: SiteModel): UploadAction ReturnCount:WPEditTextWithChipsOutlined.kt$WPEditTextWithChipsOutlined$private fun endsWithDelimiter(string: String): Boolean ReturnCount:WPEditTextWithChipsOutlined.kt$WPEditTextWithChipsOutlined$private fun removeDelimiterFromItemIfPresent(item: String?): String? - SpreadOperator:PageListEventListener.kt$PageListEventListener$(*event.mediaList.map { LocalId(it.localPostId) }.toTypedArray()) - SpreadOperator:PageListEventListener.kt$PageListEventListener$(*postsToRefresh.map { LocalId(it.id) }.toTypedArray()) - SpreadOperator:PageListViewModel.kt$PageListViewModel$(*event.mediaList.map { it.mediaId }.toLongArray()) - SpreadOperator:PostListEventListener.kt$PostListEventListener$(*event.mediaList.map { it.localPostId }.toIntArray()) - SpreadOperator:PostListEventListener.kt$PostListEventListener$(*event.mediaList.map { it.mediaId }.toLongArray()) - SpreadOperator:PostListEventListener.kt$PostListEventListener$(*postsToRefresh.map { it.id }.toIntArray()) SpreadOperator:UiHelpers.kt$UiHelpers$( uiString.stringRes, *uiString.params.map { value -> getTextOfUiString( context, value ) }.toTypedArray() ) ThrowsCount:PageListDialogHelper.kt$PageListDialogHelper$fun onNegativeClickedForBasicDialog( instanceTag: String, editPage: (RemoteId, LoadAutoSaveRevision) -> Unit, copyPage: (RemoteId) -> Unit ) ThrowsCount:PageListDialogHelper.kt$PageListDialogHelper$fun onPositiveClickedForBasicDialog( instanceTag: String, deletePage: (RemoteId) -> Unit, editPage: (RemoteId, LoadAutoSaveRevision) -> Unit, editPageFirst: (RemoteId) -> Unit ) From 0c67de25930d539ae01bd519b52d7f220bc6c18d Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Wed, 1 Nov 2023 15:47:20 +0200 Subject: [PATCH 08/15] Analysis: Guard usages of nullable on media uploaded media --- .../ui/posts/EditPostSettingsFragment.java | 2 +- .../android/ui/posts/PostListEventListener.kt | 10 ++-- .../ui/uploads/MediaUploadHandler.java | 58 ++++++++++--------- .../viewmodel/pages/PageListEventListener.kt | 5 +- 4 files changed, 40 insertions(+), 35 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostSettingsFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostSettingsFragment.java index edc8992a66c6..2fab7f9ba3db 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostSettingsFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostSettingsFragment.java @@ -1344,7 +1344,7 @@ public void onPostFormatsChanged(OnPostFormatsChanged event) { @Subscribe(threadMode = ThreadMode.MAIN) public void onMediaUploaded(OnMediaUploaded event) { - if (event.media.getMarkedLocallyAsFeatured()) { + if (event.media != null && event.media.getMarkedLocallyAsFeatured()) { refreshViews(); } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListEventListener.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListEventListener.kt index 3cb62e1617ba..f0b1f2029a11 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListEventListener.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostListEventListener.kt @@ -214,12 +214,12 @@ class PostListEventListener( if (event.isError || event.canceled) { return } - if (event.media == null || event.media.localPostId == 0 || site.id != event.media.localSiteId) { - // Not interested in media not attached to posts or not belonging to the current site - return + val media = event.media + if (media != null && media.localPostId != 0 && site.id == media.localSiteId) { + featuredMediaChanged(media.mediaId) + uploadStatusChanged(media.localPostId) } - featuredMediaChanged(event.media.mediaId) - uploadStatusChanged(event.media.localPostId) + // Not interested in media not attached to posts or not belonging to the current site } // EventBus Events diff --git a/WordPress/src/main/java/org/wordpress/android/ui/uploads/MediaUploadHandler.java b/WordPress/src/main/java/org/wordpress/android/ui/uploads/MediaUploadHandler.java index 21a57ee2c9d6..914a58610a95 100755 --- a/WordPress/src/main/java/org/wordpress/android/ui/uploads/MediaUploadHandler.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/uploads/MediaUploadHandler.java @@ -171,40 +171,44 @@ static float getOverallProgressForVideo(int videoId, float uploadProgress) { } private void handleOnMediaUploadedSuccess(@NonNull OnMediaUploaded event) { - if (event.canceled) { - AppLog.i(T.MEDIA, "MediaUploadHandler > Upload successfully canceled"); - trackUploadMediaEvents(AnalyticsTracker.Stat.MEDIA_UPLOAD_CANCELED, - getMediaFromInProgressQueueById(event.media.getId()), null); - completeUploadWithId(event.media.getId()); - uploadNextInQueue(); - } else if (event.completed) { - AppLog.i(T.MEDIA, "MediaUploadHandler > Upload completed - localId=" + event.media.getId() + " title=" - + event.media.getTitle()); - trackUploadMediaEvents(AnalyticsTracker.Stat.MEDIA_UPLOAD_SUCCESS, - getMediaFromInProgressQueueById(event.media.getId()), null); - completeUploadWithId(event.media.getId()); - uploadNextInQueue(); - } else { - AppLog.i(T.MEDIA, "MediaUploadHandler > " + event.media.getId() + " - progress: " + event.progress); + if (event.media != null) { + if (event.canceled) { + AppLog.i(T.MEDIA, "MediaUploadHandler > Upload successfully canceled"); + trackUploadMediaEvents(AnalyticsTracker.Stat.MEDIA_UPLOAD_CANCELED, + getMediaFromInProgressQueueById(event.media.getId()), null); + completeUploadWithId(event.media.getId()); + uploadNextInQueue(); + } else if (event.completed) { + AppLog.i(T.MEDIA, "MediaUploadHandler > Upload completed - localId=" + event.media.getId() + " title=" + + event.media.getTitle()); + trackUploadMediaEvents(AnalyticsTracker.Stat.MEDIA_UPLOAD_SUCCESS, + getMediaFromInProgressQueueById(event.media.getId()), null); + completeUploadWithId(event.media.getId()); + uploadNextInQueue(); + } else { + AppLog.i(T.MEDIA, "MediaUploadHandler > " + event.media.getId() + " - progress: " + event.progress); + } } } private void handleOnMediaUploadedError(@NonNull OnMediaUploaded event) { AppLog.w(T.MEDIA, "MediaUploadHandler > Error uploading media: " + event.error.message); - MediaModel media = getMediaFromInProgressQueueById(event.media.getId()); - if (media != null) { - mDispatcher.dispatch(MediaActionBuilder.newUpdateMediaAction(media)); - } + if (event.media != null) { + MediaModel media = getMediaFromInProgressQueueById(event.media.getId()); + if (media != null) { + mDispatcher.dispatch(MediaActionBuilder.newUpdateMediaAction(media)); + } - Map properties = new HashMap<>(); - properties.put("error_type", event.error.type.name()); - properties.put("error_message", event.error.message); - properties.put("error_log", event.error.logMessage); - properties.put("error_status_code", event.error.statusCode); - trackUploadMediaEvents(AnalyticsTracker.Stat.MEDIA_UPLOAD_ERROR, media, properties); + Map properties = new HashMap<>(); + properties.put("error_type", event.error.type.name()); + properties.put("error_message", event.error.message); + properties.put("error_log", event.error.logMessage); + properties.put("error_status_code", event.error.statusCode); + trackUploadMediaEvents(AnalyticsTracker.Stat.MEDIA_UPLOAD_ERROR, media, properties); - completeUploadWithId(event.media.getId()); - uploadNextInQueue(); + completeUploadWithId(event.media.getId()); + uploadNextInQueue(); + } } private synchronized void uploadNextInQueue() { diff --git a/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListEventListener.kt b/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListEventListener.kt index 79c50b243293..6376d7170940 100644 --- a/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListEventListener.kt +++ b/WordPress/src/main/java/org/wordpress/android/viewmodel/pages/PageListEventListener.kt @@ -132,8 +132,9 @@ class PageListEventListener( @Suppress("unused") @Subscribe(threadMode = BACKGROUND) fun onMediaUploaded(event: OnMediaUploaded) { - if (event.media != null && event.media.localPostId != 0 && site.id == event.media.localSiteId) { - uploadStatusChanged(LocalId(event.media.localPostId)) + val media = event.media + if (media != null && media.localPostId != 0 && site.id == media.localSiteId) { + uploadStatusChanged(LocalId(media.localPostId)) } } From 99c33d4c1b5ca2a9fac5843ecbff29b57f77229a Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Wed, 1 Nov 2023 15:49:11 +0200 Subject: [PATCH 09/15] Analysis: Guard usages of nullable upload stock media error message --- .../android/ui/mediapicker/insert/StockMediaInsertUseCase.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/insert/StockMediaInsertUseCase.kt b/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/insert/StockMediaInsertUseCase.kt index 0ce4b682f280..9d8e8dd3018b 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/insert/StockMediaInsertUseCase.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/insert/StockMediaInsertUseCase.kt @@ -31,7 +31,7 @@ class StockMediaInsertUseCase( }) emit( when { - result.error != null -> InsertModel.Error(result.error.message) + result.error != null -> InsertModel.Error(result.error.message ?: "") else -> { trackUploadedStockMediaEvent(result.mediaList) InsertModel.Success(result.mediaList.mapNotNull { Identifier.RemoteId(it.mediaId) }) From a5f6bb43c57976a0734190f51cae271aa57dedc9 Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Wed, 1 Nov 2023 15:50:27 +0200 Subject: [PATCH 10/15] Analysis: Use one argument media error constructor on upload utils --- .../java/org/wordpress/android/ui/uploads/UploadUtils.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java b/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java index d0c7325ad34f..595a1066d476 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java @@ -169,11 +169,11 @@ String getErrorMessageFromMediaError(Context context, MediaModel media, MediaErr return errorMessage; } - public static @NonNull - String getErrorMessageFromMedia(Context context, @NonNull MediaModel media) { + @NonNull + public static String getErrorMessageFromMedia(Context context, @NonNull MediaModel media) { MediaUploadModel uploadModel = UploadSqlUtils.getMediaUploadModelForLocalId(media.getId()); - MediaError error = new MediaError(MediaErrorType.GENERIC_ERROR, null, null); + MediaError error = new MediaError(MediaErrorType.GENERIC_ERROR); if (uploadModel != null) { MediaError errorFromUploadModel = uploadModel.getMediaError(); From 63112c92a121bcecb995b0d72428ad8eb78d863a Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Wed, 1 Nov 2023 15:52:43 +0200 Subject: [PATCH 11/15] Analysis: Guard usages of nullable get site media with id medial model --- .../ui/mediapicker/MediaPickerViewModel.kt | 4 +-- .../ui/stories/StoryComposerActivity.kt | 26 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/MediaPickerViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/MediaPickerViewModel.kt index aa3db1989b71..78fd876ae461 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/MediaPickerViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/MediaPickerViewModel.kt @@ -420,8 +420,8 @@ class MediaPickerViewModel @Inject constructor( is Identifier.RemoteId -> { site?.let { launch { - val media: MediaModel = mediaStore.getSiteMediaWithId(it, identifier.value) - _onNavigate.postValue(Event(PreviewMedia(media))) + val media: MediaModel? = mediaStore.getSiteMediaWithId(it, identifier.value) + media?.let { _onNavigate.postValue(Event(PreviewMedia(it))) } } } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stories/StoryComposerActivity.kt b/WordPress/src/main/java/org/wordpress/android/ui/stories/StoryComposerActivity.kt index 07172581f20b..4f6c7cdfa70e 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stories/StoryComposerActivity.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/stories/StoryComposerActivity.kt @@ -664,18 +664,20 @@ class StoryComposerActivity : ComposeLoopFrameActivity(), ) storyMediaFileDataList.add(storyMediaFileData) } else { - val mediaModel = mediaStore.getSiteMediaWithId(site, it.toLong()) - val mediaFile = fluxCUtilsWrapper.mediaFileFromMediaModel(mediaModel) - mediaFile?.let { mediafile -> - mediaFile.alt = StoryFrameItem.getAltTextFromFrameAddedViews(frame) - mediaModel.alt = mediaFile.alt - val storyMediaFileData = - saveStoryGutenbergBlockUseCase.buildMediaFileDataWithTemporaryId( - mediaFile = mediafile, - temporaryId = assignedTempId - ) - frame.id = storyMediaFileData.id - storyMediaFileDataList.add(storyMediaFileData) + site?.let { site -> + val mediaModel = mediaStore.getSiteMediaWithId(site, it.toLong()) + val mediaFile = fluxCUtilsWrapper.mediaFileFromMediaModel(mediaModel) + mediaFile?.let { mediafile -> + mediaFile.alt = StoryFrameItem.getAltTextFromFrameAddedViews(frame) + mediaModel?.alt = mediaFile.alt + val storyMediaFileData = + saveStoryGutenbergBlockUseCase.buildMediaFileDataWithTemporaryId( + mediaFile = mediafile, + temporaryId = assignedTempId + ) + frame.id = storyMediaFileData.id + storyMediaFileDataList.add(storyMediaFileData) + } } } } From 6a8cf682327b4ba8dca9f8d2229265ceff1e0e78 Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Wed, 1 Nov 2023 15:56:17 +0200 Subject: [PATCH 12/15] Analysis: Guard usages of nullable get media with local id medial model --- .../java/org/wordpress/android/ui/posts/EditPostActivity.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 8b78f89c69d1..60b0ca9b7026 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 @@ -2876,7 +2876,9 @@ postId, getSite(), mediaUri, int postId = getImmutablePost().getId(); for (int localId : localIds) { MediaModel media = mMediaStore.getMediaWithLocalId(localId); - mFeaturedImageHelper.queueFeaturedImageForUpload(postId, media); + if (media != null) { + mFeaturedImageHelper.queueFeaturedImageForUpload(postId, media); + } } if (mEditPostSettingsFragment != null) { mEditPostSettingsFragment.refreshViews(); From db19fc36682dda988f2da463ede24dee3f8f5c53 Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Wed, 1 Nov 2023 15:57:00 +0200 Subject: [PATCH 13/15] Analysis: Un-guard usages of non-null get media for post with state list --- .../android/ui/posts/EditPostActivity.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) 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 60b0ca9b7026..b482e4041f5f 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 @@ -3449,17 +3449,19 @@ private void onEditorFinalTouchesBeforeShowing() { // probably here is best for Gutenberg to start interacting with if (mShowGutenbergEditor && mEditorFragment instanceof GutenbergEditorFragment) { refreshEditorTheme(); - List failedMedia = - mMediaStore.getMediaForPostWithState(mEditPostRepository.getPost(), MediaUploadState.FAILED); - if (failedMedia != null && !failedMedia.isEmpty()) { - HashSet mediaIds = new HashSet<>(); - for (MediaModel media : failedMedia) { - // featured image isn't in the editor but in the Post Settings fragment, so we want to skip it - if (!media.getMarkedLocallyAsFeatured()) { - mediaIds.add(media.getId()); + PostImmutableModel post = mEditPostRepository.getPost(); + if (post != null) { + List failedMedia = mMediaStore.getMediaForPostWithState(post, MediaUploadState.FAILED); + if (!failedMedia.isEmpty()) { + HashSet mediaIds = new HashSet<>(); + for (MediaModel media : failedMedia) { + // featured image isn't in the editor but in the Post Settings fragment, so we want to skip it + if (!media.getMarkedLocallyAsFeatured()) { + mediaIds.add(media.getId()); + } } + ((GutenbergEditorFragment) mEditorFragment).resetUploadingMediaToFailed(mediaIds); } - ((GutenbergEditorFragment) mEditorFragment).resetUploadingMediaToFailed(mediaIds); } } else if (mShowAztecEditor && mEditorFragment instanceof AztecEditorFragment) { final EntryPoint entryPoint = (EntryPoint) getIntent().getSerializableExtra(EXTRA_ENTRY_POINT); From 3b3e5a676e9c42bb0612220bda130e973c155770 Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Tue, 7 Nov 2023 15:46:14 +0200 Subject: [PATCH 14/15] Build: Update fluxc version to pr hash (#2886) This 'FluxC' PR hash updates the library to that branch version where the 'MediaModel' is updated to its new null proof version. FluxC PR: https://github.com/wordpress-mobile/ WordPress-FluxC-Android/pull/2886 This step is required in order to check that these 'FluxC' related changes work as expected for WPAndroid. ------------------------------------------------------------------------ FYI: This change is an addition to f6f93759b05bd0b1086cdeb7f3ec4042307ddcc4. This change is necessary in order to overcome and resolve new merge conflicts. --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index da8622bbdd03..0fc3b57f5e22 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,7 @@ ext { automatticTracksVersion = '3.3.0' gutenbergMobileVersion = 'v1.107.0' wordPressAztecVersion = 'v1.8.0' - wordPressFluxCVersion = 'trunk-82a701e28d0eeb8c1612b256fd9d772182a13b6a' + wordPressFluxCVersion = '2886-feea7f92db822907a344d2f04195da23155a88bd' wordPressLoginVersion = 'trunk-9963d78096edf65f8704b803e5b93c08fc9174cd' wordPressPersistentEditTextVersion = '1.0.2' wordPressUtilsVersion = '3.10.0' From 5a9a89bffd684d8aca619589d52aa6d9ba460731 Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Wed, 8 Nov 2023 13:06:27 +0200 Subject: [PATCH 15/15] Build: Update fluxc version to trunk hash (#2886) This 'FluxC' hash updates the library to that 'trunk' version where the 'Media' model classes are updated to their new null proof version. FluxC PR: https://github.com/wordpress-mobile/ WordPress-FluxC-Android/pull/2886 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index f874ca8f0f95..f05503ce5608 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,7 @@ ext { automatticTracksVersion = '3.3.0' gutenbergMobileVersion = 'v1.107.0' wordPressAztecVersion = 'v1.8.0' - wordPressFluxCVersion = 'trunk-8712ec791066f2d86bb9aa6d21710ee7075fac05' + wordPressFluxCVersion = 'trunk-e71a4dc765b7785f753e9224512e0a76c43104e4' wordPressLoginVersion = 'trunk-9963d78096edf65f8704b803e5b93c08fc9174cd' wordPressPersistentEditTextVersion = '1.0.2' wordPressUtilsVersion = '3.10.0'