Merge and remove util classes

This commit is contained in:
len 2017-01-08 18:50:51 +01:00
parent dd56d7c0bb
commit 07cae4d684
15 changed files with 47 additions and 75 deletions

View File

@ -19,8 +19,8 @@ import kotlinx.android.synthetic.main.item_catalogue_list.view.*
class CatalogueListHolder(private val view: View, adapter: CatalogueAdapter, listener: OnListItemClickListener) :
CatalogueHolder(view, adapter, listener) {
private val favoriteColor = view.context.theme.getResourceColor(android.R.attr.textColorHint)
private val unfavoriteColor = view.context.theme.getResourceColor(android.R.attr.textColorPrimary)
private val favoriteColor = view.context.getResourceColor(android.R.attr.textColorHint)
private val unfavoriteColor = view.context.getResourceColor(android.R.attr.textColorPrimary)
/**
* Method called from [CatalogueAdapter.onBindViewHolder]. It updates the data for this

View File

@ -109,7 +109,7 @@ class CatalogueNavigationView @JvmOverloads constructor(context: Context, attrs:
else
android.R.attr.textColorSecondary
setTint(view.context.theme.getResourceColor(color))
setTint(view.context.getResourceColor(color))
}
view.setCompoundDrawablesWithIntrinsicBounds(getIcon(), null, null, null)

View File

@ -19,9 +19,9 @@ class ChaptersHolder(
listener: FlexibleViewHolder.OnListItemClickListener)
: FlexibleViewHolder(view, adapter, listener) {
private val readColor = view.context.theme.getResourceColor(android.R.attr.textColorHint)
private val unreadColor = view.context.theme.getResourceColor(android.R.attr.textColorPrimary)
private val bookmarkedColor = view.context.theme.getResourceColor(R.attr.colorAccent)
private val readColor = view.context.getResourceColor(android.R.attr.textColorHint)
private val unreadColor = view.context.getResourceColor(android.R.attr.textColorPrimary)
private val bookmarkedColor = view.context.getResourceColor(R.attr.colorAccent)
private val decimalFormat = DecimalFormat("#.###", DecimalFormatSymbols().apply { decimalSeparator = '.' })
private val df = DateFormat.getDateInstance(DateFormat.SHORT)

View File

@ -168,7 +168,7 @@ class MangaInfoFragment : BaseRxFragment<MangaInfoPresenter>() {
try {
val url = Uri.parse(source.baseUrl + presenter.manga.url)
val intent = CustomTabsIntent.Builder()
.setToolbarColor(context.theme.getResourceColor(R.attr.colorPrimary))
.setToolbarColor(context.getResourceColor(R.attr.colorPrimary))
.build()
intent.launchUrl(activity, url)
} catch (e: Exception) {

View File

@ -26,12 +26,12 @@ class RecentChaptersHolder(
/**
* Color of read chapter
*/
private var readColor = view.context.theme.getResourceColor(android.R.attr.textColorHint)
private var readColor = view.context.getResourceColor(android.R.attr.textColorHint)
/**
* Color of unread chapter
*/
private var unreadColor = view.context.theme.getResourceColor(android.R.attr.textColorPrimary)
private var unreadColor = view.context.getResourceColor(android.R.attr.textColorPrimary)
/**
* Object containing chapter information

View File

@ -41,7 +41,7 @@ class SettingsTrackingFragment : SettingsFragment() {
registerService(trackManager.aniList) {
val intent = CustomTabsIntent.Builder()
.setToolbarColor(activity.theme.getResourceColor(R.attr.colorPrimary))
.setToolbarColor(activity.getResourceColor(R.attr.colorPrimary))
.build()
intent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
intent.launchUrl(activity, AnilistApi.authUrl())

View File

@ -4,6 +4,7 @@ import android.app.Notification
import android.app.NotificationManager
import android.content.Context
import android.content.pm.PackageManager
import android.content.res.Resources
import android.net.ConnectivityManager
import android.os.PowerManager
import android.support.annotation.StringRes
@ -13,6 +14,7 @@ import android.widget.Toast
/**
* Display a toast in this context.
*
* @param resource the text resource.
* @param duration the duration of the toast. Defaults to short.
*/
@ -22,6 +24,7 @@ fun Context.toast(@StringRes resource: Int, duration: Int = Toast.LENGTH_SHORT)
/**
* Display a toast in this context.
*
* @param text the text to display.
* @param duration the duration of the toast. Defaults to short.
*/
@ -31,6 +34,7 @@ fun Context.toast(text: String?, duration: Int = Toast.LENGTH_SHORT) {
/**
* Helper method to create a notification.
*
* @param func the function that will execute inside the builder.
* @return a notification to be displayed or updated.
*/
@ -42,12 +46,37 @@ inline fun Context.notification(func: NotificationCompat.Builder.() -> Unit): No
/**
* Checks if the give permission is granted.
*
* @param permission the permission to check.
* @return true if it has permissions.
*/
fun Context.hasPermission(permission: String)
= ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
/**
* Returns the color for the given attribute.
*
* @param resource the attribute.
*/
fun Context.getResourceColor(@StringRes resource: Int): Int {
val typedArray = obtainStyledAttributes(intArrayOf(resource))
val attrValue = typedArray.getColor(0, 0)
typedArray.recycle()
return attrValue
}
/**
* Converts to dp.
*/
val Int.pxToDp: Int
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
/**
* Converts to px.
*/
val Int.dpToPx: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
/**
* Property to get the notification manager from the context.
*/
@ -64,4 +93,5 @@ val Context.connectivityManager: ConnectivityManager
* Property to get the power manager from the context.
*/
val Context.powerManager: PowerManager
get() = getSystemService(Context.POWER_SERVICE) as PowerManager
get() = getSystemService(Context.POWER_SERVICE) as PowerManager

View File

@ -1,24 +0,0 @@
package eu.kanade.tachiyomi.util
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.os.BatteryManager
object DeviceUtil {
fun isPowerConnected(context: Context): Boolean {
val intent = context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
intent?.let {
val plugged = it.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS
}
return false
}
fun isNetworkConnected(context: Context): Boolean {
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork = cm.activeNetworkInfo
return activeNetwork != null && activeNetwork.isConnectedOrConnecting
}
}

View File

@ -1,9 +0,0 @@
package eu.kanade.tachiyomi.util
import android.content.res.Resources
val Int.pxToDp: Int
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
val Int.dpToPx: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()

View File

@ -12,6 +12,7 @@ import android.widget.ImageView
fun ImageView.setVectorCompat(@DrawableRes drawable: Int, tint: Int? = null) {
val vector = VectorDrawableCompat.create(resources, drawable, context.theme)
if (tint != null) {
vector?.mutate()
vector?.setTint(tint)
}
setImageDrawable(vector)

View File

@ -1,27 +0,0 @@
package eu.kanade.tachiyomi.util
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.support.annotation.AttrRes
import android.support.annotation.StringRes
fun Resources.Theme.getResourceColor(@StringRes resource: Int): Int {
val typedArray = obtainStyledAttributes(intArrayOf(resource))
val attrValue = typedArray.getColor(0, 0)
typedArray.recycle()
return attrValue
}
fun Resources.Theme.getResourceDrawable(@StringRes resource: Int): Drawable {
val typedArray = obtainStyledAttributes(intArrayOf(resource))
val attrValue = typedArray.getDrawable(0)
typedArray.recycle()
return attrValue
}
fun Resources.Theme.getResourceId(@AttrRes resource: Int, fallback: Int): Int {
val typedArray = obtainStyledAttributes(intArrayOf(resource))
val attrValue = typedArray.getResourceId(0, fallback)
typedArray.recycle()
return attrValue
}

View File

@ -29,7 +29,7 @@ class EmptyView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
* @param textResource text of information view
*/
fun show(drawable: Int, textResource: Int) {
image_view.setVectorCompat(drawable, context.theme.getResourceColor(android.R.attr.textColorHint))
image_view.setVectorCompat(drawable, context.getResourceColor(android.R.attr.textColorHint))
text_label.text = context.getString(textResource)
this.visibility = View.VISIBLE
}

View File

@ -75,7 +75,7 @@ open class ExtendedNavigationView @JvmOverloads constructor(
*/
fun tintVector(context: Context, resId: Int): Drawable {
return VectorDrawableCompat.create(context.resources, resId, context.theme)!!.apply {
setTint(context.theme.getResourceColor(R.attr.colorAccent))
setTint(context.getResourceColor(R.attr.colorAccent))
}
}
}

View File

@ -8,6 +8,7 @@ import android.view.View
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.source.online.LoginSource
import eu.kanade.tachiyomi.data.source.online.OnlineSource
import eu.kanade.tachiyomi.util.getResourceColor
import eu.kanade.tachiyomi.util.setVectorCompat
import kotlinx.android.synthetic.main.pref_item_source.view.*
import net.xpece.android.support.preference.CheckBoxPreference
@ -31,7 +32,7 @@ class LoginCheckBoxPreference @JvmOverloads constructor(
val tint = if (source.isLogged())
Color.argb(255, 76, 175, 80)
else
Color.argb(97, 0, 0, 0)
context.getResourceColor(android.R.attr.textColorSecondary)
holder.itemView.login.setVectorCompat(R.drawable.ic_account_circle_black_24dp, tint)

View File

@ -25,7 +25,7 @@ class SwitchPreferenceCategory @JvmOverloads constructor(
CompoundButton.OnCheckedChangeListener {
init {
setTitleTextColor(context.theme.getResourceColor(R.attr.colorAccent))
setTitleTextColor(context.getResourceColor(R.attr.colorAccent))
}
private var mChecked = false