Skip to content

Instantly share code, notes, and snippets.

@terrakok
Last active February 3, 2020 23:01
Show Gist options
  • Save terrakok/6ea0f9bf90904f6573c3384b8b6e29a1 to your computer and use it in GitHub Desktop.
Save terrakok/6ea0f9bf90904f6573c3384b8b6e29a1 to your computer and use it in GitHub Desktop.
fun Resources.color(colorRes: Int) =
if (Build.VERSION.SDK_INT >= 23) {
this.getColor(colorRes, null)
} else {
this.getColor(colorRes)
}
fun ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): View {
return LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot)
}
fun View.visible(visible: Boolean) {
this.visibility = if (visible) View.VISIBLE else View.GONE
}
fun ImageView.tint(@ColorRes colorRes: Int) {
this.setColorFilter(ContextCompat.getColor(context, colorRes))
}
fun Disposable.addTo(compositeDisposable: CompositeDisposable) {
compositeDisposable.add(this)
}
fun Throwable.userMessage(resourceManager: ResourceManager) = when (this) {
is HttpException -> when (this.code()) {
304 -> resourceManager.getString(R.string.not_modified_error)
400 -> resourceManager.getString(R.string.bad_request_error)
401 -> resourceManager.getString(R.string.unauthorized_error)
403 -> resourceManager.getString(R.string.forbidden_error)
404 -> resourceManager.getString(R.string.not_found_error)
405 -> resourceManager.getString(R.string.method_not_allowed_error)
409 -> resourceManager.getString(R.string.conflict_error)
422 -> resourceManager.getString(R.string.unprocessable_error)
500 -> resourceManager.getString(R.string.server_error_error)
else -> resourceManager.getString(R.string.unknown_error)
}
is IOException -> resourceManager.getString(R.string.network_error)
else -> resourceManager.getString(R.string.unknown_error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment