From ce54e93a4d8dbef1318755e9f5cccf5ac9a4eef4 Mon Sep 17 00:00:00 2001 From: Klymentiy Haykov Date: Mon, 3 Jul 2017 16:07:31 +0900 Subject: [PATCH 1/6] Removed return of the inserted media span. --- .../aztec/formatting/LineBlockFormatter.kt | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/formatting/LineBlockFormatter.kt b/aztec/src/main/kotlin/org/wordpress/aztec/formatting/LineBlockFormatter.kt index 3fbb1999f..63e75cf80 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/formatting/LineBlockFormatter.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/formatting/LineBlockFormatter.kt @@ -125,10 +125,10 @@ class LineBlockFormatter(editor: AztecText) : AztecFormatter(editor) { val nestingLevel = AztecNestable.getNestingLevelAt(editableText, selectionStart) val span = AztecHorizontalRuleSpan( - editor.context, - ContextCompat.getDrawable(editor.context, R.drawable.img_hr), - nestingLevel, - editor + editor.context, + ContextCompat.getDrawable(editor.context, R.drawable.img_hr), + nestingLevel, + editor ) val builder = SpannableStringBuilder(Constants.MAGIC_STRING) @@ -137,26 +137,26 @@ class LineBlockFormatter(editor: AztecText) : AztecFormatter(editor) { editableText.replace(selectionStart, selectionEnd, builder) editor.setSelection( - if (selectionEnd < EndOfBufferMarkerAdder.safeLength(editor)) { - selectionEnd + 1 - } else { - selectionEnd - } + if (selectionEnd < EndOfBufferMarkerAdder.safeLength(editor)) { + selectionEnd + 1 + } else { + selectionEnd + } ) } - fun insertVideo(drawable: Drawable?, attributes: Attributes, onVideoTappedListener: OnVideoTappedListener?): AztecMediaSpan { + fun insertVideo(drawable: Drawable?, attributes: Attributes, onVideoTappedListener: OnVideoTappedListener?) { val nestingLevel = AztecNestable.getNestingLevelAt(editableText, selectionStart) val span = AztecVideoSpan(editor.context, drawable, nestingLevel, AztecAttributes(attributes), onVideoTappedListener, editor) - return insertMedia(span) + insertMedia(span) } - fun insertImage(drawable: Drawable?, attributes: Attributes, onImageTappedListener: OnImageTappedListener?): AztecMediaSpan { + fun insertImage(drawable: Drawable?, attributes: Attributes, onImageTappedListener: OnImageTappedListener?) { val span = AztecImageSpan(editor.context, drawable, AztecAttributes(attributes), onImageTappedListener, editor) - return insertMedia(span) + insertMedia(span) } - private fun insertMedia(span: AztecMediaSpan): AztecMediaSpan { + private fun insertMedia(span: AztecMediaSpan) { val spanBeforeMedia = editableText.getSpans(selectionStart, selectionEnd, AztecBlockSpan::class.java) .firstOrNull { selectionStart == editableText.getSpanEnd(it) @@ -198,7 +198,5 @@ class LineBlockFormatter(editor: AztecText) : AztecFormatter(editor) { editor.setSelection( if (selectionEnd < EndOfBufferMarkerAdder.safeLength(editor)) selectionEnd + 1 else selectionEnd) editor.isMediaAdded = true - - return span } } From 6f1f536be8f28108aef1879a5363eb5f1b42c2c8 Mon Sep 17 00:00:00 2001 From: Klymentiy Haykov Date: Mon, 3 Jul 2017 16:08:02 +0900 Subject: [PATCH 2/6] Update media span based on attributes. --- .../kotlin/org/wordpress/aztec/AztecText.kt | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index d93b65cd2..0e3c18504 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -1070,12 +1070,12 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On } } - fun insertImage(drawable: Drawable?, attributes: Attributes): AztecMediaSpan { - return lineBlockFormatter.insertImage(drawable, attributes, onImageTappedListener) + fun insertImage(drawable: Drawable?, attributes: Attributes) { + lineBlockFormatter.insertImage(drawable, attributes, onImageTappedListener) } - fun insertVideo(drawable: Drawable?, attributes: Attributes): AztecMediaSpan { - return lineBlockFormatter.insertVideo(drawable, attributes, onVideoTappedListener) + fun insertVideo(drawable: Drawable?, attributes: Attributes) { + lineBlockFormatter.insertVideo(drawable, attributes, onVideoTappedListener) } fun removeMedia(attributePredicate: AttributePredicate) { @@ -1111,10 +1111,14 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On .firstOrNull()?.attributes = attrs } - fun updateMediaSpan(mediaSpan: AztecMediaSpan) { - if (text.getSpanStart(mediaSpan) != -1 && text.getSpanEnd(mediaSpan) != -1) { - editableText.setSpan(mediaSpan, text.getSpanStart(mediaSpan), text.getSpanEnd(mediaSpan), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) - } + fun resetAttributedSpan(attributePredicate: AttributePredicate) { + text.getSpans(0, text.length, AztecMediaSpan::class.java) + .filter { + attributePredicate.matches(it.attributes) && text.getSpanStart(it) != -1 && text.getSpanEnd(it) != -1 + } + .forEach { + editableText.setSpan(it, text.getSpanStart(it), text.getSpanEnd(it), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) + } } fun setOverlayLevel(attributePredicate: AttributePredicate, index: Int, level: Int) { From d27ea0b64031cd441d5e2d4a1a5351f951632589 Mon Sep 17 00:00:00 2001 From: Klymentiy Haykov Date: Mon, 3 Jul 2017 16:08:28 +0900 Subject: [PATCH 3/6] Updated demo activity. --- .../org/wordpress/aztec/demo/MainActivity.kt | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt b/app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt index cc45eea87..b60ffcc39 100644 --- a/app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt +++ b/app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt @@ -37,7 +37,6 @@ import org.wordpress.aztec.Html import org.wordpress.aztec.glideloader.GlideVideoThumbnailLoader import org.wordpress.aztec.picassoloader.PicassoImageLoader import org.wordpress.aztec.source.SourceViewEditText -import org.wordpress.aztec.spans.AztecMediaSpan import org.wordpress.aztec.toolbar.AztecToolbar import org.wordpress.aztec.toolbar.AztecToolbarClickListener import org.xml.sax.Attributes @@ -127,7 +126,7 @@ class MainActivity : AppCompatActivity(), LONG_TEXT + VIDEO - private val isRunningTest : Boolean by lazy { + private val isRunningTest: Boolean by lazy { try { Class.forName("android.support.test.espresso.Espresso") true @@ -221,14 +220,14 @@ class MainActivity : AppCompatActivity(), fun insertImageAndSimulateUpload(bitmap: Bitmap?, mediaPath: String) { val (id, attrs) = generateAttributesForMedia(mediaPath, isVideo = false) - val mediaSpan = aztec.insertImage(BitmapDrawable(resources, bitmap), attrs) - insertMediaAndSimulateUpload(id, attrs, mediaSpan) + aztec.insertImage(BitmapDrawable(resources, bitmap), attrs) + insertMediaAndSimulateUpload(id, attrs) } fun insertVideoAndSimulateUpload(bitmap: Bitmap?, mediaPath: String) { val (id, attrs) = generateAttributesForMedia(mediaPath, isVideo = true) - val mediaSpan = aztec.insertVideo(BitmapDrawable(resources, bitmap), attrs) - insertMediaAndSimulateUpload(id, attrs, mediaSpan) + aztec.insertVideo(BitmapDrawable(resources, bitmap), attrs) + insertMediaAndSimulateUpload(id, attrs) } private fun generateAttributesForMedia(mediaPath: String, isVideo: Boolean): Pair { @@ -246,7 +245,7 @@ class MainActivity : AppCompatActivity(), return Pair(id, attrs) } - private fun insertMediaAndSimulateUpload(id: String, attrs: AztecAttributes, mediaSpan: AztecMediaSpan) { + private fun insertMediaAndSimulateUpload(id: String, attrs: AztecAttributes) { val predicate = object : AztecText.AttributePredicate { override fun matches(attrs: Attributes): Boolean { return attrs.getValue("id") == id @@ -269,7 +268,7 @@ class MainActivity : AppCompatActivity(), val runnable: Runnable = Runnable { aztec.setOverlayLevel(predicate, 1, progress) aztec.updateElementAttributes(predicate, attrs) - aztec.updateMediaSpan(mediaSpan) + aztec.resetAttributedSpan(predicate) progress += 2000 if (progress >= 10000) { From c35c50ea8a5894f00d072ba81c8373dcddb16480 Mon Sep 17 00:00:00 2001 From: Klymentiy Haykov Date: Mon, 3 Jul 2017 17:24:41 +0900 Subject: [PATCH 4/6] renamed method --- aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index 0e3c18504..fda3b4935 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -1111,7 +1111,7 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On .firstOrNull()?.attributes = attrs } - fun resetAttributedSpan(attributePredicate: AttributePredicate) { + fun resetAttributeMediaSpan(attributePredicate: AttributePredicate) { text.getSpans(0, text.length, AztecMediaSpan::class.java) .filter { attributePredicate.matches(it.attributes) && text.getSpanStart(it) != -1 && text.getSpanEnd(it) != -1 From 15421b5f4906ab8e967fde82533d4927ba6a8966 Mon Sep 17 00:00:00 2001 From: Klymentiy Haykov Date: Mon, 3 Jul 2017 17:26:21 +0900 Subject: [PATCH 5/6] Missed rename. --- app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt b/app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt index b60ffcc39..63c713b90 100644 --- a/app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt +++ b/app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt @@ -268,7 +268,7 @@ class MainActivity : AppCompatActivity(), val runnable: Runnable = Runnable { aztec.setOverlayLevel(predicate, 1, progress) aztec.updateElementAttributes(predicate, attrs) - aztec.resetAttributedSpan(predicate) + aztec.resetAttributeMediaSpan(predicate) progress += 2000 if (progress >= 10000) { From 035f1163a5992864934faa6dbbbd9e853371cd2c Mon Sep 17 00:00:00 2001 From: Klymentiy Haykov Date: Mon, 3 Jul 2017 17:27:42 +0900 Subject: [PATCH 6/6] Typos. --- app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt | 2 +- aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt b/app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt index 63c713b90..783832ef8 100644 --- a/app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt +++ b/app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt @@ -268,7 +268,7 @@ class MainActivity : AppCompatActivity(), val runnable: Runnable = Runnable { aztec.setOverlayLevel(predicate, 1, progress) aztec.updateElementAttributes(predicate, attrs) - aztec.resetAttributeMediaSpan(predicate) + aztec.resetAttributedMediaSpan(predicate) progress += 2000 if (progress >= 10000) { diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index fda3b4935..ede499629 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -1111,7 +1111,7 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On .firstOrNull()?.attributes = attrs } - fun resetAttributeMediaSpan(attributePredicate: AttributePredicate) { + fun resetAttributedMediaSpan(attributePredicate: AttributePredicate) { text.getSpans(0, text.length, AztecMediaSpan::class.java) .filter { attributePredicate.matches(it.attributes) && text.getSpanStart(it) != -1 && text.getSpanEnd(it) != -1