https://medium.com/nerd-for-tech/useful-kotlin-extensions-for-android-d8652f64bca8
유용한 확장함수
fun Context.getCompatColor(@ColorRes colorId: Int) = ResourcesCompat.getColor(resources, colorId, null)
fun Context.getCompatDrawable(@DrawableRes drawableId: Int) = AppCompatResources.getDrawable(this, drawableId)!
<-------------------------------------------------------------->
// Activity
getCompatColor(R.color.white)
// Fragment
requireContext().getCompatColor(R.color.white)
퍼미션 관련
// Traditional way (Activity)
(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
// Extension (Activity)
hasPermissions(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION)
Date
val isoFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
fun Date.toISOFormat() : String = isoFormatter.format(this)
<-------------------------------------------------------------->
Date(1620862687852).toISOFormat() // 2021-05-12 23:38:07.540
view raw
PriceFormat
val priceFormatter = DecimalFormat("###,###,###.00")
fun Double.formatPrice(): String = priceFormatter.format(this)
<-------------------------------------------------------------->
fun Context.formatPrice(price: Double) : String {
val formatter = DecimalFormat(getString(R.string.price_formatter))
return formatter.format(price)
}