diff --git a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/any/AnyExtensions.kt b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/any/AnyExtensions.kt new file mode 100644 index 00000000..36c225f6 --- /dev/null +++ b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/any/AnyExtensions.kt @@ -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::class.java) + * + **/ +fun String.fromJson(type: Type): T? { + if (this.isEmpty()) return null + return try { + gson.fromJson(this, type) + } catch (exception: Exception) { + Log.e(JCHUCOMPONENTS_ERROR, exception.message.orEmpty()) + null + } +} \ No newline at end of file diff --git a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/constants/LogConstants.kt b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/constants/LogConstants.kt new file mode 100644 index 00000000..5d7488af --- /dev/null +++ b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/constants/LogConstants.kt @@ -0,0 +1,3 @@ +package com.jeluchu.jchucomponents.ktx.constants + +const val JCHUCOMPONENTS_ERROR = "JCHU-[ERROR]" \ No newline at end of file diff --git a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/context/NetworkExtensions.kt b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/context/NetworkExtensions.kt index 5707ef77..88f73a41 100644 --- a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/context/NetworkExtensions.kt +++ b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/context/NetworkExtensions.kt @@ -11,8 +11,8 @@ import android.content.Context import android.net.ConnectivityManager import android.net.NetworkCapabilities import androidx.annotation.RequiresPermission -import com.jeluchu.jchucomponents.ktx.ints.empty -import com.jeluchu.jchucomponents.ktx.ints.orEmpty +import com.jeluchu.jchucomponents.ktx.numbers.empty +import com.jeluchu.jchucomponents.ktx.numbers.orEmpty import com.jeluchu.jchucomponents.ktx.packageutils.buildIsMAndLower import com.jeluchu.jchucomponents.ktx.packageutils.buildIsMarshmallowAndUp import com.jeluchu.jchucomponents.ktx.packageutils.buildIsPAndUp diff --git a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/date/DateExtensions.kt b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/date/DateExtensions.kt index b805d833..9b02c087 100644 --- a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/date/DateExtensions.kt +++ b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/date/DateExtensions.kt @@ -6,6 +6,9 @@ package com.jeluchu.jchucomponents.ktx.date +import android.provider.Settings.System.DATE_FORMAT +import com.jeluchu.jchucomponents.ktx.numbers.orEmpty +import java.text.ParseException import java.text.SimpleDateFormat import java.util.* @@ -47,6 +50,23 @@ const val DATE_FORMAT_TIMESTAMP = "dd/MM/yyyy HH:mm" */ const val DATE_FORMAT_ONLY_TIME = "HH:mm 'H'" +/** + * + * Constant to perform the time formatting as follows: "EEEE, MMMM d, yyyy - hh:mm:ss a" + * For example: "21:02 hrs" + * + * E: Day of week + * M: Month of year + * d: Day of month + * y: Year + * H: Hour + * m: Minutes + * s: Seconds + * a: AM / PM + * + */ +const val DATE_FORMAT_WEEK_AND_MONTH_TIME = "EEEE, MMMM d, yyyy - hh:mm:ss a" + /** ---- FORMATS ------------------------------------------------------------------------------- **/ fun Date.format(): String = diff --git a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/decimals/DecimalsExtensions.kt b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/decimals/DecimalsExtensions.kt deleted file mode 100644 index 4a110916..00000000 --- a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/decimals/DecimalsExtensions.kt +++ /dev/null @@ -1,13 +0,0 @@ -/* - * - * Copyright 2022 Jeluchu - * - */ - -package com.jeluchu.jchucomponents.ktx.decimals - -import java.math.RoundingMode - -fun Float.roundTo(n: Int): Float = toBigDecimal().setScale(n, RoundingMode.UP).toFloat() - -fun Double.roundTo(n: Int): Double = toBigDecimal().setScale(n, RoundingMode.UP).toDouble() \ No newline at end of file diff --git a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/lists/ListsExtensions.kt b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/lists/ListsExtensions.kt index a6509b96..6ac8636d 100644 --- a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/lists/ListsExtensions.kt +++ b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/lists/ListsExtensions.kt @@ -28,8 +28,5 @@ fun LazyListState.reenableScrolling(scope: CoroutineScope) { } } -fun MutableList.addAllIfNotExist(elements: Collection) { - for (element in elements) { - if (!contains(element)) add(element) - } -} \ No newline at end of file +val List.penultimateElement: T? + get() = if (isEmpty()) null else get(size - 2) \ No newline at end of file diff --git a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/lists/MutableListExtensions.kt b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/lists/MutableListExtensions.kt new file mode 100644 index 00000000..6c890769 --- /dev/null +++ b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/lists/MutableListExtensions.kt @@ -0,0 +1,11 @@ +package com.jeluchu.jchucomponents.ktx.lists + +fun MutableList.addAllIfNotExist(elements: Collection) { + for (element in elements) { + if (!contains(element)) add(element) + } +} + +fun MutableList.concatenateLowercase() : String { + return this.joinToString("") { s -> s.lowercase() } +} \ No newline at end of file diff --git a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/numbers/DecimalsExtensions.kt b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/numbers/DecimalsExtensions.kt new file mode 100644 index 00000000..312867e1 --- /dev/null +++ b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/numbers/DecimalsExtensions.kt @@ -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) +} \ No newline at end of file diff --git a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/ints/IntExtensions.kt b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/numbers/IntExtensions.kt similarity index 97% rename from jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/ints/IntExtensions.kt rename to jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/numbers/IntExtensions.kt index 8022c25e..4cdb9109 100644 --- a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/ints/IntExtensions.kt +++ b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/numbers/IntExtensions.kt @@ -4,7 +4,7 @@ * */ -package com.jeluchu.jchucomponents.ktx.ints +package com.jeluchu.jchucomponents.ktx.numbers import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.height diff --git a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/strings/StringExtensions.kt b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/strings/StringExtensions.kt index 01e9b737..4cbcf840 100644 --- a/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/strings/StringExtensions.kt +++ b/jchucomponents-ktx/src/main/java/com/jeluchu/jchucomponents/ktx/strings/StringExtensions.kt @@ -30,6 +30,8 @@ import java.util.regex.Pattern import org.intellij.lang.annotations.RegExp import org.json.JSONObject +fun String.removeFirstLastChar(): String = this.substring(1, this.length - 1) + /** ---- FORMATER STRINGS ---------------------------------------------------------------------- **/ fun String.addSpaceAfterEvery4Chars() = replace("....".toRegex(), "$0 ") fun String.addSpaceAfterEvery3Chars() = replace("...".toRegex(), "$0 ") @@ -335,7 +337,6 @@ fun String.jwtBodyToJsonObject(): JSONObject? { return result } - /** ---- CURRENCIES ---------------------------------------------------------------------------- **/ fun String.getCurrencyString() = diff --git a/jchucomponents-ui/src/main/java/com/jeluchu/jchucomponents/ui/composables/cards/StoriesCard.kt b/jchucomponents-ui/src/main/java/com/jeluchu/jchucomponents/ui/composables/cards/StoriesCard.kt index 02eb3e9b..d551629c 100644 --- a/jchucomponents-ui/src/main/java/com/jeluchu/jchucomponents/ui/composables/cards/StoriesCard.kt +++ b/jchucomponents-ui/src/main/java/com/jeluchu/jchucomponents/ui/composables/cards/StoriesCard.kt @@ -28,7 +28,7 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.constraintlayout.compose.ConstraintLayout -import com.jeluchu.jchucomponents.ktx.ints.isNotEmpty +import com.jeluchu.jchucomponents.ktx.numbers.isNotEmpty import com.jeluchu.jchucomponents.ktx.strings.empty import com.jeluchu.jchucomponents.ui.composables.images.NetworkImage diff --git a/jchucomponents-ui/src/main/java/com/jeluchu/jchucomponents/ui/composables/chips/ChipTagView.kt b/jchucomponents-ui/src/main/java/com/jeluchu/jchucomponents/ui/composables/chips/ChipTagView.kt index 70542d92..9c44d649 100644 --- a/jchucomponents-ui/src/main/java/com/jeluchu/jchucomponents/ui/composables/chips/ChipTagView.kt +++ b/jchucomponents-ui/src/main/java/com/jeluchu/jchucomponents/ui/composables/chips/ChipTagView.kt @@ -28,7 +28,7 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp -import com.jeluchu.jchucomponents.ktx.ints.isNotEmpty +import com.jeluchu.jchucomponents.ktx.numbers.isNotEmpty import com.jeluchu.jchucomponents.ktx.strings.empty import com.jeluchu.jchucomponents.ui.composables.images.NetworkImage diff --git a/jchucomponents-ui/src/main/java/com/jeluchu/jchucomponents/ui/composables/images/DoubleTapAnimation.kt b/jchucomponents-ui/src/main/java/com/jeluchu/jchucomponents/ui/composables/images/DoubleTapAnimation.kt index f1335ba3..e25cfb86 100644 --- a/jchucomponents-ui/src/main/java/com/jeluchu/jchucomponents/ui/composables/images/DoubleTapAnimation.kt +++ b/jchucomponents-ui/src/main/java/com/jeluchu/jchucomponents/ui/composables/images/DoubleTapAnimation.kt @@ -27,7 +27,7 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.vectorResource import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp -import com.jeluchu.jchucomponents.ktx.ints.empty +import com.jeluchu.jchucomponents.ktx.numbers.empty import com.jeluchu.jchucomponents.ktx.strings.empty import com.jeluchu.jchucomponents.ui.runtime.remember.rememberMutableStateOf