/* * Copyright (C) 2017 Moez Bhatti * * This file is part of QKSMS. * * QKSMS is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * QKSMS is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with QKSMS. If not, see . */ package common.util.extensions import android.app.Activity import android.content.Context import android.support.annotation.StringRes import android.support.v4.content.ContextCompat import android.util.TypedValue import android.view.inputmethod.InputMethodManager import android.widget.Toast fun Context.getColorCompat(colorRes: Int): Int { return ContextCompat.getColor(this, colorRes) } /** * Tries to resolve a resource id from the current theme, based on the [attributeId] */ fun Context.resolveThemeAttribute(attributeId: Int, default: Int = 0): Int { val outValue = TypedValue() val wasResolved = theme.resolveAttribute(attributeId, outValue, true) return if (wasResolved) outValue.resourceId else default } fun Context.resolveThemeColor(attributeId: Int, default: Int = 0): Int { val outValue = TypedValue() val wasResolved = theme.resolveAttribute(attributeId, outValue, true) return if (wasResolved) getColorCompat(outValue.resourceId) else default } fun Context.makeToast(@StringRes res: Int, duration: Int = Toast.LENGTH_SHORT) { Toast.makeText(this, res, duration).show() } fun Context.makeToast(text: String, duration: Int = Toast.LENGTH_SHORT) { Toast.makeText(this, text, duration).show() } fun Activity.dismissKeyboard() { window.currentFocus?.let { focus -> val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(focus.windowToken, 0) focus.clearFocus() } }