mirror of
https://github.com/yuliskov/SmartTube.git
synced 2026-05-12 15:59:26 -05:00
refactor
This commit is contained in:
+26
-21
@@ -1,11 +1,17 @@
|
||||
package com.liskovsoft.smartyoutubetv2.tv.ui.settings;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.leanback.preference.LeanbackListPreferenceDialogFragment;
|
||||
import androidx.leanback.preference.LeanbackPreferenceDialogFragment;
|
||||
import androidx.leanback.preference.LeanbackPreferenceFragment;
|
||||
import androidx.leanback.preference.LeanbackSettingsFragment;
|
||||
import androidx.preference.DialogPreference;
|
||||
import androidx.preference.MultiSelectListPreference;
|
||||
import androidx.preference.MultiSelectListPreferenceDialogFragment;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceFragment;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
@@ -13,6 +19,8 @@ import com.liskovsoft.sharedutils.helpers.Helpers;
|
||||
import com.liskovsoft.smartyoutubetv2.common.app.presenters.VideoSettingsPresenter;
|
||||
import com.liskovsoft.smartyoutubetv2.common.app.presenters.VideoSettingsPresenter.SettingsCategory;
|
||||
import com.liskovsoft.smartyoutubetv2.common.app.views.VideoSettingsView;
|
||||
import com.liskovsoft.smartyoutubetv2.tv.ui.settings.strings.StringListPreference;
|
||||
import com.liskovsoft.smartyoutubetv2.tv.ui.settings.strings.StringListPreferenceDialogFragment;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -83,30 +91,16 @@ public class AppSettingsFragment extends LeanbackSettingsFragment
|
||||
|
||||
//@Override
|
||||
//public boolean onPreferenceDisplayDialog(@NonNull PreferenceFragment caller, Preference pref) {
|
||||
// if (caller == null) {
|
||||
// throw new IllegalArgumentException("Cannot display dialog for preference " + pref
|
||||
// + ", Caller must not be null!");
|
||||
// }
|
||||
// final Fragment f;
|
||||
// if (pref instanceof ListPreference) {
|
||||
// final ListPreference listPreference = (ListPreference) pref;
|
||||
// f = LeanbackListPreferenceDialogFragment.newInstanceSingle(listPreference.getKey());
|
||||
// f.setTargetFragment(caller, 0);
|
||||
// startPreferenceFragment(f);
|
||||
// } else if (pref instanceof MultiSelectListPreference) {
|
||||
// MultiSelectListPreference listPreference = (MultiSelectListPreference) pref;
|
||||
// f = LeanbackListPreferenceDialogFragment.newInstanceMulti(listPreference.getKey());
|
||||
// if (pref instanceof StringListPreference) {
|
||||
// StringListPreference listPreference = (StringListPreference) pref;
|
||||
// LeanbackPreferenceDialogFragment f = StringListPreferenceDialogFragment.newInstanceStringList(listPreference.getKey());
|
||||
// f.setTargetFragment(caller, 0);
|
||||
// startPreferenceFragment(f);
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
// // TODO
|
||||
// // else if (pref instanceof EditTextPreference) {
|
||||
// //
|
||||
// // }
|
||||
// else {
|
||||
// return false;
|
||||
// }
|
||||
// return true;
|
||||
//
|
||||
// return super.onPreferenceDisplayDialog(caller, pref);
|
||||
//}
|
||||
|
||||
public static class PrefFragment extends LeanbackPreferenceFragment {
|
||||
@@ -115,6 +109,17 @@ public class AppSettingsFragment extends LeanbackSettingsFragment
|
||||
private SettingsFragmentHelper mManager;
|
||||
private String mTitle;
|
||||
|
||||
@Override
|
||||
public void onDisplayPreferenceDialog(Preference preference) {
|
||||
//if (preference instanceof StringListPreference) {
|
||||
// StringListPreferenceDialogFragment f = StringListPreferenceDialogFragment.newInstanceStringList(preference.getKey());
|
||||
// f.setTargetFragment(this, 0);
|
||||
// f.show(getFragmentManager(), DIALOG_FRAGMENT_TAG);
|
||||
//}
|
||||
|
||||
super.onDisplayPreferenceDialog(preference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle bundle, String s) {
|
||||
mStyledContext = (Context) Helpers.getField(this, "mStyledContext");
|
||||
|
||||
+1
@@ -7,6 +7,7 @@ import androidx.preference.Preference;
|
||||
import androidx.preference.SwitchPreference;
|
||||
import com.liskovsoft.smartyoutubetv2.common.app.models.playback.ui.OptionItem;
|
||||
import com.liskovsoft.smartyoutubetv2.common.app.presenters.VideoSettingsPresenter.SettingsCategory;
|
||||
import com.liskovsoft.smartyoutubetv2.tv.ui.settings.strings.StringListPreference;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.liskovsoft.smartyoutubetv2.tv.ui.settings;
|
||||
package com.liskovsoft.smartyoutubetv2.tv.ui.settings.strings;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
+164
@@ -0,0 +1,164 @@
|
||||
package com.liskovsoft.smartyoutubetv2.tv.ui.settings.strings;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import androidx.collection.ArraySet;
|
||||
import androidx.leanback.preference.LeanbackListPreferenceDialogFragment;
|
||||
import androidx.preference.DialogPreference;
|
||||
import androidx.preference.ListPreference;
|
||||
import androidx.preference.MultiSelectListPreference;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import androidx.recyclerview.widget.RecyclerView.Adapter;
|
||||
import com.liskovsoft.smartyoutubetv2.tv.R;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class StringListPreferenceDialogFragment extends LeanbackListPreferenceDialogFragment {
|
||||
private static final String SAVE_STATE_IS_MULTI =
|
||||
"LeanbackListPreferenceDialogFragment.isMulti";
|
||||
private static final String SAVE_STATE_ENTRIES = "LeanbackListPreferenceDialogFragment.entries";
|
||||
private static final String SAVE_STATE_ENTRY_VALUES =
|
||||
"LeanbackListPreferenceDialogFragment.entryValues";
|
||||
private static final String SAVE_STATE_TITLE = "LeanbackListPreferenceDialogFragment.title";
|
||||
private static final String SAVE_STATE_MESSAGE = "LeanbackListPreferenceDialogFragment.message";
|
||||
private static final String SAVE_STATE_INITIAL_SELECTIONS =
|
||||
"LeanbackListPreferenceDialogFragment.initialSelections";
|
||||
private static final String SAVE_STATE_INITIAL_SELECTION =
|
||||
"LeanbackListPreferenceDialogFragment.initialSelection";
|
||||
|
||||
private boolean mMulti;
|
||||
private CharSequence[] mEntries;
|
||||
private CharSequence[] mEntryValues;
|
||||
private CharSequence mDialogTitle;
|
||||
private CharSequence mDialogMessage;
|
||||
Set<String> mInitialSelections;
|
||||
private String mInitialSelection;
|
||||
|
||||
public static StringListPreferenceDialogFragment newInstanceStringList(String key) {
|
||||
final Bundle args = new Bundle(1);
|
||||
args.putString(ARG_KEY, key);
|
||||
|
||||
final StringListPreferenceDialogFragment
|
||||
fragment = new StringListPreferenceDialogFragment();
|
||||
fragment.setArguments(args);
|
||||
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
final DialogPreference preference = getPreference();
|
||||
mDialogTitle = preference.getDialogTitle();
|
||||
mDialogMessage = preference.getDialogMessage();
|
||||
|
||||
if (preference instanceof ListPreference) {
|
||||
mMulti = false;
|
||||
mEntries = ((ListPreference) preference).getEntries();
|
||||
mEntryValues = ((ListPreference) preference).getEntryValues();
|
||||
mInitialSelection = ((ListPreference) preference).getValue();
|
||||
} else if (preference instanceof MultiSelectListPreference) {
|
||||
mMulti = true;
|
||||
mEntries = ((MultiSelectListPreference) preference).getEntries();
|
||||
mEntryValues = ((MultiSelectListPreference) preference).getEntryValues();
|
||||
mInitialSelections = ((MultiSelectListPreference) preference).getValues();
|
||||
} else {
|
||||
throw new IllegalArgumentException("Preference must be a ListPreference or "
|
||||
+ "MultiSelectListPreference");
|
||||
}
|
||||
} else {
|
||||
mDialogTitle = savedInstanceState.getCharSequence(SAVE_STATE_TITLE);
|
||||
mDialogMessage = savedInstanceState.getCharSequence(SAVE_STATE_MESSAGE);
|
||||
mMulti = savedInstanceState.getBoolean(SAVE_STATE_IS_MULTI);
|
||||
mEntries = savedInstanceState.getCharSequenceArray(SAVE_STATE_ENTRIES);
|
||||
mEntryValues = savedInstanceState.getCharSequenceArray(SAVE_STATE_ENTRY_VALUES);
|
||||
if (mMulti) {
|
||||
final String[] initialSelections = savedInstanceState.getStringArray(
|
||||
SAVE_STATE_INITIAL_SELECTIONS);
|
||||
mInitialSelections = new ArraySet<>(
|
||||
initialSelections != null ? initialSelections.length : 0);
|
||||
if (initialSelections != null) {
|
||||
Collections.addAll(mInitialSelections, initialSelections);
|
||||
}
|
||||
} else {
|
||||
mInitialSelection = savedInstanceState.getString(SAVE_STATE_INITIAL_SELECTION);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Adapter<ViewHolder> onCreateAdapter() {
|
||||
return new AdapterMulti(mEntries, mEntryValues, mInitialSelections);
|
||||
}
|
||||
|
||||
public class AdapterMultiStrings extends RecyclerView.Adapter<ViewHolder>
|
||||
implements ViewHolder.OnItemClickListener {
|
||||
|
||||
private final CharSequence[] mEntries;
|
||||
private final CharSequence[] mEntryValues;
|
||||
private final Set<String> mSelections;
|
||||
|
||||
public AdapterMultiStrings(CharSequence[] entries, CharSequence[] entryValues,
|
||||
Set<String> initialSelections) {
|
||||
mEntries = entries;
|
||||
mEntryValues = entryValues;
|
||||
mSelections = new HashSet<>(initialSelections);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
|
||||
final View view = inflater.inflate(R.layout.string_list_preference_item_multi, parent,
|
||||
false);
|
||||
return new ViewHolder(view, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(ViewHolder holder, int position) {
|
||||
holder.getWidgetView().setChecked(
|
||||
mSelections.contains(mEntryValues[position].toString()));
|
||||
holder.getTitleView().setText(mEntries[position]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return mEntries.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemClick(ViewHolder viewHolder) {
|
||||
final int index = viewHolder.getAdapterPosition();
|
||||
if (index == RecyclerView.NO_POSITION) {
|
||||
return;
|
||||
}
|
||||
final String entry = mEntryValues[index].toString();
|
||||
if (mSelections.contains(entry)) {
|
||||
mSelections.remove(entry);
|
||||
} else {
|
||||
mSelections.add(entry);
|
||||
}
|
||||
final MultiSelectListPreference multiSelectListPreference
|
||||
= (MultiSelectListPreference) getPreference();
|
||||
// Pass copies of the set to callChangeListener and setValues to avoid mutations
|
||||
if (multiSelectListPreference.callChangeListener(new HashSet<>(mSelections))) {
|
||||
multiSelectListPreference.setValues(new HashSet<>(mSelections));
|
||||
mInitialSelections = mSelections;
|
||||
} else {
|
||||
// Change refused, back it out
|
||||
if (mSelections.contains(entry)) {
|
||||
mSelections.remove(entry);
|
||||
} else {
|
||||
mSelections.add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:descendantFocusability="blocksDescendants"
|
||||
android:orientation="horizontal"
|
||||
android:paddingStart="@dimen/lb_preference_item_padding_start"
|
||||
android:paddingEnd="@dimen/lb_preference_item_padding_end" >
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:focusable="false"
|
||||
android:clickable="false"
|
||||
android:layout_marginEnd="@dimen/lb_preference_item_icon_margin_end"
|
||||
android:visibility="gone"
|
||||
android:layout_gravity="center_vertical" />
|
||||
|
||||
<LinearLayout android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
<Space android:layout_width="0dp" android:layout_height="@dimen/lb_preference_item_text_space_top" />
|
||||
<TextView
|
||||
android:id="@android:id/title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="@dimen/lb_preference_item_primary_text_margin_bottom"
|
||||
android:fontFamily="sans-serif"
|
||||
android:textColor="@color/lb_preference_item_primary_text_color"
|
||||
android:textSize="@dimen/lb_preference_item_primary_text_size"/>
|
||||
<Space android:layout_width="0dp" android:layout_height="@dimen/lb_preference_item_text_space_bottom" />
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user