Skip to content

Commit

Permalink
Merge pull request #401 from wordpress-mobile/issue/394-reset-media-s…
Browse files Browse the repository at this point in the history
…pan-by-attributes

Issue/394 reset media span by attributes
  • Loading branch information
0nko authored Jul 3, 2017
2 parents 0262782 + 035f116 commit 9f3ff46
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
15 changes: 7 additions & 8 deletions app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import org.wordpress.aztec.*
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
Expand Down Expand Up @@ -124,7 +123,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
Expand Down Expand Up @@ -218,14 +217,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<String, AztecAttributes> {
Expand All @@ -243,7 +242,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
Expand All @@ -266,7 +265,7 @@ class MainActivity : AppCompatActivity(),
val runnable: Runnable = Runnable {
aztec.setOverlayLevel(predicate, 1, progress)
aztec.updateElementAttributes(predicate, attrs)
aztec.updateMediaSpan(mediaSpan)
aztec.resetAttributedMediaSpan(predicate)
progress += 2000

if (progress >= 10000) {
Expand Down
20 changes: 12 additions & 8 deletions aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 resetAttributedMediaSpan(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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
}

0 comments on commit 9f3ff46

Please sign in to comment.