From 7e5fe216e4c7e18a3e43f00cc5799ac92f3d83f1 Mon Sep 17 00:00:00 2001 From: tehcneko <7764726+tehcneko@users.noreply.github.com> Date: Sat, 20 Mar 2021 18:39:48 +0800 Subject: [PATCH] Update dialog design --- .../com/hippo/app/CheckBoxDialogBuilder.java | 1 + .../hippo/preference/SeekBarPreference.java | 229 ------------------ .../res/layout/dialog_checkbox_builder.xml | 17 +- 3 files changed, 12 insertions(+), 235 deletions(-) delete mode 100644 app/src/main/java/com/hippo/preference/SeekBarPreference.java diff --git a/app/src/main/java/com/hippo/app/CheckBoxDialogBuilder.java b/app/src/main/java/com/hippo/app/CheckBoxDialogBuilder.java index 5b0216168..b518b9a30 100644 --- a/app/src/main/java/com/hippo/app/CheckBoxDialogBuilder.java +++ b/app/src/main/java/com/hippo/app/CheckBoxDialogBuilder.java @@ -39,6 +39,7 @@ public class CheckBoxDialogBuilder extends AlertDialog.Builder { mCheckBox = view.findViewById(R.id.checkbox); mCheckBox.setText(checkText); mCheckBox.setChecked(checked); + view.setOnClickListener(v -> mCheckBox.toggle()); } public boolean isChecked() { diff --git a/app/src/main/java/com/hippo/preference/SeekBarPreference.java b/app/src/main/java/com/hippo/preference/SeekBarPreference.java deleted file mode 100644 index f1d1786c0..000000000 --- a/app/src/main/java/com/hippo/preference/SeekBarPreference.java +++ /dev/null @@ -1,229 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.hippo.preference; - -import android.content.Context; -import android.content.res.TypedArray; -import android.os.Parcel; -import android.os.Parcelable; -import android.util.AttributeSet; -import android.widget.SeekBar; - -import androidx.preference.Preference; -import androidx.preference.PreferenceViewHolder; - -import com.hippo.ehviewer.R; - -public class SeekBarPreference extends Preference implements SeekBar.OnSeekBarChangeListener { - - private int mProgress; - private int mMax; - private boolean mTrackingTouch; - - public SeekBarPreference(Context context) { - super(context); - init(context, null, 0, 0); - } - - public SeekBarPreference(Context context, AttributeSet attrs) { - super(context, attrs); - init(context, attrs, 0, 0); - } - - public SeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - init(context, attrs, defStyleAttr, 0); - } - - private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { - TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SeekBarPreference, defStyleAttr, defStyleRes); - setMax(a.getInt(R.styleable.SeekBarPreference_max, mMax)); - a.recycle(); - - setLayoutResource(R.layout.preference_widget_seekbar); - } - - @Override - public void onBindViewHolder(PreferenceViewHolder holder) { - SeekBar seekBar = holder.itemView.findViewById(R.id.seekbar); - seekBar.setOnSeekBarChangeListener(this); - seekBar.setMax(mMax); - seekBar.setProgress(mProgress); - seekBar.setEnabled(isEnabled()); - super.onBindViewHolder(holder); - } - - @Override - protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { - setProgress(restoreValue ? getPersistedInt(mProgress) - : (Integer) defaultValue); - } - - @Override - protected Object onGetDefaultValue(TypedArray a, int index) { - return a.getInt(index, 0); - } - - public void setMax(int max) { - if (max != mMax) { - mMax = max; - notifyChanged(); - } - } - - private void setProgress(int progress, boolean notifyChanged) { - if (progress > mMax) { - progress = mMax; - } - if (progress < 0) { - progress = 0; - } - if (progress != mProgress) { - mProgress = progress; - persistInt(progress); - if (notifyChanged) { - notifyChanged(); - } - } - } - - public int getProgress() { - return mProgress; - } - - public void setProgress(int progress) { - setProgress(progress, true); - } - - /** - * Persist the seekBar's progress value if callChangeListener - * returns true, otherwise set the seekBar's progress to the stored value - */ - void syncProgress(SeekBar seekBar) { - int progress = seekBar.getProgress(); - if (progress != mProgress) { - if (callChangeListener(progress)) { - setProgress(progress, false); - } else { - seekBar.setProgress(mProgress); - } - } - } - - @Override - public void onProgressChanged( - SeekBar seekBar, int progress, boolean fromUser) { - if (fromUser && !mTrackingTouch) { - syncProgress(seekBar); - } - } - - @Override - public void onStartTrackingTouch(SeekBar seekBar) { - mTrackingTouch = true; - } - - @Override - public void onStopTrackingTouch(SeekBar seekBar) { - mTrackingTouch = false; - if (seekBar.getProgress() != mProgress) { - syncProgress(seekBar); - } - } - - @Override - protected Parcelable onSaveInstanceState() { - /* - * Suppose a client uses this preference type without persisting. We - * must save the instance state so it is able to, for example, survive - * orientation changes. - */ - - final Parcelable superState = super.onSaveInstanceState(); - if (isPersistent()) { - // No need to save instance state since it's persistent - return superState; - } - - // Save the instance state - final SavedState myState = new SavedState(superState); - myState.progress = mProgress; - myState.max = mMax; - return myState; - } - - @Override - protected void onRestoreInstanceState(Parcelable state) { - if (!state.getClass().equals(SavedState.class)) { - // Didn't save state for us in onSaveInstanceState - super.onRestoreInstanceState(state); - return; - } - - // Restore the instance state - SavedState myState = (SavedState) state; - super.onRestoreInstanceState(myState.getSuperState()); - mProgress = myState.progress; - mMax = myState.max; - notifyChanged(); - } - - /** - * SavedState, a subclass of {@link BaseSavedState}, will store the state - * of MyPreference, a subclass of Preference. - *

- * It is important to always call through to super methods. - */ - private static class SavedState extends BaseSavedState { - @SuppressWarnings("unused") - public static final Parcelable.Creator CREATOR = - new Parcelable.Creator() { - @Override - public SavedState createFromParcel(Parcel in) { - return new SavedState(in); - } - - @Override - public SavedState[] newArray(int size) { - return new SavedState[size]; - } - }; - int progress; - int max; - - public SavedState(Parcel source) { - super(source); - - // Restore the click counter - progress = source.readInt(); - max = source.readInt(); - } - - public SavedState(Parcelable superState) { - super(superState); - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - super.writeToParcel(dest, flags); - - // Save the click counter - dest.writeInt(progress); - dest.writeInt(max); - } - } -} diff --git a/app/src/main/res/layout/dialog_checkbox_builder.xml b/app/src/main/res/layout/dialog_checkbox_builder.xml index 4f06fcd03..608393690 100644 --- a/app/src/main/res/layout/dialog_checkbox_builder.xml +++ b/app/src/main/res/layout/dialog_checkbox_builder.xml @@ -14,16 +14,21 @@ ~ limitations under the License. --> - + android:orientation="vertical" + android:paddingHorizontal="?attr/dialogPreferredPadding" + android:minHeight="?listPreferredItemHeightSmall" + android:background="?selectableItemBackground"> + android:focusable="false" + android:clickable="false" + android:layout_gravity="center_vertical" + android:background="@null"/> - +