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

Fix crash in added views #292

Merged
merged 6 commits into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -36,7 +36,6 @@ import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import androidx.core.view.GestureDetectorCompat
import androidx.core.view.ViewCompat
import androidx.lifecycle.Lifecycle.State
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.automattic.photoeditor.OnPhotoEditorListener
Expand Down Expand Up @@ -322,6 +321,7 @@ class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelectorTapped
// see https://github.com/Automattic/portkey-android/issues/285 for details
Log.d("PORTKEY", "Being passed a SaveResult, render the Story")
storyViewModel.loadStory(storySaveResult.storyIndex)

if (!storySaveResult.success) {
val errorCount = storySaveResult.frameSaveResult.count { it.resultReason != SaveSuccess }
val firstFound = storySaveResult.frameSaveResult.first { it.resultReason != SaveSuccess }
Expand Down Expand Up @@ -654,14 +654,6 @@ class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelectorTapped
private fun saveStoryPostHook(result: StorySaveResult) {
doUnbindService()

if (!result.success && lifecycle.currentState.isAtLeast(State.STARTED)) {
// given saveStory for static images works with a ghost off screen buffer by removing /
// adding views to it,
// we need to refresh the selection so added views get properly re-added after frame iteration ends
storyViewModel.loadStory(result.storyIndex)
refreshStoryFrameSelection()
}

// re-enable layout change animations
photoEditorView.layoutTransition = transition

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ class FrameSaveManager(private val photoEditor: PhotoEditor) : CoroutineScope {
saveProgressListener?.onFrameSaveCompleted(frameIndex)
} catch (ex: Exception) {
saveProgressListener?.onFrameSaveFailed(frameIndex, ex.message)
} finally {
// if anything happened, just make sure the added views were removed from the offscreen
// photoEditor layout, otherwise it won't be possible to re-add them when we show the
// error screen and the user taps on the errored frames (crash will happen)
// Also, it's okay if this gets called more than once (for instance, when no exceptions
// are thrown) given it internally does check whether the parent contains the view before
// attempting to remove it
releaseAddedViewsAfterSnapshot(frame)
}
}
}
Expand All @@ -134,17 +142,23 @@ class FrameSaveManager(private val photoEditor: PhotoEditor) : CoroutineScope {
): File {
// prepare the ghostview with its background image and the AddedViews on top of it
preparePhotoEditorViewForSnapshot(frame, ghostPhotoEditorView)

val file = withContext(Dispatchers.IO) {
return@withContext photoEditor.saveImageFromPhotoEditorViewAsLoopFrameFile(frameIndex, ghostPhotoEditorView)
}

releaseAddedViewsAfterSnapshot(frame)

return file
}

private suspend fun releaseAddedViewsAfterSnapshot(frame: StoryFrameItem) {
withContext(Dispatchers.Main) {
// don't forget to remove these views from ghost offscreen view before exiting
for (oneView in frame.addedViews) {
removeViewFromParent(oneView.view)
}
}
return file
}

private suspend fun saveVideoFrame(
Expand Down