Skip to content

Commit 9f3ff46

Browse files
authored
Merge pull request #401 from wordpress-mobile/issue/394-reset-media-span-by-attributes
Issue/394 reset media span by attributes
2 parents 0262782 + 035f116 commit 9f3ff46

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import org.wordpress.aztec.*
3434
import org.wordpress.aztec.glideloader.GlideVideoThumbnailLoader
3535
import org.wordpress.aztec.picassoloader.PicassoImageLoader
3636
import org.wordpress.aztec.source.SourceViewEditText
37-
import org.wordpress.aztec.spans.AztecMediaSpan
3837
import org.wordpress.aztec.toolbar.AztecToolbar
3938
import org.wordpress.aztec.toolbar.AztecToolbarClickListener
4039
import org.xml.sax.Attributes
@@ -124,7 +123,7 @@ class MainActivity : AppCompatActivity(),
124123
LONG_TEXT +
125124
VIDEO
126125

127-
private val isRunningTest : Boolean by lazy {
126+
private val isRunningTest: Boolean by lazy {
128127
try {
129128
Class.forName("android.support.test.espresso.Espresso")
130129
true
@@ -218,14 +217,14 @@ class MainActivity : AppCompatActivity(),
218217

219218
fun insertImageAndSimulateUpload(bitmap: Bitmap?, mediaPath: String) {
220219
val (id, attrs) = generateAttributesForMedia(mediaPath, isVideo = false)
221-
val mediaSpan = aztec.insertImage(BitmapDrawable(resources, bitmap), attrs)
222-
insertMediaAndSimulateUpload(id, attrs, mediaSpan)
220+
aztec.insertImage(BitmapDrawable(resources, bitmap), attrs)
221+
insertMediaAndSimulateUpload(id, attrs)
223222
}
224223

225224
fun insertVideoAndSimulateUpload(bitmap: Bitmap?, mediaPath: String) {
226225
val (id, attrs) = generateAttributesForMedia(mediaPath, isVideo = true)
227-
val mediaSpan = aztec.insertVideo(BitmapDrawable(resources, bitmap), attrs)
228-
insertMediaAndSimulateUpload(id, attrs, mediaSpan)
226+
aztec.insertVideo(BitmapDrawable(resources, bitmap), attrs)
227+
insertMediaAndSimulateUpload(id, attrs)
229228
}
230229

231230
private fun generateAttributesForMedia(mediaPath: String, isVideo: Boolean): Pair<String, AztecAttributes> {
@@ -243,7 +242,7 @@ class MainActivity : AppCompatActivity(),
243242
return Pair(id, attrs)
244243
}
245244

246-
private fun insertMediaAndSimulateUpload(id: String, attrs: AztecAttributes, mediaSpan: AztecMediaSpan) {
245+
private fun insertMediaAndSimulateUpload(id: String, attrs: AztecAttributes) {
247246
val predicate = object : AztecText.AttributePredicate {
248247
override fun matches(attrs: Attributes): Boolean {
249248
return attrs.getValue("id") == id
@@ -266,7 +265,7 @@ class MainActivity : AppCompatActivity(),
266265
val runnable: Runnable = Runnable {
267266
aztec.setOverlayLevel(predicate, 1, progress)
268267
aztec.updateElementAttributes(predicate, attrs)
269-
aztec.updateMediaSpan(mediaSpan)
268+
aztec.resetAttributedMediaSpan(predicate)
270269
progress += 2000
271270

272271
if (progress >= 10000) {

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,12 +1070,12 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
10701070
}
10711071
}
10721072

1073-
fun insertImage(drawable: Drawable?, attributes: Attributes): AztecMediaSpan {
1074-
return lineBlockFormatter.insertImage(drawable, attributes, onImageTappedListener)
1073+
fun insertImage(drawable: Drawable?, attributes: Attributes) {
1074+
lineBlockFormatter.insertImage(drawable, attributes, onImageTappedListener)
10751075
}
10761076

1077-
fun insertVideo(drawable: Drawable?, attributes: Attributes): AztecMediaSpan {
1078-
return lineBlockFormatter.insertVideo(drawable, attributes, onVideoTappedListener)
1077+
fun insertVideo(drawable: Drawable?, attributes: Attributes) {
1078+
lineBlockFormatter.insertVideo(drawable, attributes, onVideoTappedListener)
10791079
}
10801080

10811081
fun removeMedia(attributePredicate: AttributePredicate) {
@@ -1111,10 +1111,14 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
11111111
.firstOrNull()?.attributes = attrs
11121112
}
11131113

1114-
fun updateMediaSpan(mediaSpan: AztecMediaSpan) {
1115-
if (text.getSpanStart(mediaSpan) != -1 && text.getSpanEnd(mediaSpan) != -1) {
1116-
editableText.setSpan(mediaSpan, text.getSpanStart(mediaSpan), text.getSpanEnd(mediaSpan), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
1117-
}
1114+
fun resetAttributedMediaSpan(attributePredicate: AttributePredicate) {
1115+
text.getSpans(0, text.length, AztecMediaSpan::class.java)
1116+
.filter {
1117+
attributePredicate.matches(it.attributes) && text.getSpanStart(it) != -1 && text.getSpanEnd(it) != -1
1118+
}
1119+
.forEach {
1120+
editableText.setSpan(it, text.getSpanStart(it), text.getSpanEnd(it), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
1121+
}
11181122
}
11191123

11201124
fun setOverlayLevel(attributePredicate: AttributePredicate, index: Int, level: Int) {

aztec/src/main/kotlin/org/wordpress/aztec/formatting/LineBlockFormatter.kt

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ class LineBlockFormatter(editor: AztecText) : AztecFormatter(editor) {
125125
val nestingLevel = AztecNestable.getNestingLevelAt(editableText, selectionStart)
126126

127127
val span = AztecHorizontalRuleSpan(
128-
editor.context,
129-
ContextCompat.getDrawable(editor.context, R.drawable.img_hr),
130-
nestingLevel,
131-
editor
128+
editor.context,
129+
ContextCompat.getDrawable(editor.context, R.drawable.img_hr),
130+
nestingLevel,
131+
editor
132132
)
133133

134134
val builder = SpannableStringBuilder(Constants.MAGIC_STRING)
@@ -137,26 +137,26 @@ class LineBlockFormatter(editor: AztecText) : AztecFormatter(editor) {
137137
editableText.replace(selectionStart, selectionEnd, builder)
138138

139139
editor.setSelection(
140-
if (selectionEnd < EndOfBufferMarkerAdder.safeLength(editor)) {
141-
selectionEnd + 1
142-
} else {
143-
selectionEnd
144-
}
140+
if (selectionEnd < EndOfBufferMarkerAdder.safeLength(editor)) {
141+
selectionEnd + 1
142+
} else {
143+
selectionEnd
144+
}
145145
)
146146
}
147147

148-
fun insertVideo(drawable: Drawable?, attributes: Attributes, onVideoTappedListener: OnVideoTappedListener?): AztecMediaSpan {
148+
fun insertVideo(drawable: Drawable?, attributes: Attributes, onVideoTappedListener: OnVideoTappedListener?) {
149149
val nestingLevel = AztecNestable.getNestingLevelAt(editableText, selectionStart)
150150
val span = AztecVideoSpan(editor.context, drawable, nestingLevel, AztecAttributes(attributes), onVideoTappedListener, editor)
151-
return insertMedia(span)
151+
insertMedia(span)
152152
}
153153

154-
fun insertImage(drawable: Drawable?, attributes: Attributes, onImageTappedListener: OnImageTappedListener?): AztecMediaSpan {
154+
fun insertImage(drawable: Drawable?, attributes: Attributes, onImageTappedListener: OnImageTappedListener?) {
155155
val span = AztecImageSpan(editor.context, drawable, AztecAttributes(attributes), onImageTappedListener, editor)
156-
return insertMedia(span)
156+
insertMedia(span)
157157
}
158158

159-
private fun insertMedia(span: AztecMediaSpan): AztecMediaSpan {
159+
private fun insertMedia(span: AztecMediaSpan) {
160160
val spanBeforeMedia = editableText.getSpans(selectionStart, selectionEnd, AztecBlockSpan::class.java)
161161
.firstOrNull {
162162
selectionStart == editableText.getSpanEnd(it)
@@ -198,7 +198,5 @@ class LineBlockFormatter(editor: AztecText) : AztecFormatter(editor) {
198198
editor.setSelection(
199199
if (selectionEnd < EndOfBufferMarkerAdder.safeLength(editor)) selectionEnd + 1 else selectionEnd)
200200
editor.isMediaAdded = true
201-
202-
return span
203201
}
204202
}

0 commit comments

Comments
 (0)