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) : class CatalogueListHolder(private val view: View, adapter: CatalogueAdapter, listener: OnListItemClickListener) :
CatalogueHolder(view, adapter, listener) { CatalogueHolder(view, adapter, listener) {
private val favoriteColor = view.context.theme.getResourceColor(android.R.attr.textColorHint) private val favoriteColor = view.context.getResourceColor(android.R.attr.textColorHint)
private val unfavoriteColor = view.context.theme.getResourceColor(android.R.attr.textColorPrimary) private val unfavoriteColor = view.context.getResourceColor(android.R.attr.textColorPrimary)
/** /**
* Method called from [CatalogueAdapter.onBindViewHolder]. It updates the data for this * 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 else
android.R.attr.textColorSecondary android.R.attr.textColorSecondary
setTint(view.context.theme.getResourceColor(color)) setTint(view.context.getResourceColor(color))
} }
view.setCompoundDrawablesWithIntrinsicBounds(getIcon(), null, null, null) view.setCompoundDrawablesWithIntrinsicBounds(getIcon(), null, null, null)

View file

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

View file

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

View file

@ -26,12 +26,12 @@ class RecentChaptersHolder(
/** /**
* Color of read chapter * 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 * 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 * Object containing chapter information

View file

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

View file

@ -4,6 +4,7 @@ import android.app.Notification
import android.app.NotificationManager import android.app.NotificationManager
import android.content.Context import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.res.Resources
import android.net.ConnectivityManager import android.net.ConnectivityManager
import android.os.PowerManager import android.os.PowerManager
import android.support.annotation.StringRes import android.support.annotation.StringRes
@ -13,6 +14,7 @@ import android.widget.Toast
/** /**
* Display a toast in this context. * Display a toast in this context.
*
* @param resource the text resource. * @param resource the text resource.
* @param duration the duration of the toast. Defaults to short. * @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. * Display a toast in this context.
*
* @param text the text to display. * @param text the text to display.
* @param duration the duration of the toast. Defaults to short. * @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. * Helper method to create a notification.
*
* @param func the function that will execute inside the builder. * @param func the function that will execute inside the builder.
* @return a notification to be displayed or updated. * @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. * Checks if the give permission is granted.
*
* @param permission the permission to check. * @param permission the permission to check.
* @return true if it has permissions. * @return true if it has permissions.
*/ */
fun Context.hasPermission(permission: String) fun Context.hasPermission(permission: String)
= ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED = 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. * 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. * Property to get the power manager from the context.
*/ */
val Context.powerManager: PowerManager 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) { fun ImageView.setVectorCompat(@DrawableRes drawable: Int, tint: Int? = null) {
val vector = VectorDrawableCompat.create(resources, drawable, context.theme) val vector = VectorDrawableCompat.create(resources, drawable, context.theme)
if (tint != null) { if (tint != null) {
vector?.mutate()
vector?.setTint(tint) vector?.setTint(tint)
} }
setImageDrawable(vector) 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 * @param textResource text of information view
*/ */
fun show(drawable: Int, textResource: Int) { 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) text_label.text = context.getString(textResource)
this.visibility = View.VISIBLE this.visibility = View.VISIBLE
} }

View file

@ -75,7 +75,7 @@ open class ExtendedNavigationView @JvmOverloads constructor(
*/ */
fun tintVector(context: Context, resId: Int): Drawable { fun tintVector(context: Context, resId: Int): Drawable {
return VectorDrawableCompat.create(context.resources, resId, context.theme)!!.apply { 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.R
import eu.kanade.tachiyomi.data.source.online.LoginSource import eu.kanade.tachiyomi.data.source.online.LoginSource
import eu.kanade.tachiyomi.data.source.online.OnlineSource import eu.kanade.tachiyomi.data.source.online.OnlineSource
import eu.kanade.tachiyomi.util.getResourceColor
import eu.kanade.tachiyomi.util.setVectorCompat import eu.kanade.tachiyomi.util.setVectorCompat
import kotlinx.android.synthetic.main.pref_item_source.view.* import kotlinx.android.synthetic.main.pref_item_source.view.*
import net.xpece.android.support.preference.CheckBoxPreference import net.xpece.android.support.preference.CheckBoxPreference
@ -31,7 +32,7 @@ class LoginCheckBoxPreference @JvmOverloads constructor(
val tint = if (source.isLogged()) val tint = if (source.isLogged())
Color.argb(255, 76, 175, 80) Color.argb(255, 76, 175, 80)
else 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) holder.itemView.login.setVectorCompat(R.drawable.ic_account_circle_black_24dp, tint)

View file

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