Convert all legacy view classes to kotlin

This commit is contained in:
FooIbar authored and tarsin committed 2023-06-22 10:01:41 +08:00
1 parent 1c07c61307
commit 0cd83d3e4c
5 files changed
+147 -241

No files matched your search

@@ -13,58 +13,41 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.ui.legacy
package com.hippo.ehviewer.ui.legacy;
import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.FrameLayout;
class DrawerView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : FrameLayout(context, attrs) {
private var mMaxWidth = 0
public class DrawerView extends FrameLayout {
private static final int[] SIZE_ATTRS = new int[]{
android.R.attr.maxWidth
};
private int mMaxWidth;
public DrawerView(Context context) {
super(context);
init(context, null);
init {
val a = context.obtainStyledAttributes(attrs, SIZE_ATTRS)
mMaxWidth = a.getDimensionPixelOffset(0, 0)
a.recycle()
}
public DrawerView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
override fun onMeasure(widthSpec: Int, heightSpec: Int) {
var widthSpec = widthSpec
when (MeasureSpec.getMode(widthSpec)) {
MeasureSpec.EXACTLY -> {}
MeasureSpec.AT_MOST -> widthSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(widthSpec).coerceAtMost(mMaxWidth),
MeasureSpec.EXACTLY,
)
public DrawerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, SIZE_ATTRS);
mMaxWidth = a.getDimensionPixelOffset(0, 0);
a.recycle();
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
switch (MeasureSpec.getMode(widthSpec)) {
case MeasureSpec.EXACTLY:
// Nothing to do
break;
case MeasureSpec.AT_MOST:
widthSpec = MeasureSpec.makeMeasureSpec(
Math.min(MeasureSpec.getSize(widthSpec), mMaxWidth), MeasureSpec.EXACTLY);
break;
case MeasureSpec.UNSPECIFIED:
widthSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, MeasureSpec.EXACTLY);
break;
MeasureSpec.UNSPECIFIED ->
widthSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, MeasureSpec.EXACTLY)
}
// Let super sort out the height
super.onMeasure(widthSpec, heightSpec);
super.onMeasure(widthSpec, heightSpec)
}
companion object {
private val SIZE_ATTRS = intArrayOf(android.R.attr.maxWidth)
}
}
@@ -13,62 +13,43 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.ui.legacy
package com.hippo.ehviewer.ui.legacy;
import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.widget.RatingBar
import android.widget.RatingBar.OnRatingBarChangeListener
import androidx.appcompat.widget.AppCompatRatingBar
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.RatingBar;
class GalleryRatingBar @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : AppCompatRatingBar(context, attrs), OnRatingBarChangeListener {
private var mListener: OnUserRateListener? = null
import androidx.appcompat.widget.AppCompatRatingBar;
public class GalleryRatingBar extends AppCompatRatingBar
implements RatingBar.OnRatingBarChangeListener {
private OnUserRateListener mListener;
public GalleryRatingBar(Context context) {
super(context);
init();
init {
onRatingBarChangeListener = this
}
public GalleryRatingBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public GalleryRatingBar(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
setOnRatingBarChangeListener(this);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
if (mListener != null) {
mListener.onUserRate(getRating());
mListener!!.onUserRate(rating)
}
}
public void setOnUserRateListener(OnUserRateListener l) {
mListener = l;
fun setOnUserRateListener(l: OnUserRateListener?) {
mListener = l
}
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
override fun onRatingChanged(ratingBar: RatingBar, rating: Float, fromUser: Boolean) {
if (rating <= 0.0f) {
setRating(0.5f);
setRating(0.5f)
}
}
public interface OnUserRateListener {
void onUserRate(float rating);
interface OnUserRateListener {
fun onUserRate(rating: Float)
}
}
@@ -13,85 +13,69 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.ui.legacy
package com.hippo.ehviewer.ui.legacy;
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.widget.ListView
import com.hippo.ehviewer.R
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ListView;
@SuppressLint("CustomViewStyleable")
class IndicatingListView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : ListView(context, attrs) {
private val mPaint = Paint()
private val mTemp = Rect()
private var mIndicatorHeight = 0
import androidx.annotation.NonNull;
import com.hippo.ehviewer.R;
public class IndicatingListView extends ListView {
private final Paint mPaint = new Paint();
private final Rect mTemp = new Rect();
private int mIndicatorHeight;
public IndicatingListView(Context context) {
super(context);
init(context, null);
init {
val a = context.obtainStyledAttributes(attrs, R.styleable.Indicating)
mIndicatorHeight = a.getDimensionPixelOffset(R.styleable.Indicating_indicatorHeight, 1)
mPaint.color = a.getColor(
R.styleable.Indicating_indicatorColor,
Color.BLACK,
)
mPaint.style = Paint.Style.FILL
a.recycle()
}
public IndicatingListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
private fun fillTopIndicatorDrawRect() {
mTemp[0, 0, width] = mIndicatorHeight
}
public IndicatingListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
private fun fillBottomIndicatorDrawRect() {
mTemp[0, height - mIndicatorHeight, width] = height
}
private void init(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Indicating);
mIndicatorHeight = a.getDimensionPixelOffset(R.styleable.Indicating_indicatorHeight, 1);
mPaint.setColor(a.getColor(R.styleable.Indicating_indicatorColor, Color.BLACK));
mPaint.setStyle(Paint.Style.FILL);
a.recycle();
private fun needShowTopIndicator(): Boolean {
return canScrollVertically(-1)
}
private void fillTopIndicatorDrawRect() {
mTemp.set(0, 0, getWidth(), mIndicatorHeight);
private fun needShowBottomIndicator(): Boolean {
return canScrollVertically(1)
}
private void fillBottomIndicatorDrawRect() {
mTemp.set(0, getHeight() - mIndicatorHeight, getWidth(), getHeight());
}
private boolean needShowTopIndicator() {
return canScrollVertically(-1);
}
private boolean needShowBottomIndicator() {
return canScrollVertically(1);
}
@Override
public void draw(@NonNull Canvas canvas) {
super.draw(canvas);
final int restoreCount = canvas.save();
canvas.translate(getScrollX(), getScrollY());
override fun draw(canvas: Canvas) {
super.draw(canvas)
val restoreCount = canvas.save()
canvas.translate(scrollX.toFloat(), scrollY.toFloat())
// Draw top indicator
if (needShowTopIndicator()) {
fillTopIndicatorDrawRect();
canvas.drawRect(mTemp, mPaint);
fillTopIndicatorDrawRect()
canvas.drawRect(mTemp, mPaint)
}
// Draw bottom indicator
if (needShowBottomIndicator()) {
fillBottomIndicatorDrawRect();
canvas.drawRect(mTemp, mPaint);
fillBottomIndicatorDrawRect()
canvas.drawRect(mTemp, mPaint)
}
canvas.restoreToCount(restoreCount);
canvas.restoreToCount(restoreCount)
}
}
@@ -13,80 +13,55 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.ui.legacy
package com.hippo.ehviewer.ui.legacy;
import android.annotation.SuppressLint
import android.content.Context
import android.text.Spanned
import android.text.style.ClickableSpan
import android.util.AttributeSet
import android.view.MotionEvent
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Layout;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.MotionEvent;
class LinkifyTextView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : ObservedTextView(context, attrs) {
var currentSpan: ClickableSpan? = null
private set
import androidx.annotation.NonNull;
public class LinkifyTextView extends ObservedTextView {
private ClickableSpan mCurrentSpan;
public LinkifyTextView(Context context) {
super(context);
}
public LinkifyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LinkifyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public ClickableSpan getCurrentSpan() {
return mCurrentSpan;
}
public void clearCurrentSpan() {
mCurrentSpan = null;
fun clearCurrentSpan() {
currentSpan = null
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
override fun onTouchEvent(event: MotionEvent): Boolean {
// Let the parent or grandparent of TextView to handles click aciton.
// Otherwise click effect like ripple will not work, and if touch area
// do not contain a url, the TextView will still get MotionEvent.
// onTouchEven must be called with MotionEvent.ACTION_DOWN for each touch
// action on it, so we analyze touched url here.
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mCurrentSpan = null;
if (getText() instanceof Spanned) {
if (event.action == MotionEvent.ACTION_DOWN) {
currentSpan = null
if (text is Spanned) {
// Get this code from android.text.method.LinkMovementMethod.
// Work fine !
int x = (int) event.getX();
int y = (int) event.getY();
x -= getTotalPaddingLeft();
y -= getTotalPaddingTop();
x += getScrollX();
y += getScrollY();
Layout layout = getLayout();
var x = event.x.toInt()
var y = event.y.toInt()
x -= totalPaddingLeft
y -= totalPaddingTop
x += scrollX
y += scrollY
val layout = layout
if (null != layout) {
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ClickableSpan[] spans = ((Spanned) getText()).getSpans(off, off, ClickableSpan.class);
if (spans.length > 0) {
mCurrentSpan = spans[0];
val line = layout.getLineForVertical(y)
val off = layout.getOffsetForHorizontal(line, x.toFloat())
val spans = (text as Spanned).getSpans(off, off, ClickableSpan::class.java)
if (spans.isNotEmpty()) {
currentSpan = spans[0]
}
}
}
}
return super.onTouchEvent(event);
return super.onTouchEvent(event)
}
}
@@ -13,55 +13,38 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.ui.legacy
package com.hippo.ehviewer.ui.legacy;
import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import android.content.Context;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatTextView;
public class ObservedTextView extends AppCompatTextView {
private OnWindowAttachListener mOnWindowAttachListener;
public ObservedTextView(Context context) {
super(context);
}
public ObservedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ObservedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
open class ObservedTextView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : AppCompatTextView(context, attrs) {
private var mOnWindowAttachListener: OnWindowAttachListener? = null
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (mOnWindowAttachListener != null) {
mOnWindowAttachListener.onAttachedToWindow();
mOnWindowAttachListener!!.onAttachedToWindow()
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
if (mOnWindowAttachListener != null) {
mOnWindowAttachListener.onDetachedFromWindow();
mOnWindowAttachListener!!.onDetachedFromWindow()
}
}
public void setOnWindowAttachListener(OnWindowAttachListener onWindowAttachListener) {
mOnWindowAttachListener = onWindowAttachListener;
fun setOnWindowAttachListener(onWindowAttachListener: OnWindowAttachListener?) {
mOnWindowAttachListener = onWindowAttachListener
}
public interface OnWindowAttachListener {
void onAttachedToWindow();
void onDetachedFromWindow();
interface OnWindowAttachListener {
fun onAttachedToWindow()
fun onDetachedFromWindow()
}
}