From 05adde552d9713e613f549e6e4614350b85f99dc Mon Sep 17 00:00:00 2001 From: len Date: Tue, 8 Mar 2016 01:22:56 +0100 Subject: [PATCH] Kotlinize some widgets --- .../ui/reader/viewer/webtoon/WebtoonReader.kt | 2 +- .../tachiyomi/widget/AutofitRecyclerView.java | 71 ------------------- .../tachiyomi/widget/AutofitRecyclerView.kt | 45 ++++++++++++ .../tachiyomi/widget/MinMaxNumberPicker.java | 36 ---------- .../tachiyomi/widget/MinMaxNumberPicker.kt | 23 ++++++ .../tachiyomi/widget/PTSansTextView.java | 60 ---------------- .../kanade/tachiyomi/widget/PTSansTextView.kt | 41 +++++++++++ .../widget/PreCachingLayoutManager.java | 28 -------- .../widget/PreCachingLayoutManager.kt | 22 ++++++ .../widget/preference/IntListPreference.kt | 8 +-- .../widget/preference/MangaSyncLoginDialog.kt | 8 +-- .../widget/preference/SourceLoginDialog.kt | 8 +-- 12 files changed, 142 insertions(+), 210 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/AutofitRecyclerView.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/AutofitRecyclerView.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/MinMaxNumberPicker.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/MinMaxNumberPicker.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/PTSansTextView.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/PTSansTextView.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/PreCachingLayoutManager.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/PreCachingLayoutManager.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.kt index 5438d9c3ce..76672abce7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonReader.kt @@ -72,7 +72,7 @@ class WebtoonReader : BaseReader() { scrollDistance = screenHeight * 3 / 4 layoutManager = PreCachingLayoutManager(activity) - layoutManager.setExtraLayoutSpace(screenHeight / 2) + layoutManager.extraLayoutSpace = screenHeight / 2 if (savedState != null) { layoutManager.scrollToPositionWithOffset(savedState.getInt(SAVED_POSITION), 0) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/AutofitRecyclerView.java b/app/src/main/java/eu/kanade/tachiyomi/widget/AutofitRecyclerView.java deleted file mode 100644 index 8071405e02..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/AutofitRecyclerView.java +++ /dev/null @@ -1,71 +0,0 @@ -package eu.kanade.tachiyomi.widget; - -import android.content.Context; -import android.content.res.TypedArray; -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.util.AttributeSet; -import android.widget.ImageView; - -import eu.kanade.tachiyomi.R; - -public class AutofitRecyclerView extends RecyclerView { - - private GridLayoutManager manager; - private int columnWidth = -1; - private int spanCount = 0; - - public AutofitRecyclerView(Context context) { - super(context); - init(context, null); - } - - public AutofitRecyclerView(Context context, AttributeSet attrs) { - super(context, attrs); - init(context, attrs); - } - - public AutofitRecyclerView(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - init(context, attrs); - } - - private void init(Context context, AttributeSet attrs) { - if (attrs != null) { - int[] attrsArray = { - android.R.attr.columnWidth - }; - TypedArray array = context.obtainStyledAttributes(attrs, attrsArray); - columnWidth = array.getDimensionPixelSize(0, -1); - array.recycle(); - } - - manager = new GridLayoutManager(getContext(), 1); - setLayoutManager(manager); - } - - @Override - protected void onMeasure(int widthSpec, int heightSpec) { - super.onMeasure(widthSpec, heightSpec); - if (spanCount == 0 && columnWidth > 0) { - int spanCount = Math.max(1, getMeasuredWidth() / columnWidth); - manager.setSpanCount(spanCount); - } - } - - public void setSpanCount(int spanCount) { - this.spanCount = spanCount; - if (spanCount > 0) { - manager.setSpanCount(spanCount); - } - } - - public int getSpanCount() { - return manager.getSpanCount(); - } - - public int getItemWidth() { - return getMeasuredWidth() / getSpanCount(); - } - -} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/AutofitRecyclerView.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/AutofitRecyclerView.kt new file mode 100644 index 0000000000..11a8e11405 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/AutofitRecyclerView.kt @@ -0,0 +1,45 @@ +package eu.kanade.tachiyomi.widget + +import android.content.Context +import android.support.v7.widget.GridLayoutManager +import android.support.v7.widget.RecyclerView +import android.util.AttributeSet + +class AutofitRecyclerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : + RecyclerView(context, attrs) { + + private val manager = GridLayoutManager(context, 1) + + private var columnWidth = -1 + + var spanCount = 0 + set(value) { + field = value + if (value > 0) { + manager.spanCount = value + } + } + + val itemWidth: Int + get() = measuredWidth / manager.spanCount + + init { + if (attrs != null) { + val attrsArray = intArrayOf(android.R.attr.columnWidth) + val array = context.obtainStyledAttributes(attrs, attrsArray) + columnWidth = array.getDimensionPixelSize(0, -1) + array.recycle() + } + + layoutManager = manager + } + + override fun onMeasure(widthSpec: Int, heightSpec: Int) { + super.onMeasure(widthSpec, heightSpec) + if (spanCount == 0 && columnWidth > 0) { + val spanCount = Math.max(1, measuredWidth / columnWidth) + manager.spanCount = spanCount + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/MinMaxNumberPicker.java b/app/src/main/java/eu/kanade/tachiyomi/widget/MinMaxNumberPicker.java deleted file mode 100644 index fcaebf464b..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/MinMaxNumberPicker.java +++ /dev/null @@ -1,36 +0,0 @@ -package eu.kanade.tachiyomi.widget; - -import android.content.Context; -import android.content.res.TypedArray; -import android.util.AttributeSet; -import android.widget.NumberPicker; - -import eu.kanade.tachiyomi.R; - -public class MinMaxNumberPicker extends NumberPicker{ - - public MinMaxNumberPicker(Context context) { - super(context); - } - - public MinMaxNumberPicker(Context context, AttributeSet attrs) { - super(context, attrs); - processAttributeSet(context, attrs); - } - - public MinMaxNumberPicker(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - processAttributeSet(context, attrs); - } - - private void processAttributeSet(Context context, AttributeSet attrs) { - TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MinMaxNumberPicker, 0, 0); - try { - setMinValue(ta.getInt(R.styleable.MinMaxNumberPicker_min, 0)); - setMaxValue(ta.getInt(R.styleable.MinMaxNumberPicker_max, 0)); - } finally { - ta.recycle(); - } - } -} - diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/MinMaxNumberPicker.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/MinMaxNumberPicker.kt new file mode 100644 index 0000000000..07adb8d0c5 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/MinMaxNumberPicker.kt @@ -0,0 +1,23 @@ +package eu.kanade.tachiyomi.widget + +import android.content.Context +import android.util.AttributeSet +import android.widget.NumberPicker +import eu.kanade.tachiyomi.R + +class MinMaxNumberPicker @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : + NumberPicker(context, attrs) { + + init { + if (attrs != null) { + val ta = context.obtainStyledAttributes(attrs, R.styleable.MinMaxNumberPicker, 0, 0) + try { + minValue = ta.getInt(R.styleable.MinMaxNumberPicker_min, 0) + maxValue = ta.getInt(R.styleable.MinMaxNumberPicker_max, 0) + } finally { + ta.recycle() + } + } + } +} + diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/PTSansTextView.java b/app/src/main/java/eu/kanade/tachiyomi/widget/PTSansTextView.java deleted file mode 100644 index 497ba634be..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/PTSansTextView.java +++ /dev/null @@ -1,60 +0,0 @@ -package eu.kanade.tachiyomi.widget; - -import android.content.Context; -import android.content.res.TypedArray; -import android.graphics.Canvas; -import android.graphics.Typeface; -import android.util.AttributeSet; -import android.widget.TextView; - -import eu.kanade.tachiyomi.R; - - -public class PTSansTextView extends TextView { - private final static int PTSANS_NARROW = 0; - private final static int PTSANS_NARROW_BOLD = 1; - - - public PTSansTextView(Context c) { - super(c); - } - - public PTSansTextView(Context c, AttributeSet attrs) { - super(c, attrs); - parseAttributes(c, attrs); - } - - public PTSansTextView(Context c, AttributeSet attrs, int defStyle) { - super(c, attrs, defStyle); - parseAttributes(c, attrs); - } - - private void parseAttributes(Context c, AttributeSet attrs) { - TypedArray values = c.obtainStyledAttributes(attrs, R.styleable.PTSansTextView); - - //The value 0 is a default, but shouldn't ever be used since the attr is an enum - int typeface = values.getInt(R.styleable.PTSansTextView_typeface, 0); - - switch(typeface) { - case PTSANS_NARROW: - //You can instantiate your typeface anywhere, I would suggest as a - //singleton somewhere to avoid unnecessary copies - setTypeface(Typeface.createFromAsset(c.getAssets(), "fonts/PTSans-Narrow.ttf")); - break; - case PTSANS_NARROW_BOLD: - setTypeface(Typeface.createFromAsset(c.getAssets(), "fonts/PTSans-NarrowBold.ttf")); - break; - default: - throw new IllegalArgumentException("Font not found " + typeface); - } - - values.recycle(); - } - - @Override - public void draw(Canvas canvas) { - // Draw two times for a more visible shadow around the text - super.draw(canvas); - super.draw(canvas); - } -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/PTSansTextView.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/PTSansTextView.kt new file mode 100644 index 0000000000..007cfefbec --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/PTSansTextView.kt @@ -0,0 +1,41 @@ +package eu.kanade.tachiyomi.widget + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Typeface +import android.util.AttributeSet +import android.widget.TextView +import eu.kanade.tachiyomi.R + + +class PTSansTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : + TextView(context, attrs) { + + companion object { + const val PTSANS_NARROW = 0 + const val PTSANS_NARROW_BOLD = 1 + } + + init { + if (attrs != null) { + val values = context.obtainStyledAttributes(attrs, R.styleable.PTSansTextView) + + val typeface = values.getInt(R.styleable.PTSansTextView_typeface, 0) + + when (typeface) { + PTSANS_NARROW -> setTypeface(Typeface.createFromAsset(context.assets, "fonts/PTSans-Narrow.ttf")) + PTSANS_NARROW_BOLD -> setTypeface(Typeface.createFromAsset(context.assets, "fonts/PTSans-NarrowBold.ttf")) + else -> throw IllegalArgumentException("Font not found " + typeface) + } + + values.recycle() + } + } + + override fun draw(canvas: Canvas) { + // Draw two times for a more visible shadow around the text + super.draw(canvas) + super.draw(canvas) + } + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/PreCachingLayoutManager.java b/app/src/main/java/eu/kanade/tachiyomi/widget/PreCachingLayoutManager.java deleted file mode 100644 index 909f3ecf14..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/PreCachingLayoutManager.java +++ /dev/null @@ -1,28 +0,0 @@ -package eu.kanade.tachiyomi.widget; - -import android.content.Context; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; - -public class PreCachingLayoutManager extends LinearLayoutManager { - - private static final int DEFAULT_EXTRA_LAYOUT_SPACE = 600; - private int extraLayoutSpace = -1; - - public PreCachingLayoutManager(Context context) { - super(context); - } - - public void setExtraLayoutSpace(int extraLayoutSpace) { - this.extraLayoutSpace = extraLayoutSpace; - } - - @Override - protected int getExtraLayoutSpace(RecyclerView.State state) { - if (extraLayoutSpace > 0) { - return extraLayoutSpace; - } - return DEFAULT_EXTRA_LAYOUT_SPACE; - } - -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/PreCachingLayoutManager.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/PreCachingLayoutManager.kt new file mode 100644 index 0000000000..f742a0e578 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/PreCachingLayoutManager.kt @@ -0,0 +1,22 @@ +package eu.kanade.tachiyomi.widget + +import android.content.Context +import android.support.v7.widget.LinearLayoutManager +import android.support.v7.widget.RecyclerView + +class PreCachingLayoutManager(context: Context) : LinearLayoutManager(context) { + + companion object { + const val DEFAULT_EXTRA_LAYOUT_SPACE = 600 + } + + var extraLayoutSpace = 0 + + override fun getExtraLayoutSpace(state: RecyclerView.State): Int { + if (extraLayoutSpace > 0) { + return extraLayoutSpace + } + return DEFAULT_EXTRA_LAYOUT_SPACE + } + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListPreference.kt index 5d65ab8f90..2eb95e2ece 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListPreference.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/IntListPreference.kt @@ -4,12 +4,8 @@ import android.content.Context import android.support.v7.preference.ListPreference import android.util.AttributeSet -class IntListPreference : ListPreference { - constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { - } - - constructor(context: Context) : super(context) { - } +class IntListPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : + ListPreference(context, attrs) { override fun persistString(value: String?): Boolean { return value != null && persistInt(value.toInt()) diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MangaSyncLoginDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MangaSyncLoginDialog.kt index 630c416785..4780289940 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MangaSyncLoginDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/MangaSyncLoginDialog.kt @@ -34,10 +34,10 @@ class MangaSyncLoginDialog : LoginDialogPreference() { sync = (activity as SettingsActivity).syncManager.getService(syncId) } - override fun setCredentialsOnView(view: View) { - view.accounts_login.text = getString(R.string.accounts_login_title, sync.name) - view.username.setText(preferences.getMangaSyncUsername(sync)) - view.password.setText(preferences.getMangaSyncPassword(sync)) + override fun setCredentialsOnView(view: View) = with(view) { + accounts_login.text = getString(R.string.accounts_login_title, sync.name) + username.setText(preferences.getMangaSyncUsername(sync)) + password.setText(preferences.getMangaSyncPassword(sync)) } override fun checkLogin() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SourceLoginDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SourceLoginDialog.kt index 47bcc8e20e..f95ddc02c7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SourceLoginDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/SourceLoginDialog.kt @@ -34,10 +34,10 @@ class SourceLoginDialog : LoginDialogPreference() { source = (activity as SettingsActivity).sourceManager.get(sourceId)!! } - override fun setCredentialsOnView(view: View) { - view.accounts_login.text = getString(R.string.accounts_login_title, source.name) - view.username.setText(preferences.getSourceUsername(source)) - view.password.setText(preferences.getSourcePassword(source)) + override fun setCredentialsOnView(view: View) = with(view) { + accounts_login.text = getString(R.string.accounts_login_title, source.name) + username.setText(preferences.getSourceUsername(source)) + password.setText(preferences.getSourcePassword(source)) } override fun checkLogin() {