Skip to content

Commit cf8385e

Browse files
committed
Fix serialazable CharSequenceText
1 parent eb113e4 commit cf8385e

File tree

1 file changed

+39
-1
lines changed
  • omegatypes/src/main/java/com/omega_r/libs/omegatypes

1 file changed

+39
-1
lines changed

omegatypes/src/main/java/com/omega_r/libs/omegatypes/Text.kt

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ package com.omega_r.libs.omegatypes
22

33
import android.app.Activity
44
import android.content.Context
5+
import android.text.Html
6+
import android.text.SpannableString
57
import android.text.SpannableStringBuilder
68
import android.widget.EditText
79
import android.widget.TextView
810
import android.widget.Toast
11+
import java.io.IOException
12+
import java.io.ObjectInputStream
13+
import java.io.ObjectOutputStream
914
import java.io.Serializable
1015
import java.util.*
1116

@@ -136,9 +141,14 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
136141

137142

138143
private class CharSequenceText internal constructor(
139-
private val charSequence: CharSequence,
144+
private var charSequence: CharSequence,
140145
textStyle: TextStyle?) : Text(textStyle) {
141146

147+
companion object {
148+
private const val TYPE_SPANNABLE = 0.toByte()
149+
private const val TYPE_STRING = 1.toByte()
150+
}
151+
142152
override fun isEmpty(): Boolean = charSequence.isEmpty()
143153

144154
override fun getString(context: Context): String? {
@@ -149,6 +159,34 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
149159
return (defaultTextStyle + textStyle)?.applyStyle(context, charSequence) ?: charSequence
150160
}
151161

162+
@Throws(IOException::class)
163+
private fun writeObject(stream: ObjectOutputStream) {
164+
charSequence.let { charSequence->
165+
when (charSequence) {
166+
is SpannableString -> {
167+
stream.writeByte(TYPE_SPANNABLE.toInt())
168+
stream.writeObject(Html.toHtml(charSequence))
169+
}
170+
is String -> {
171+
stream.writeByte(TYPE_STRING.toInt())
172+
stream.writeUTF(charSequence)
173+
}
174+
else -> {
175+
throw IOException("Unknown type " + charSequence::class.simpleName)
176+
}
177+
}
178+
}
179+
}
180+
181+
@Throws(IOException::class, ClassNotFoundException::class)
182+
private fun readObject(stream: ObjectInputStream) {
183+
charSequence = when (val type = stream.readByte()) {
184+
TYPE_SPANNABLE -> SpannableString(Html.fromHtml(stream.readObject() as String))
185+
TYPE_STRING -> stream.readObject() as String
186+
else -> throw IOException("Unknown type = $type")
187+
}
188+
}
189+
152190
override fun equals(other: Any?): Boolean {
153191
if (this === other) return true
154192
if (javaClass != other?.javaClass) return false

0 commit comments

Comments
 (0)