Skip to content

Commit 19e9696

Browse files
committed
Added placeholder for image
Added StringHolder for custom class
1 parent 15f9019 commit 19e9696

File tree

3 files changed

+64
-27
lines changed

3 files changed

+64
-27
lines changed

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

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ import java.io.*
1414

1515
open class Image : Serializable {
1616

17-
open fun applyImage(imageView: ImageView) {
18-
imageView.setImageDrawable(null)
17+
open fun applyImage(imageView: ImageView, placeholderResId: Int = 0) {
18+
if (placeholderResId == 0) {
19+
imageView.setImageDrawable(null)
20+
} else {
21+
imageView.setImageResource(placeholderResId)
22+
}
1923
}
2024

21-
open fun applyBackground(view: View) {
22-
applyBackground(view, null)
25+
open fun applyBackground(view: View, placeholderResId: Int = 0) {
26+
if (placeholderResId == 0) {
27+
view.setBackgroundResource(placeholderResId)
28+
} else {
29+
applyBackground(view, null)
30+
}
2331
}
2432

2533

@@ -61,11 +69,11 @@ open class Image : Serializable {
6169

6270
class ResourceImage(private val resId: Int) : Image() {
6371

64-
override fun applyImage(imageView: ImageView) {
72+
override fun applyImage(imageView: ImageView, placeholderResId: Int) {
6573
imageView.setImageResource(resId)
6674
}
6775

68-
override fun applyBackground(view: View) {
76+
override fun applyBackground(view: View, placeholderResId: Int) {
6977
view.setBackgroundResource(resId)
7078
}
7179

@@ -78,11 +86,11 @@ open class Image : Serializable {
7886

7987
class DrawableImage(private val drawable: Drawable) : Image() {
8088

81-
override fun applyImage(imageView: ImageView) {
89+
override fun applyImage(imageView: ImageView, placeholderResId: Int) {
8290
imageView.setImageDrawable(drawable)
8391
}
8492

85-
override fun applyBackground(view: View) {
93+
override fun applyBackground(view: View, placeholderResId: Int) {
8694
applyBackground(view, drawable)
8795
}
8896

@@ -94,11 +102,11 @@ open class Image : Serializable {
94102

95103
class BitmapImage(private val bitmap: Bitmap) : Image() {
96104

97-
override fun applyImage(imageView: ImageView) {
105+
override fun applyImage(imageView: ImageView, placeholderResId: Int) {
98106
imageView.setImageBitmap(bitmap)
99107
}
100108

101-
override fun applyBackground(view: View) {
109+
override fun applyBackground(view: View, placeholderResId: Int) {
102110
applyBackground(view, BitmapDrawable(view.resources, bitmap))
103111
}
104112

@@ -142,15 +150,29 @@ fun Drawable.toBitmap(
142150
return bitmap
143151
}
144152

145-
fun ImageView.setImage(image: Image?) {
146-
image?.applyImage(this) ?: setImageDrawable(null)
153+
@JvmOverloads
154+
fun ImageView.setImage(image: Image?, placeholderResId: Int = 0) {
155+
if (image != null) {
156+
image.applyImage(this, placeholderResId)
157+
} else {
158+
if (placeholderResId == 0) {
159+
setImageDrawable(null)
160+
} else {
161+
setImageResource(placeholderResId)
162+
}
163+
}
147164
}
148165

149-
fun View.setBackground(image: Image?) {
150-
image?.applyBackground(this) ?: applyBackground(this, null)
151-
166+
@JvmOverloads
167+
fun View.setBackground(image: Image?, placeholderResId: Int = 0) {
168+
if (image != null) {
169+
image.applyBackground(this, placeholderResId)
170+
} else {
171+
if (placeholderResId == 0) {
172+
applyBackground(this, null)
173+
} else {
174+
setBackgroundResource(placeholderResId)
175+
}
176+
}
152177
}
153178

154-
fun Image.applyTo(imageView: ImageView) {
155-
imageView.setImage(this)
156-
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ open class Text private constructor() : Serializable {
2424
}
2525

2626
override fun hashCode(): Int {
27-
return 31 * 17 + "".hashCode()
27+
return 31 * 17
2828
}
2929

3030
companion object {
@@ -38,8 +38,10 @@ open class Text private constructor() : Serializable {
3838
fun from(stringRes: Int): Text = ResourceText(stringRes)
3939

4040
@JvmStatic
41-
fun from(stringRes: Int, vararg formatArgs: Any): Text =
42-
FormatResourceText(stringRes, *formatArgs)
41+
fun from(stringRes: Int, vararg formatArgs: Any): Text = FormatResourceText(stringRes, *formatArgs)
42+
43+
@JvmStatic
44+
fun from(stringHolder: StringHolder): Text = stringHolder.getStringText()?.let { StringText(it) } ?: empty()
4345

4446
@JvmStatic
4547
fun from(throwable: Throwable): Text = StringText(throwable.message)
@@ -117,6 +119,12 @@ open class Text private constructor() : Serializable {
117119

118120
}
119121

122+
interface StringHolder {
123+
124+
fun getStringText(): String?
125+
126+
}
127+
120128
}
121129

122130
fun TextView.setText(text: Text) {

picasso/src/main/java/com/omega_r/libs/omegatypes/picasso/ImageExtension.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,26 @@ fun Image.Companion.from(url: String) = PicassoImage(url)
2222

2323
class PicassoImage(private val url: String) : Image() {
2424

25-
override fun applyImage(imageView: ImageView) {
25+
override fun applyImage(imageView: ImageView, placeholderResId: Int) {
2626
Picasso.get()
2727
.load(url)
28-
.fit()
29-
.centerCrop()
30-
.into(imageView)
28+
.apply {
29+
if (placeholderResId != 0) placeholder(placeholderResId)
30+
fit()
31+
centerCrop()
32+
into(imageView)
33+
}
3134
}
3235

33-
override fun applyBackground(view: View) {
34-
applyBackground(view, null)
36+
override fun applyBackground(view: View, placeholderResId: Int) {
37+
super.applyBackground(view, placeholderResId)
3538

3639
val requestCreator = Picasso.get().load(url)
3740

41+
if (placeholderResId != 0) {
42+
requestCreator.placeholder(placeholderResId)
43+
}
44+
3845
if (view.width > 0 && view.height > 0) {
3946
requestCreator.resize(view.width, view.height)
4047
}

0 commit comments

Comments
 (0)