NewPipe/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsDialog.java

299 lines
10 KiB
Java

package org.schabi.newpipe.settings;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import org.schabi.newpipe.R;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ContentSettingsDialog extends DialogFragment {
public AllAdapter allAdapter;
public UsedAdapter usedAdapter;
List<String> usedTabs = new ArrayList<>();
public String[] allTabs = new String[7];
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_contentsettings, container);
}
@Override
public void onViewCreated(@NonNull View rootView, @Nullable Bundle savedInstanceState) {
super.onViewCreated(rootView, savedInstanceState);
tabNames();
initButtons(rootView);
initUsedTabs();
RecyclerView allTabs = rootView.findViewById(R.id.tabs);
allTabs.setLayoutManager(new LinearLayoutManager(getContext()));
allAdapter = new AllAdapter();
allTabs.setAdapter(allAdapter);
allTabs.addItemDecoration(new DividerItemDecoration(getActivity()));
RecyclerView usedTabs = rootView.findViewById(R.id.usedTabs);
usedTabs.setLayoutManager(new LinearLayoutManager(getContext()));
usedAdapter = new UsedAdapter();
usedTabs.setAdapter(usedAdapter);
usedTabs.addItemDecoration(new DividerItemDecoration(getActivity()));
}
private void initButtons(View rootView){
rootView.findViewById(R.id.cancelText).setOnClickListener((v -> {
dismiss();
}));
rootView.findViewById(R.id.confirmText).setOnClickListener((v -> {
StringBuilder save = new StringBuilder();
if(usedTabs.size()==0) {
save = new StringBuilder("0");
} else {
for(String s: usedTabs) {
save.append(s);
save.append("\n");
}
}
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("saveUsedTabs", save.toString());
editor.commit();
dismiss();
}));
}
private void initUsedTabs() {
String save = PreferenceManager.getDefaultSharedPreferences(getContext()).getString("saveUsedTabs", "1\n2\n4\n");
String tabs[] = save.trim().split("\n");
usedTabs.addAll(Arrays.asList(tabs));
}
private void tabNames() {
allTabs[0] = getString(R.string.blank_page_summary);
allTabs[1] = getString(R.string.kiosk_page_summary);
allTabs[2] = getString(R.string.subscription_page_summary);
allTabs[3] = getString(R.string.feed_page_summary);
allTabs[4] = getString(R.string.tab_bookmarks);
allTabs[5] = getString(R.string.title_activity_history);
allTabs[6] = getString(R.string.channel_page_summary);
}
private void addTab(int position) {
if(position!=6) {
usedTabs.add(String.valueOf(position));
usedAdapter.notifyDataSetChanged();
} else {
SelectChannelFragment selectChannelFragment = new SelectChannelFragment();
selectChannelFragment.setOnSelectedLisener((String url, String name, int service) -> {
usedTabs.add(position+"\t"+url+"\t"+name+"\t"+service);
usedAdapter.notifyDataSetChanged();
});
selectChannelFragment.show(getFragmentManager(), "select_channel");
}
}
private void removeTab(int position) {
usedTabs.remove(position);
usedAdapter.notifyDataSetChanged();
}
private void upTab(int position){
String first = usedTabs.get(position - 1);
String second = usedTabs.get(position);
usedTabs.set(position - 1, second);
usedTabs.set(position, first);
usedAdapter.notifyDataSetChanged();
}
private void downTab(int position){
String first = usedTabs.get(position + 1);
String second = usedTabs.get(position);
usedTabs.set(position + 1, second);
usedTabs.set(position, first);
usedAdapter.notifyDataSetChanged();
}
public class AllAdapter extends RecyclerView.Adapter<AllAdapter.TabViewHolder>{
@Override
public TabViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.dialog_contentsettingtab, parent, false);
return new TabViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull TabViewHolder holder, int position) {
holder.bind(position);
}
@Override
public int getItemCount() {
return allTabs.length;
}
class TabViewHolder extends RecyclerView.ViewHolder {
TextView text;
Button add;
Button up;
Button down;
public TabViewHolder(View itemView) {
super(itemView);
text = itemView.findViewById(R.id.tabName);
add = itemView.findViewById(R.id.buttonAddRemove);
up = itemView.findViewById(R.id.buttonUp);
down = itemView.findViewById(R.id.buttonDown);
}
void bind(int position) {
up.setBackgroundResource(0);
down.setBackgroundResource(0);
text.setText(allTabs[position]);
add.setBackgroundResource(R.drawable.ic_add);
add.setOnClickListener(v -> {
addTab(position);
});
}
}
}
public class UsedAdapter extends RecyclerView.Adapter<UsedAdapter.TabViewHolder>{
@Override
public TabViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.dialog_contentsettingtab, parent, false);
return new TabViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull TabViewHolder holder, int position) {
holder.bind(position);
}
@Override
public int getItemCount() {
return usedTabs.size();
}
class TabViewHolder extends RecyclerView.ViewHolder {
TextView text;
Button remove;
Button up;
Button down;
public TabViewHolder(View itemView) {
super(itemView);
text = itemView.findViewById(R.id.tabName);
remove = itemView.findViewById(R.id.buttonAddRemove);
up = itemView.findViewById(R.id.buttonUp);
down = itemView.findViewById(R.id.buttonDown);
}
void bind(int position) {
if(position!=0) {
up.setBackgroundResource(R.drawable.ic_arrow_up_white);
up.setOnClickListener(v -> {
upTab(position);
});
} else up.setBackgroundResource(0);
if(position!=usedTabs.size()-1) {
down.setBackgroundResource(R.drawable.ic_arrow_down_white);
down.setOnClickListener(v -> {
downTab(position);
});
} else down.setBackgroundResource(0);
if(usedTabs.get(position).startsWith("6\t")) {
String channelInfo[] = usedTabs.get(position).split("\t");
String channelName = "";
if(channelInfo.length==4) channelName = channelInfo[2];
String textToSet = allTabs[6]+": "+channelName;
text.setText(textToSet);
} else {
text.setText(allTabs[Integer.parseInt(usedTabs.get(position))]);
}
remove.setBackgroundResource(R.drawable.ic_remove);
remove.setOnClickListener(v -> {
removeTab(position);
});
}
}
}
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private final int[] ATTRS = new int[]{android.R.attr.listDivider};
private Drawable divider;
public DividerItemDecoration(Context context) {
final TypedArray styledAttributes = context.obtainStyledAttributes(ATTRS);
divider = styledAttributes.getDrawable(0);
styledAttributes.recycle();
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + divider.getIntrinsicHeight();
divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
}
}
}