Skip to content

Commit eb67123

Browse files
committed
Add AssetImage
1 parent c616fd6 commit eb67123

11 files changed

Lines changed: 141 additions & 27 deletions

File tree

app/src/main/assets/test.png

46.9 KB
Loading

app/src/main/assets/test.webp

20.3 KB
Loading

app/src/main/java/omega_r/com/omegatypesexample/MainActivity.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import android.widget.TextView
1212
import androidx.core.content.res.ResourcesCompat
1313
import androidx.core.text.HtmlCompat
1414
import com.omega_r.libs.omegatypes.*
15+
import com.omega_r.libs.omegatypes.Size.Unit.SP
1516
import com.omega_r.libs.omegatypes.file.File
1617
import com.omega_r.libs.omegatypes.file.from
1718
import com.omega_r.libs.omegatypes.image.*
@@ -51,8 +52,8 @@ class MainActivity : BaseActivity() {
5152
.append(addText)
5253
.append(Text.empty())
5354
.append(Text.from(R.string.text_format_test, "1-9"))
54-
55-
.append(Text.from("test "))
55+
.append(Text.from(" test Image -> "))
56+
// .append(AssetImage("test.webp"))
5657
.append(Text.from(Image.from(R.drawable.ic_test)))
5758
.append(
5859
Text.from(
@@ -71,6 +72,8 @@ class MainActivity : BaseActivity() {
7172

7273
text.applyTo(exampleTextView) // or exampleTextView.setText(text)
7374

75+
exampleTextView.setTextSize(16.sp)
76+
7477
val list = listOf(
7578
Text.from("1", TextStyle.color(Color.fromAttribute(R.attr.colorAccent))),
7679
Text.from("2", TextStyle.color(Color.fromAttribute(R.attr.colorAccent))),
@@ -79,8 +82,8 @@ class MainActivity : BaseActivity() {
7982

8083
title = list.join(",", postfix = ".").getCharSequence(this)
8184

82-
val image =
83-
Image.from("https://dejagerart.com/wp-content/uploads/2018/09/Test-Logo-Circle-black-transparent.png")
85+
val image = AssetImage("test.webp")
86+
//Image.from("https://dejagerart.com/wp-content/uploads/2018/09/Test-Logo-Circle-black-transparent.png")
8487

8588
// val image = intent.getSerializableExtra("test") as? Image ?: run {
8689
//

glide/src/main/java/com/omega_r/libs/omegatypes/image/GlideImagesProcessor.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ open class GlideImagesProcessor(
5454
is BitmapImage -> load(image.bitmap)
5555
is DrawableImage -> load(image.drawable)
5656
is ByteArrayImage -> load(image.byteArray)
57+
is AssetImage -> load("file:///android_asset/" + image.fileName)
5758
else -> null
5859
}
5960
}

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

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import android.widget.TextView
1212
import java.io.Serializable
1313
import java.util.*
1414

15-
1615
/**
1716
* Created by Anton Knyazev on 18.05.2019.
1817
*/
@@ -23,9 +22,11 @@ abstract class Color : Serializable {
2322

2423
abstract fun getColorInt(context: Context): Int
2524

26-
open fun getColorStateList(context: Context): ColorStateList {
27-
return ColorStateList.valueOf(getColorInt(context))
28-
}
25+
open fun getColorStateList(context: Context): ColorStateList = ColorStateList.valueOf(getColorInt(context))
26+
27+
abstract override fun equals(other: Any?): Boolean
28+
29+
abstract override fun hashCode(): Int
2930

3031
companion object {
3132

@@ -79,17 +80,47 @@ abstract class Color : Serializable {
7980

8081
@JvmStatic
8182
fun fromColorList(colorStateList: ColorStateList): Color = ColorStateListColor(colorStateList)
82-
8383
}
8484

8585
class IntColor(private val colorInt: Int) : Color() {
86+
8687
override fun getColorInt(context: Context) = colorInt
88+
89+
override fun equals(other: Any?): Boolean {
90+
if (this === other) return true
91+
if (other !is IntColor) return false
92+
93+
if (colorInt != other.colorInt) return false
94+
95+
return true
96+
}
97+
98+
override fun hashCode(): Int {
99+
return colorInt
100+
}
87101
}
88102

89103
class HexStringColor(val hexString: String) : Color() {
104+
90105
private val colorInt: Int = GraphicColor.parseColor(hexString)
91106

92107
override fun getColorInt(context: Context) = colorInt
108+
109+
override fun equals(other: Any?): Boolean {
110+
if (this === other) return true
111+
if (other !is HexStringColor) return false
112+
113+
if (hexString != other.hexString) return false
114+
if (colorInt != other.colorInt) return false
115+
116+
return true
117+
}
118+
119+
override fun hashCode(): Int {
120+
var result = hexString.hashCode()
121+
result = 31 * result + colorInt
122+
return result
123+
}
93124
}
94125

95126
class ResourceColor(private val colorRes: Int) : Color() {
@@ -109,14 +140,26 @@ abstract class Color : Serializable {
109140
context.resources.getColorStateList(colorRes)
110141
}
111142
}
143+
144+
override fun equals(other: Any?): Boolean {
145+
if (this === other) return true
146+
if (other !is ResourceColor) return false
147+
148+
if (colorRes != other.colorRes) return false
149+
150+
return true
151+
}
152+
153+
override fun hashCode(): Int {
154+
return colorRes
155+
}
112156
}
113157

114158
class AttrThemeColor(private val attrInt: Int) : Color() {
115159

116160
companion object {
117161

118162
private val cache = WeakHashMap<Resources.Theme, SparseIntArray>()
119-
120163
}
121164

122165
override fun getColorInt(context: Context): Int {
@@ -149,6 +192,18 @@ abstract class Color : Serializable {
149192
}
150193
}
151194

195+
override fun equals(other: Any?): Boolean {
196+
if (this === other) return true
197+
if (other !is AttrThemeColor) return false
198+
199+
if (attrInt != other.attrInt) return false
200+
201+
return true
202+
}
203+
204+
override fun hashCode(): Int {
205+
return attrInt
206+
}
152207
}
153208

154209
class ColorStateListColor(private val colorStateList: ColorStateList) : Color() {
@@ -161,8 +216,19 @@ abstract class Color : Serializable {
161216
return colorStateList
162217
}
163218

164-
}
219+
override fun equals(other: Any?): Boolean {
220+
if (this === other) return true
221+
if (other !is ColorStateListColor) return false
222+
223+
if (colorStateList != other.colorStateList) return false
165224

225+
return true
226+
}
227+
228+
override fun hashCode(): Int {
229+
return colorStateList.hashCode()
230+
}
231+
}
166232
}
167233

168234
var TextView.textColor: Color
@@ -187,5 +253,5 @@ var View.backgroundColor: Color?
187253
get() = (background as? ColorDrawable)?.color?.let { Color.fromInt(it) }
188254
set(value) {
189255
value?.let { setBackgroundColor(it.getColorInt(context)) }
190-
?: setBackgroundDrawable(null)
256+
?: setBackgroundDrawable(null)
191257
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
133133
return string
134134
}
135135

136+
override fun toString(): String {
137+
return string.toString()
138+
}
139+
136140
override fun equals(other: Any?): Boolean {
137141
if (this === other) return true
138142
if (javaClass != other?.javaClass) return false
@@ -170,6 +174,10 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
170174
return (defaultTextStyle + textStyle)?.applyStyle(context, charSequence) ?: charSequence
171175
}
172176

177+
override fun toString(): String {
178+
return charSequence.toString()
179+
}
180+
173181
@Throws(IOException::class)
174182
private fun writeObject(stream: ObjectOutputStream) {
175183
charSequence.let { charSequence ->
@@ -231,6 +239,10 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
231239
return context.getString(stringRes)
232240
}
233241

242+
override fun toString(): String {
243+
return "ResourceText($stringRes)"
244+
}
245+
234246
override fun equals(other: Any?): Boolean {
235247
if (this === other) return true
236248
if (javaClass != other?.javaClass) return false
@@ -271,6 +283,7 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
271283
return String.format(context.getLocale(), string, *formatArgs)
272284
}
273285

286+
274287
override fun getCharSequence(context: Context, textStyle: TextStyle?): CharSequence {
275288
val text = getText(context)
276289
return if (text is Spanned || formatArgs.firstOrNull { it is Text || it is Image } != null) {
@@ -312,6 +325,10 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
312325

313326
override fun getText(context: Context): CharSequence = context.getText(stringRes)
314327

328+
override fun toString(): String {
329+
return "ResourceFormatText($stringRes, $formatArgs)"
330+
}
331+
315332
override fun equals(other: Any?): Boolean {
316333
if (this === other) return true
317334
if (javaClass != other?.javaClass) return false
@@ -360,6 +377,10 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
360377
result = 31 * result + formatArgs.contentHashCode()
361378
return result
362379
}
380+
381+
override fun toString(): String {
382+
return "PluralsFormatText(res=$res, quantity=$quantity, formatArgs=$formatArgs)"
383+
}
363384
}
364385

365386
private class ArrayText constructor(
@@ -413,6 +434,10 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
413434
result = 31 * result + texts.contentHashCode()
414435
return result
415436
}
437+
438+
override fun toString(): String {
439+
return "ArrayText(texts=$texts)"
440+
}
416441
}
417442

418443
interface StringHolder {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.omega_r.libs.omegatypes
22

33
import android.content.Context
44
import android.content.res.Resources
5+
import com.omega_r.libs.omegatypes.image.Image
56
import java.io.Serializable
67

78
/**
@@ -27,6 +28,8 @@ class TextBuilder(capacity: Int) : Serializable {
2728
return this
2829
}
2930

31+
fun append(image: Image): TextBuilder = append(Text.from(image))
32+
3033
fun append(string: String) = append(Text.from(string))
3134

3235
fun append(stringRes: Int) = append(Text.from(stringRes))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.omega_r.libs.omegatypes.image
2+
3+
import android.content.Context
4+
import android.graphics.Bitmap
5+
import com.omega_r.libs.omegatypes.decoders.toBitmap
6+
7+
data class AssetImage(val fileName: String) : BaseBitmapImage() {
8+
9+
companion object {
10+
11+
init {
12+
ImageProcessors.default.addImageProcessor(AssetImage::class, Processor())
13+
}
14+
}
15+
16+
class Processor : BaseBitmapImage.Processor<AssetImage>(true) {
17+
18+
override suspend fun getBitmap(context: Context, image: AssetImage, width: Int?, height: Int?): Bitmap? {
19+
return context.assets.open(image.fileName).use {
20+
it.toBitmap(width, height)
21+
}
22+
}
23+
}
24+
}

omegatypes/src/main/java/com/omega_r/libs/omegatypes/image/FileImage.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import com.omega_r.libs.omegatypes.decoders.toBitmap
77
import com.omega_r.libs.omegatypes.file.File
88
import com.omega_r.libs.omegatypes.file.FileSystems
99

10-
/**
11-
* Created by Anton Knyazev on 2019-10-07.
12-
*/
1310
/**
1411
* Created by Anton Knyazev on 2019-10-03.
1512
*/

omegatypes/src/main/java/com/omega_r/libs/omegatypes/image/UrlImage.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ data class UrlImage(val baseUrl: String? = null, val relativeUrl: String) : Base
2626
private fun String.isAbsoluteUrl(): Boolean = PATTERN_ABSOLUTE_URL.matcher(this).matches()
2727

2828
}
29-
30-
29+
3130
val url: String
3231
get() = if (relativeUrl.isAbsoluteUrl()) relativeUrl else {
3332
val baseUrl = (baseUrl ?: defaultBaseUrl ?: "").removeSuffix("/")

0 commit comments

Comments
 (0)