@@ -2,10 +2,15 @@ package com.omega_r.libs.omegatypes
22
33import android.app.Activity
44import android.content.Context
5+ import android.text.Html
6+ import android.text.SpannableString
57import android.text.SpannableStringBuilder
68import android.widget.EditText
79import android.widget.TextView
810import android.widget.Toast
11+ import java.io.IOException
12+ import java.io.ObjectInputStream
13+ import java.io.ObjectOutputStream
914import java.io.Serializable
1015import 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