This file contains hidden or 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
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) |
This file contains hidden or 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
fun Double.round(decimals: Int): Double { | |
var multiplier = 1.0 | |
repeat(decimals) { multiplier *= 10 } | |
return round(this * multiplier) / multiplier | |
} |
This file contains hidden or 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
/* | |
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 |
This file contains hidden or 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
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) |
This file contains hidden or 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
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(){ |
This file contains hidden or 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
// 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) | |
} |
This file contains hidden or 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
// 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 | |
} |
This file contains hidden or 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
// 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 |
This file contains hidden or 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
// 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) |
This file contains hidden or 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
// 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) |