Skip to content

Instantly share code, notes, and snippets.

@moisesaq
moisesaq / CurrentLanguage
Last active August 10, 2019 17:13
[Android]get current language of device
Locale.getDefault().getLanguage() ---> en
Locale.getDefault().getISO3Language() ---> eng
Locale.getDefault().getCountry() ---> US
Locale.getDefault().getISO3Country() ---> USA
Locale.getDefault().getDisplayCountry() ---> United States
Locale.getDefault().getDisplayName() ---> English (United States)
Locale.getDefault().toString() ---> en_US
Locale.getDefault().getDisplayLanguage()---> English
@moisesaq
moisesaq / friendlyTimeDiff.java
Last active June 15, 2017 20:48
Friendly Time Difference
String friendlyTimeDiff(long timeDifferenceMilliseconds) {
long diffSeconds = timeDifferenceMilliseconds / 1000;
long diffMinutes = timeDifferenceMilliseconds / (60 * 1000);
long diffHours = timeDifferenceMilliseconds / (60 * 60 * 1000);
long diffDays = timeDifferenceMilliseconds / (60 * 60 * 1000 * 24);
long diffWeeks = timeDifferenceMilliseconds / (60 * 60 * 1000 * 24 * 7);
long diffMonths = (long) (timeDifferenceMilliseconds / (60 * 60 * 1000 * 24 * 30.41666666));
long diffYears = timeDifferenceMilliseconds / ((long)60 * 60 * 1000 * 24 * 365);
if (diffSeconds < 1) {
@moisesaq
moisesaq / UICollectionView - Selection by scroll horizontal
Last active August 10, 2019 17:13
[iOS]Select a item by scroll horizontal in a collection view
//------------------------------------- METHOD 1 ------------------------------------
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let index = Int(targetContentOffset.move().x / view.frame.width)
let indexPath = IndexPath(item: index, section: 0)
print("Current index path: \(indexPath.row)")
}
//------------------------------------- METHOD 2 ------------------------------------
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
var visibleRect = CGRect()
@moisesaq
moisesaq / NavigationBuilder.kt
Last active July 15, 2020 04:43
Simple builder to create two intent for Google Maps and Waze
class NavigationBuilder {
fun mapsIntent(location: Location, address: String): Intent {
val uri = Uri.parse("geo:${location.lat},${location.lng}?q=$address")
return Intent(Intent.ACTION_VIEW, uri).apply { setPackage("com.google.android.apps.maps") }
}
fun wazeIntent(location: Location, address: String): Intent {
val uri = Uri.parse("https://waze.com/ul?q=$address&ll=${location.lat},${location.lng}&navigate=yes")
return Intent(Intent.ACTION_VIEW, uri)