-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New extensions for Any or MutableList
- Loading branch information
Showing
13 changed files
with
109 additions
and
25 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/any/AnyExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.jeluchu.jchucomponents.ktx.any | ||
|
||
import android.util.Log | ||
import com.jeluchu.jchucomponents.ktx.constants.JCHUCOMPONENTS_ERROR | ||
import com.jeluchu.jchucomponents.ktx.gson.gson | ||
import java.lang.reflect.Type | ||
|
||
fun Any?.isNull() = this == null | ||
|
||
/** | ||
* | ||
* | ||
* Ex: object.toJson() ?: "" / object.toJson().orEmpty() | ||
* | ||
**/ | ||
fun Any.toJson(): String? { | ||
return try { | ||
gson.toJson(this) | ||
} catch (exception: Exception) { | ||
Log.e(JCHUCOMPONENTS_ERROR, exception.message.orEmpty()) | ||
null | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* Below method uses generics and can convert JSONString | ||
* to Any type of object depending on the type provided | ||
* | ||
* Ex: json.fromJson<Object>(Object::class.java) | ||
* | ||
**/ | ||
fun <T> String.fromJson(type: Type): T? { | ||
if (this.isEmpty()) return null | ||
return try { | ||
gson.fromJson<T>(this, type) | ||
} catch (exception: Exception) { | ||
Log.e(JCHUCOMPONENTS_ERROR, exception.message.orEmpty()) | ||
null | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/constants/LogConstants.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.jeluchu.jchucomponents.ktx.constants | ||
|
||
const val JCHUCOMPONENTS_ERROR = "JCHU-[ERROR]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
...omponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/decimals/DecimalsExtensions.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...omponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/lists/MutableListExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.jeluchu.jchucomponents.ktx.lists | ||
|
||
fun <T> MutableList<T>.addAllIfNotExist(elements: Collection<T>) { | ||
for (element in elements) { | ||
if (!contains(element)) add(element) | ||
} | ||
} | ||
|
||
fun MutableList<String>.concatenateLowercase() : String { | ||
return this.joinToString("") { s -> s.lowercase() } | ||
} |
24 changes: 24 additions & 0 deletions
24
...components-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/numbers/DecimalsExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* | ||
* Copyright 2022 Jeluchu | ||
* | ||
*/ | ||
|
||
package com.jeluchu.jchucomponents.ktx.numbers | ||
|
||
import java.math.RoundingMode | ||
import java.text.DecimalFormat | ||
|
||
fun Float.roundTo(n: Int): Float = toBigDecimal().setScale(n, RoundingMode.UP).toFloat() | ||
|
||
fun Double.roundTo(n: Int): Double = toBigDecimal().setScale(n, RoundingMode.UP).toDouble() | ||
|
||
fun String.toPriceAmount(): String { | ||
val dec = DecimalFormat("###,###,###.00") | ||
return dec.format(this.toDouble()) | ||
} | ||
|
||
fun Double.toPriceAmount(): String { | ||
val dec = DecimalFormat("###,###,###.00") | ||
return dec.format(this) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters