Skip to content

Commit c703c6c

Browse files
committed
Notify, let the client decide on html mode change
For example, the client can know whether the images have finished uploading or not and might decide to inhibit the toggle until finished.
1 parent 2cc111c commit c703c6c

File tree

4 files changed

+41
-34
lines changed

4 files changed

+41
-34
lines changed

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ import org.xml.sax.Attributes
3535
import org.xml.sax.helpers.AttributesImpl
3636
import java.io.File
3737

38-
class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback,
39-
View.OnTouchListener, PopupMenu.OnMenuItemClickListener, AztecToolbarClickListener,
40-
AztecText.OnMediaTappedListener, AztecText.OnImeBackListener {
38+
class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback, View.OnTouchListener,
39+
PopupMenu.OnMenuItemClickListener, AztecToolbarClickListener, AztecText.OnMediaTappedListener,
40+
AztecText.OnImeBackListener {
4141
companion object {
4242
private val HEADING =
4343
"<h1>Heading 1</h1>" +
@@ -148,6 +148,7 @@ class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback,
148148
val attrs = AttributesImpl()
149149
attrs.addAttribute("", "src", "src", "string", mediaPath) // Temporary source value. Replace with URL after uploaded.
150150
attrs.addAttribute("", "id", "id", "string", id)
151+
attrs.addAttribute("", "uploading", "uploading", "string", "true")
151152

152153
aztec.insertMedia(BitmapDrawable(resources, bitmap), attrs)
153154

@@ -172,6 +173,7 @@ class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback,
172173
progress += 2000
173174

174175
if (progress >= 10000) {
176+
attrs.removeAttribute(attrs.getIndex("uploading"))
175177
aztec.clearOverlays(predicate, attrs)
176178
}
177179
}
@@ -519,6 +521,22 @@ class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback,
519521
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
520522
}
521523

524+
override fun onToolbarHtmlModeClicked() {
525+
val uploadingPredicate = object : AztecText.AttributePredicate {
526+
override fun matches(attrs: Attributes): Boolean {
527+
return attrs.getIndex("uploading") > -1
528+
}
529+
}
530+
531+
val mediaPending = aztec.getAllMediaAttributes(uploadingPredicate).size > 0
532+
533+
if (mediaPending) {
534+
ToastUtils.showToast(this, R.string.media_upload_dialog_message)
535+
} else {
536+
formattingToolbar.toggleEditorMode()
537+
}
538+
}
539+
522540
override fun onToolbarAddMediaClicked() {
523541
mediaMenu = PopupMenu(this, formattingToolbar)
524542
mediaMenu?.setOnMenuItemClickListener(this)

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -962,14 +962,18 @@ class AztecText : EditText, TextWatcher {
962962
}
963963

964964
fun getMediaAttributes(attributePredicate: AttributePredicate): Attributes? {
965-
text.getSpans(0, text.length, AztecMediaSpan::class.java).forEach {
966-
if (it.attributes != null) {
967-
if (attributePredicate.matches(it.attributes as Attributes)) {
968-
return it.attributes
969-
}
970-
}
971-
}
972-
973-
return null;
965+
return getAllMediaAttributes(attributePredicate).firstOrNull()
966+
}
967+
968+
fun getAllMediaAttributes(attributePredicate: AttributePredicate): List<Attributes?> {
969+
return text
970+
.getSpans(0, text.length, AztecMediaSpan::class.java)
971+
.filter {
972+
if (it.attributes != null) {
973+
return@filter attributePredicate.matches(it.attributes as Attributes)
974+
} else {
975+
return@filter false
976+
}}
977+
.map { it.attributes }
974978
}
975979
}

aztec/src/main/kotlin/org/wordpress/aztec/toolbar/AztecToolbar.kt

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ import android.widget.PopupMenu
99
import android.widget.PopupMenu.OnMenuItemClickListener
1010
import android.widget.Toast
1111
import android.widget.ToggleButton
12-
import org.wordpress.android.util.ToastUtils
1312
import org.wordpress.aztec.AztecText
1413
import org.wordpress.aztec.R
1514
import org.wordpress.aztec.TextFormat
1615
import org.wordpress.aztec.source.SourceViewEditText
17-
import org.wordpress.aztec.spans.AztecMediaSpan
1816
import java.util.*
1917

2018
class AztecToolbar : FrameLayout, OnMenuItemClickListener {
@@ -169,25 +167,20 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
169167
ToolbarAction.ADD_MEDIA -> aztecToolbarListener?.onToolbarAddMediaClicked()
170168
ToolbarAction.HEADING -> headingMenu?.show()
171169
ToolbarAction.LINK -> editor!!.showLinkDialog()
172-
ToolbarAction.HTML -> toggleEditorMode()
170+
ToolbarAction.HTML -> aztecToolbarListener?.onToolbarHtmlModeClicked()
173171
else -> {
174172
Toast.makeText(context, "Unsupported action", Toast.LENGTH_SHORT).show()
175173
}
176174
}
177175
}
178176

179-
fun toggleEditorMode() {
177+
public fun toggleEditorMode() {
180178
if (editor!!.visibility == View.VISIBLE) {
181-
if (!editor!!.isMediaAdded || allImagesUploaded()) {
182-
sourceEditor!!.displayStyledAndFormattedHtml(editor!!.toHtml(true))
183-
editor!!.visibility = View.GONE
184-
sourceEditor!!.visibility = View.VISIBLE
179+
sourceEditor!!.displayStyledAndFormattedHtml(editor!!.toHtml(true))
180+
editor!!.visibility = View.GONE
181+
sourceEditor!!.visibility = View.VISIBLE
185182

186-
toggleHtmlMode(true)
187-
} else {
188-
toggleButton(findViewById(ToolbarAction.HTML.buttonId), false)
189-
ToastUtils.showToast(context, R.string.media_upload_dialog_message)
190-
}
183+
toggleHtmlMode(true)
191184
} else {
192185
editor!!.fromHtml(sourceEditor!!.getPureHtml(true))
193186
editor!!.visibility = View.VISIBLE
@@ -197,15 +190,6 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
197190
}
198191
}
199192

200-
private fun allImagesUploaded(): Boolean {
201-
editor!!.text?.getSpans(0, editor!!.length(), AztecMediaSpan::class.java)?.forEach {
202-
if (it.getSource().isNullOrBlank() || !it.getSource().startsWith("http")) {
203-
return false
204-
}
205-
}
206-
return true
207-
}
208-
209193
private fun selectHeaderMenu(textFormats: ArrayList<TextFormat>) {
210194
headingMenu?.menu?.getItem(0)?.isChecked = true
211195
textFormats.forEach {

aztec/src/main/kotlin/org/wordpress/aztec/toolbar/AztecToolbarClickListener.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ package org.wordpress.aztec.toolbar
22

33
interface AztecToolbarClickListener {
44
fun onToolbarAddMediaClicked()
5+
fun onToolbarHtmlModeClicked()
56
}

0 commit comments

Comments
 (0)