Refactored ThreeStateButton, converted to kotlin, removed unnecessary redraws.

MAILAND-1043
This commit is contained in:
Tomasz Giszczak 2020-12-11 17:14:53 +01:00
parent 4440d97bf7
commit 15075cae13
4 changed files with 90 additions and 150 deletions

View File

@ -177,7 +177,7 @@ public class ManageLabelsDialogFragment extends AbstractDialogFragment implement
protected void initUi(final View rootView) {
mList.setOnItemLongClickListener(labelItemLongClick);
mColorsGrid.setOnItemClickListener(this);
mArchiveCheckbox.numberOfStates = 2;
mArchiveCheckbox.setNumberOfStates(2);
MessagesDatabase messagesDatabase = MessagesDatabaseFactory.Companion.getInstance(getContext().getApplicationContext()).getDatabase();
messagesDatabase.getAllLabels().observe(this,new LabelsObserver());
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

View File

@ -75,7 +75,7 @@ public class LabelsAdapter extends ArrayAdapter<LabelsAdapter.LabelItem> {
}
final ViewHolder viewHolder = (ViewHolder) convertView.getTag();
viewHolder.mLabelCheck.setOnStateChangedListener(null);
viewHolder.mLabelCheck.numberOfStates = item.states;
viewHolder.mLabelCheck.setNumberOfStates(item.states);
viewHolder.mLabelCheck.setVisibility(View.VISIBLE);
viewHolder.mLabelCheck.setState(item.isAttached ? ThreeStateButton.STATE_CHECKED
: ThreeStateButton.STATE_UNPRESSED);

View File

@ -1,148 +0,0 @@
/*
* Copyright (c) 2020 Proton Technologies AG
*
* This file is part of ProtonMail.
*
* ProtonMail is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ProtonMail is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ProtonMail. If not, see https://www.gnu.org/licenses/.
*/
package ch.protonmail.android.views;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import androidx.annotation.DrawableRes;
import androidx.appcompat.widget.AppCompatButton;
import ch.protonmail.android.R;
public class ThreeStateButton extends AppCompatButton {
public static final int STATE_UNPRESSED = 0;
public static final int STATE_CHECKED = 1;
public static final int STATE_PRESSED = 2;
public int numberOfStates = 3;
private int state;
private View.OnClickListener onStateChangedListener = null;
/**
* @param context
*/
public ThreeStateButton(Context context) {
super(context);
initConfig();
}
/**
* @param context
* @param attrs
*/
public ThreeStateButton(Context context, AttributeSet attrs) {
super(context, attrs);
initConfig();
}
/**
* @param context
* @param attrs
* @param defStyle
*/
public ThreeStateButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initConfig();
}
private void nextState() {
state++;
state = state % numberOfStates;
//this.setPressed(false);
// forces to redraw the view
invalidate();
if (onStateChangedListener != null) {
onStateChangedListener.onClick(this);
}
}
private void setButtonBackground(@DrawableRes int backgroundDrawableId) {
setBackground(getResources().getDrawable(backgroundDrawableId));
}
private void initConfig() {
// initialize variables
state = ThreeStateButton.STATE_UNPRESSED;
setButtonBackground(R.drawable.mail_check);
// listeners
setOnClickListener(v -> nextState());
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) && (event.getAction() == KeyEvent.ACTION_UP)) {
nextState();
this.setPressed(false);
}
return false;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
switch (state) {
case 0:
setButtonBackground(R.drawable.mail_check);
break;
case 1:
setButtonBackground(R.drawable.mail_check_active);
break;
case 2:
// draw a tick
setButtonBackground(R.drawable.mail_check_neutral);
break;
default:
break;
}
}
public void setOnStateChangedListener(View.OnClickListener listener) {
onStateChangedListener = listener;
}
public boolean isUnPressed() {
return (state == ThreeStateButton.STATE_UNPRESSED);
}
public boolean isPressed() {
return (state == ThreeStateButton.STATE_PRESSED);
}
public boolean isChecked() {
return (state == ThreeStateButton.STATE_CHECKED);
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
// forces to redraw the view
invalidate();
}
}

View File

@ -0,0 +1,88 @@
/*
* Copyright (c) 2020 Proton Technologies AG
*
* This file is part of ProtonMail.
*
* ProtonMail is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ProtonMail is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ProtonMail. If not, see https://www.gnu.org/licenses/.
*/
package ch.protonmail.android.views
import android.content.Context
import android.util.AttributeSet
import android.view.KeyEvent
import androidx.annotation.DrawableRes
import androidx.appcompat.widget.AppCompatButton
import androidx.core.content.res.ResourcesCompat
import ch.protonmail.android.R
class ThreeStateButton @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppCompatButton(context, attrs, defStyleAttr) {
var numberOfStates = 3
var state = 0
set(value) {
field = value
when (state) {
0 -> setButtonBackground(R.drawable.mail_check)
1 -> setButtonBackground(R.drawable.mail_check_active)
2 -> setButtonBackground(R.drawable.mail_check_neutral)
else -> {
throw IllegalStateException("Unsupported view state")
}
}
}
private var onStateChangedListener: OnClickListener? = null
init {
state = STATE_UNPRESSED
setButtonBackground(R.drawable.mail_check)
setOnClickListener { nextState() }
}
private fun nextState() {
state++
state %= numberOfStates
// forces to redraw the view
onStateChangedListener?.onClick(this)
}
private fun setButtonBackground(@DrawableRes backgroundDrawableId: Int) {
background = ResourcesCompat.getDrawable(resources, backgroundDrawableId, null)
}
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER && event.action == KeyEvent.ACTION_UP) {
nextState()
this.isPressed = false
}
return false
}
fun setOnStateChangedListener(listener: OnClickListener?) {
onStateChangedListener = listener
}
override fun isPressed(): Boolean = state == STATE_PRESSED
companion object {
const val STATE_UNPRESSED = 0
const val STATE_CHECKED = 1
const val STATE_PRESSED = 2
}
}