From 250028599a649c1898e633b89e21973179e35643 Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Thu, 2 Jul 2020 11:00:34 -0300 Subject: [PATCH 01/23] setup kotlinx serialization plugin --- build.gradle | 2 ++ photoeditor/build.gradle | 2 ++ stories/build.gradle | 2 ++ 3 files changed, 6 insertions(+) diff --git a/build.gradle b/build.gradle index 834da88af..f0eeb58b6 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,7 @@ buildscript { ext.kotlinVersion = '1.3.61' + ext.serializationVersion = '0.14.0' repositories { google() jcenter() @@ -10,6 +11,7 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:3.5.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" + classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlinVersion" classpath 'com.automattic.android:fetchstyle:1.1' classpath 'io.sentry:sentry-android-gradle-plugin:1.7.27' // NOTE: Do not place your application dependencies here; they belong diff --git a/photoeditor/build.gradle b/photoeditor/build.gradle index 92cb2cbc4..e500caf7c 100644 --- a/photoeditor/build.gradle +++ b/photoeditor/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' +apply plugin: 'kotlinx-serialization' android { compileSdkVersion rootProject.compileSdkVersion @@ -36,6 +37,7 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion" + implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationVersion" implementation "androidx.appcompat:appcompat:$appCompatVersion" implementation "androidx.core:core-ktx:$coreVersion" diff --git a/stories/build.gradle b/stories/build.gradle index 2defded7c..545e636d9 100644 --- a/stories/build.gradle +++ b/stories/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' +apply plugin: 'kotlinx-serialization' android { compileSdkVersion rootProject.compileSdkVersion @@ -34,6 +35,7 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion" + implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationVersion" implementation "androidx.appcompat:appcompat:$appCompatVersion" implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion" From c25f9cd92eba646978a82d0c61de61e73e2d38bc Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Thu, 2 Jul 2020 11:24:05 -0300 Subject: [PATCH 02/23] added @Serializable annotations, changed AddedViewList constructor to make it Serializable --- .../automattic/photoeditor/views/added/AddedViewList.kt | 9 ++++++--- .../stories/compose/ComposeLoopFrameActivity.kt | 2 +- .../wordpress/stories/compose/frame/StorySaveEvents.kt | 1 + .../java/com/wordpress/stories/compose/story/Story.kt | 3 +++ .../wordpress/stories/compose/story/StoryFrameItem.kt | 5 +++++ .../stories/compose/story/StoryFrameItemType.kt | 3 +++ 6 files changed, 19 insertions(+), 4 deletions(-) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt index 1b0e80d27..a01126275 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt @@ -3,12 +3,15 @@ package com.automattic.photoeditor.views.added import android.view.View import com.automattic.photoeditor.views.ViewType import java.util.ArrayList +import kotlinx.serialization.Serializable -class AddedViewList(addedViewList: AddedViewList? = null) : ArrayList() { - init { - addedViewList?.let { +@Serializable +class AddedViewList : ArrayList() { + fun copyOf(addedViewList: AddedViewList): AddedViewList { + addedViewList.let { addAll(it) } + return this } fun containsView(element: View): Boolean { diff --git a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt index 808dc5b57..0ee0ca35d 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt @@ -1059,7 +1059,7 @@ abstract class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelec } // set addedViews on the current frame (copy array so we don't share the same one with PhotoEditor) - currentStoryFrameItem?.addedViews = AddedViewList(photoEditor.getViewsAdded()) + currentStoryFrameItem?.addedViews = AddedViewList().copyOf(photoEditor.getViewsAdded()) } private fun showMediaPicker() { diff --git a/stories/src/main/java/com/wordpress/stories/compose/frame/StorySaveEvents.kt b/stories/src/main/java/com/wordpress/stories/compose/frame/StorySaveEvents.kt index f240d761a..e9ebd0a8e 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/frame/StorySaveEvents.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/frame/StorySaveEvents.kt @@ -24,6 +24,7 @@ class StorySaveEvents { @Parcelize data class FrameSaveResult(val frameIndex: FrameIndex, val resultReason: SaveResultReason) : Parcelable + @kotlinx.serialization.Serializable sealed class SaveResultReason : Parcelable { @Parcelize object SaveSuccess : SaveResultReason() diff --git a/stories/src/main/java/com/wordpress/stories/compose/story/Story.kt b/stories/src/main/java/com/wordpress/stories/compose/story/Story.kt index 9d62eba95..126df92f0 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/story/Story.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/story/Story.kt @@ -1,3 +1,6 @@ package com.wordpress.stories.compose.story +import kotlinx.serialization.Serializable + +@Serializable data class Story(val frames: ArrayList, var title: String? = null) diff --git a/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameItem.kt b/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameItem.kt index 47f86df48..466d0390a 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameItem.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameItem.kt @@ -5,15 +5,20 @@ import com.automattic.photoeditor.views.added.AddedViewList import com.wordpress.stories.compose.frame.StorySaveEvents.SaveResultReason import com.wordpress.stories.compose.frame.StorySaveEvents.SaveResultReason.SaveSuccess import com.wordpress.stories.compose.story.StoryFrameItemType.IMAGE +import kotlinx.serialization.ContextualSerialization +import kotlinx.serialization.Serializable import java.io.File +@Serializable data class StoryFrameItem( val source: BackgroundSource, val frameItemType: StoryFrameItemType = IMAGE, var addedViews: AddedViewList = AddedViewList(), var saveResultReason: SaveResultReason = SaveSuccess, + @ContextualSerialization var composedFrameFile: File? = null ) { + @Serializable sealed class BackgroundSource { data class UriBackgroundSource(var contentUri: Uri? = null) : BackgroundSource() data class FileBackgroundSource(var file: File? = null) : BackgroundSource() diff --git a/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameItemType.kt b/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameItemType.kt index fdbfde6da..2851648d0 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameItemType.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameItemType.kt @@ -1,5 +1,8 @@ package com.wordpress.stories.compose.story +import kotlinx.serialization.Serializable + +@Serializable sealed class StoryFrameItemType { fun isSameType(second: StoryFrameItemType): Boolean { return this::class == second::class From b855ea60e08452098fdd0c8c81b77eeb45d1bd0c Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Fri, 3 Jul 2020 11:26:22 -0300 Subject: [PATCH 03/23] implemented serializer/deserializer and added AddedViewInfo structure to hold serializable data for Views --- .../com/automattic/photoeditor/PhotoEditor.kt | 35 +++--- .../automattic/photoeditor/views/ViewType.kt | 3 + .../photoeditor/views/added/AddedView.kt | 101 +++++++++++++++++- .../photoeditor/views/added/AddedViewInfo.kt | 12 +++ .../photoeditor/views/added/AddedViewList.kt | 20 ++-- .../compose/ComposeLoopFrameActivity.kt | 12 ++- .../stories/compose/frame/FrameSaveManager.kt | 8 +- .../compose/story/StorySerializerUtils.kt | 28 +++++ 8 files changed, 192 insertions(+), 27 deletions(-) create mode 100644 photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewInfo.kt create mode 100644 stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt index dac85a8d4..dd7938bcd 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt @@ -35,6 +35,7 @@ import com.automattic.photoeditor.views.ViewType import com.automattic.photoeditor.views.ViewType.EMOJI import com.automattic.photoeditor.views.ViewType.TEXT import com.automattic.photoeditor.views.added.AddedView +import com.automattic.photoeditor.views.added.AddedView.Companion import com.automattic.photoeditor.views.added.AddedViewList import com.automattic.photoeditor.views.brush.BrushDrawingView import com.automattic.photoeditor.views.brush.BrushViewChangeListener @@ -300,7 +301,9 @@ class PhotoEditor private constructor(builder: Builder) : inputTextView.setTextColor(colorCode) parentView.updateViewLayout(view, view.layoutParams) val i = addedViews.indexOfView(view) - if (i > -1) addedViews[i] = AddedView(view, addedViews[i].viewType) + if (i > -1) { + addedViews[i] = AddedView.buildAddedViewFromView(view, addedViews[i].viewType) + } } } @@ -379,7 +382,7 @@ class PhotoEditor private constructor(builder: Builder) : ) params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE) parentView.addView(rootView, params) - addedViews.add(AddedView(rootView, viewType, sourceUri)) + addedViews.add(AddedView.buildAddedViewFromView(rootView, viewType, sourceUri)) mOnPhotoEditorListener?.onAddViewListener(viewType, addedViews.size) } @@ -544,7 +547,7 @@ class PhotoEditor private constructor(builder: Builder) : redoViews.add(removeView) } mOnPhotoEditorListener?.onRemoveViewListener(addedViews.size) - val viewTag = removeView.view.tag + val viewTag = removeView.view?.tag (viewTag as? ViewType)?.let { mOnPhotoEditorListener?.onRemoveViewListener(it, addedViews.size) } @@ -567,7 +570,7 @@ class PhotoEditor private constructor(builder: Builder) : parentView.addView(redoView.view) addedViews.add(redoView) } - val viewTag = redoView.view.tag + val viewTag = redoView.view?.tag if (viewTag != null && viewTag is ViewType) { mOnPhotoEditorListener?.onAddViewListener(viewTag, addedViews.size) } @@ -585,7 +588,9 @@ class PhotoEditor private constructor(builder: Builder) : */ fun clearAllViews() { for (addedView in addedViews) { - parentView.removeView(addedView.view) + addedView.view?.let { + parentView.removeView(it) + } } if (addedViews.containsView(brushDrawingView)) { @@ -735,9 +740,9 @@ class PhotoEditor private constructor(builder: Builder) : val viewPositionInfo = ViewPositionInfo( originalCanvasWidth, originalCanvasHeight, - v.view.width, - v.view.height, - v.view.matrix + requireNotNull(v.view).width, + requireNotNull(v.view).height, + requireNotNull(v.view).matrix ) when (v.viewType) { ViewType.STICKER_ANIMATED -> { @@ -887,9 +892,9 @@ class PhotoEditor private constructor(builder: Builder) : val viewPositionInfo = ViewPositionInfo( widthParent, heightParent, - v.view.width, - v.view.height, - v.view.matrix + requireNotNull(v.view).width, + requireNotNull(v.view).height, + requireNotNull(v.view).matrix ) when (v.viewType) { ViewType.STICKER_ANIMATED -> { @@ -975,15 +980,17 @@ class PhotoEditor private constructor(builder: Builder) : if (redoViews.size > 0) { redoViews.removeAt(redoViews.size - 1) } - addedViews.add(AddedView(brushDrawingView, ViewType.BRUSH_DRAWING)) + addedViews.add(AddedView.buildAddedViewFromView(brushDrawingView, ViewType.BRUSH_DRAWING)) mOnPhotoEditorListener?.onAddViewListener(ViewType.BRUSH_DRAWING, addedViews.size) } override fun onViewRemoved(brushDrawingView: BrushDrawingView) { if (addedViews.size > 0) { val removeView = addedViews.removeAt(addedViews.size - 1) - if (removeView.view !is BrushDrawingView) { - parentView.removeView(removeView.view) + removeView.view?.let { + if (it !is BrushDrawingView) { + parentView.removeView(it) + } } redoViews.add(removeView) } diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/views/ViewType.kt b/photoeditor/src/main/java/com/automattic/photoeditor/views/ViewType.kt index 2d548118c..b9e61d5e4 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/views/ViewType.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/views/ViewType.kt @@ -1,5 +1,7 @@ package com.automattic.photoeditor.views +import kotlinx.serialization.Serializable + /** * * @@ -10,6 +12,7 @@ package com.automattic.photoeditor.views * @version 0.1.1 * @since 18/01/2017. */ +@Serializable enum class ViewType { BRUSH_DRAWING, TEXT, diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt index a6812b5a2..1079add2a 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt @@ -2,6 +2,105 @@ package com.automattic.photoeditor.views.added import android.net.Uri import android.view.View +import android.widget.TextView +import com.automattic.photoeditor.R import com.automattic.photoeditor.views.ViewType +import com.automattic.photoeditor.views.ViewType.EMOJI +import com.automattic.photoeditor.views.ViewType.TEXT +import kotlinx.serialization.CompositeDecoder +import kotlinx.serialization.Decoder +import kotlinx.serialization.Encoder +import kotlinx.serialization.KSerializer +import kotlinx.serialization.MissingFieldException +import kotlinx.serialization.SerialDescriptor +import kotlinx.serialization.Serializable +import kotlinx.serialization.SerializationException +import kotlinx.serialization.Serializer +import kotlinx.serialization.internal.StringDescriptor +import kotlinx.serialization.withName -class AddedView(val view: View, val viewType: ViewType, val uri: Uri? = null) +@Serializable +class AddedView( + @Transient val view: View?, + val viewType: ViewType, + val viewInfo: AddedViewInfo, + @Serializable(with = UriSerializer::class) + val uri: Uri? = null +) { + @Serializer(forClass = AddedView::class) + companion object : KSerializer { + override val descriptor: SerialDescriptor = StringDescriptor.withName("AddedView") + + override fun serialize(encoder: Encoder, obj: AddedView) { + val compositeOutput = encoder.beginStructure(descriptor) + compositeOutput.encodeSerializableElement(descriptor, 0, ViewType.serializer(), obj.viewType) + compositeOutput.encodeSerializableElement(descriptor, 1, AddedViewInfo.serializer(), obj.viewInfo) + obj.uri?.let { + compositeOutput.encodeSerializableElement(descriptor, 2, UriSerializer, it) + } + compositeOutput.endStructure(descriptor) + } + + override fun deserialize(decoder: Decoder): AddedView { + var viewType: ViewType? = null + var viewInfo: AddedViewInfo? = null + var uri: Uri? = null + + decoder.beginStructure(descriptor).run { + loop@ while (true) { + when (val i = decodeElementIndex(descriptor)) { + CompositeDecoder.READ_DONE -> break@loop + 0 -> viewType = decodeSerializableElement(descriptor, i, ViewType.serializer()) + 1 -> viewInfo = decodeSerializableElement(descriptor, i, AddedViewInfo.serializer()) + 2 -> uri = decodeSerializableElement(descriptor, i, UriSerializer) + else -> throw SerializationException("Unknown index $i") + } + } + endStructure(descriptor) + } + return AddedView( + null, // this will be initialized later when inflated and added to a ViewGroup + viewType ?: throw MissingFieldException("viewType"), + viewInfo ?: throw MissingFieldException("viewInfo"), + uri + ) + } + + fun buildAddedViewFromView( + view: View, + viewType: ViewType, + uri: Uri? = null + ) : AddedView { + var text = "" + when (viewType) { + EMOJI -> { + val txtView = view.findViewById(R.id.tvPhotoEditorEmoji) + text = txtView.text.toString() + } + TEXT -> { + val txtView = view.findViewById(R.id.tvPhotoEditorText) + text = txtView.text.toString() + } + } + var viewInfo = AddedViewInfo( + view.rotation, + view.translationX, + view.translationY, + view.scaleX, + text + ) + return AddedView(view, viewType, viewInfo, uri) + } + } + + @Serializer(forClass = Uri::class) + object UriSerializer : KSerializer { + override fun deserialize(input: Decoder): Uri { + return Uri.parse(input.decodeString()) + } + + override fun serialize(output: Encoder, obj: Uri) { + output.encodeString(obj.toString()) + } + } +} diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewInfo.kt b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewInfo.kt new file mode 100644 index 000000000..d08732a60 --- /dev/null +++ b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewInfo.kt @@ -0,0 +1,12 @@ +package com.automattic.photoeditor.views.added + +import kotlinx.serialization.Serializable + +@Serializable +data class AddedViewInfo( + val rotation: Float, + val translationX: Float, + val translationY: Float, + val scale: Float, + val text: String +) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt index a01126275..43d1658b3 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt @@ -16,25 +16,31 @@ class AddedViewList : ArrayList() { fun containsView(element: View): Boolean { for (n in this) { - if (n.view == element) { - return true + n.view?.let{ + if (it == element) { + return true + } } } return false } fun removeView(element: View): AddedView? { for (n in this) { - if (n.view == element) { - this.remove(n) - return n + n.view?.let { + if (it == element) { + this.remove(n) + return n + } } } return null } fun indexOfView(element: View): Int { for (n in this) { - if (n.view == element) { - return this.indexOf(n) + n.view?.let { + if (it == element) { + return this.indexOf(n) + } } } return -1 diff --git a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt index 0ee0ca35d..7cfb325f1 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt @@ -91,6 +91,8 @@ import com.wordpress.stories.compose.story.StoryRepository import com.wordpress.stories.compose.story.StoryViewModel import com.wordpress.stories.compose.story.StoryViewModel.StoryFrameListItemUiState.StoryFrameListItemUiStateFrame import com.wordpress.stories.compose.story.StoryViewModelFactory +import com.wordpress.stories.compose.story.deserializeAddedViews +import com.wordpress.stories.compose.story.serializeAddedViews import com.wordpress.stories.compose.text.TextEditorDialogFragment import com.wordpress.stories.util.KEY_STORY_SAVE_RESULT import com.wordpress.stories.util.STATE_KEY_CURRENT_STORY_INDEX @@ -1055,11 +1057,17 @@ abstract class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelec // purge multitouch listeners val addedViews = photoEditor.getViewsAdded() for (addedView in addedViews) { - addedView.view.setOnTouchListener(null) + addedView.view?.setOnTouchListener(null) } // set addedViews on the current frame (copy array so we don't share the same one with PhotoEditor) currentStoryFrameItem?.addedViews = AddedViewList().copyOf(photoEditor.getViewsAdded()) + + // TODO REMOVE TEST CODE START + val serializedObj = serializeAddedViews(photoEditor.getViewsAdded()) + val deserializedObj = deserializeAddedViews(serializedObj) + currentStoryFrameItem?.addedViews = deserializedObj + // TODO REMOVE TEST CODE END } private fun showMediaPicker() { @@ -1760,7 +1768,7 @@ abstract class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelec // now call addViewToParent the addedViews remembered by this frame newSelectedFrame.addedViews.let { for (oneView in it) { - photoEditor.addViewToParentWithTouchListener(oneView.view, oneView.viewType) + photoEditor.addViewToParentWithTouchListener(requireNotNull(oneView.view), oneView.viewType) } } diff --git a/stories/src/main/java/com/wordpress/stories/compose/frame/FrameSaveManager.kt b/stories/src/main/java/com/wordpress/stories/compose/frame/FrameSaveManager.kt index db04374d7..4a6d0c691 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/frame/FrameSaveManager.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/frame/FrameSaveManager.kt @@ -277,8 +277,10 @@ class FrameSaveManager(private val photoEditor: PhotoEditor) : CoroutineScope { withContext(Dispatchers.Main) { // now call addViewToParent the addedViews remembered by this frame for (oneView in frame.addedViews) { - removeViewFromParent(oneView.view) - ghostPhotoEditorView.addView(oneView.view, getViewLayoutParams()) + oneView.view?.let { + removeViewFromParent(it) + ghostPhotoEditorView.addView(it, getViewLayoutParams()) + } } } } @@ -315,7 +317,7 @@ class FrameSaveManager(private val photoEditor: PhotoEditor) : CoroutineScope { fun releaseAddedViews(frame: StoryFrameItem) { // don't forget to remove these views from ghost offscreen view before exiting for (oneView in frame.addedViews) { - removeViewFromParent(oneView.view) + removeViewFromParent(requireNotNull(oneView.view)) } } } diff --git a/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt b/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt new file mode 100644 index 000000000..a80667dde --- /dev/null +++ b/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt @@ -0,0 +1,28 @@ +package com.wordpress.stories.compose.story + +import com.automattic.photoeditor.views.added.AddedViewList +import kotlinx.serialization.json.Json + +fun serializeStory(story: Story): String { + return Json.stringify(Story.serializer(), story) +} + +fun serializeStoryFrameItem(storyFrameItem: StoryFrameItem): String { + return Json.stringify(StoryFrameItem.serializer(), storyFrameItem) +} + +fun deserializeStory(story: String): Story { + return Json.parse(Story.serializer(), story) +} + +fun deserializeStoryFrameItem(storyFrameItem: String): StoryFrameItem { + return Json.parse(StoryFrameItem.serializer(), storyFrameItem) +} + +fun serializeAddedViews(addedViews: AddedViewList): String { + return Json.stringify(AddedViewList.serializer(), addedViews) +} + +fun deserializeAddedViews(addedViews: String): AddedViewList { + return Json.parse(AddedViewList.serializer(), addedViews) +} From ce53d6abce6431bea1ff7f045945b70bb20bb52c Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Sat, 4 Jul 2020 20:35:11 -0300 Subject: [PATCH 04/23] added update() method to update AddedViewInfo when it's saved --- .../photoeditor/views/added/AddedView.kt | 27 ++++++++++++++++--- .../compose/ComposeLoopFrameActivity.kt | 12 ++++++++- .../compose/story/StorySerializerUtils.kt | 5 ++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt index 1079add2a..84fa07dcd 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt @@ -23,7 +23,7 @@ import kotlinx.serialization.withName class AddedView( @Transient val view: View?, val viewType: ViewType, - val viewInfo: AddedViewInfo, + var viewInfo: AddedViewInfo, @Serializable(with = UriSerializer::class) val uri: Uri? = null ) { @@ -47,7 +47,7 @@ class AddedView( var uri: Uri? = null decoder.beginStructure(descriptor).run { - loop@ while (true) { + loop@while (true) { when (val i = decodeElementIndex(descriptor)) { CompositeDecoder.READ_DONE -> break@loop 0 -> viewType = decodeSerializableElement(descriptor, i, ViewType.serializer()) @@ -71,6 +71,10 @@ class AddedView( viewType: ViewType, uri: Uri? = null ) : AddedView { + return AddedView(view, viewType, buildViewInfoFromView(view, getTextFromActualView(view, viewType)), uri) + } + + fun getTextFromActualView(view: View, viewType: ViewType): String { var text = "" when (viewType) { EMOJI -> { @@ -82,14 +86,29 @@ class AddedView( text = txtView.text.toString() } } - var viewInfo = AddedViewInfo( + return text + } + + fun buildViewInfoFromView(view: View, text: String): AddedViewInfo { + return AddedViewInfo( view.rotation, view.translationX, view.translationY, view.scaleX, text ) - return AddedView(view, viewType, viewInfo, uri) + } + } + + fun update() { + view?.let { + viewInfo = AddedViewInfo( + it.rotation, + it.translationX, + it.translationY, + it.scaleX, + getTextFromActualView(view, viewType) + ) } } diff --git a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt index 50010370a..1c668f078 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt @@ -56,6 +56,7 @@ import com.automattic.photoeditor.util.FileUtils.Companion.getLoopFrameFile import com.automattic.photoeditor.util.PermissionUtils import com.automattic.photoeditor.views.ViewType import com.automattic.photoeditor.views.ViewType.TEXT +import com.automattic.photoeditor.views.added.AddedView import com.automattic.photoeditor.views.added.AddedViewList import com.bumptech.glide.Glide import com.bumptech.glide.load.resource.bitmap.CenterCrop @@ -92,6 +93,7 @@ import com.wordpress.stories.compose.story.StoryViewModel import com.wordpress.stories.compose.story.StoryViewModel.StoryFrameListItemUiState.StoryFrameListItemUiStateFrame import com.wordpress.stories.compose.story.StoryViewModelFactory import com.wordpress.stories.compose.story.deserializeAddedViews +import com.wordpress.stories.compose.story.serializeAddedViewTest import com.wordpress.stories.compose.story.serializeAddedViews import com.wordpress.stories.compose.text.TextEditorDialogFragment import com.wordpress.stories.util.KEY_STORY_SAVE_RESULT @@ -1054,7 +1056,11 @@ abstract class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelec // purge multitouch listeners val addedViews = photoEditor.getViewsAdded() for (addedView in addedViews) { - addedView.view?.setOnTouchListener(null) + addedView.view?.let { + // while iterating, also update the ViewInfo for each view + addedView.update() + addedView.view?.setOnTouchListener(null) + } } // set addedViews on the current frame (copy array so we don't share the same one with PhotoEditor) @@ -1062,6 +1068,10 @@ abstract class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelec // TODO REMOVE TEST CODE START val serializedObj = serializeAddedViews(photoEditor.getViewsAdded()) + for (item in photoEditor.getViewsAdded()) { + val tetstSerializedObj = serializeAddedViewTest(item) + Log.d("PORTKEY", "ACA VA: " + tetstSerializedObj) + } val deserializedObj = deserializeAddedViews(serializedObj) currentStoryFrameItem?.addedViews = deserializedObj // TODO REMOVE TEST CODE END diff --git a/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt b/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt index a80667dde..191a6b9fd 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt @@ -1,5 +1,6 @@ package com.wordpress.stories.compose.story +import com.automattic.photoeditor.views.added.AddedView import com.automattic.photoeditor.views.added.AddedViewList import kotlinx.serialization.json.Json @@ -23,6 +24,10 @@ fun serializeAddedViews(addedViews: AddedViewList): String { return Json.stringify(AddedViewList.serializer(), addedViews) } +fun serializeAddedViewTest(addedView: AddedView): String { + return Json.stringify(AddedView.serializer(), addedView) +} + fun deserializeAddedViews(addedViews: String): AddedViewList { return Json.parse(AddedViewList.serializer(), addedViews) } From 9af1b214f01a51551f41f03ebbe73652bf04235b Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Sun, 5 Jul 2020 20:52:06 -0300 Subject: [PATCH 05/23] removed custom serializer, fixed bad import preventing View from being marked Transient (wrong Transient annotation being imported) --- .../photoeditor/views/added/AddedView.kt | 90 ++++++------------- 1 file changed, 27 insertions(+), 63 deletions(-) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt index 84fa07dcd..17141ee6f 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt @@ -7,87 +7,51 @@ import com.automattic.photoeditor.R import com.automattic.photoeditor.views.ViewType import com.automattic.photoeditor.views.ViewType.EMOJI import com.automattic.photoeditor.views.ViewType.TEXT -import kotlinx.serialization.CompositeDecoder +import kotlinx.serialization.Transient import kotlinx.serialization.Decoder import kotlinx.serialization.Encoder import kotlinx.serialization.KSerializer -import kotlinx.serialization.MissingFieldException -import kotlinx.serialization.SerialDescriptor import kotlinx.serialization.Serializable -import kotlinx.serialization.SerializationException import kotlinx.serialization.Serializer -import kotlinx.serialization.internal.StringDescriptor -import kotlinx.serialization.withName @Serializable class AddedView( - @Transient val view: View?, + @Transient val view: View? = null, val viewType: ViewType, var viewInfo: AddedViewInfo, @Serializable(with = UriSerializer::class) val uri: Uri? = null ) { - @Serializer(forClass = AddedView::class) - companion object : KSerializer { - override val descriptor: SerialDescriptor = StringDescriptor.withName("AddedView") - - override fun serialize(encoder: Encoder, obj: AddedView) { - val compositeOutput = encoder.beginStructure(descriptor) - compositeOutput.encodeSerializableElement(descriptor, 0, ViewType.serializer(), obj.viewType) - compositeOutput.encodeSerializableElement(descriptor, 1, AddedViewInfo.serializer(), obj.viewInfo) - obj.uri?.let { - compositeOutput.encodeSerializableElement(descriptor, 2, UriSerializer, it) + companion object { + fun buildAddedViewFromView( + view: View, + viewType: ViewType, + uri: Uri? = null + ) : AddedView { + return AddedView( + view, + viewType, + buildViewInfoFromView(view, + getTextFromActualView(view, viewType) + ), + uri + ) } - compositeOutput.endStructure(descriptor) - } - - override fun deserialize(decoder: Decoder): AddedView { - var viewType: ViewType? = null - var viewInfo: AddedViewInfo? = null - var uri: Uri? = null - decoder.beginStructure(descriptor).run { - loop@while (true) { - when (val i = decodeElementIndex(descriptor)) { - CompositeDecoder.READ_DONE -> break@loop - 0 -> viewType = decodeSerializableElement(descriptor, i, ViewType.serializer()) - 1 -> viewInfo = decodeSerializableElement(descriptor, i, AddedViewInfo.serializer()) - 2 -> uri = decodeSerializableElement(descriptor, i, UriSerializer) - else -> throw SerializationException("Unknown index $i") + fun getTextFromActualView(view: View, viewType: ViewType): String { + var text = "" + when (viewType) { + EMOJI -> { + val txtView = view.findViewById(R.id.tvPhotoEditorEmoji) + text = txtView.text.toString() + } + TEXT -> { + val txtView = view.findViewById(R.id.tvPhotoEditorText) + text = txtView.text.toString() } } - endStructure(descriptor) - } - return AddedView( - null, // this will be initialized later when inflated and added to a ViewGroup - viewType ?: throw MissingFieldException("viewType"), - viewInfo ?: throw MissingFieldException("viewInfo"), - uri - ) - } - - fun buildAddedViewFromView( - view: View, - viewType: ViewType, - uri: Uri? = null - ) : AddedView { - return AddedView(view, viewType, buildViewInfoFromView(view, getTextFromActualView(view, viewType)), uri) - } - - fun getTextFromActualView(view: View, viewType: ViewType): String { - var text = "" - when (viewType) { - EMOJI -> { - val txtView = view.findViewById(R.id.tvPhotoEditorEmoji) - text = txtView.text.toString() - } - TEXT -> { - val txtView = view.findViewById(R.id.tvPhotoEditorText) - text = txtView.text.toString() - } + return text } - return text - } fun buildViewInfoFromView(view: View, text: String): AddedViewInfo { return AddedViewInfo( From 5531659994a3a5bace426caaab7ac048ce70ba79 Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Sun, 5 Jul 2020 21:11:29 -0300 Subject: [PATCH 06/23] using ArrayListSerializer for AddedViewList --- .../stories/compose/story/StorySerializerUtils.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt b/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt index 191a6b9fd..28a46489c 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt @@ -2,6 +2,7 @@ package com.wordpress.stories.compose.story import com.automattic.photoeditor.views.added.AddedView import com.automattic.photoeditor.views.added.AddedViewList +import kotlinx.serialization.internal.ArrayListSerializer import kotlinx.serialization.json.Json fun serializeStory(story: Story): String { @@ -21,7 +22,7 @@ fun deserializeStoryFrameItem(storyFrameItem: String): StoryFrameItem { } fun serializeAddedViews(addedViews: AddedViewList): String { - return Json.stringify(AddedViewList.serializer(), addedViews) + return Json.stringify(ArrayListSerializer(AddedView.serializer()), addedViews) } fun serializeAddedViewTest(addedView: AddedView): String { @@ -29,5 +30,7 @@ fun serializeAddedViewTest(addedView: AddedView): String { } fun deserializeAddedViews(addedViews: String): AddedViewList { - return Json.parse(AddedViewList.serializer(), addedViews) + val newList = AddedViewList() + newList.addAll(Json.parse(ArrayListSerializer(AddedView.serializer()), addedViews)) + return newList } From 9d4867c6cd68130e831941a9c3b237152f058a97 Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Mon, 6 Jul 2020 11:15:59 -0300 Subject: [PATCH 07/23] made changes so added views can be either referenced (rememberd) or re-created from AddedViewInfo --- .../com/automattic/photoeditor/PhotoEditor.kt | 51 ++++++++++++++++--- .../compose/ComposeLoopFrameActivity.kt | 2 +- .../stories/compose/frame/FrameSaveManager.kt | 4 +- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt index dd7938bcd..0cde48392 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt @@ -23,6 +23,7 @@ import androidx.annotation.ColorInt import androidx.annotation.IntRange import androidx.annotation.RequiresPermission import androidx.annotation.UiThread +import androidx.core.content.ContextCompat import androidx.core.widget.TextViewCompat import androidx.emoji.text.EmojiCompat import com.automattic.photoeditor.gesture.MultiTouchListener @@ -36,6 +37,7 @@ import com.automattic.photoeditor.views.ViewType.EMOJI import com.automattic.photoeditor.views.ViewType.TEXT import com.automattic.photoeditor.views.added.AddedView import com.automattic.photoeditor.views.added.AddedView.Companion +import com.automattic.photoeditor.views.added.AddedViewInfo import com.automattic.photoeditor.views.added.AddedViewList import com.automattic.photoeditor.views.brush.BrushDrawingView import com.automattic.photoeditor.views.brush.BrushViewChangeListener @@ -234,9 +236,10 @@ class PhotoEditor private constructor(builder: Builder) : * @param colorCodeTextView text color to be displayed */ @SuppressLint("ClickableViewAccessibility") - fun addText(text: String, colorCodeTextView: Int, textTypeface: Typeface? = null, fontSizeSp: Float = 18f) { + fun addText(text: String, colorCodeTextView: Int, textTypeface: Typeface? = null, fontSizeSp: Float = 18f): View? { brushDrawingView.brushDrawingMode = false - getLayout(ViewType.TEXT)?.apply { + val view: View? + view = getLayout(ViewType.TEXT)?.apply { val textInputTv = findViewById(R.id.tvPhotoEditorText) textInputTv.text = text @@ -270,6 +273,7 @@ class PhotoEditor private constructor(builder: Builder) : mOnPhotoEditorListener?.onEditTextChangeListener(this, textInput, currentTextColor, true) } } + return view } /** @@ -313,8 +317,8 @@ class PhotoEditor private constructor(builder: Builder) : * * @param emojiName unicode in form of string to display emoji */ - fun addEmoji(emojiName: String) { - addEmoji(null, emojiName) + fun addEmoji(emojiName: String): View? { + return addEmoji(null, emojiName) } /** @@ -324,9 +328,10 @@ class PhotoEditor private constructor(builder: Builder) : * @param emojiTypeface typeface for custom font to show emoji unicode in specific font * @param emojiName unicode in form of string to display emoji */ - fun addEmoji(emojiTypeface: Typeface?, emojiName: String) { + fun addEmoji(emojiTypeface: Typeface?, emojiName: String): View? { brushDrawingView.brushDrawingMode = false - getLayout(ViewType.EMOJI)?.apply { + val view = getLayout(ViewType.EMOJI) + view?.apply { val emojiTextView = findViewById(R.id.tvPhotoEditorEmoji) if (emojiTypeface != null) { @@ -369,6 +374,7 @@ class PhotoEditor private constructor(builder: Builder) : // setOnTouchListener(multiTouchListenerInstance) addViewToParent(this, ViewType.EMOJI) } + return view } /** @@ -386,7 +392,38 @@ class PhotoEditor private constructor(builder: Builder) : mOnPhotoEditorListener?.onAddViewListener(viewType, addedViews.size) } - fun addViewToParentWithTouchListener(rootView: View, viewType: ViewType, sourceUri: Uri? = null) { + fun addViewToParentWithTouchListener(addedView: AddedView) { + addedView.view?.let { + addViewToParentWithTouchListener(it, addedView.viewType, addedView.uri) + } ?: buildViewFromAddedViewInfo(addedView.viewInfo, addedView.viewType) + } + + private fun buildViewFromAddedViewInfo(addedViewInfo: AddedViewInfo, viewType: ViewType): View? { + var view: View? = null + when (viewType) { + EMOJI -> { + // create emoji view layout + view = addEmoji(addedViewInfo.text) + } + TEXT -> { + // create TEXT view layout + view = addText(addedViewInfo.text, ContextCompat.getColor(context, android.R.color.white)) + } + } + + // now apply all parameters to newly created view object + view?.let { + it.rotation = addedViewInfo.rotation + it.translationX = addedViewInfo.translationX + it.translationY = addedViewInfo.translationY + it.scaleX = addedViewInfo.scale + it.scaleY = addedViewInfo.scale + } + + return view + } + + private fun addViewToParentWithTouchListener(rootView: View, viewType: ViewType, sourceUri: Uri? = null) { val multiTouchListenerInstance = getNewMultitouchListener(rootView) // newMultiTouchListener when { viewType == EMOJI -> { diff --git a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt index 1c668f078..25a8d2839 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt @@ -1775,7 +1775,7 @@ abstract class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelec // now call addViewToParent the addedViews remembered by this frame newSelectedFrame.addedViews.let { for (oneView in it) { - photoEditor.addViewToParentWithTouchListener(requireNotNull(oneView.view), oneView.viewType) + photoEditor.addViewToParentWithTouchListener(oneView) } } diff --git a/stories/src/main/java/com/wordpress/stories/compose/frame/FrameSaveManager.kt b/stories/src/main/java/com/wordpress/stories/compose/frame/FrameSaveManager.kt index 4a6d0c691..9c9bba8e9 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/frame/FrameSaveManager.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/frame/FrameSaveManager.kt @@ -317,7 +317,9 @@ class FrameSaveManager(private val photoEditor: PhotoEditor) : CoroutineScope { fun releaseAddedViews(frame: StoryFrameItem) { // don't forget to remove these views from ghost offscreen view before exiting for (oneView in frame.addedViews) { - removeViewFromParent(requireNotNull(oneView.view)) + oneView.view?.let { + removeViewFromParent(it) + } } } } From 80659856554cb909784aef851284095d46554660 Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Mon, 6 Jul 2020 11:39:44 -0300 Subject: [PATCH 08/23] added AddedViewTextInfo class to hold information inherent to TextViews (font size, color, text) --- .../com/automattic/photoeditor/PhotoEditor.kt | 12 +++++- .../photoeditor/views/added/AddedView.kt | 37 ++++++++++--------- .../photoeditor/views/added/AddedViewInfo.kt | 5 ++- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt index 0cde48392..ad204aae7 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt @@ -403,11 +403,19 @@ class PhotoEditor private constructor(builder: Builder) : when (viewType) { EMOJI -> { // create emoji view layout - view = addEmoji(addedViewInfo.text) + view = addEmoji(addedViewInfo.addedViewTextInfo.text) + // apply specific TextView parameters for emoji (fontsize) + val emojiTextView = view?.findViewById(R.id.tvPhotoEditorEmoji) + emojiTextView?.textSize = addedViewInfo.addedViewTextInfo.fontSizeSp } TEXT -> { // create TEXT view layout - view = addText(addedViewInfo.text, ContextCompat.getColor(context, android.R.color.white)) + // view = addText(addedViewInfo.addedViewTextInfo.text, ContextCompat.getColor(context, android.R.color.white)) + view = addText(addedViewInfo.addedViewTextInfo.text, addedViewInfo.addedViewTextInfo.textColor) + // apply specific TextView parameters for text (fontsize, text color) + val normalTextView = view?.findViewById(R.id.tvPhotoEditorText) + normalTextView?.textSize = addedViewInfo.addedViewTextInfo.fontSizeSp + normalTextView?.setTextColor(addedViewInfo.addedViewTextInfo.textColor) } } diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt index 17141ee6f..5e6eb2d28 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt @@ -31,35 +31,38 @@ class AddedView( return AddedView( view, viewType, - buildViewInfoFromView(view, - getTextFromActualView(view, viewType) + buildViewInfoFromView( + view, + getTextInfoFromActualView(view, viewType) ), uri ) } - fun getTextFromActualView(view: View, viewType: ViewType): String { - var text = "" - when (viewType) { - EMOJI -> { - val txtView = view.findViewById(R.id.tvPhotoEditorEmoji) - text = txtView.text.toString() - } - TEXT -> { - val txtView = view.findViewById(R.id.tvPhotoEditorText) - text = txtView.text.toString() - } + fun getTextInfoFromActualView(view: View, viewType: ViewType): AddedViewTextInfo { + val txtView: TextView + when (viewType) { + EMOJI -> { + txtView = view.findViewById(R.id.tvPhotoEditorEmoji) + } + TEXT -> { + txtView = view.findViewById(R.id.tvPhotoEditorText) + } + else -> { + // default text + txtView = view.findViewById(R.id.tvPhotoEditorText) } - return text } + return AddedViewTextInfo(txtView.text.toString(), txtView.textSize, txtView.currentTextColor) + } - fun buildViewInfoFromView(view: View, text: String): AddedViewInfo { + fun buildViewInfoFromView(view: View, addedViewText: AddedViewTextInfo): AddedViewInfo { return AddedViewInfo( view.rotation, view.translationX, view.translationY, view.scaleX, - text + addedViewText ) } } @@ -71,7 +74,7 @@ class AddedView( it.translationX, it.translationY, it.scaleX, - getTextFromActualView(view, viewType) + getTextInfoFromActualView(view, viewType) ) } } diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewInfo.kt b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewInfo.kt index d08732a60..2efc0e1b6 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewInfo.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewInfo.kt @@ -8,5 +8,8 @@ data class AddedViewInfo( val translationX: Float, val translationY: Float, val scale: Float, - val text: String + val addedViewTextInfo: AddedViewTextInfo ) + +@Serializable +data class AddedViewTextInfo(val text: String, val fontSizeSp: Float, val textColor: Int) From f76a38064f1088f363cde81aa711d664625d8f6d Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Mon, 6 Jul 2020 11:43:45 -0300 Subject: [PATCH 09/23] fixed lint warnings --- .../com/automattic/photoeditor/PhotoEditor.kt | 2 -- .../photoeditor/views/added/AddedView.kt | 26 +++++++++---------- .../photoeditor/views/added/AddedViewList.kt | 2 +- .../compose/ComposeLoopFrameActivity.kt | 3 +-- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt index ad204aae7..0e89b6ff7 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt @@ -23,7 +23,6 @@ import androidx.annotation.ColorInt import androidx.annotation.IntRange import androidx.annotation.RequiresPermission import androidx.annotation.UiThread -import androidx.core.content.ContextCompat import androidx.core.widget.TextViewCompat import androidx.emoji.text.EmojiCompat import com.automattic.photoeditor.gesture.MultiTouchListener @@ -36,7 +35,6 @@ import com.automattic.photoeditor.views.ViewType import com.automattic.photoeditor.views.ViewType.EMOJI import com.automattic.photoeditor.views.ViewType.TEXT import com.automattic.photoeditor.views.added.AddedView -import com.automattic.photoeditor.views.added.AddedView.Companion import com.automattic.photoeditor.views.added.AddedViewInfo import com.automattic.photoeditor.views.added.AddedViewList import com.automattic.photoeditor.views.brush.BrushDrawingView diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt index 5e6eb2d28..1810b99ae 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt @@ -24,20 +24,20 @@ class AddedView( ) { companion object { fun buildAddedViewFromView( - view: View, - viewType: ViewType, - uri: Uri? = null - ) : AddedView { - return AddedView( + view: View, + viewType: ViewType, + uri: Uri? = null + ): AddedView { + return AddedView( + view, + viewType, + buildViewInfoFromView( view, - viewType, - buildViewInfoFromView( - view, - getTextInfoFromActualView(view, viewType) - ), - uri - ) - } + getTextInfoFromActualView(view, viewType) + ), + uri + ) + } fun getTextInfoFromActualView(view: View, viewType: ViewType): AddedViewTextInfo { val txtView: TextView diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt index 43d1658b3..a65896287 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt @@ -16,7 +16,7 @@ class AddedViewList : ArrayList() { fun containsView(element: View): Boolean { for (n in this) { - n.view?.let{ + n.view?.let { if (it == element) { return true } diff --git a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt index 25a8d2839..0e6b87d25 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt @@ -56,7 +56,6 @@ import com.automattic.photoeditor.util.FileUtils.Companion.getLoopFrameFile import com.automattic.photoeditor.util.PermissionUtils import com.automattic.photoeditor.views.ViewType import com.automattic.photoeditor.views.ViewType.TEXT -import com.automattic.photoeditor.views.added.AddedView import com.automattic.photoeditor.views.added.AddedViewList import com.bumptech.glide.Glide import com.bumptech.glide.load.resource.bitmap.CenterCrop @@ -1070,7 +1069,7 @@ abstract class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelec val serializedObj = serializeAddedViews(photoEditor.getViewsAdded()) for (item in photoEditor.getViewsAdded()) { val tetstSerializedObj = serializeAddedViewTest(item) - Log.d("PORTKEY", "ACA VA: " + tetstSerializedObj) + Log.d("PORTKEY", "ACA VA: " + tetstSerializedObj) } val deserializedObj = deserializeAddedViews(serializedObj) currentStoryFrameItem?.addedViews = deserializedObj From 97cf627a7759e8454b15338db3c3a798f7e3e95f Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Mon, 6 Jul 2020 11:45:58 -0300 Subject: [PATCH 10/23] removed commented code line --- .../src/main/java/com/automattic/photoeditor/PhotoEditor.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt index 0e89b6ff7..bd6eacd5e 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt @@ -408,7 +408,6 @@ class PhotoEditor private constructor(builder: Builder) : } TEXT -> { // create TEXT view layout - // view = addText(addedViewInfo.addedViewTextInfo.text, ContextCompat.getColor(context, android.R.color.white)) view = addText(addedViewInfo.addedViewTextInfo.text, addedViewInfo.addedViewTextInfo.textColor) // apply specific TextView parameters for text (fontsize, text color) val normalTextView = view?.findViewById(R.id.tvPhotoEditorText) From 5c8a3d8ff41553551988b794e6c0005fe31a5156 Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Mon, 6 Jul 2020 11:46:17 -0300 Subject: [PATCH 11/23] clarified comment --- .../src/main/java/com/automattic/photoeditor/PhotoEditor.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt index bd6eacd5e..41be317f3 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt @@ -416,7 +416,7 @@ class PhotoEditor private constructor(builder: Builder) : } } - // now apply all parameters to newly created view object + // now apply all common parameters to newly created view object view?.let { it.rotation = addedViewInfo.rotation it.translationX = addedViewInfo.translationX From a12891c6ee558849cdcbbb73b9b1bca45836deef Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Mon, 6 Jul 2020 12:16:14 -0300 Subject: [PATCH 12/23] only trigger the texteditor if view is a new addition (not a re-add from story slide switching) --- .../com/automattic/photoeditor/PhotoEditor.kt | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt index 41be317f3..a1e7c93f4 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt @@ -234,7 +234,13 @@ class PhotoEditor private constructor(builder: Builder) : * @param colorCodeTextView text color to be displayed */ @SuppressLint("ClickableViewAccessibility") - fun addText(text: String, colorCodeTextView: Int, textTypeface: Typeface? = null, fontSizeSp: Float = 18f): View? { + fun addText( + text: String, + colorCodeTextView: Int, + textTypeface: Typeface? = null, + fontSizeSp: Float = 18f, + isViewReadd: Boolean = false + ): View? { brushDrawingView.brushDrawingMode = false val view: View? view = getLayout(ViewType.TEXT)?.apply { @@ -264,8 +270,8 @@ class PhotoEditor private constructor(builder: Builder) : setOnTouchListener(multiTouchListenerInstance) addViewToParent(this, ViewType.TEXT) - // now open TextEditor right away - if (mOnPhotoEditorListener != null) { + // now open TextEditor right away if this is new text being added + if (mOnPhotoEditorListener != null && !isViewReadd) { val textInput = textInputTv.text.toString() val currentTextColor = textInputTv.currentTextColor mOnPhotoEditorListener?.onEditTextChangeListener(this, textInput, currentTextColor, true) @@ -408,7 +414,11 @@ class PhotoEditor private constructor(builder: Builder) : } TEXT -> { // create TEXT view layout - view = addText(addedViewInfo.addedViewTextInfo.text, addedViewInfo.addedViewTextInfo.textColor) + view = addText( + text = addedViewInfo.addedViewTextInfo.text, + colorCodeTextView = addedViewInfo.addedViewTextInfo.textColor, + isViewReadd = true + ) // apply specific TextView parameters for text (fontsize, text color) val normalTextView = view?.findViewById(R.id.tvPhotoEditorText) normalTextView?.textSize = addedViewInfo.addedViewTextInfo.fontSizeSp From e55719421310dd9d42d5d25071453ca928489c8b Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Mon, 6 Jul 2020 12:49:45 -0300 Subject: [PATCH 13/23] using the actual calculated value in px for fontSize --- .../main/java/com/automattic/photoeditor/PhotoEditor.kt | 8 +++++--- .../automattic/photoeditor/views/added/AddedViewInfo.kt | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt index a1e7c93f4..5143a0953 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt @@ -53,8 +53,8 @@ import kotlinx.android.synthetic.main.view_photo_editor_emoji.view.* import kotlinx.android.synthetic.main.view_photo_editor_text.view.* import java.io.File import java.io.FileInputStream -import java.util.ArrayList import java.lang.ref.WeakReference +import java.util.ArrayList /** * @@ -410,7 +410,8 @@ class PhotoEditor private constructor(builder: Builder) : view = addEmoji(addedViewInfo.addedViewTextInfo.text) // apply specific TextView parameters for emoji (fontsize) val emojiTextView = view?.findViewById(R.id.tvPhotoEditorEmoji) - emojiTextView?.textSize = addedViewInfo.addedViewTextInfo.fontSizeSp + // the actual calculated text size as obtained from the view is expressed in px. + emojiTextView?.setTextSize(TypedValue.COMPLEX_UNIT_PX, addedViewInfo.addedViewTextInfo.fontSizePx) } TEXT -> { // create TEXT view layout @@ -421,7 +422,8 @@ class PhotoEditor private constructor(builder: Builder) : ) // apply specific TextView parameters for text (fontsize, text color) val normalTextView = view?.findViewById(R.id.tvPhotoEditorText) - normalTextView?.textSize = addedViewInfo.addedViewTextInfo.fontSizeSp + // the actual calculated text size as obtained from the view is expressed in px. + normalTextView?.setTextSize(TypedValue.COMPLEX_UNIT_PX, addedViewInfo.addedViewTextInfo.fontSizePx) normalTextView?.setTextColor(addedViewInfo.addedViewTextInfo.textColor) } } diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewInfo.kt b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewInfo.kt index 2efc0e1b6..275572d27 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewInfo.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewInfo.kt @@ -12,4 +12,4 @@ data class AddedViewInfo( ) @Serializable -data class AddedViewTextInfo(val text: String, val fontSizeSp: Float, val textColor: Int) +data class AddedViewTextInfo(val text: String, val fontSizePx: Float, val textColor: Int) From f98e5bd0b1ab8e4630658106241f63c5053ade9c Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Mon, 6 Jul 2020 13:06:36 -0300 Subject: [PATCH 14/23] modified test log comment --- .../com/wordpress/stories/compose/ComposeLoopFrameActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt index 0e6b87d25..be099e8a6 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt @@ -1069,7 +1069,7 @@ abstract class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelec val serializedObj = serializeAddedViews(photoEditor.getViewsAdded()) for (item in photoEditor.getViewsAdded()) { val tetstSerializedObj = serializeAddedViewTest(item) - Log.d("PORTKEY", "ACA VA: " + tetstSerializedObj) + Log.d("PORTKEY", "serialized obj: " + tetstSerializedObj) } val deserializedObj = deserializeAddedViews(serializedObj) currentStoryFrameItem?.addedViews = deserializedObj From 402b9ba99c8ffbecc234cc02437a4bfd5adf6074 Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Mon, 6 Jul 2020 13:08:04 -0300 Subject: [PATCH 15/23] renamed method --- .../com/wordpress/stories/compose/ComposeLoopFrameActivity.kt | 4 ++-- .../wordpress/stories/compose/story/StorySerializerUtils.kt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt index be099e8a6..6a7c623d7 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt @@ -92,7 +92,7 @@ import com.wordpress.stories.compose.story.StoryViewModel import com.wordpress.stories.compose.story.StoryViewModel.StoryFrameListItemUiState.StoryFrameListItemUiStateFrame import com.wordpress.stories.compose.story.StoryViewModelFactory import com.wordpress.stories.compose.story.deserializeAddedViews -import com.wordpress.stories.compose.story.serializeAddedViewTest +import com.wordpress.stories.compose.story.serializeAddedView import com.wordpress.stories.compose.story.serializeAddedViews import com.wordpress.stories.compose.text.TextEditorDialogFragment import com.wordpress.stories.util.KEY_STORY_SAVE_RESULT @@ -1068,7 +1068,7 @@ abstract class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelec // TODO REMOVE TEST CODE START val serializedObj = serializeAddedViews(photoEditor.getViewsAdded()) for (item in photoEditor.getViewsAdded()) { - val tetstSerializedObj = serializeAddedViewTest(item) + val tetstSerializedObj = serializeAddedView(item) Log.d("PORTKEY", "serialized obj: " + tetstSerializedObj) } val deserializedObj = deserializeAddedViews(serializedObj) diff --git a/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt b/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt index 28a46489c..4a1735a7d 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt @@ -25,7 +25,7 @@ fun serializeAddedViews(addedViews: AddedViewList): String { return Json.stringify(ArrayListSerializer(AddedView.serializer()), addedViews) } -fun serializeAddedViewTest(addedView: AddedView): String { +fun serializeAddedView(addedView: AddedView): String { return Json.stringify(AddedView.serializer(), addedView) } From ef642bb512ee8623ae996d0e9079de7f4f73fc3f Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Tue, 7 Jul 2020 08:45:18 -0300 Subject: [PATCH 16/23] updated proguard rules to keep serializer classes --- photoeditor/proguard-rules.pro | 9 +++++++++ stories/proguard-rules.pro | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/photoeditor/proguard-rules.pro b/photoeditor/proguard-rules.pro index f1b424510..13a84487a 100644 --- a/photoeditor/proguard-rules.pro +++ b/photoeditor/proguard-rules.pro @@ -19,3 +19,12 @@ # If you keep the line number information, uncomment this to # hide the original source file name. #-renamesourcefileattribute SourceFile +-keepattributes *Annotation*, InnerClasses +-dontnote kotlinx.serialization.SerializationKt +-keep,includedescriptorclasses class com.automattic.photoeditor.**$$serializer { *; } # <-- change package name to your app's +-keepclassmembers class com.automattic.photoeditor.** { # <-- change package name to your app's + *** Companion; +} +-keepclasseswithmembers class com.automattic.photoeditor.** { # <-- change package name to your app's + kotlinx.serialization.KSerializer serializer(...); +} diff --git a/stories/proguard-rules.pro b/stories/proguard-rules.pro index f1b424510..7a4ec1ed2 100644 --- a/stories/proguard-rules.pro +++ b/stories/proguard-rules.pro @@ -19,3 +19,12 @@ # If you keep the line number information, uncomment this to # hide the original source file name. #-renamesourcefileattribute SourceFile +-keepattributes *Annotation*, InnerClasses +-dontnote kotlinx.serialization.SerializationKt +-keep,includedescriptorclasses class com.wordpress.stories.**$$serializer { *; } # <-- change package name to your app's +-keepclassmembers class com.wordpress.stories.** { # <-- change package name to your app's + *** Companion; +} +-keepclasseswithmembers class com.wordpress.stories.** { # <-- change package name to your app's + kotlinx.serialization.KSerializer serializer(...); +} \ No newline at end of file From 6ee2f548136056cf13bde83e97f890f83e7268b0 Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Tue, 7 Jul 2020 09:25:22 -0300 Subject: [PATCH 17/23] removed test code --- .../stories/compose/ComposeLoopFrameActivity.kt | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt index 6a7c623d7..0b57fe874 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt @@ -1064,16 +1064,6 @@ abstract class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelec // set addedViews on the current frame (copy array so we don't share the same one with PhotoEditor) currentStoryFrameItem?.addedViews = AddedViewList().copyOf(photoEditor.getViewsAdded()) - - // TODO REMOVE TEST CODE START - val serializedObj = serializeAddedViews(photoEditor.getViewsAdded()) - for (item in photoEditor.getViewsAdded()) { - val tetstSerializedObj = serializeAddedView(item) - Log.d("PORTKEY", "serialized obj: " + tetstSerializedObj) - } - val deserializedObj = deserializeAddedViews(serializedObj) - currentStoryFrameItem?.addedViews = deserializedObj - // TODO REMOVE TEST CODE END } private fun showMediaPicker() { From 53c04cfc4896ff05acdd96947e5c458edc927914 Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Tue, 7 Jul 2020 09:27:26 -0300 Subject: [PATCH 18/23] removed unused imports --- .../com/wordpress/stories/compose/ComposeLoopFrameActivity.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt index 0b57fe874..fcd33b4e6 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt @@ -91,9 +91,6 @@ import com.wordpress.stories.compose.story.StoryRepository import com.wordpress.stories.compose.story.StoryViewModel import com.wordpress.stories.compose.story.StoryViewModel.StoryFrameListItemUiState.StoryFrameListItemUiStateFrame import com.wordpress.stories.compose.story.StoryViewModelFactory -import com.wordpress.stories.compose.story.deserializeAddedViews -import com.wordpress.stories.compose.story.serializeAddedView -import com.wordpress.stories.compose.story.serializeAddedViews import com.wordpress.stories.compose.text.TextEditorDialogFragment import com.wordpress.stories.util.KEY_STORY_SAVE_RESULT import com.wordpress.stories.util.STATE_KEY_CURRENT_STORY_INDEX From 3713a9b397b7311ca7680e1255129c48279d3b00 Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Wed, 8 Jul 2020 08:17:57 -0300 Subject: [PATCH 19/23] renamed param --- .../src/main/java/com/automattic/photoeditor/PhotoEditor.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt index 5143a0953..aeda08c2b 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt @@ -239,7 +239,7 @@ class PhotoEditor private constructor(builder: Builder) : colorCodeTextView: Int, textTypeface: Typeface? = null, fontSizeSp: Float = 18f, - isViewReadd: Boolean = false + isViewBeingReadded: Boolean = false ): View? { brushDrawingView.brushDrawingMode = false val view: View? @@ -271,7 +271,7 @@ class PhotoEditor private constructor(builder: Builder) : addViewToParent(this, ViewType.TEXT) // now open TextEditor right away if this is new text being added - if (mOnPhotoEditorListener != null && !isViewReadd) { + if (mOnPhotoEditorListener != null && !isViewBeingReadded) { val textInput = textInputTv.text.toString() val currentTextColor = textInputTv.currentTextColor mOnPhotoEditorListener?.onEditTextChangeListener(this, textInput, currentTextColor, true) @@ -418,7 +418,7 @@ class PhotoEditor private constructor(builder: Builder) : view = addText( text = addedViewInfo.addedViewTextInfo.text, colorCodeTextView = addedViewInfo.addedViewTextInfo.textColor, - isViewReadd = true + isViewBeingReadded = true ) // apply specific TextView parameters for text (fontsize, text color) val normalTextView = view?.findViewById(R.id.tvPhotoEditorText) From aed363400a3268d61c0ba28e3d563db575d0319d Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Fri, 17 Jul 2020 10:56:14 -0300 Subject: [PATCH 20/23] renamed variable --- .../com/automattic/photoeditor/PhotoEditor.kt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt index aeda08c2b..d3e34e9d7 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt @@ -942,17 +942,17 @@ class PhotoEditor private constructor(builder: Builder) : // get the images currently on top of the screen, and add them as Filters to the mp4composer val filterCollection = ArrayList() - for (v in addedViews) { + for (oneView in addedViews) { val viewPositionInfo = ViewPositionInfo( widthParent, heightParent, - requireNotNull(v.view).width, - requireNotNull(v.view).height, - requireNotNull(v.view).matrix + requireNotNull(oneView.view).width, + requireNotNull(oneView.view).height, + requireNotNull(oneView.view).matrix ) - when (v.viewType) { + when (oneView.viewType) { ViewType.STICKER_ANIMATED -> { - v.uri?.path?.let { path -> + oneView.uri?.path?.let { path -> val file = File(path) val fileInputStream = FileInputStream(file) filterCollection.add(GlGifWatermarkFilter(context, fileInputStream, viewPositionInfo)) @@ -960,7 +960,9 @@ class PhotoEditor private constructor(builder: Builder) : } else -> { clearHelperBox() - filterCollection.add(GlWatermarkFilter(BitmapUtil.createBitmapFromView(v.view), viewPositionInfo)) + filterCollection.add( + GlWatermarkFilter(BitmapUtil.createBitmapFromView(oneView.view), viewPositionInfo) + ) } } } From 8e84597de165cb274b84e2d4021a3d7124f1910b Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Fri, 17 Jul 2020 15:34:10 -0300 Subject: [PATCH 21/23] simplified fun expression with assignment --- .../compose/story/StorySerializerUtils.kt | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt b/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt index 4a1735a7d..01146ac1b 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/story/StorySerializerUtils.kt @@ -5,29 +5,19 @@ import com.automattic.photoeditor.views.added.AddedViewList import kotlinx.serialization.internal.ArrayListSerializer import kotlinx.serialization.json.Json -fun serializeStory(story: Story): String { - return Json.stringify(Story.serializer(), story) -} +fun serializeStory(story: Story): String = Json.stringify(Story.serializer(), story) -fun serializeStoryFrameItem(storyFrameItem: StoryFrameItem): String { - return Json.stringify(StoryFrameItem.serializer(), storyFrameItem) -} +fun serializeStoryFrameItem(storyFrameItem: StoryFrameItem) = + Json.stringify(StoryFrameItem.serializer(), storyFrameItem) -fun deserializeStory(story: String): Story { - return Json.parse(Story.serializer(), story) -} +fun deserializeStory(story: String) = Json.parse(Story.serializer(), story) -fun deserializeStoryFrameItem(storyFrameItem: String): StoryFrameItem { - return Json.parse(StoryFrameItem.serializer(), storyFrameItem) -} +fun deserializeStoryFrameItem(storyFrameItem: String) = Json.parse(StoryFrameItem.serializer(), storyFrameItem) -fun serializeAddedViews(addedViews: AddedViewList): String { - return Json.stringify(ArrayListSerializer(AddedView.serializer()), addedViews) -} +fun serializeAddedViews(addedViews: AddedViewList) = + Json.stringify(ArrayListSerializer(AddedView.serializer()), addedViews) -fun serializeAddedView(addedView: AddedView): String { - return Json.stringify(AddedView.serializer(), addedView) -} +fun serializeAddedView(addedView: AddedView) = Json.stringify(AddedView.serializer(), addedView) fun deserializeAddedViews(addedViews: String): AddedViewList { val newList = AddedViewList() From 4492f53753f89af68a6bc2b8e6ffcfa27fcf9da1 Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Fri, 17 Jul 2020 15:36:40 -0300 Subject: [PATCH 22/23] renamed iterator temporal variable name for clarity --- .../photoeditor/views/added/AddedViewList.kt | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt index a65896287..bac2c7da9 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedViewList.kt @@ -15,8 +15,8 @@ class AddedViewList : ArrayList() { } fun containsView(element: View): Boolean { - for (n in this) { - n.view?.let { + for (oneView in this) { + oneView.view?.let { if (it == element) { return true } @@ -25,21 +25,21 @@ class AddedViewList : ArrayList() { return false } fun removeView(element: View): AddedView? { - for (n in this) { - n.view?.let { + for (oneView in this) { + oneView.view?.let { if (it == element) { - this.remove(n) - return n + this.remove(oneView) + return oneView } } } return null } fun indexOfView(element: View): Int { - for (n in this) { - n.view?.let { + for (oneView in this) { + oneView.view?.let { if (it == element) { - return this.indexOf(n) + return this.indexOf(oneView) } } } @@ -47,8 +47,8 @@ class AddedViewList : ArrayList() { } fun containsAnyAddedViewsOfType(type: ViewType): Boolean { - for (v: AddedView in this) { - if (v.viewType == type) { + for (oneView: AddedView in this) { + if (oneView.viewType == type) { return true } } From 7a65da278751755111f7c0d1fab7a4447504da0f Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Fri, 17 Jul 2020 15:38:01 -0300 Subject: [PATCH 23/23] returning assignment in when statement itself --- .../com/automattic/photoeditor/views/added/AddedView.kt | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt index 1810b99ae..bcfd5062a 100644 --- a/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt +++ b/photoeditor/src/main/java/com/automattic/photoeditor/views/added/AddedView.kt @@ -40,17 +40,16 @@ class AddedView( } fun getTextInfoFromActualView(view: View, viewType: ViewType): AddedViewTextInfo { - val txtView: TextView - when (viewType) { + val txtView: TextView = when (viewType) { EMOJI -> { - txtView = view.findViewById(R.id.tvPhotoEditorEmoji) + view.findViewById(R.id.tvPhotoEditorEmoji) } TEXT -> { - txtView = view.findViewById(R.id.tvPhotoEditorText) + view.findViewById(R.id.tvPhotoEditorText) } else -> { // default text - txtView = view.findViewById(R.id.tvPhotoEditorText) + view.findViewById(R.id.tvPhotoEditorText) } } return AddedViewTextInfo(txtView.text.toString(), txtView.textSize, txtView.currentTextColor)