Skip to content

Commit

Permalink
Migrate TextUnderButton to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanvelzen committed Mar 21, 2022
1 parent 149f3c9 commit 0f6107b
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 127 deletions.
138 changes: 58 additions & 80 deletions app/src/main/java/org/jellyfin/androidtv/ui/TextUnderButton.kt
Original file line number Diff line number Diff line change
@@ -1,81 +1,59 @@
package org.jellyfin.androidtv.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;

import org.jellyfin.androidtv.data.model.GotFocusEvent;
import org.jellyfin.androidtv.databinding.TextUnderButtonBinding;
import org.jellyfin.androidtv.util.Utils;

public class TextUnderButton extends RelativeLayout {
private TextView mLabel;
private ImageView mButton;

private GotFocusEvent mGotFocusListener;

public TextUnderButton(Context context, @DrawableRes int imageResource, int size, String label, final OnClickListener clicked) {
this(context, imageResource, size, null, label, clicked);
}

public TextUnderButton(Context context, @DrawableRes int imageResource, int size, Integer padding, String label, final OnClickListener clicked) {
super(context);

LayoutInflater inflater = LayoutInflater.from(context);
TextUnderButtonBinding binding = TextUnderButtonBinding.inflate(inflater, this, true);

setFocusable(true);
setClickable(true);
setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
setOnClickListener(clicked);
setOnFocusChangeListener(focusChange);

mLabel = binding.label;
if (label == null) {
mLabel.setVisibility(GONE);
} else {
mLabel.setText(label);
}

mButton = binding.imageButton;

if (label != null) mButton.setContentDescription(label);

mButton.setImageResource(imageResource);
mButton.setMaxHeight(size);
if (padding != null) {
int amt = Utils.convertDpToPixel(context, padding);
mButton.setPadding(amt, amt, amt, amt);
}
}

public void setText(String text) {
mLabel.setText(text);
}

protected OnFocusChangeListener focusChange = new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus && mGotFocusListener != null) mGotFocusListener.gotFocus(v);
}
};

public void setGotFocusListener(GotFocusEvent event) {
mGotFocusListener = event;
}

@Override
public void setEnabled(boolean enabled) {
setFocusable(enabled);
setFocusableInTouchMode(enabled);
}

public boolean isVisible() {
return getVisibility() == View.VISIBLE;
}
package org.jellyfin.androidtv.ui

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.annotation.DrawableRes
import androidx.core.view.isVisible
import androidx.core.view.setPadding
import org.jellyfin.androidtv.databinding.TextUnderButtonBinding
import org.jellyfin.androidtv.util.dp

class TextUnderButton @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) {
val binding = TextUnderButtonBinding.inflate(LayoutInflater.from(context), this, true)

init {
isFocusable = true
isFocusableInTouchMode = true
descendantFocusability = FOCUS_BLOCK_DESCENDANTS
}

fun setLabel(text: String?) {
binding.label.isVisible = text != null
binding.label.text = text
binding.imageButton.contentDescription = text
}

fun setIcon(@DrawableRes resource: Int, maxHeight: Int? = null) {
binding.imageButton.setImageResource(resource)
binding.imageButton.maxHeight = maxHeight ?: Int.MAX_VALUE
}

fun setPadding(padding: Int?) {
binding.imageButton.setPadding(padding?.dp(context) ?: 0)
}

companion object {
@JvmStatic
@Suppress("LongParameterList")
fun create(
context: Context,
@DrawableRes icon: Int,
maxHeight: Int? = null,
padding: Int? = null,
label: String? = null,
onClickListener: OnClickListener
) = TextUnderButton(context).apply {
setLabel(label)
setIcon(icon, maxHeight)
setPadding(padding)
setOnClickListener(onClickListener)
}
}
}
Loading

0 comments on commit 0f6107b

Please sign in to comment.