Last active
October 13, 2019 15:28
-
-
Save henriquehorbovyi/ba2a0923c72d47783102efaf7f8b220c to your computer and use it in GitHub Desktop.
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
// use >> 10.dp(context) | |
fun Int.dp(context: Context): Float { | |
return this * context.resources.displayMetrics.density | |
} | |
/* View Extensions */ | |
// use >> bindView(R.id.example) | |
fun <V : View> Activity.bindView(@IdRes id: Int) = unsafeLazy { findViewById<V>(id) } | |
inline fun <T> unsafeLazy(noinline initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer) | |
fun View.visible(){ | |
visibility = View.VISIBLE | |
} | |
fun View.gone(){ | |
visibility = View.GONE | |
} | |
fun View.invisible(){ | |
visibility = View.INVISIBLE | |
} | |
fun View.slideExit() { | |
if (translationY == 0f) animate().translationY(-height.toFloat()) | |
} | |
fun View.slideEnter() { | |
if (translationY < 0f) animate().translationY(0f) | |
} | |
fun ViewGroup.inflate(layoutResource: Int): View{ | |
return LayoutInflater.from(context).inflate(layoutResource, this, false) | |
} | |
// or | |
inline fun <reified V : View> ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): V { | |
return LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot) as V | |
} | |
// use > val view = parent.inflate(R.layout.item_list) | |
// return MyViewHolder(view) | |
/* View Extensions */ | |
/** Date to String */ | |
fun Long.toDateString(dateFormat: Int = DateFormat.MEDIUM): String { | |
val df = DateFormat.getDateInstance(dateFormat, Locale.getDefault()) | |
return df.format(this) | |
} | |
/** Making notification bar transparent */ | |
fun Activity.statusBarColor(color: Int){ | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
window.apply { | |
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | |
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) | |
statusBarColor = color | |
} | |
} | |
} | |
// use > statusBarColor(R.color.RED) | |
/** Check if the permission passed as argument is granted. */ | |
fun Activity.isPermissionGranted( | |
permission: String, | |
permissionNotGranted: () -> Unit, | |
permissionGranted: () -> Unit) { | |
if(ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){ | |
permissionNotGranted() | |
}else{ | |
permissionGranted() | |
} | |
} | |
inline fun <reified T : Activity> Activity.startActivity() { | |
startActivity(Intent(this, T::class.java)) | |
} | |
// use > startActivity<SecondActivity>() | |
/* +++ ARCH COMPONENTS +++ */ | |
/** ViewModel */ | |
// val viewMode: ExampleViewModel by bindViewModel() | |
inline fun <reified T : ViewModel> FragmentActivity.bindViewModel() = unsafeLazy { | |
ViewModelProviders.of(this).get(T::class.java) | |
} | |
inline fun <reified T : ViewModel> Activity.getViewModel(): T { | |
return ViewModelProviders.of(this)[T::class.java] | |
} | |
// use > val eventsViewModel = getViewModel<EventsViewModel>() | |
inline fun <reified T : ViewModel> Activity.withViewModel(body: T.() -> Unit): T { | |
val vm = getViewModel<T>() | |
vm.body() | |
return vm | |
} | |
/* use > withViewModel<EventsViewModel> { | |
eventList.observe(this@EventsActivity, Observer(::updateUIMethod)) | |
} */ | |
/** LiveData */ | |
fun <T : Any, L : LiveData<T>> LifecycleOwner.observe(liveData: L, body: (T?) -> Unit) { | |
liveData.observe(this, Observer(body)) | |
} | |
/* use > activity.observe(eventList){ updateUIMethod() } | |
or > observe(eventList, ::updateUIMethod) | |
together > | |
withViewModel<EventsViewModel> { | |
observe(eventList, ::updateUIMethod) | |
} | |
*/ | |
fun Fragment.openGallery() { | |
val intent = Intent(Intent.ACTION_GET_CONTENT) | |
intent.type = "image/*" | |
val extraMime = arrayOf("image/*", "video/*") | |
intent.putExtra(Intent.EXTRA_MIME_TYPES, extraMime) | |
intent.addCategory(Intent.CATEGORY_OPENABLE) | |
intent.resolveActivity(activity!!.packageManager)?.also { | |
startActivityForResult( | |
Intent.createChooser(intent, getString(R.string.select_photo)), | |
Constants.REQUEST_CODE_GALLERY | |
) | |
} | |
} | |
fun Fragment.openCamera(action: String, fileName: String, folder: String, requestCode: Int): File? { | |
val intent = Intent(action) | |
if (intent.resolveActivity(activity!!.packageManager) != null) { | |
val file = activity?.createMediaFile(fileName, folder) | |
if (file != null) { | |
val uri = FileProvider.getUriForFile(activity!!, activity!!.packageName, file) | |
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri) | |
if (action == MediaStore.ACTION_VIDEO_CAPTURE) { | |
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1) | |
} | |
startActivityForResult(intent, requestCode) | |
return file | |
} | |
} | |
return null | |
} | |
fun Fragment.takePicture(): File? { | |
return openCamera( | |
MediaStore.ACTION_IMAGE_CAPTURE, | |
"IMG_${Calendar.getInstance().timeInMillis}.jpg", | |
Environment.DIRECTORY_PICTURES, | |
REQUEST_CODE_TAKE_PHOTO | |
) | |
} | |
fun Fragment.takeVideo(): File? { | |
return openCamera( | |
MediaStore.ACTION_VIDEO_CAPTURE, | |
"VID_${Calendar.getInstance().timeInMillis}.mp4", | |
Environment.DIRECTORY_MOVIES, | |
REQUEST_CODE_RECORD_VIDEO | |
) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment