Functions:
- Set Icons Color Function
- Check expiration date
- Format Time (Int to String)
- Format Time (LocalTime to String)
- Format Time (Calendar to String)
- Format Date (Int to String)
- Format Duration Time
- Difference Date
- Format Money (Double to String)
- BitMap To String
- String To BitMap
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Base64.*
import android.widget.ImageView
import java.io.ByteArrayOutputStream
import java.math.BigDecimal
import java.text.NumberFormat
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import java.util.*
/**
* object containing common functions
* for the App
*/
object CommonApp{
//Set Icons Color Function
fun setIconColor(context: Context, icon: ImageView?, color: Int){
icon?.setColorFilter(
ContextCompat.getColor(context, color)
)
}
//Check expiration date
fun isDateExpired(dayOfMonth: Int, monthOfYear: Int, year: Int): Boolean {
val expDate = GregorianCalendar(year, monthOfYear, dayOfMonth)
val now = GregorianCalendar()
return now.after(expDate)
}
//////////////////////// FORMAT TIME /////////////////////////////
fun formatTime(time: Int): String {
return SimpleDateFormat("HH:mm")
.format(Date(time * 1000L))
}
fun formatTime(time: LocalTime): String {
val formatter = DateTimeFormatter.ofPattern("HH:mm")
return time.format(formatter)
}
fun formatTime(time: Calendar): String {
val format = SimpleDateFormat("HH:mm")
return format.format(time.time)
}
//////////////////////// FORMAT DATE /////////////////////////////
fun formatDate(date: Int): String {
return SimpleDateFormat("dd MMMM yyyy")
.format(Date(date * 1000L))
}
fun formatMonthYear(date: Int): String {
return SimpleDateFormat("MMMM yyyy")
.format(Date(date * 1000L))
}
//////////////////////// FORMAT MINUTES /////////////////////////////
fun formatDurationTime(minutes: Int, initial: String): String = if (minutes < 60) {
"$minutes $initial"
} else {
val min = (minutes) % 60
val hours = (minutes) / 60
String.format("%02dh %02dm", hours, min)
}
fun differenceDate(fromDateTime: LocalDateTime, toDateTime: LocalDateTime) {
var tempDateTime = LocalDateTime.from(fromDateTime)
val days = tempDateTime.until(toDateTime, ChronoUnit.DAYS)
tempDateTime = tempDateTime.plusDays(days)
val hours = tempDateTime.until(toDateTime, ChronoUnit.HOURS)
tempDateTime = tempDateTime.plusHours(hours)
val minutes = tempDateTime.until(toDateTime, ChronoUnit.MINUTES)
tempDateTime = tempDateTime.plusMinutes(minutes)
val seconds = tempDateTime.until(toDateTime, ChronoUnit.SECONDS)
}
//////////////////////// FORMAT MONEY /////////////////////////////
fun formatMoney(amount: Double): String {
val formatter = NumberFormat.getCurrencyInstance(Locale.ITALY)
return formatter.format(BigDecimal(amount))
}
//////////////////////// CONVERT BITMAP /////////////////////////////
fun BitMapToString(bitmap: Bitmap): String {
val baos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos)
val b = baos.toByteArray()
return encodeToString(b, DEFAULT)
}
fun StringToBitMap(encodedString: String?): Bitmap? {
return try {
val encodeByte: ByteArray = decode(encodedString, DEFAULT)
BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.size)
} catch (e: Exception) {
e.message
null
}
}
}