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

Mobile stories block - part 2: (on "done" editing clicked) #12982

Merged
Merged
Show file tree
Hide file tree
Changes from 62 commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
c21e3e3
passing blockId, mediaFiles and state in result
mzorz Sep 17, 2020
b9212ef
connect onActivityResult in EditPostActivity to call GutenbergEditorF…
mzorz Sep 17, 2020
acf2ef8
refactor to reuse block mediaFiles building
mzorz Sep 17, 2020
cd42ffb
connected the DONE button to pass the updated content story block bac…
mzorz Sep 17, 2020
edfe5af
fixed merge conflict
mzorz Sep 18, 2020
4873009
Merge branch 'try/jetpack-stories-block' into try/jetpack-stories-blo…
mzorz Sep 18, 2020
aacfac7
fixed merge conflict, moving call within failedLoadingOrReCreatingSto…
mzorz Sep 18, 2020
50769fa
Merge branch 'try/jetpack-stories-block' into try/jetpack-stories-blo…
mzorz Sep 18, 2020
9ef826d
fixed lint warnings
mzorz Sep 18, 2020
d7884c8
modified the bridge to have instances of EditorMedialListener so thes…
mzorz Sep 21, 2020
7f5453b
udpated stories lib hash
mzorz Sep 21, 2020
1e41de1
added all listeners to handle Story Save progress when editing a Stor…
mzorz Sep 21, 2020
e532633
updated stories library commit hash
mzorz Sep 22, 2020
2a74a35
removed unused param constant key
mzorz Sep 22, 2020
b545212
added FrameSaveXyz status EventBus events for Story frame flattened m…
mzorz Sep 22, 2020
baaad61
using StorySaveMediaListener and removed redundant interface StoryEdi…
mzorz Sep 24, 2020
a2e5aaa
using made-up temporaryIds out of StoryIndex-FrameIndex to paint save…
mzorz Sep 24, 2020
e5adabf
made changes to support String mediaIds as opposed to ints, so we can…
mzorz Sep 24, 2020
3db3235
using StoryRepositoryWrapper so it's injectable and lets StoryMediaSa…
mzorz Sep 24, 2020
d2af486
updated gutenberg-mobile commit hash, also passing overall story save…
mzorz Sep 25, 2020
5874a48
added progress calculation and progress calls
mzorz Sep 25, 2020
fcef7a8
implemented new onStorySaveResult gutenberg bridge method
mzorz Sep 25, 2020
edbc1e7
udpated commit hash for gutenberg mobile with stories block update
mzorz Sep 28, 2020
83a977e
added StoryFrameMediaModelCreatedEvent event to signal Gutenberg of t…
mzorz Sep 28, 2020
a2f0111
updated gutenberg hash, fixed media upload succesful mediaID and remo…
mzorz Sep 29, 2020
e0a108d
making sure to pass the Post's localId so it's loaded back in the bri…
mzorz Sep 29, 2020
12b2563
avoid IOBException by making sure the list is non-emtpy
mzorz Sep 29, 2020
77d27cf
only save a post in the bridge if we are not editing it - if we are e…
mzorz Sep 29, 2020
6114c61
fixed bug, was using the 'alt' field to look for a temporary id, shou…
mzorz Sep 29, 2020
0b016d6
fixed comment
mzorz Sep 29, 2020
23fcff5
refactored and added method StoriesPrefs.replaceLocalMediaIdKeyedSlid…
mzorz Sep 29, 2020
a8bfdb3
enclosed one check for media !=null needed by all other checks
mzorz Sep 29, 2020
b0ccacd
moved else if, wasn't ever going to run otherwise
mzorz Sep 29, 2020
ce59912
added call to updateAndSavePostAsync after adding a remote media to t…
mzorz Sep 29, 2020
9749799
fixed line length
mzorz Sep 29, 2020
fcfb771
updated stories lib hash to keep up with camera preview launch fix
mzorz Sep 29, 2020
3b6095c
updated gutenberg-mobile hash, commented lines
mzorz Sep 29, 2020
9bc74f3
using StoryRepositoryWrapper
mzorz Sep 29, 2020
7fedb76
updated stories cmomit hash with bugfixes
mzorz Sep 30, 2020
d9978b9
updated stories lib hash - fixed lint warnings
mzorz Sep 30, 2020
2ec71f9
removed old comment
mzorz Sep 30, 2020
e9dc830
add support for editing an existing uploaded Story and adding a frame…
mzorz Sep 30, 2020
4e005ae
implemented git onFrameRemove() listener method and cleaning StoriesP…
mzorz Sep 30, 2020
144e3ce
updated commit hash for stories library
mzorz Sep 30, 2020
64cc559
safeguard against a Story being corrupt (for example a crash when sav…
mzorz Sep 30, 2020
de7a2ba
updated gutenberg mobile commit hash
mzorz Sep 30, 2020
7c04d20
fixed lint warnings
mzorz Sep 30, 2020
0d554b4
updated mobile gutenberg commit hash
mzorz Sep 30, 2020
35f2f55
updated commit hash for gutenberg mobile with fix in gutenberg mobile…
mzorz Oct 1, 2020
8c0a245
updated gutenberg-mobile and jetpack story block commit hash
mzorz Oct 1, 2020
27c628a
fixed merge conflict
mzorz Oct 1, 2020
34509cb
added explicit null check
mzorz Oct 1, 2020
85a10af
updated commit hash for mobile-gutenberg
mzorz Oct 1, 2020
05eef82
fixed merge conflicts with base branch
mzorz Oct 7, 2020
240aebb
changed method signature for kotlin's Any
mzorz Oct 7, 2020
a6dee81
moved FrameSaveListener methods to StoriesEventListener lifecycle obs…
mzorz Oct 7, 2020
ecbdeb8
Merge branch 'try/jetpack-stories-block' into try/jetpack-stories-blo…
mzorz Oct 8, 2020
36a025f
fixed merge conflicts
mzorz Oct 8, 2020
1e5e84d
fixed method signature
mzorz Oct 8, 2020
4ca77a1
updated from base branch
mzorz Oct 9, 2020
8a4bc32
updated gutenberg-mobile hash
mzorz Oct 9, 2020
8905d3f
updated gutenberg-mobile hash
mzorz Oct 10, 2020
670fb08
updated gutenberg mobile hash
mzorz Oct 10, 2020
f3c949e
using new method names for gutetnberg-mobile bridge
mzorz Oct 10, 2020
69b1aa2
updated gutenberg-mobile commit hash
mzorz Oct 10, 2020
f7c9657
renamed bridge methods to more generic onMediaFilesEditorLoadRequestL…
mzorz Oct 10, 2020
864317f
updated gutenberg-mobile commit hash with bridge method renames
mzorz Oct 10, 2020
88fdf9d
updated commit hash for gutenberg-mobile and stories-android, minor l…
mzorz Oct 11, 2020
338208d
Merge branch 'try/jetpack-stories-block-on-done' into try/jetpack-sto…
mzorz Oct 11, 2020
745a1c7
updated gutenberg-mobile commit hash
mzorz Oct 11, 2020
d065c92
using eventBusWrapper
mzorz Oct 14, 2020
29f27c8
Update WordPress/src/main/java/org/wordpress/android/ui/posts/editor/…
mzorz Oct 14, 2020
5505f7d
Update WordPress/src/main/java/org/wordpress/android/ui/stories/SaveS…
mzorz Oct 14, 2020
8b9885f
stripped String type from method name, as it can be inferred from the…
mzorz Oct 14, 2020
f0c509d
Update WordPress/src/main/java/org/wordpress/android/ui/stories/Story…
mzorz Oct 14, 2020
8def90f
Update WordPress/src/main/java/org/wordpress/android/ui/stories/media…
mzorz Oct 14, 2020
eff3b28
renamed to shorten name
mzorz Oct 14, 2020
873a364
updated gutenberg-mobile commit hash
mzorz Oct 14, 2020
bb63fc3
updated commit hash for gutenberg-mobile
mzorz Oct 14, 2020
5349075
Merge pull request #13101 from wordpress-mobile/try/jetpack-stories-b…
mzorz Oct 14, 2020
640183e
updated commit hash after merge
mzorz Oct 14, 2020
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 @@ -18,6 +18,7 @@
import org.wordpress.android.analytics.AnalyticsTracker;
import org.wordpress.android.analytics.AnalyticsTracker.Stat;
import org.wordpress.android.datasets.ReaderPostTable;
import org.wordpress.android.fluxc.model.LocalOrRemoteId.LocalId;
import org.wordpress.android.fluxc.model.PostImmutableModel;
import org.wordpress.android.fluxc.model.PostModel;
import org.wordpress.android.fluxc.model.SiteModel;
Expand Down Expand Up @@ -98,12 +99,14 @@
import static org.wordpress.android.analytics.AnalyticsTracker.Stat.READER_ARTICLE_DETAIL_REBLOGGED;
import static org.wordpress.android.analytics.AnalyticsTracker.Stat.READER_ARTICLE_REBLOGGED;
import static org.wordpress.android.analytics.AnalyticsTracker.Stat.STATS_ACCESS_ERROR;
import static org.wordpress.android.editor.gutenberg.GutenbergEditorFragment.ARG_STORY_BLOCK_ID;
import static org.wordpress.android.imageeditor.preview.PreviewImageFragment.ARG_EDIT_IMAGE_DATA;
import static org.wordpress.android.login.LoginMode.WPCOM_LOGIN_ONLY;
import static org.wordpress.android.ui.media.MediaBrowserActivity.ARG_BROWSER_TYPE;
import static org.wordpress.android.ui.pages.PagesActivityKt.EXTRA_PAGE_REMOTE_ID_KEY;
import static org.wordpress.android.ui.stories.StoryComposerActivity.KEY_ALL_UNFLATTENED_LOADED_SLIDES;
import static org.wordpress.android.ui.stories.StoryComposerActivity.KEY_LAUNCHED_FROM_GUTENBERG;
import static org.wordpress.android.ui.stories.StoryComposerActivity.KEY_POST_LOCAL_ID;
import static org.wordpress.android.viewmodel.activitylog.ActivityLogDetailViewModelKt.ACTIVITY_LOG_ID_KEY;

public class ActivityLauncher {
Expand Down Expand Up @@ -741,19 +744,23 @@ public static void editStoryWithMediaIdsForResult(
public static void editStoryForResult(
Activity activity,
SiteModel site,
LocalId localPostId,
int storyIndex,
boolean allStorySlidesAreEditable,
boolean launchedFromGutenberg
boolean launchedFromGutenberg,
String storyBlockId
) {
if (site == null) {
return;
}

Intent intent = new Intent(activity, StoryComposerActivity.class);
intent.putExtra(WordPress.SITE, site);
intent.putExtra(KEY_POST_LOCAL_ID, localPostId.getValue());
intent.putExtra(KEY_STORY_INDEX, storyIndex);
intent.putExtra(KEY_LAUNCHED_FROM_GUTENBERG, launchedFromGutenberg);
intent.putExtra(KEY_ALL_UNFLATTENED_LOADED_SLIDES, allStorySlidesAreEditable);
intent.putExtra(ARG_STORY_BLOCK_ID, storyBlockId);
activity.startActivityForResult(intent, RequestCodes.EDIT_STORY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.wordpress.android.editor.gutenberg.GutenbergEditorFragment;
import org.wordpress.android.editor.gutenberg.GutenbergPropsBuilder;
import org.wordpress.android.editor.gutenberg.GutenbergWebViewAuthorizationData;
import org.wordpress.android.editor.gutenberg.StorySaveMediaListener;
import org.wordpress.android.fluxc.Dispatcher;
import org.wordpress.android.fluxc.action.AccountAction;
import org.wordpress.android.fluxc.generated.AccountActionBuilder;
Expand All @@ -82,6 +83,7 @@
import org.wordpress.android.fluxc.model.CauseOfOnPostChanged.RemoteAutoSavePost;
import org.wordpress.android.fluxc.model.EditorTheme;
import org.wordpress.android.fluxc.model.EditorThemeSupport;
import org.wordpress.android.fluxc.model.LocalOrRemoteId.LocalId;
import org.wordpress.android.fluxc.model.MediaModel;
import org.wordpress.android.fluxc.model.MediaModel.MediaUploadState;
import org.wordpress.android.fluxc.model.PostImmutableModel;
Expand Down Expand Up @@ -150,6 +152,7 @@
import org.wordpress.android.ui.posts.editor.StorePostViewModel.ActivityFinishState;
import org.wordpress.android.ui.posts.editor.StorePostViewModel.UpdateFromEditor;
import org.wordpress.android.ui.posts.editor.StorePostViewModel.UpdateFromEditor.PostFields;
import org.wordpress.android.ui.posts.editor.StoriesEventListener;
import org.wordpress.android.ui.posts.editor.media.AddExistingMediaSource;
import org.wordpress.android.ui.posts.editor.media.EditorMedia;
import org.wordpress.android.ui.posts.editor.media.EditorMediaListener;
Expand All @@ -162,6 +165,8 @@
import org.wordpress.android.ui.prefs.SiteSettingsInterface;
import org.wordpress.android.ui.reader.utils.ReaderUtilsWrapper;
import org.wordpress.android.ui.stockmedia.StockMediaPickerActivity;
import org.wordpress.android.ui.stories.StoryRepositoryWrapper;
import org.wordpress.android.ui.stories.prefs.StoriesPrefs;
import org.wordpress.android.ui.stories.usecase.LoadStoryFromStoriesPrefsUseCase;
import org.wordpress.android.ui.stories.usecase.LoadStoryFromStoriesPrefsUseCase.ReCreateStoryResult;
import org.wordpress.android.ui.uploads.PostEvents;
Expand Down Expand Up @@ -390,7 +395,10 @@ enum RestartEditorOptions {
@Inject ModalLayoutPickerFeatureConfig mModalLayoutPickerFeatureConfig;
@Inject CrashLogging mCrashLogging;
@Inject MediaPickerLauncher mMediaPickerLauncher;
@Inject StoryRepositoryWrapper mStoryRepositoryWrapper;
@Inject LoadStoryFromStoriesPrefsUseCase mLoadStoryFromStoriesPrefsUseCase;
@Inject StoriesPrefs mStoriesPrefs;
@Inject StoriesEventListener mStoriesEventListener;

private StorePostViewModel mViewModel;

Expand Down Expand Up @@ -573,6 +581,10 @@ protected void onCreate(Bundle savedInstanceState) {
if (mEditorFragment instanceof EditorMediaUploadListener) {
mEditorMediaUploadListener = (EditorMediaUploadListener) mEditorFragment;
}

if (mEditorFragment instanceof StorySaveMediaListener) {
mStoriesEventListener.setSaveMediaListener((StorySaveMediaListener) mEditorFragment);
}
}

if (mSite == null) {
Expand Down Expand Up @@ -649,6 +661,8 @@ protected void onCreate(Bundle savedInstanceState) {
ActivityId.trackLastActivity(ActivityId.POST_EDITOR);

setupPrepublishingBottomSheetRunnable();

mStoriesEventListener.start(this.getLifecycle(), mSite);
}

private void fetchSiteSettings() {
Expand Down Expand Up @@ -1586,13 +1600,30 @@ private ActivityFinishState savePostOnline(boolean isFirstTimePublish) {
}

private void onUploadSuccess(MediaModel media) {
// TODO Should this statement check media.getLocalPostId() == mEditPostRepository.getId()?
if (media != null && !media.getMarkedLocallyAsFeatured() && mEditorMediaUploadListener != null) {
mEditorMediaUploadListener.onMediaUploadSucceeded(String.valueOf(media.getId()),
FluxCUtils.mediaFileFromMediaModel(media));
} else if (media != null && media.getMarkedLocallyAsFeatured() && media.getLocalPostId() == mEditPostRepository
.getId()) {
setFeaturedImageId(media.getMediaId(), false);
if (media != null) {
// TODO Should this statement check media.getLocalPostId() == mEditPostRepository.getId()?
if (!media.getMarkedLocallyAsFeatured() && mEditorMediaUploadListener != null) {
mEditorMediaUploadListener.onMediaUploadSucceeded(String.valueOf(media.getId()),
FluxCUtils.mediaFileFromMediaModel(media));
if (PostUtils.contentContainsWPStoryGutenbergBlocks(mEditPostRepository.getContent())) {
// make sure to sync the local post object with the UI and save
updateAndSavePostAsync();
// if this is a Story media item, then make sure to keep up with the StoriesPrefs serialized slides
// this looks for the slide saved with the local id key (media.getId()), and re-converts it to
// mediaId.
// Also: we don't need to worry about checking if this mediaModel corresponds to a media upload
// within a story block in this post: we will only replace items for which a local-keyed frame has
// been created before, which can only happen when using the Story Creator.
mStoriesPrefs.replaceLocalMediaIdKeyedSlideWithRemoteMediaIdKeyedSlide(
media.getId(),
media.getMediaId(),
mSite.getId()
);
}
} else if (media.getMarkedLocallyAsFeatured() && media.getLocalPostId() == mEditPostRepository
.getId()) {
setFeaturedImageId(media.getMediaId(), false);
}
}
}

Expand Down Expand Up @@ -2162,6 +2193,10 @@ public Fragment getItem(int position) {

reattachUploadingMediaForAztec();
}

if (mEditorFragment instanceof StorySaveMediaListener) {
mStoriesEventListener.setSaveMediaListener((StorySaveMediaListener) mEditorFragment);
}
break;
case PAGE_SETTINGS:
mEditPostSettingsFragment = (EditPostSettingsFragment) fragment;
Expand Down Expand Up @@ -2456,6 +2491,13 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}

if (requestCode == RequestCodes.EDIT_STORY) {
if (mEditorFragment instanceof GutenbergEditorFragment) {
mEditorFragment.onActivityResult(requestCode, resultCode, data);
return;
}
}

if (data != null || ((requestCode == RequestCodes.TAKE_PHOTO || requestCode == RequestCodes.TAKE_VIDEO
|| requestCode == RequestCodes.PHOTO_PICKER))) {
switch (requestCode) {
Expand Down Expand Up @@ -3067,12 +3109,31 @@ public void onTrackableEvent(TrackableEvent event, Map<String, String> propertie
}

@Override public void onStoryComposerLoadRequested(ArrayList<Object> mediaFiles, String blockId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NitPick 😅 Hi @mzorz could we put these dialogs and such forth in this method in another class? Or is it not worth it? I just glanced at the code again and it might not be worth the effort. If we could somehow simplify the dialog code with a Util function or something so we don't add too many lines here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I understand where you come from - I think the logic is simple enough to follow but it's true it's "too vertical" and exceeds the screen, to add that we don't want this class to keep growing much. Might do that at a later phase 👍 (added to my ToDo list as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just following up here - addressed this here #13198

if (mLoadStoryFromStoriesPrefsUseCase.anyMediaIdsInGutenbergStoryBlockAreCorrupt(mediaFiles)) {
mzorz marked this conversation as resolved.
Show resolved Hide resolved
// unfortunately the medaiIds seem corrupt so, show a dialog and bail
mzorz marked this conversation as resolved.
Show resolved Hide resolved
AlertDialog.Builder builder = new MaterialAlertDialogBuilder(this);
builder.setTitle(getString(R.string.dialog_edit_story_unavailable_title));
builder.setMessage(getString(R.string.dialog_edit_story_corrupt_message));
builder.setPositiveButton(R.string.dialog_button_ok, (dialog, id) -> {
dialog.dismiss();
});
AlertDialog dialog = builder.create();
dialog.show();
return;
}

ReCreateStoryResult result = mLoadStoryFromStoriesPrefsUseCase
.loadStoryFromMemoryOrRecreateFromPrefs(mSite, mediaFiles);
if (!result.getNoSlidesLoaded()) {
// Story instance loaded or re-created! Load it
// Story instance loaded or re-created! Load it onto the StoryComposer for editing now
ActivityLauncher.editStoryForResult(
this, mSite, result.getStoryIndex(), result.getAllStorySlidesAreEditable(), true
this,
mSite,
new LocalId(mEditPostRepository.getId()),
result.getStoryIndex(),
result.getAllStorySlidesAreEditable(),
true,
blockId
);
} else {
// unfortunately we couldn't even load the remote media Ids indicated by the StoryBlock so we can't allow
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package org.wordpress.android.ui.posts.editor

import android.net.Uri
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.State.CREATED
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.wordpress.stories.compose.frame.StorySaveEvents.FrameSaveCompleted
import com.wordpress.stories.compose.frame.StorySaveEvents.FrameSaveFailed
import com.wordpress.stories.compose.frame.StorySaveEvents.FrameSaveProgress
import com.wordpress.stories.compose.frame.StorySaveEvents.FrameSaveStart
import com.wordpress.stories.compose.frame.StorySaveEvents.StorySaveResult
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.wordpress.android.editor.gutenberg.StorySaveMediaListener
import org.wordpress.android.fluxc.Dispatcher
import org.wordpress.android.fluxc.model.MediaModel
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.store.MediaStore
import org.wordpress.android.ui.stories.SaveStoryGutenbergBlockUseCase.Companion.TEMPORARY_ID_PREFIX
import org.wordpress.android.ui.stories.StoryRepositoryWrapper
import org.wordpress.android.ui.stories.media.StoryMediaSaveUploadBridge.StoryFrameMediaModelCreatedEvent
import org.wordpress.android.util.FluxCUtils
import org.wordpress.android.util.helpers.MediaFile
import javax.inject.Inject

class StoriesEventListener @Inject constructor(
private val dispatcher: Dispatcher,
private val mediaStore: MediaStore,
private val storyRepositoryWrapper: StoryRepositoryWrapper
) : LifecycleObserver {
private lateinit var lifecycle: Lifecycle
private lateinit var site: SiteModel
private var storySaveMediaListener: StorySaveMediaListener? = null

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private fun onCreate() {
dispatcher.register(this)
EventBus.getDefault().register(this)
mzorz marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Handles the [Lifecycle.Event.ON_DESTROY] event to cleanup the registration for dispatcher and removing the
* observer for lifecycle.
*/
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private fun onDestroy() {
lifecycle.removeObserver(this)
dispatcher.unregister(this)
EventBus.getDefault().unregister(this)
}

fun start(lifecycle: Lifecycle, site: SiteModel) {
this.site = site
this.lifecycle = lifecycle
this.lifecycle.addObserver(this)
}

fun setSaveMediaListener(newListener: StorySaveMediaListener) {
storySaveMediaListener = newListener
}

// Story Frame Save Service events
@Subscribe(threadMode = ThreadMode.MAIN)
fun onStoryFrameSaveStart(event: FrameSaveStart) {
if (!lifecycle.currentState.isAtLeast(CREATED)) {
return
}
val localMediaId = event.frameId.toString()
val progress: Float = storyRepositoryWrapper.getCurrentStorySaveProgress(event.storyIndex, 0.0f)
mzorz marked this conversation as resolved.
Show resolved Hide resolved
storySaveMediaListener?.onMediaSaveReattached(localMediaId, progress)
}

@Subscribe(threadMode = ThreadMode.MAIN)
fun onStoryFrameSaveProgress(event: FrameSaveProgress) {
if (!lifecycle.currentState.isAtLeast(CREATED)) {
return
}
val localMediaId = event.frameId.toString()
val progress: Float = storyRepositoryWrapper.getCurrentStorySaveProgress(
event.storyIndex,
event.progress
)
storySaveMediaListener?.onMediaSaveProgress(localMediaId, progress)
}

@Subscribe(threadMode = ThreadMode.MAIN)
fun onStoryFrameSaveCompleted(event: FrameSaveCompleted) {
if (!lifecycle.currentState.isAtLeast(CREATED)) {
return
}
val localMediaId = requireNotNull(event.frameId)

// check whether this is a temporary file being just saved (so we don't have a proper local MediaModel yet)
// catch ( NumberFormatException e)
if (localMediaId.startsWith(TEMPORARY_ID_PREFIX)) {
val (frames) = storyRepositoryWrapper.getStoryAtIndex(event.storyIndex)

// first, update the media's url
val frame = frames[event.frameIndex]
storySaveMediaListener?.onMediaSaveSucceeded(
localMediaId,
Uri.fromFile(frame.composedFrameFile).toString()
)

// now update progress
val totalProgress: Float = storyRepositoryWrapper.getCurrentStorySaveProgress(
event.storyIndex,
0.0f
)
storySaveMediaListener?.onMediaSaveProgress(localMediaId, totalProgress)
} else {
val mediaModel: MediaModel = mediaStore.getSiteMediaWithId(site, localMediaId.toLong())
if (mediaModel != null) {
val mediaFile: MediaFile = FluxCUtils.mediaFileFromMediaModel(mediaModel)
storySaveMediaListener?.onMediaSaveSucceeded(localMediaId, mediaFile.getFileURL())
}
}
}

@Subscribe(threadMode = ThreadMode.MAIN)
fun onStoryFrameMediaModelCreated(event: StoryFrameMediaModelCreatedEvent) {
if (!lifecycle.currentState.isAtLeast(CREATED)) {
return
}
storySaveMediaListener?.onMediaModelCreatedForFile(event.oldId, event.newId, event.oldUrl)
}

@Subscribe(threadMode = ThreadMode.MAIN)
fun onStoryFrameSaveFailed(event: FrameSaveFailed) {
if (!lifecycle.currentState.isAtLeast(CREATED)) {
return
}
val localMediaId = event.frameId.toString()
// just update progress, we may have still some other frames in this story that need be saved.
// we will send the Failed signal once all the Story frames have been processed
val progress: Float = storyRepositoryWrapper.getCurrentStorySaveProgress(event.storyIndex, 0.0f)
storySaveMediaListener?.onMediaSaveReattached(localMediaId, progress)
}

@Subscribe(threadMode = ThreadMode.MAIN)
fun onStorySaveProcessFinished(event: StorySaveResult) {
if (!lifecycle.currentState.isAtLeast(CREATED)) {
return
}
val story = storyRepositoryWrapper.getStoryAtIndex(event.storyIndex)
if (event.isSuccess() && event.frameSaveResult.size == story.frames.size) {
// take the first frame IDs and mediaUri
val localMediaId = story.frames[0].id.toString()
val mediaUrl: String = Uri.fromFile(story.frames[0].composedFrameFile).toString()
storySaveMediaListener?.onMediaSaveSucceeded(localMediaId, mediaUrl)
} else {
val localMediaId = story.frames[0].id.toString()
storySaveMediaListener?.onMediaSaveFailed(localMediaId)
}
}
}
Loading