Kotlinize some widgets

This commit is contained in:
len 2016-03-08 01:22:56 +01:00
parent 0ddbfd1036
commit 05adde552d
12 changed files with 142 additions and 210 deletions

View file

@ -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)
}

View file

@ -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();
}
}

View file

@ -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
}
}
}

View file

@ -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();
}
}
}

View file

@ -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()
}
}
}
}

View file

@ -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);
}
}

View file

@ -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)
}
}

View file

@ -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;
}
}

View file

@ -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
}
}

View file

@ -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())

View file

@ -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() {

View file

@ -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() {