Skip to content

Commit 4cc9e2e

Browse files
Merge pull request #23 from Omega-R/feature/plurals_text
Feature/plurals text
2 parents 9643cfe + d91b26a commit 4cc9e2e

File tree

1 file changed

+42
-0
lines changed
  • omegatypes/src/main/java/com/omega_r/libs/omegatypes

1 file changed

+42
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
3333
@JvmOverloads
3434
fun from(stringRes: Int, vararg formatArgs: Any, textStyle: TextStyle? = null): Text = FormatResourceText(stringRes, *formatArgs, textStyle = textStyle)
3535

36+
@JvmStatic
37+
@JvmOverloads
38+
fun from(stringRes: Int, quantity: Int, vararg formatArgs: Any, textStyle: TextStyle? = null): Text = PluralsText(stringRes, quantity, *formatArgs, textStyle = textStyle)
39+
3640
@JvmStatic
3741
@JvmOverloads
3842
fun from(stringHolder: StringHolder, textStyle: TextStyle? = null): Text = stringHolder.getStringText()?.let { from(it, textStyle) }
@@ -228,6 +232,44 @@ open class Text(protected val defaultTextStyle: TextStyle?) : Serializable, Text
228232

229233
}
230234

235+
class PluralsText internal constructor(
236+
private val res: Int,
237+
private val quantity: Int,
238+
private vararg val formatArgs: Any,
239+
textStyle: TextStyle? = null
240+
) : Text(textStyle) {
241+
242+
override fun isEmpty(): Boolean = res <= 0
243+
244+
override fun getString(context: Context): String? {
245+
return context.resources.getQuantityString(res, quantity, *formatArgs)
246+
}
247+
248+
override fun equals(other: Any?): Boolean {
249+
if (this === other) return true
250+
if (javaClass != other?.javaClass) return false
251+
if (!super.equals(other)) return false
252+
253+
other as PluralsText
254+
255+
if (res != other.res) return false
256+
if (quantity != other.quantity) return false
257+
if (!formatArgs.contentEquals(other.formatArgs)) return false
258+
259+
return true
260+
}
261+
262+
override fun hashCode(): Int {
263+
var result = super.hashCode()
264+
result = 31 * result + res
265+
result = 31 * result + quantity
266+
result = 31 * result + formatArgs.contentHashCode()
267+
return result
268+
}
269+
270+
271+
}
272+
231273
private class ArrayText internal constructor(
232274
private vararg val texts: Text,
233275
textStyle: TextStyle?

0 commit comments

Comments
 (0)