Skip to content

Commit

Permalink
New extensions for Any or MutableList
Browse files Browse the repository at this point in the history
  • Loading branch information
jeluchu committed Jan 25, 2023
1 parent 239c643 commit 42f5104
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 25 deletions.
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
}
}
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]"
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand Down Expand Up @@ -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 =
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,5 @@ fun LazyListState.reenableScrolling(scope: CoroutineScope) {
}
}

fun <T> MutableList<T>.addAllIfNotExist(elements: Collection<T>) {
for (element in elements) {
if (!contains(element)) add(element)
}
}
val <T> List<T?>.penultimateElement: T?
get() = if (isEmpty()) null else get(size - 2)
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() }
}
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ")
Expand Down Expand Up @@ -335,7 +337,6 @@ fun String.jwtBodyToJsonObject(): JSONObject? {
return result
}


/** ---- CURRENCIES ---------------------------------------------------------------------------- **/

fun String.getCurrencyString() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 42f5104

Please sign in to comment.