Skip to content

Commit fe6d6f6

Browse files
authored
Merge pull request #336 from wordpress-mobile/feature/tag-refactoring
Repetitive code cleanup
2 parents 0d8712c + 98b78de commit fe6d6f6

28 files changed

+71
-340
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ class AztecParser {
347347

348348
private fun withinBlock(out: StringBuilder, text: Spanned, start: Int, end: Int,
349349
blockSpan: AztecBlockSpan, parents: ArrayList<AztecNestable>?, nestingLevel: Int) {
350-
out.append("<${blockSpan.getStartTag()}>")
350+
out.append("<${blockSpan.startTag}>")
351351
withinHtml(out, text, start, end, parents, nestingLevel)
352-
out.append("</${blockSpan.getEndTag()}>")
352+
out.append("</${blockSpan.endTag}>")
353353

354354
if (end > 0
355355
&& text[end - 1] == Constants.NEWLINE
@@ -402,7 +402,7 @@ class AztecParser {
402402
val span = spans[j]
403403

404404
if (span is AztecInlineSpan) {
405-
out.append("<${span.getStartTag()}>")
405+
out.append("<${span.startTag}>")
406406
}
407407

408408
if (span is CommentSpan) {
@@ -416,7 +416,7 @@ class AztecParser {
416416
}
417417

418418
if (span is AztecHorizontalLineSpan) {
419-
out.append("<${span.getStartTag()}>")
419+
out.append("<${span.startTag}>")
420420
i = next
421421
}
422422

@@ -436,7 +436,7 @@ class AztecParser {
436436
val span = spans[j]
437437

438438
if (span is AztecInlineSpan) {
439-
out.append("</${span.getEndTag()}>")
439+
out.append("</${span.endTag}>")
440440
}
441441

442442
if (span is AztecCommentSpan || span is CommentSpan) {

aztec/src/main/kotlin/org/wordpress/aztec/source/SourceViewEditText.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import android.text.TextWatcher
1313
import android.util.AttributeSet
1414
import android.view.KeyEvent
1515
import android.view.View
16-
import android.widget.EditText
1716
import org.wordpress.aztec.AztecText
1817
import org.wordpress.aztec.History
1918
import org.wordpress.aztec.R

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecCodeSpan.kt

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,67 +19,25 @@ package org.wordpress.aztec.spans
1919

2020
import android.graphics.Color
2121
import android.graphics.Typeface
22-
import android.os.Parcel
23-
import android.text.ParcelableSpan
2422
import android.text.TextPaint
2523
import android.text.style.MetricAffectingSpan
2624
import org.wordpress.aztec.AztecAttributes
2725
import org.wordpress.aztec.formatting.InlineFormatter
2826

29-
class AztecCodeSpan : MetricAffectingSpan, ParcelableSpan, AztecInlineSpan {
27+
class AztecCodeSpan(override var attributes: AztecAttributes = AztecAttributes()) : MetricAffectingSpan(), AztecInlineSpan {
3028

31-
private val TAG: String = "code"
29+
override val TAG = "code"
3230

3331
private var codeBackground: Int = 0
3432
private var codeBackgroundAlpha: Float = 0.0f
3533
private var codeColor: Int = 0
3634

37-
override var attributes: AztecAttributes = AztecAttributes()
38-
39-
constructor(attributes: AztecAttributes = AztecAttributes()) : super() {
40-
this.attributes = attributes
41-
}
42-
4335
constructor(codeStyle: InlineFormatter.CodeStyle, attributes: AztecAttributes = AztecAttributes()) : this(attributes) {
4436
this.codeBackground = codeStyle.codeBackground
4537
this.codeBackgroundAlpha = codeStyle.codeBackgroundAlpha
4638
this.codeColor = codeStyle.codeColor
4739
}
4840

49-
constructor(src: Parcel) {
50-
this.codeBackground = src.readInt()
51-
this.codeBackgroundAlpha = src.readFloat()
52-
this.codeColor = src.readInt()
53-
}
54-
55-
override fun getSpanTypeId(): Int {
56-
return getSpanTypeIdInternal()
57-
}
58-
59-
fun getSpanTypeIdInternal(): Int {
60-
return AztecSpanIds.CODE_SPAN
61-
}
62-
63-
override fun describeContents(): Int {
64-
return 0
65-
}
66-
67-
override fun writeToParcel(dest: Parcel, flags: Int) {
68-
dest.writeInt(codeBackground)
69-
dest.writeInt(codeColor)
70-
}
71-
72-
override fun getStartTag(): String {
73-
if (attributes.isEmpty()) {
74-
return TAG
75-
}
76-
return TAG + " " + attributes
77-
}
78-
79-
override fun getEndTag(): String {
80-
return TAG
81-
}
82-
8341
override fun updateDrawState(tp: TextPaint?) {
8442
configureTextPaint(tp)
8543
}

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecCursorSpan.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ class AztecCursorSpan {
66
companion object {
77
val AZTEC_CURSOR_TAG = "aztec_cursor"
88
}
9-
109
}

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecHeadingSpan.kt

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class AztecHeadingSpan @JvmOverloads constructor(
1717
var headerStyle: BlockFormatter.HeaderStyle = BlockFormatter.HeaderStyle(0)
1818
) : MetricAffectingSpan(), AztecBlockSpan, LineHeightSpan, UpdateLayout {
1919

20+
override val TAG: String
21+
get() = heading.tag
22+
2023
override var endBeforeBleed: Int = -1
2124
override var startBeforeCollapse: Int = -1
2225

@@ -32,6 +35,15 @@ class AztecHeadingSpan @JvmOverloads constructor(
3235
var previousFontMetrics: Paint.FontMetricsInt? = null
3336
var previousTextScale: Float = 1.0f
3437

38+
enum class Heading constructor(internal val scale: Float, internal val tag: String) {
39+
H1(SCALE_H1, "h1"),
40+
H2(SCALE_H2, "h2"),
41+
H3(SCALE_H3, "h3"),
42+
H4(SCALE_H4, "h4"),
43+
H5(SCALE_H5, "h5"),
44+
H6(SCALE_H6, "h6")
45+
}
46+
3547
companion object {
3648
private val SCALE_H1: Float = 1.73f
3749
private val SCALE_H2: Float = 1.32f
@@ -40,7 +52,7 @@ class AztecHeadingSpan @JvmOverloads constructor(
4052
private val SCALE_H5: Float = 0.72f
4153
private val SCALE_H6: Float = 0.60f
4254

43-
fun getTextFormat(tag: String): TextFormat {
55+
fun tagToTextFormat(tag: String): TextFormat {
4456
when (tag.toLowerCase()) {
4557
"h1" -> return TextFormat.FORMAT_HEADING_1
4658
"h2" -> return TextFormat.FORMAT_HEADING_2
@@ -71,7 +83,7 @@ class AztecHeadingSpan @JvmOverloads constructor(
7183

7284
constructor(nestingLevel: Int, tag: String, attrs: AztecAttributes = AztecAttributes(),
7385
headerStyle: BlockFormatter.HeaderStyle = BlockFormatter.HeaderStyle(0))
74-
: this(nestingLevel, getTextFormat(tag), attrs, headerStyle)
86+
: this(nestingLevel, tagToTextFormat(tag), attrs, headerStyle)
7587

7688
override fun chooseHeight(text: CharSequence, start: Int, end: Int, spanstartv: Int, v: Int, fm: Paint.FontMetricsInt) {
7789
val spanned = text as Spanned
@@ -114,26 +126,6 @@ class AztecHeadingSpan @JvmOverloads constructor(
114126

115127
}
116128

117-
enum class Heading constructor(internal val scale: Float) {
118-
H1(SCALE_H1),
119-
H2(SCALE_H2),
120-
H3(SCALE_H3),
121-
H4(SCALE_H4),
122-
H5(SCALE_H5),
123-
H6(SCALE_H6)
124-
}
125-
126-
override fun getStartTag(): String {
127-
if (attributes.isEmpty()) {
128-
return getTag()
129-
}
130-
return getTag() + " " + attributes
131-
}
132-
133-
override fun getEndTag(): String {
134-
return getTag()
135-
}
136-
137129
override fun updateDrawState(textPaint: TextPaint) {
138130
textPaint.textSize *= heading.scale
139131
textPaint.isFakeBoldText = true
@@ -148,16 +140,4 @@ class AztecHeadingSpan @JvmOverloads constructor(
148140

149141
textPaint.textSize *= heading.scale
150142
}
151-
152-
private fun getTag(): String {
153-
when (heading.scale) {
154-
SCALE_H1 -> return "h1"
155-
SCALE_H2 -> return "h2"
156-
SCALE_H3 -> return "h3"
157-
SCALE_H4 -> return "h4"
158-
SCALE_H5 -> return "h5"
159-
SCALE_H6 -> return "h6"
160-
else -> return "h1"
161-
}
162-
}
163143
}

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecHorizontalLineSpan.kt

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,9 @@ import android.content.Context
44
import android.graphics.drawable.Drawable
55
import org.wordpress.aztec.AztecAttributes
66

7-
class AztecHorizontalLineSpan(context: Context, drawable: Drawable, override var nestingLevel: Int) :
7+
class AztecHorizontalLineSpan(context: Context, drawable: Drawable, override var nestingLevel: Int,
8+
override var attributes: AztecAttributes = AztecAttributes()) :
89
AztecDynamicImageSpan(context, drawable), AztecFullWidthImageSpan, AztecSpan {
910

10-
private val TAG: String = "hr"
11-
12-
override var attributes: AztecAttributes = AztecAttributes()
13-
14-
override fun getStartTag(): String {
15-
if (attributes.isEmpty()) {
16-
return TAG
17-
}
18-
return TAG + " " + attributes
19-
}
20-
21-
override fun getEndTag(): String {
22-
return TAG
23-
}
11+
override val TAG: String = "hr"
2412
}

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecListItemSpan.kt

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,8 @@ import org.wordpress.aztec.AztecAttributes
44

55
class AztecListItemSpan(override var nestingLevel: Int, override var attributes: AztecAttributes = AztecAttributes()) : AztecBlockSpan {
66

7-
private val TAG = "li"
7+
override val TAG = "li"
88

99
override var endBeforeBleed: Int = -1
1010
override var startBeforeCollapse: Int = -1
11-
12-
override fun getStartTag(): String {
13-
if (attributes.isEmpty()) {
14-
return TAG
15-
}
16-
return TAG + " " + attributes
17-
}
18-
19-
override fun getEndTag(): String {
20-
return TAG
21-
}
22-
2311
}

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecMediaClickableSpan.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.text.style.ClickableSpan
44
import android.view.View
55

66
class AztecMediaClickableSpan(private val mediaSpan: AztecMediaSpan) : ClickableSpan() {
7+
78
override fun onClick(view: View) {
89
this.mediaSpan.onClick()
910
}

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecMediaSpan.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import android.graphics.Paint
66
import android.graphics.Rect
77
import android.graphics.drawable.Drawable
88
import android.view.Gravity
9-
import android.view.View
109
import org.wordpress.aztec.AztecAttributes
1110
import org.wordpress.aztec.AztecText
1211
import org.wordpress.aztec.AztecText.OnMediaTappedListener

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecOrderedListSpan.kt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,7 @@ class AztecOrderedListSpan(
3030
var listStyle: BlockFormatter.ListStyle = BlockFormatter.ListStyle(0, 0, 0, 0, 0)
3131
) : AztecListSpan(nestingLevel, listStyle.verticalPadding) {
3232

33-
private val TAG = "ol"
34-
35-
override fun getStartTag(): String {
36-
if (attributes.isEmpty()) {
37-
return TAG
38-
}
39-
return TAG + " " + attributes
40-
}
41-
42-
override fun getEndTag(): String {
43-
return TAG
44-
}
33+
override val TAG = "ol"
4534

4635
override fun getLeadingMargin(first: Boolean): Int {
4736
return listStyle.indicatorMargin + 2 * listStyle.indicatorWidth + listStyle.indicatorPadding

0 commit comments

Comments
 (0)