From 07cae4d6840c123714a793bcfc5e1dd68cb607ad Mon Sep 17 00:00:00 2001 From: len Date: Sun, 8 Jan 2017 18:50:51 +0100 Subject: [PATCH] Merge and remove util classes --- .../ui/catalogue/CatalogueListHolder.kt | 4 +-- .../ui/catalogue/CatalogueNavigationView.kt | 2 +- .../ui/manga/chapter/ChaptersHolder.kt | 6 ++-- .../ui/manga/info/MangaInfoFragment.kt | 2 +- .../ui/recent_updates/RecentChaptersHolder.kt | 4 +-- .../ui/setting/SettingsTrackingFragment.kt | 2 +- .../tachiyomi/util/ContextExtensions.kt | 32 ++++++++++++++++++- .../eu/kanade/tachiyomi/util/DeviceUtil.kt | 24 -------------- .../tachiyomi/util/DimensionExtensions.kt | 9 ------ .../tachiyomi/util/ImageViewExtensions.kt | 1 + .../kanade/tachiyomi/util/ThemeExtensions.kt | 27 ---------------- .../eu/kanade/tachiyomi/widget/EmptyView.kt | 2 +- .../widget/ExtendedNavigationView.kt | 2 +- .../preference/LoginCheckBoxPreference.kt | 3 +- .../preference/SwitchPreferenceCategory.kt | 2 +- 15 files changed, 47 insertions(+), 75 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/DeviceUtil.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/DimensionExtensions.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/ThemeExtensions.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueListHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueListHolder.kt index 10d9e6bed..65f4f6712 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueListHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueListHolder.kt @@ -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 diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueNavigationView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueNavigationView.kt index 8c63d7bb4..3c9d20abd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueNavigationView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueNavigationView.kt @@ -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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersHolder.kt index e34a6c407..a5f7e09c4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersHolder.kt @@ -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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoFragment.kt index fe206a072..1f17394ed 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoFragment.kt @@ -168,7 +168,7 @@ class MangaInfoFragment : BaseRxFragment() { 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) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersHolder.kt index 8465adf0a..7f41f8bc2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersHolder.kt @@ -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 diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsTrackingFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsTrackingFragment.kt index a5617fbcd..922d83958 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsTrackingFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsTrackingFragment.kt @@ -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()) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/ContextExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/ContextExtensions.kt index f07a0533c..10b3f27b2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/ContextExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/ContextExtensions.kt @@ -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 \ No newline at end of file + get() = getSystemService(Context.POWER_SERVICE) as PowerManager + diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/DeviceUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/DeviceUtil.kt deleted file mode 100644 index e7dc9e559..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/util/DeviceUtil.kt +++ /dev/null @@ -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 - } -} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/DimensionExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/DimensionExtensions.kt deleted file mode 100644 index 83e191cd3..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/util/DimensionExtensions.kt +++ /dev/null @@ -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() \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/ImageViewExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/ImageViewExtensions.kt index 64bba887a..0419476da 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/ImageViewExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/ImageViewExtensions.kt @@ -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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/ThemeExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/ThemeExtensions.kt deleted file mode 100644 index da0f90282..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/util/ThemeExtensions.kt +++ /dev/null @@ -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 -} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/EmptyView.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/EmptyView.kt index 0a3e8eb23..7950ff0f0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/EmptyView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/EmptyView.kt @@ -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 } diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/ExtendedNavigationView.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/ExtendedNavigationView.kt index afb033ccf..baa26946c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/ExtendedNavigationView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/ExtendedNavigationView.kt @@ -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)) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginCheckBoxPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginCheckBoxPreference.kt index 1d4ef5862..fad34089a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginCheckBoxPreference.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginCheckBoxPreference.kt @@ -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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SwitchPreferenceCategory.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SwitchPreferenceCategory.kt index e4f3d38b6..f07ccbc72 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SwitchPreferenceCategory.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SwitchPreferenceCategory.kt @@ -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