mirror of https://github.com/FongMi/TV.git
parent
0fba955d4f
commit
553de537c9
@ -0,0 +1,122 @@ |
||||
package com.fongmi.android.tv.bean; |
||||
|
||||
import android.text.TextUtils; |
||||
|
||||
import com.fongmi.android.tv.R; |
||||
import com.fongmi.android.tv.utils.ResUtil; |
||||
import com.github.catvod.utils.Prefers; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
public class Button { |
||||
|
||||
public int id; |
||||
public int resId; |
||||
|
||||
private static List<Button> buttons; |
||||
|
||||
|
||||
public static List<Button> all() { |
||||
if (buttons != null) return buttons; |
||||
buttons = new ArrayList<>(); |
||||
buttons.add(new Button(0, R.string.home_vod)); |
||||
buttons.add(new Button(1, R.string.home_live)); |
||||
buttons.add(new Button(2, R.string.home_search)); |
||||
buttons.add(new Button(3, R.string.home_keep)); |
||||
buttons.add(new Button(4, R.string.home_push)); |
||||
buttons.add(new Button(5, R.string.home_history_short)); |
||||
buttons.add(new Button(6, R.string.home_setting)); |
||||
return buttons; |
||||
} |
||||
|
||||
public static List<Button> sortedAll() { |
||||
String buttons = Prefers.getString("home_buttons_sorted", "0,1,2,3,4,5,6"); |
||||
if (TextUtils.isEmpty(buttons)) return all(); |
||||
String[] buttonsArr = buttons.split(","); |
||||
List<Button> buttonList = new ArrayList<>(); |
||||
Map<Integer, Button> allButtons = getMap(all()); |
||||
for(int i=0; i<buttonsArr.length; i++) { |
||||
int id = Integer.parseInt(buttonsArr[i]); |
||||
if (allButtons.containsKey(id)) buttonList.add(allButtons.get(id)); |
||||
} |
||||
return buttonList; |
||||
} |
||||
|
||||
public static Map<Integer, Button> getMap(List<Button> buttons) { |
||||
Map<Integer, Button> map = new LinkedHashMap<>(); |
||||
if (buttons.size() == 0) return map; |
||||
for(int i=0; i<buttons.size(); i++) { |
||||
Button one = buttons.get(i); |
||||
map.put(one.getId(), one); |
||||
} |
||||
return map; |
||||
} |
||||
|
||||
public static List<Button> getButtons() { |
||||
String buttons = Prefers.getString("home_buttons", "0,1,2,3,4,5,6"); |
||||
if (TextUtils.isEmpty(buttons)) return new ArrayList<>(); |
||||
String[] buttonsArr = buttons.split(","); |
||||
List<Button> buttonList = new ArrayList<>(); |
||||
Map<Integer, Button> allButtons = getMap(all()); |
||||
for(int i=0; i<buttonsArr.length; i++) { |
||||
int id = Integer.parseInt(buttonsArr[i]); |
||||
if (allButtons.containsKey(id)) buttonList.add(allButtons.get(id)); |
||||
} |
||||
return buttonList; |
||||
} |
||||
|
||||
public static Map<Integer, Button> getButtonsMap() { |
||||
return getMap(getButtons()); |
||||
} |
||||
|
||||
public static void save(Map<Integer, Button> map) { |
||||
List<Integer> ids = new ArrayList<>(); |
||||
if (map.size() > 0) { |
||||
for(Integer id : map.keySet()) { |
||||
ids.add(id); |
||||
} |
||||
} |
||||
String buttonsStr = TextUtils.join(",", ids); |
||||
Prefers.put("home_buttons", buttonsStr); |
||||
} |
||||
|
||||
public static void saveSorted(Map<Integer, Button> map) { |
||||
List<Integer> ids = new ArrayList<>(); |
||||
if (map.size() > 0) { |
||||
for(Integer id : map.keySet()) { |
||||
ids.add(id); |
||||
} |
||||
} |
||||
String buttonsStr = TextUtils.join(",", ids); |
||||
Prefers.put("home_buttons_sorted", buttonsStr); |
||||
} |
||||
|
||||
public Button(int id, int resId) { |
||||
this.id = id; |
||||
this.resId = resId; |
||||
} |
||||
|
||||
public String getName() { |
||||
return ResUtil.getString(resId); |
||||
} |
||||
|
||||
public int getResId() { |
||||
return resId; |
||||
} |
||||
|
||||
public int getId() { |
||||
return id; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (this == obj) return true; |
||||
if (!(obj instanceof Button)) return false; |
||||
Button it = (Button) obj; |
||||
return getId() == it.getId(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,101 @@ |
||||
package com.fongmi.android.tv.ui.adapter; |
||||
|
||||
import android.view.Gravity; |
||||
import android.view.LayoutInflater; |
||||
import android.view.ViewGroup; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.recyclerview.widget.RecyclerView; |
||||
|
||||
import com.fongmi.android.tv.bean.Button; |
||||
import com.fongmi.android.tv.databinding.AdapterButtonsBinding; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
public class ButtonsAdapter extends RecyclerView.Adapter<ButtonsAdapter.ViewHolder> { |
||||
|
||||
private List<Button> mItems; |
||||
|
||||
public ButtonsAdapter() { |
||||
this.mItems = Button.sortedAll(); |
||||
} |
||||
|
||||
@Override |
||||
public int getItemCount() { |
||||
return mItems.size(); |
||||
} |
||||
|
||||
@NonNull |
||||
@Override |
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
||||
return new ViewHolder(AdapterButtonsBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false)); |
||||
} |
||||
|
||||
@Override |
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { |
||||
Button item = mItems.get(position); |
||||
holder.binding.text.setText(item.getName()); |
||||
holder.binding.check.setChecked(getChecked(item)); |
||||
holder.binding.select.setOnLongClickListener(v -> onItemLongClick(item)); |
||||
holder.binding.select.setOnClickListener(v -> onItemClick(item, position)); |
||||
holder.binding.text.setGravity(Gravity.START); |
||||
holder.binding.up.setOnClickListener(v -> onUpClick(item, position)); |
||||
} |
||||
|
||||
private boolean getChecked(Button item) { |
||||
Map<Integer, Button> map = Button.getButtonsMap(); |
||||
if (map.containsKey(item.getId())) return true; |
||||
return false; |
||||
} |
||||
|
||||
private void onItemClick(Button item, int position) { |
||||
boolean checked = getChecked(item); |
||||
Map<Integer, Button> map = Button.getButtonsMap(); |
||||
if (checked) map.remove(item.getId()); |
||||
else map.put(item.getId(), item); |
||||
Button.save(map); |
||||
notifyItemRangeChanged(0, getItemCount()); |
||||
} |
||||
|
||||
private boolean onItemLongClick(Button item) { |
||||
boolean checked = getChecked(item); |
||||
Map<Integer, Button> map = new LinkedHashMap<>(); |
||||
if (!checked) map = Button.getMap(mItems); |
||||
Button.save(map); |
||||
notifyItemRangeChanged(0, getItemCount()); |
||||
return true; |
||||
} |
||||
|
||||
private void onUpClick(Button item, int position) { |
||||
if (position == 0) return; |
||||
List<Button> buttonList = Button.sortedAll(); |
||||
Button button = buttonList.get(position); |
||||
buttonList.remove(position); |
||||
buttonList.add(position - 1, button); |
||||
Map<Integer, Button> map = Button.getMap(buttonList); |
||||
Button.saveSorted(map); |
||||
mItems = Button.sortedAll(); |
||||
List<Button> btns = new ArrayList<>(); |
||||
Map<Integer, Button> btnsMap= Button.getButtonsMap(); |
||||
for(int i=0; i<mItems.size(); i++) { |
||||
if (btnsMap.containsKey(mItems.get(i).getId())) btns.add(mItems.get(i)); |
||||
} |
||||
Button.save(Button.getMap(btns)); |
||||
notifyItemRangeChanged(0, getItemCount()); |
||||
} |
||||
|
||||
|
||||
static class ViewHolder extends RecyclerView.ViewHolder { |
||||
|
||||
private final AdapterButtonsBinding binding; |
||||
|
||||
ViewHolder(@NonNull AdapterButtonsBinding binding) { |
||||
super(binding.getRoot()); |
||||
this.binding = binding; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,65 @@ |
||||
package com.fongmi.android.tv.ui.dialog; |
||||
|
||||
import android.app.Activity; |
||||
import android.view.LayoutInflater; |
||||
import android.view.WindowManager; |
||||
|
||||
import androidx.appcompat.app.AlertDialog; |
||||
import androidx.recyclerview.widget.GridLayoutManager; |
||||
|
||||
import com.fongmi.android.tv.databinding.DialogButtonsBinding; |
||||
import com.fongmi.android.tv.ui.adapter.ButtonsAdapter; |
||||
import com.fongmi.android.tv.ui.custom.SpaceItemDecoration; |
||||
import com.fongmi.android.tv.utils.ResUtil; |
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder; |
||||
|
||||
public class ButtonsDialog { |
||||
private final DialogButtonsBinding binding; |
||||
private final ButtonsAdapter adapter; |
||||
private final AlertDialog dialog; |
||||
|
||||
|
||||
public static ButtonsDialog create(Activity activity) { |
||||
return new ButtonsDialog(activity); |
||||
} |
||||
|
||||
public ButtonsDialog(Activity activity) { |
||||
this.adapter = new ButtonsAdapter(); |
||||
this.binding = DialogButtonsBinding.inflate(LayoutInflater.from(activity)); |
||||
this.dialog = new MaterialAlertDialogBuilder(activity).setView(binding.getRoot()).create(); |
||||
} |
||||
|
||||
public void show() { |
||||
initView(); |
||||
initEvent(); |
||||
} |
||||
|
||||
private void initView() { |
||||
setRecyclerView(); |
||||
setDialog(); |
||||
} |
||||
|
||||
private void initEvent() { |
||||
|
||||
} |
||||
|
||||
private void setRecyclerView() { |
||||
binding.recycler.setAdapter(adapter); |
||||
binding.recycler.setHasFixedSize(true); |
||||
binding.recycler.setItemAnimator(null); |
||||
binding.recycler.addItemDecoration(new SpaceItemDecoration(1, 16)); |
||||
binding.recycler.setLayoutManager(new GridLayoutManager(dialog.getContext(), 1)); |
||||
binding.recycler.post(() -> binding.recycler.scrollToPosition(0)); |
||||
|
||||
} |
||||
|
||||
private void setDialog() { |
||||
if (adapter.getItemCount() == 0) return; |
||||
WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); |
||||
params.width = (int) (ResUtil.getScreenWidth() * 0.4f); |
||||
dialog.getWindow().setAttributes(params); |
||||
dialog.getWindow().setDimAmount(0); |
||||
dialog.show(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,10 @@ |
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:width="24dp" |
||||
android:height="24dp" |
||||
android:tint="@color/white" |
||||
android:viewportWidth="24" |
||||
android:viewportHeight="24"> |
||||
<path |
||||
android:fillColor="@android:color/white" |
||||
android:pathData="M4,12l1.41,1.41L11,7.83V20h2V7.83l5.58,5.59L20,12l-8,-8 -8,8z" /> |
||||
</vector> |
||||
@ -0,0 +1,53 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="40dp" |
||||
android:orientation="horizontal"> |
||||
|
||||
<LinearLayout |
||||
android:id="@+id/select" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:background="@drawable/selector_text" |
||||
android:focusable="true" |
||||
android:focusableInTouchMode="true" |
||||
android:gravity="center_vertical" |
||||
android:orientation="horizontal"> |
||||
<TextView |
||||
android:id="@+id/text" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:ellipsize="marquee" |
||||
android:gravity="center" |
||||
android:singleLine="true" |
||||
android:textColor="@color/text" |
||||
android:textSize="18sp" |
||||
tools:text="泥巴" /> |
||||
|
||||
<com.google.android.material.checkbox.MaterialCheckBox |
||||
android:id="@+id/check" |
||||
android:layout_width="24dp" |
||||
android:layout_height="24dp" |
||||
android:layout_marginStart="12dp" |
||||
android:buttonTint="@color/white" |
||||
android:clickable="false" |
||||
android:enabled="false" |
||||
android:focusable="false" |
||||
android:focusableInTouchMode="false" /> |
||||
</LinearLayout> |
||||
|
||||
<ImageView |
||||
android:id="@+id/up" |
||||
android:layout_width="40dp" |
||||
android:layout_height="40dp" |
||||
android:layout_marginLeft="10dp" |
||||
android:background="@drawable/selector_text" |
||||
android:focusable="true" |
||||
android:focusableInTouchMode="true" |
||||
android:padding="8dp" |
||||
android:src="@drawable/ic_action_up" /> |
||||
|
||||
</LinearLayout> |
||||
@ -0,0 +1,18 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="horizontal" |
||||
android:padding="16dp"> |
||||
|
||||
<com.fongmi.android.tv.ui.custom.CustomRecyclerView |
||||
android:id="@+id/recycler" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
app:maxHeight="264dp" |
||||
tools:itemCount="5" |
||||
tools:listitem="@layout/adapter_buttons" /> |
||||
</LinearLayout> |
||||
Loading…
Reference in new issue