package com.margelo.nitro.progressandmodal import android.content.Context import android.util.TypedValue import android.view.animation.AlphaAnimation import android.view.animation.Animation import android.widget.ImageView import androidx.core.content.ContextCompat object PAMColors { const val CONTAINER_BG = "#F3F4F6" const val TITLE_COLOR = "#030712" const val SUBTITLE_COLOR = "#6B7280" } object ViewUtils { fun dpToPx(context: Context, dp: Float): Float { return TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, dp, context.resources.displayMetrics ) } fun fadeIn(view: android.view.View) { val anim = AlphaAnimation(0f, 1f).apply { duration = 250 fillAfter = true } view.startAnimation(anim) } fun fadeOut(view: android.view.View, onComplete: () -> Unit) { val anim = AlphaAnimation(1f, 0f).apply { duration = 250 fillAfter = true setAnimationListener(object : Animation.AnimationListener { override fun onAnimationStart(a: Animation?) {} override fun onAnimationRepeat(a: Animation?) {} override fun onAnimationEnd(a: Animation?) { onComplete() } }) } view.startAnimation(anim) } fun loadImageBitmap(uri: String, imageView: ImageView) { if (uri.startsWith("http://") || uri.startsWith("https://")) { Thread { try { val url = java.net.URL(uri) val connection = url.openConnection() as java.net.HttpURLConnection connection.doInput = true connection.connect() val bitmap = android.graphics.BitmapFactory.decodeStream(connection.inputStream) imageView.post { imageView.setImageBitmap(bitmap) } } catch (e: Exception) { e.printStackTrace() } }.start() } else { var resName = if (uri.contains("/")) uri.substringAfterLast("/").substringBeforeLast(".") else uri.substringBeforeLast(".") val context = imageView.context val resId = context.resources.getIdentifier(resName, "drawable", context.packageName) if (resId != 0) { imageView.setImageDrawable(ContextCompat.getDrawable(context, resId)) } } } }