Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a "Set as Featured Image" Button to Image Block #14503

Merged
merged 97 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from 82 commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
85774dc
Update strings.xml with text to be used
Apr 21, 2021
4190066
Add OnSetFeaturedImageListener()
Apr 21, 2021
86e6bb0
Add onSetFeaturedImageButtonClicked() function
Apr 21, 2021
51be414
Add updateFeaturedImage() function
Apr 21, 2021
4ab70b2
Add setFeaturedImage() and removeFeaturedImage() functions
Apr 21, 2021
0dd7399
Create dialog in showFeaturedImageConfirmationDialog()
Apr 21, 2021
29d2d0b
Send a notice depending on whether a featured image is set or removed
Apr 21, 2021
7ee5029
Update reference to latest Gutenberg Mobile commit
Apr 21, 2021
0f54607
added an update featured image use case.
jd-alexander Apr 23, 2021
9d72f88
Created mechanisms to track when the gb featured image is picked
jd-alexander Apr 23, 2021
d3b8972
Integrated the new use case and approach to EditPost and Post Settings
jd-alexander Apr 23, 2021
ce3785e
Update remaining references to "IMAGE_PICKED" to "IMAGE_PICKED_POST_S…
Apr 27, 2021
c1278d6
Remove unused imports from EditPostSettingsFragment.java
Apr 27, 2021
dfc7be8
Merge branch 'develop' into gb/1011-set-featured-button
Apr 27, 2021
08efc40
Remove removeFeaturedImage() function and consolidate into a single s…
Apr 27, 2021
655bb60
Bump Gutenberg Mobile
Apr 27, 2021
cffaf48
Merge branch 'develop' into gb/1011-set-featured-button
SiobhyB May 11, 2021
2e5b213
Refactor logic for around showFeaturedImageConfirmationDialog()
May 11, 2021
71f8f36
Update build.gradle with latest Gutenberg Mobile reference
May 12, 2021
34b78ce
Update build.gradle to reference latest commit from Gutenberg Mobile
May 12, 2021
6ba7938
Merge branch 'develop' into gb/1011-set-featured-button
May 12, 2021
e0b301d
Merge branch 'develop' into gb/1011-set-featured-button
May 12, 2021
74323e9
Merge branch 'develop' into gb/1011-set-featured-button
May 12, 2021
754688a
Merge branch 'develop' into gb/1011-set-featured-button
May 12, 2021
3ccdc1d
Merge branch 'develop' into gb/1011-set-featured-button
May 16, 2021
f4358c4
Update reference to latest Gutenberg Mobile commit
May 16, 2021
6ba9fc2
Merge branch 'develop' into gb/1011-set-featured-button
SiobhyB May 16, 2021
d6d26ec
Merge branch 'gb/1011-set-featured-button' of github.com:wordpress-mo…
May 16, 2021
621ce38
Update reference to latest Gutenberg Mobile commit
May 16, 2021
57c3059
Bump Gutenberg Mobile reference
May 18, 2021
1187bc1
Updated gutenberg-mobile ref.
jd-alexander May 19, 2021
610348e
added inject annotation to the constructor of the use case.
jd-alexander May 19, 2021
0cfaf4f
Updated gutenberg-mobile ref.
jd-alexander May 20, 2021
617c5e2
removed comment about disabling Audio block since it is now on all sites
jd-alexander May 20, 2021
672fd07
Merge branch 'gb/1011-set-featured-button' into gb/1011-set-featured-…
jd-alexander May 20, 2021
70dff2c
formatted the use case.
jd-alexander May 20, 2021
d1e7eda
sync stories submodule.
jd-alexander May 20, 2021
fa76582
Create GutenbergDialogFragment.kt using BasicDialogFragment.kt as a t…
May 20, 2021
41c25e0
Add mediaId to GutenbergDialogFragment's constructor
May 20, 2021
d772cbf
Edit "onNegativeClicked" and "onPositiveClicked" so that interface ca…
May 20, 2021
193a41b
Implement BasicDialogPositiveClickInterface and BasicDialogNegativeCl…
May 20, 2021
1a988c7
Refactor showFeaturedImageConfirmationDialog() to use new GutenbergDi…
May 20, 2021
246334e
Add "org.jetbrains.annotations.NotNull" import
May 20, 2021
8bb8837
Fill out "onPositiveClicked" function
May 20, 2021
f35cea5
Fill out onNegativeClicked() function
May 20, 2021
3da4908
Rename instances of "BasicDialog" to "GutenbergDialog"
May 20, 2021
983ff6c
Update reference to Gutenberg Mobile
May 20, 2021
cd572af
Rename "onPositiveClicked" and "onNegativeClicked"
May 20, 2021
f0ff0ba
Merge branch 'develop' into gb/1011-set-featured-button
May 20, 2021
349fe90
Reformat code in GutenbergDialogFragment.kt
May 20, 2021
010505f
Update build.grade with latest reference to Gutenberg Mobile
May 20, 2021
f57eeaf
Save "mFeaturedImageId" in savedInstanceState() and onSaveInstanceSta…
May 20, 2021
3e18f04
Remove redundant comma
May 20, 2021
aa15b61
Add checks to ensure GutenbergDialogPositiveClickInterface and Gutenb…
May 21, 2021
101970c
Don't allow "mPositiveButtonLabel" to be null
May 21, 2021
24a0532
Add "lateinit" modifier to mPositiveButtonLabel
May 21, 2021
f7b81e9
Merge branch 'gb/1011-set-featured-button' into gb/1011-set-featured-…
May 21, 2021
7348943
Add Tracks event that fires when a featured image is removed from the…
May 21, 2021
bf1942f
Refactor null-checks and casting in setPositiveButton() and setNegati…
May 24, 2021
fe93e0b
Refactor if/else statement in onSetFeaturedImageButtonClicked()
May 24, 2021
33fd368
Remove call to sendToJSFeaturedImageId() from within setFeaturedImage()
May 24, 2021
854dd6d
Remove unnecessary semi-colons
May 24, 2021
62caa42
Refine string names
May 24, 2021
b4863bb
Merge branch 'develop' into gb/1011-set-featured-button
May 24, 2021
6fd6fc0
Update string name being passed to showNotice() calls
May 24, 2021
3751c08
Update comment to clarify reasoning behind "setFeaturedImageId" call
May 25, 2021
b4dc825
Reword "featured_image_replace_dialog_title" string
May 25, 2021
9630c29
Introduce "MEDIA_ID_NO_FEATURED_IMAGE_SET" const
May 25, 2021
7924341
Refactor onDismiss() in GutenbergDialogFragment
May 26, 2021
d41cd34
Bump reference to Gutenberg Mobile
May 26, 2021
ccb7292
Merge branch 'develop' into gb/1011-set-featured-button
May 26, 2021
36b6e2e
Remove unecessary comment before 'setFeaturedImageId'
May 26, 2021
1be6762
Fix 'exceeded max line length' ktlint error
May 26, 2021
c917a0c
Remove conditional surrounding 'GutenbergDialogOnDismissByOutsideTouc…
May 26, 2021
c0626bf
Update build.gradle reference to latest commit from Gutenberg Mobile
May 26, 2021
2e07265
Merge branch 'develop' into gutenberg/audio-block-capability
jd-alexander May 27, 2021
58c6d56
Merge branch 'develop' into gutenberg/audio-block-capability
jd-alexander May 27, 2021
96faacf
renamed capability
jd-alexander May 27, 2021
2fa9eb7
sync stories submodule hash.
jd-alexander May 27, 2021
74a2251
updated gutenberg-mobile ref.
jd-alexander May 27, 2021
2a1ef13
Bump Gutenberg Mobile reference
May 27, 2021
105509e
Merge branch 'develop' into gb/1011-set-featured-button
SiobhyB May 30, 2021
a7dabab
Update Gutenberg Mobile ref
fluiddot May 31, 2021
bccaaa9
reformatted use case
jd-alexander Jun 1, 2021
e81d2c2
Merge pull request #14521 from wordpress-mobile/gb/1011-set-featured-…
jd-alexander Jun 1, 2021
92816a9
Update Gutenberg Mobile ref
fluiddot Jun 1, 2021
d52f22c
Merge branch 'develop' into gb/1011-set-featured-button
Jun 1, 2021
fb3ba95
Import constant for use in EditPostActivity
Jun 1, 2021
40310c3
Rename references to 'mediaId' to 'id'
Jun 1, 2021
1eb55fa
Reword dialog's title for clarity.
Jun 2, 2021
c1f6842
Undo title case in notices for consistency
Jun 2, 2021
8c5005f
Update to latest Gutenberg Mobile reference
Jun 2, 2021
db9e884
Merge branch 'develop' into gutenberg/audio-block-capability
fluiddot Jun 3, 2021
54d9cf9
Update Gutenberg Mobile ref
fluiddot Jun 3, 2021
d7953d4
Update Gutenberg Mobile ref
fluiddot Jun 3, 2021
2caa7c0
Bump Gutenberg Mobile reference
Jun 3, 2021
3e2d759
Merge branch 'gutenberg/audio-block-capability' into gb/1011-set-feat…
fluiddot Jun 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private void doMediaUrisSelected(@NonNull List<? extends Uri> mediaUris, @NonNul
final String mimeType = getContentResolver().getType(mediaUri);

mFeaturedImageHelper.trackFeaturedImageEvent(
FeaturedImageHelper.TrackableEvent.IMAGE_PICKED,
FeaturedImageHelper.TrackableEvent.IMAGE_PICKED_POST_SETTINGS,
mLocalPostId
);

Expand Down Expand Up @@ -330,7 +330,7 @@ private void doMediaIdsSelected(ArrayList<Long> mediaIds, @NonNull PhotoPickerMe
// if user chose a featured image, track image picked event
if (mBrowserType == MediaBrowserType.FEATURED_IMAGE_PICKER) {
mFeaturedImageHelper.trackFeaturedImageEvent(
FeaturedImageHelper.TrackableEvent.IMAGE_PICKED,
FeaturedImageHelper.TrackableEvent.IMAGE_PICKED_POST_SETTINGS,
mLocalPostId
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
import static org.wordpress.android.analytics.AnalyticsTracker.Stat.APP_REVIEWS_EVENT_INCREMENTED_BY_PUBLISHING_POST_OR_PAGE;
import static org.wordpress.android.imageeditor.preview.PreviewImageFragment.PREVIEW_IMAGE_REDUCED_SIZE_FACTOR;
import static org.wordpress.android.ui.history.HistoryDetailContainerFragment.KEY_REVISION;
import static org.wordpress.android.editor.gutenberg.GutenbergEditorFragment.MEDIA_ID_NO_FEATURED_IMAGE_SET;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;
Expand Down Expand Up @@ -403,6 +404,7 @@ enum RestartEditorOptions {
@Inject StoriesPrefs mStoriesPrefs;
@Inject StoriesEventListener mStoriesEventListener;
@Inject ContactInfoBlockFeatureConfig mContactInfoBlockFeatureConfig;
@Inject UpdateFeaturedImageUseCase mUpdateFeaturedImageUseCase;

private StorePostViewModel mViewModel;
private StorageUtilsViewModel mStorageUtilsViewModel;
Expand Down Expand Up @@ -1673,7 +1675,7 @@ private void onUploadSuccess(MediaModel media) {
}
} else if (media.getMarkedLocallyAsFeatured() && media.getLocalPostId() == mEditPostRepository
.getId()) {
setFeaturedImageId(media.getMediaId(), false);
setFeaturedImageId(media.getMediaId(), false, false);
}
}
}
Expand Down Expand Up @@ -2484,12 +2486,32 @@ protected void setPostContentFromShareAction() {
}
}

private void setFeaturedImageId(final long mediaId, final boolean imagePicked) {
if (mEditPostSettingsFragment != null) {
mEditPostSettingsFragment.updateFeaturedImage(mediaId, imagePicked);
if (mEditorFragment instanceof GutenbergEditorFragment) {
((GutenbergEditorFragment) mEditorFragment).sendToJSFeaturedImageId((int) mediaId);
private void setFeaturedImageId(final long mediaId, final boolean imagePicked, final boolean isGutenbergEditor) {
if (isGutenbergEditor) {
EditPostRepository postRepository = getEditPostRepository();
if (postRepository == null) {
return;
}

int postId = getEditPostRepository().getId();
if (mediaId == MEDIA_ID_NO_FEATURED_IMAGE_SET) {
mFeaturedImageHelper.trackFeaturedImageEvent(
FeaturedImageHelper.TrackableEvent.IMAGE_REMOVED_GUTENBERG_EDITOR,
postId
);
} else {
mFeaturedImageHelper.trackFeaturedImageEvent(
FeaturedImageHelper.TrackableEvent.IMAGE_PICKED_GUTENBERG_EDITOR,
postId
);
}
mUpdateFeaturedImageUseCase.updateFeaturedImage(mediaId, postRepository,
postModel -> null);
} else if (mEditPostSettingsFragment != null) {
mEditPostSettingsFragment.updateFeaturedImage(mediaId, imagePicked);
}
if (mEditorFragment instanceof GutenbergEditorFragment) {
((GutenbergEditorFragment) mEditorFragment).sendToJSFeaturedImageId((int) mediaId);
}
}

Expand Down Expand Up @@ -2600,13 +2622,13 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
// user chose a featured image
if (data.hasExtra(MediaPickerConstants.EXTRA_MEDIA_ID)) {
long mediaId = data.getLongExtra(MediaPickerConstants.EXTRA_MEDIA_ID, 0);
setFeaturedImageId(mediaId, true);
setFeaturedImageId(mediaId, true, false);
} else if (data.hasExtra(MediaPickerConstants.EXTRA_MEDIA_QUEUED_URIS)) {
List<Uri> uris = convertStringArrayIntoUrisList(
data.getStringArrayExtra(MediaPickerConstants.EXTRA_MEDIA_QUEUED_URIS));
int postId = getImmutablePost().getId();
mFeaturedImageHelper.trackFeaturedImageEvent(
FeaturedImageHelper.TrackableEvent.IMAGE_PICKED,
FeaturedImageHelper.TrackableEvent.IMAGE_PICKED_POST_SETTINGS,
postId
);
for (Uri mediaUri : uris) {
Expand Down Expand Up @@ -2894,6 +2916,11 @@ public void onEditPostPublishedSettingsClick() {
}
}

@Override
public void updateFeaturedImage(final long mediaId, final boolean imagePicked) {
setFeaturedImageId(mediaId, imagePicked, true);
}

@Override
public void onAddMediaClicked() {
if (mEditorPhotoPicker.isPhotoPickerShowing()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public class EditPostSettingsFragment extends Fragment {
@Inject AnalyticsTrackerWrapper mAnalyticsTrackerWrapper;
@Inject UpdatePostStatusUseCase mUpdatePostStatusUseCase;
@Inject MediaPickerLauncher mMediaPickerLauncher;
@Inject UpdateFeaturedImageUseCase mUpdateFeaturedImageUseCase;

@Inject ViewModelProvider.Factory mViewModelFactory;
private EditPostPublishSettingsViewModel mPublishedViewModel;
Expand Down Expand Up @@ -1000,23 +1001,21 @@ public void updateFeaturedImage(long featuredImageId, boolean imagePicked) {
if (isAdded() && imagePicked) {
int postId = getEditPostRepository().getId();
mFeaturedImageHelper.trackFeaturedImageEvent(
TrackableEvent.IMAGE_PICKED,
TrackableEvent.IMAGE_PICKED_POST_SETTINGS,
postId
);
}

EditPostRepository postRepository = getEditPostRepository();
if (postRepository == null) {
return;
}
postRepository.updateAsync(postModel -> {
postModel.setFeaturedImageId(featuredImageId);
return true;
}, (postModel, result) -> {
if (result == UpdatePostResult.Updated.INSTANCE) {
updateFeaturedImageView(postModel);
}
return null;
});

mUpdateFeaturedImageUseCase.updateFeaturedImage(featuredImageId, postRepository,
postModel -> {
updateFeaturedImageView(postModel);
return null;
});
}

private void clearFeaturedImage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ class FeaturedImageHelper @Inject constructor(

enum class TrackableEvent(val label: Stat) {
IMAGE_SET_CLICKED(Stat.FEATURED_IMAGE_SET_CLICKED_POST_SETTINGS),
IMAGE_PICKED(Stat.FEATURED_IMAGE_PICKED_POST_SETTINGS),
IMAGE_PICKED_POST_SETTINGS(Stat.FEATURED_IMAGE_PICKED_POST_SETTINGS),
IMAGE_PICKED_GUTENBERG_EDITOR(Stat.FEATURED_IMAGE_PICKED_GUTENBERG_EDITOR),
IMAGE_REMOVED_GUTENBERG_EDITOR(Stat.FEATURED_IMAGE_REMOVED_GUTENBERG_EDITOR),
IMAGE_UPLOAD_CANCELED(Stat.FEATURED_IMAGE_UPLOAD_CANCELED_POST_SETTINGS),
IMAGE_UPLOAD_RETRY_CLICKED(Stat.FEATURED_IMAGE_UPLOAD_RETRY_CLICKED_POST_SETTINGS),
IMAGE_REMOVE_CLICKED(Stat.FEATURED_IMAGE_REMOVE_CLICKED_POST_SETTINGS)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.wordpress.android.ui.posts

import org.wordpress.android.fluxc.model.PostImmutableModel
import org.wordpress.android.fluxc.model.PostModel
import org.wordpress.android.ui.posts.EditPostRepository.UpdatePostResult
import org.wordpress.android.ui.posts.EditPostRepository.UpdatePostResult.Updated
import javax.inject.Inject

class UpdateFeaturedImageUseCase @Inject constructor() {
fun updateFeaturedImage(
featuredImageId: Long,
editPostRepository: EditPostRepository,
onPostFeaturedImageUpdated: (PostImmutableModel) -> Unit
) {
editPostRepository.updateAsync({ postModel: PostModel ->
postModel.setFeaturedImageId(featuredImageId)
true
}) { postModel: PostImmutableModel, result: UpdatePostResult ->
if (result === Updated) {
onPostFeaturedImageUpdated.invoke(postModel)
}
}
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
ext.kotlin_ktx_version = '1.2.0'
ext.wordPressUtilsVersion = 'develop-bb54ee34c5fec5fa7375ce90a356adb5adbdcae0'
ext.detektVersion = '1.15.0'
ext.gutenbergMobileVersion = 'v1.54.0'
ext.gutenbergMobileVersion = '3116-0af44c12fd41a3f5ba62164c873eda2cef80c1ca'

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ public enum Stat {
WELCOME_NO_SITES_INTERSTITIAL_DISMISSED,
FEATURED_IMAGE_SET_CLICKED_POST_SETTINGS,
FEATURED_IMAGE_PICKED_POST_SETTINGS,
FEATURED_IMAGE_PICKED_GUTENBERG_EDITOR,
FEATURED_IMAGE_REMOVED_GUTENBERG_EDITOR,
FEATURED_IMAGE_UPLOAD_CANCELED_POST_SETTINGS,
FEATURED_IMAGE_UPLOAD_RETRY_CLICKED_POST_SETTINGS,
FEATURED_IMAGE_REMOVE_CLICKED_POST_SETTINGS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1890,6 +1890,10 @@ public static String getEventNameForStat(AnalyticsTracker.Stat stat) {
return "featured_image_set_clicked_post_settings";
case FEATURED_IMAGE_PICKED_POST_SETTINGS:
return "featured_image_picked_post_settings";
case FEATURED_IMAGE_PICKED_GUTENBERG_EDITOR:
return "featured_image_picked_gutenberg_editor";
case FEATURED_IMAGE_REMOVED_GUTENBERG_EDITOR:
return "featured_image_removed_gutenberg_editor";
case FEATURED_IMAGE_UPLOAD_CANCELED_POST_SETTINGS:
return "featured_image_upload_canceled_post_settings";
case FEATURED_IMAGE_UPLOAD_RETRY_CLICKED_POST_SETTINGS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public static MediaType getEditorMimeType(MediaFile mediaFile) {
public interface EditorFragmentListener extends DialogVisibilityProvider {
void onEditorFragmentInitialized();
void onEditorFragmentContentReady(ArrayList<Object> unsupportedBlocks, boolean replaceBlockActionWaiting);
void updateFeaturedImage(long mediaId, boolean imagePicked);
void onAddMediaClicked();
void onAddMediaImageClicked(boolean allowMultipleSelection);
void onAddMediaVideoClicked(boolean allowMultipleSelection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode.OnMediaEditorListener;
import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode.OnMediaLibraryButtonListener;
import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode.OnMediaFilesCollectionBasedBlockEditorListener;
import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode.OnSetFeaturedImageListener;

import java.util.ArrayList;

Expand Down Expand Up @@ -56,6 +57,7 @@ public boolean hasReceivedAnyContent() {
public void attachToContainer(ViewGroup viewGroup, OnMediaLibraryButtonListener onMediaLibraryButtonListener,
OnReattachMediaUploadQueryListener onReattachQueryListener,
OnReattachMediaSavingQueryListener onStorySavingReattachQueryListener,
OnSetFeaturedImageListener onSetFeaturedImageListener,
OnEditorMountListener onEditorMountListener,
OnEditorAutosaveListener onEditorAutosaveListener,
OnAuthHeaderRequestedListener onAuthHeaderRequestedListener,
Expand All @@ -76,6 +78,7 @@ public void attachToContainer(ViewGroup viewGroup, OnMediaLibraryButtonListener
onMediaLibraryButtonListener,
onReattachQueryListener,
onStorySavingReattachQueryListener,
onSetFeaturedImageListener,
onEditorMountListener,
onEditorAutosaveListener,
onAuthHeaderRequestedListener,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package org.wordpress.android.editor.gutenberg

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.fragment.app.Fragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class GutenbergDialogFragment() : AppCompatDialogFragment() {
private lateinit var mTag: String
hypest marked this conversation as resolved.
Show resolved Hide resolved
private lateinit var mMessage: CharSequence
private lateinit var mPositiveButtonLabel: CharSequence
private var mTitle: CharSequence? = null
private var mNegativeButtonLabel: CharSequence? = null
private var mId: Int = 0
private var dismissedByPositiveButton = false
private var dismissedByNegativeButton = false

interface GutenbergDialogPositiveClickInterface {
fun onGutenbergDialogPositiveClicked(instanceTag: String, id: Int)
}

interface GutenbergDialogNegativeClickInterface {
fun onGutenbergDialogNegativeClicked(instanceTag: String)
}

interface GutenbergDialogOnDismissByOutsideTouchInterface {
fun onDismissByOutsideTouch(instanceTag: String)
}

fun initialize(
tag: String,
title: CharSequence?,
message: CharSequence,
positiveButtonLabel: CharSequence,
negativeButtonLabel: CharSequence? = null,
id: Int
) {
mTag = tag
mTitle = title
mMessage = message
mPositiveButtonLabel = positiveButtonLabel
mNegativeButtonLabel = negativeButtonLabel
mId = id
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.isCancelable = true
val theme = 0
setStyle(STYLE_NORMAL, theme)

if (savedInstanceState != null) {
mTag = requireNotNull(savedInstanceState.getString(STATE_KEY_TAG))
mTitle = savedInstanceState.getCharSequence(STATE_KEY_TITLE)
mMessage = requireNotNull(savedInstanceState.getCharSequence(STATE_KEY_MESSAGE))
mPositiveButtonLabel = requireNotNull(savedInstanceState.getCharSequence(STATE_KEY_POSITIVE_BUTTON_LABEL))
mNegativeButtonLabel = savedInstanceState.getCharSequence(STATE_KEY_NEGATIVE_BUTTON_LABEL)
mId = savedInstanceState.getInt(STATE_KEY_ID)
}
}

override fun onSaveInstanceState(outState: Bundle) {
outState.putString(STATE_KEY_TAG, mTag)
outState.putCharSequence(STATE_KEY_TITLE, mTitle)
outState.putCharSequence(STATE_KEY_MESSAGE, mMessage)
outState.putCharSequence(STATE_KEY_POSITIVE_BUTTON_LABEL, mPositiveButtonLabel)
outState.putCharSequence(STATE_KEY_NEGATIVE_BUTTON_LABEL, mNegativeButtonLabel)
outState.putInt(STATE_KEY_ID, mId)
super.onSaveInstanceState(outState)
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = MaterialAlertDialogBuilder(requireActivity())
builder.setMessage(mMessage)

mTitle?.let {
builder.setTitle(mTitle)
}

mPositiveButtonLabel?.let {
builder.setPositiveButton(mPositiveButtonLabel) { _, _ ->
dismissedByPositiveButton = true
(parentFragment as? GutenbergDialogPositiveClickInterface)?.let {
it.onGutenbergDialogPositiveClicked(mTag, mId)
}
}.setCancelable(true)
}

mNegativeButtonLabel?.let {
builder.setNegativeButton(mNegativeButtonLabel) { _, _ ->
dismissedByNegativeButton = true
(parentFragment as? GutenbergDialogNegativeClickInterface)?.let {
it.onGutenbergDialogNegativeClicked(mTag)
}
}
}

return builder.create()
}

override fun onAttach(context: Context) {
super.onAttach(context)
val parentFragment: Fragment? = parentFragment
if (parentFragment !is GutenbergDialogPositiveClickInterface) {
throw RuntimeException("Parent fragment must implement GutenbergDialogPositiveClickInterface")
}
if (mNegativeButtonLabel != null && parentFragment !is GutenbergDialogNegativeClickInterface) {
throw RuntimeException("Parent fragment must implement GutenbergDialogNegativeClickInterface")
}
}

override fun onDismiss(dialog: DialogInterface) {
// Only handle the event if it wasn't triggered by a button
if (!dismissedByPositiveButton && !dismissedByNegativeButton) {
(parentFragment as? GutenbergDialogOnDismissByOutsideTouchInterface)?.let {
it.onDismissByOutsideTouch(mTag)
}
}
super.onDismiss(dialog)
}

companion object {
private const val STATE_KEY_TAG = "state_key_tag"
private const val STATE_KEY_TITLE = "state_key_title"
private const val STATE_KEY_MESSAGE = "state_key_message"
private const val STATE_KEY_POSITIVE_BUTTON_LABEL = "state_key_positive_button_label"
private const val STATE_KEY_NEGATIVE_BUTTON_LABEL = "state_key_negative_button_label"
private const val STATE_KEY_ID = "state_key_id"
}
}
Loading