Skip to content

Instantly share code, notes, and snippets.

View nadar71's full-sized avatar
🏠
Working from home

Simone Mapelli nadar71

🏠
Working from home
View GitHub Profile
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
// Converting from String to Date
fun String.getDateWithServerTimeStamp(): Date? {
val dateFormat = SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
Locale.ENGLISH)
@nadar71
nadar71 / NumberExtensions.kt
Created October 21, 2020 09:49
Round double extensions
fun Double.round(decimals: Int): Double {
var multiplier = 1.0
repeat(decimals) { multiplier *= 10 }
return round(this * multiplier) / multiplier
}
@nadar71
nadar71 / LiveDataExtensions.kt
Created October 21, 2020 09:48
Extension to avoid false positive in room query
/*
Extension to avoid false positive in room query :
https://stackoverflow.com/questions/47215666/room-livedata-from-dao-will-trigger-observer-onchanged-on-every-update-even-i
https://medium.com/androiddevelopers/7-pro-tips-for-room-fbadea4bfbd1#5e38
*/
fun <T> LiveData<T>.getDistinct(): LiveData<T> {
val distinctLiveData = MediatorLiveData<T>()
distinctLiveData.addSource(this, object : Observer<T> {
private var initialized = false
private var lastObj: T? = null
import java.text.SimpleDateFormat
import java.util.*
// Converting from Date to String
fun Date.getStringTimeStampWithDate(): String {
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
Locale.ENGLISH)
// dateFormat.timeZone = TimeZone.getTimeZone("UTC")
return dateFormat.format(this)
import java.util.*
fun Calendar.resetTime(){
set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}
fun Calendar.endDay(){
@nadar71
nadar71 / Utility.kt
Created October 21, 2020 09:47
open url in browser
// open url in browser
fun openUrlInBrowser(urlToOpen: String, view: View, context: Context, options: Bundle?) {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(urlToOpen)
startActivity(context,intent,options)
}
@nadar71
nadar71 / Utility.kt
Created October 21, 2020 09:46
Return the local time midnight for the provided normalized UTC date.
// Return the local time midnight for the provided normalized UTC date.
fun getLocalMidnightFromNormalizedUtcDate(normalizedUtcDate: Long): Long {
val timeZone = TimeZone.getDefault()
val gmtOffset = timeZone.getOffset(normalizedUtcDate).toLong()
return normalizedUtcDate - gmtOffset
}
@nadar71
nadar71 / Utility.kt
Created October 21, 2020 09:44
Basic Check if internet connection is on. Obsolete, using deprecated methods, maust implement this : https://medium.com/swlh/how-to-check-internet-connection-on-android-q-ea7c5a103e3
// Check if internet connection is on
val isConnectionOk: Boolean
get() {
val connManager = (DiceApplication.getsContext() as DiceApplication)
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val netinfo = connManager.activeNetworkInfo
if (netinfo != null && netinfo.isConnected) {
Log.d(TAG, "Connections is OK !")
return true
} else
@nadar71
nadar71 / Utility.kt
Created October 21, 2020 09:41
Show blocking alert
// generic alert
fun showGenericBlockingAlert(title: String, msg: String, activity: Activity) {
val dialogBuilder = AlertDialog.Builder(activity)
dialogBuilder.setMessage(msg)
.setCancelable(false)
.setPositiveButton("OK") { dialog, id ->
}
val alert = dialogBuilder.create()
alert.setCancelable(false)
@nadar71
nadar71 / Utility.kt
Created October 21, 2020 09:40
Returns the number of milliseconds (UTC time) for today's date at midnight in the local time zone
// Returns the number of milliseconds (UTC time) for today's date at midnight in the local time zone
val normalizedUtcMsForToday: Long
get() {
val utcNowMillis = System.currentTimeMillis()
val currentTimeZone = TimeZone.getDefault()
val gmtOffsetMillis = currentTimeZone.getOffset(utcNowMillis).toLong()
val timeSinceEpochLocalTimeMillis = utcNowMillis + gmtOffsetMillis
val daysSinceEpochLocal =
TimeUnit.MILLISECONDS.toDays(timeSinceEpochLocalTimeMillis)