Skip to content

Commit e34429a

Browse files
authored
Merge pull request #21399 from wordpress-mobile/feat/gutenberg-kit-media-library
feat: GutenbergKit Media Library support
2 parents 835f19a + caf348f commit e34429a

File tree

9 files changed

+144
-38
lines changed

9 files changed

+144
-38
lines changed

WordPress/src/main/java/org/wordpress/android/ui/mediapicker/MediaItem.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ data class MediaItem(
109109
return arrayOfNulls(size)
110110
}
111111
}
112+
113+
public fun fromId(id: Long): Identifier {
114+
return RemoteId(id)
115+
}
112116
}
113117
}
114118
}

WordPress/src/main/java/org/wordpress/android/ui/mediapicker/MediaPickerFragment.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ class MediaPickerFragment : Fragment(), MenuProvider {
232232
}
233233
}
234234

235+
if (mediaPickerSetup.initialSelection.isNotEmpty()) {
236+
selectedIds = mediaPickerSetup.initialSelection.map { Identifier.fromId(it.toLong()) }
237+
}
238+
235239
val layoutManager = GridLayoutManager(
236240
activity,
237241
NUM_COLUMNS

WordPress/src/main/java/org/wordpress/android/ui/mediapicker/MediaPickerSetup.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ data class MediaPickerSetup(
1717
val editingEnabled: Boolean,
1818
val queueResults: Boolean,
1919
val defaultSearchView: Boolean,
20-
@StringRes val title: Int
20+
@StringRes val title: Int,
21+
val initialSelection: List<Int> = emptyList()
2122
) {
2223
enum class DataSource {
2324
DEVICE, WP_LIBRARY, STOCK_LIBRARY, GIF_LIBRARY
@@ -40,6 +41,7 @@ data class MediaPickerSetup(
4041
bundle.putBoolean(KEY_QUEUE_RESULTS, queueResults)
4142
bundle.putBoolean(KEY_DEFAULT_SEARCH_VIEW, defaultSearchView)
4243
bundle.putInt(KEY_TITLE, title)
44+
bundle.putIntegerArrayList(KEY_INITIAL_SELECTION, ArrayList(initialSelection))
4345
}
4446

4547
fun toIntent(intent: Intent) {
@@ -55,6 +57,7 @@ data class MediaPickerSetup(
5557
intent.putExtra(KEY_QUEUE_RESULTS, queueResults)
5658
intent.putExtra(KEY_DEFAULT_SEARCH_VIEW, defaultSearchView)
5759
intent.putExtra(KEY_TITLE, title)
60+
intent.putIntegerArrayListExtra(KEY_INITIAL_SELECTION, ArrayList(initialSelection))
5861
}
5962

6063
companion object {
@@ -70,6 +73,7 @@ data class MediaPickerSetup(
7073
private const val KEY_QUEUE_RESULTS = "key_queue_results"
7174
private const val KEY_DEFAULT_SEARCH_VIEW = "key_default_search_view"
7275
private const val KEY_TITLE = "key_title"
76+
private const val KEY_INITIAL_SELECTION = "key_initial_selection"
7377

7478
fun fromBundle(bundle: Bundle): MediaPickerSetup {
7579
val dataSource = DataSource.values()[bundle.getInt(KEY_PRIMARY_DATA_SOURCE)]
@@ -88,6 +92,7 @@ data class MediaPickerSetup(
8892
val queueResults = bundle.getBoolean(KEY_QUEUE_RESULTS)
8993
val defaultSearchView = bundle.getBoolean(KEY_DEFAULT_SEARCH_VIEW)
9094
val title = bundle.getInt(KEY_TITLE)
95+
val initialSelection = bundle.getIntegerArrayList(KEY_INITIAL_SELECTION)?.toList() ?: emptyList()
9196
return MediaPickerSetup(
9297
dataSource,
9398
availableDataSources,
@@ -100,7 +105,8 @@ data class MediaPickerSetup(
100105
editingEnabled,
101106
queueResults,
102107
defaultSearchView,
103-
title
108+
title,
109+
initialSelection
104110
)
105111
}
106112

@@ -122,6 +128,7 @@ data class MediaPickerSetup(
122128
val queueResults = intent.getBooleanExtra(KEY_QUEUE_RESULTS, false)
123129
val defaultSearchView = intent.getBooleanExtra(KEY_DEFAULT_SEARCH_VIEW, false)
124130
val title = intent.getIntExtra(KEY_TITLE, R.string.photo_picker_photo_or_video_title)
131+
val initialSelection = intent.getIntegerArrayListExtra(KEY_INITIAL_SELECTION)?.toList() ?: emptyList()
125132
return MediaPickerSetup(
126133
dataSource,
127134
availableDataSources,
@@ -134,7 +141,8 @@ data class MediaPickerSetup(
134141
editingEnabled,
135142
queueResults,
136143
defaultSearchView,
137-
title
144+
title,
145+
initialSelection
138146
)
139147
}
140148
}

WordPress/src/main/java/org/wordpress/android/ui/photopicker/MediaPickerLauncher.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,15 @@ class MediaPickerLauncher @Inject constructor() {
188188
)
189189
}
190190

191-
fun viewWPMediaLibraryPickerForResult(activity: Activity, site: SiteModel, browserType: MediaBrowserType) {
191+
fun viewWPMediaLibraryPickerForResult(
192+
activity: Activity,
193+
site: SiteModel,
194+
browserType: MediaBrowserType,
195+
initialSelection: List<Int> = emptyList()
196+
) {
192197
val intent = MediaPickerActivity.buildIntent(
193198
activity,
194-
buildWPMediaLibraryPickerSetup(browserType),
199+
buildWPMediaLibraryPickerSetup(browserType, initialSelection),
195200
site
196201
)
197202
val requestCode: Int = if (browserType.canMultiselect()) {
@@ -293,7 +298,10 @@ class MediaPickerLauncher @Inject constructor() {
293298
)
294299
}
295300

296-
private fun buildWPMediaLibraryPickerSetup(browserType: MediaBrowserType): MediaPickerSetup {
301+
private fun buildWPMediaLibraryPickerSetup(
302+
browserType: MediaBrowserType,
303+
initialSelection: List<Int>
304+
): MediaPickerSetup {
297305
val allowedTypes = mutableSetOf<MediaType>()
298306
if (browserType.isImagePicker) {
299307
allowedTypes.add(IMAGE)
@@ -322,7 +330,8 @@ class MediaPickerLauncher @Inject constructor() {
322330
editingEnabled = false,
323331
queueResults = false,
324332
defaultSearchView = false,
325-
title = R.string.wp_media_title
333+
title = R.string.wp_media_title,
334+
initialSelection = initialSelection
326335
)
327336
}
328337
}

WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ import org.wordpress.aztec.AztecExceptionHandler
253253
import org.wordpress.aztec.exceptions.DynamicLayoutGetBlockIndexOutOfBoundsException
254254
import org.wordpress.aztec.util.AztecLog
255255
import org.wordpress.gutenberg.GutenbergView
256+
import org.wordpress.gutenberg.MediaType
256257
import java.io.File
257258
import java.util.Locale
258259
import java.util.regex.Matcher
@@ -2470,6 +2471,18 @@ class EditPostActivity : LocaleAwareActivity(), EditorFragmentActivity, EditorIm
24702471
storePostViewModel.savePostWithDelay()
24712472
}
24722473
})
2474+
editorFragment?.onOpenMediaLibrary(object: GutenbergView.OpenMediaLibraryListener {
2475+
override fun onOpenMediaLibrary(config: GutenbergView.OpenMediaLibraryConfig) {
2476+
editorPhotoPicker?.allowMultipleSelection = config.multiple
2477+
val mediaType = mapAllowedTypesToMediaBrowserType(config.allowedTypes, config.multiple)
2478+
val initialSelection = when (val value = config.value) {
2479+
is GutenbergView.Value.Single -> listOf(value.value)
2480+
is GutenbergView.Value.Multiple -> value.toList()
2481+
else -> emptyList()
2482+
}
2483+
openMediaLibrary(mediaType, initialSelection)
2484+
}
2485+
})
24732486
} else {
24742487
editorFragment?.titleOrContentChanged?.observe(this@EditPostActivity) { _: Editable? ->
24752488
storePostViewModel.savePostWithDelay()
@@ -2495,6 +2508,15 @@ class EditPostActivity : LocaleAwareActivity(), EditorFragmentActivity, EditorIm
24952508
private val numPagesInEditor: Int = 4
24962509
}
24972510

2511+
private fun openMediaLibrary(mediaType: MediaBrowserType, initialSelection: List<Int>) {
2512+
mediaPickerLauncher.viewWPMediaLibraryPickerForResult(
2513+
activity = this,
2514+
site = siteModel,
2515+
browserType = mediaType,
2516+
initialSelection = initialSelection
2517+
)
2518+
}
2519+
24982520
private fun onXpostsSettingsCapability(isXpostsCapable: Boolean) {
24992521
isXPostsCapable = isXpostsCapable
25002522
if (editorFragment is GutenbergEditorFragment) {
@@ -4066,3 +4088,19 @@ class EditPostActivity : LocaleAwareActivity(), EditorFragmentActivity, EditorIm
40664088
const val GROUP_THREE = 3
40674089
}
40684090
}
4091+
4092+
fun mapAllowedTypesToMediaBrowserType(allowedTypes: Array<MediaType>, multiple: Boolean): MediaBrowserType {
4093+
return when {
4094+
allowedTypes.contains(MediaType.IMAGE) && allowedTypes.contains(MediaType.VIDEO) -> {
4095+
if (multiple) MediaBrowserType.GUTENBERG_MEDIA_PICKER else MediaBrowserType.GUTENBERG_SINGLE_MEDIA_PICKER
4096+
}
4097+
allowedTypes.contains(MediaType.IMAGE) -> {
4098+
if (multiple) MediaBrowserType.GUTENBERG_IMAGE_PICKER else MediaBrowserType.GUTENBERG_SINGLE_IMAGE_PICKER
4099+
}
4100+
allowedTypes.contains(MediaType.VIDEO) -> {
4101+
if (multiple) MediaBrowserType.GUTENBERG_VIDEO_PICKER else MediaBrowserType.GUTENBERG_SINGLE_VIDEO_PICKER
4102+
}
4103+
allowedTypes.contains(MediaType.AUDIO) -> MediaBrowserType.GUTENBERG_SINGLE_AUDIO_FILE_PICKER
4104+
else -> if (multiple) MediaBrowserType.GUTENBERG_MEDIA_PICKER else MediaBrowserType.GUTENBERG_SINGLE_FILE_PICKER
4105+
}
4106+
}

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ google-play-services-auth = '20.4.1'
7171
google-services = '4.4.2'
7272
gravatar = '2.0.1'
7373
greenrobot-eventbus = '3.3.1'
74-
gutenberg-kit = 'trunk-a58a46f3fbb892f311b562e3c122d7ef4ebbfe33'
74+
gutenberg-kit = 'trunk-c62f3f03a4fdcd31263e9cc014f7a4c65c18131a'
7575
gutenberg-mobile = 'v1.121.0'
7676
indexos-media-for-mobile = '43a9026f0973a2f0a74fa813132f6a16f7499c3a'
7777
jackson-databind = '2.12.7.1'

libs/editor/src/main/java/org/wordpress/android/editor/AztecEditorFragment.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
import org.wordpress.aztec.util.AztecLog;
108108
import org.wordpress.aztec.watchers.EndOfBufferMarkerAdder;
109109
import org.wordpress.gutenberg.GutenbergView.ContentChangeListener;
110+
import org.wordpress.gutenberg.GutenbergView.OpenMediaLibraryListener;
110111
import org.xml.sax.Attributes;
111112

112113
import java.util.ArrayList;
@@ -710,6 +711,9 @@ public Pair<CharSequence, CharSequence> getTitleAndContent(CharSequence original
710711
@Override public void onEditorContentChanged(@Nullable ContentChangeListener listener) {
711712
}
712713

714+
@Override public void onOpenMediaLibrary(@Nullable OpenMediaLibraryListener listener) {
715+
}
716+
713717
@Override
714718
public void onToolbarCollapseButtonClicked() {
715719
mEditorFragmentListener.onTrackableEvent(TrackableEvent.ELLIPSIS_COLLAPSE_BUTTON_TAPPED);

libs/editor/src/main/java/org/wordpress/android/editor/EditorFragmentAbstract.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.wordpress.android.util.helpers.MediaFile;
2222
import org.wordpress.android.util.helpers.MediaGallery;
2323
import org.wordpress.gutenberg.GutenbergView.ContentChangeListener;
24+
import org.wordpress.gutenberg.GutenbergView.OpenMediaLibraryListener;
2425

2526
import java.util.ArrayList;
2627
import java.util.HashMap;
@@ -40,6 +41,7 @@ public class EditorFragmentNotAddedException extends Exception {
4041
public abstract Pair<CharSequence, CharSequence> getTitleAndContent(CharSequence originalContent) throws
4142
EditorFragmentNotAddedException;
4243
public abstract void onEditorContentChanged(ContentChangeListener listener);
44+
public abstract void onOpenMediaLibrary(OpenMediaLibraryListener listener);
4345
public abstract LiveData<Editable> getTitleOrContentChanged();
4446
public abstract void appendMediaFile(MediaFile mediaFile, String imageUrl, ImageLoader imageLoader);
4547
public abstract void appendMediaFiles(Map<String, MediaFile> mediaList);

0 commit comments

Comments
 (0)