Code reviewed
This commit is contained in:
parent
f29fa939ab
commit
8dde25532a
13 changed files with 150 additions and 297 deletions
|
|
@ -0,0 +1,41 @@
|
|||
package org.schabi.newpipe.settings;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.schabi.newpipe.R;
|
||||
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
||||
|
||||
public class AddTabsDialog {
|
||||
private final AlertDialog dialog;
|
||||
|
||||
public AddTabsDialog(@NonNull final Context context,
|
||||
@NonNull final String title,
|
||||
@NonNull final String[] commands,
|
||||
@NonNull final DialogInterface.OnClickListener actions) {
|
||||
|
||||
final View bannerView = View.inflate(context, R.layout.dialog_title, null);
|
||||
bannerView.setSelected(true);
|
||||
|
||||
TextView titleView = bannerView.findViewById(R.id.itemTitleView);
|
||||
titleView.setText(title);
|
||||
|
||||
TextView detailsView = bannerView.findViewById(R.id.itemAdditionalDetails);
|
||||
detailsView.setVisibility(View.GONE);
|
||||
|
||||
dialog = new AlertDialog.Builder(context)
|
||||
.setCustomTitle(bannerView)
|
||||
.setItems(commands, actions)
|
||||
.create();
|
||||
}
|
||||
|
||||
public void show() {
|
||||
dialog.show();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,63 +1,50 @@
|
|||
package org.schabi.newpipe.settings;
|
||||
|
||||
import android.content.Context;
|
||||
import android.app.Dialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.preference.PreferenceManager;
|
||||
import android.support.v7.widget.CardView;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.helper.ItemTouchHelper;
|
||||
import android.util.TypedValue;
|
||||
import android.view.DragEvent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.schabi.newpipe.R;
|
||||
import org.schabi.newpipe.database.LocalItem;
|
||||
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry;
|
||||
import org.schabi.newpipe.local.holder.LocalPlaylistStreamItemHolder;
|
||||
import org.schabi.newpipe.util.NavigationHelper;
|
||||
import org.schabi.newpipe.util.OnClickGesture;
|
||||
import org.schabi.newpipe.util.ServiceHelper;
|
||||
import org.schabi.newpipe.info_list.InfoItemDialog;
|
||||
import org.schabi.newpipe.util.ThemeHelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class ContentSettingsMain extends Fragment {
|
||||
public class ChoseTabsFragment extends Fragment {
|
||||
|
||||
public ContentSettingsMain.UsedAdapter usedAdapter;
|
||||
public ChoseTabsFragment.SelectedTabsAdapter selectedTabsAdapter;
|
||||
|
||||
RecyclerView usedTabsView;
|
||||
RecyclerView selectedTabsView;
|
||||
|
||||
List<String> usedTabs = new ArrayList<>();
|
||||
List<String> selectedTabs = new ArrayList<>();
|
||||
private String saveString;
|
||||
public String[] allTabs = new String[7];
|
||||
public String[] availableTabs = new String[7];
|
||||
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
((AppCompatActivity)getContext()).getSupportActionBar().setTitle(R.string.main_page_content);
|
||||
return inflater.inflate(R.layout.dialog_contentsettings, container, false);
|
||||
return inflater.inflate(R.layout.fragment_chose_tabs, container, false);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -69,24 +56,24 @@ public class ContentSettingsMain extends Fragment {
|
|||
initUsedTabs();
|
||||
initButton(rootView);
|
||||
|
||||
usedTabsView = rootView.findViewById(R.id.usedTabs);
|
||||
usedTabsView.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
usedAdapter = new ContentSettingsMain.UsedAdapter();
|
||||
selectedTabsView = rootView.findViewById(R.id.usedTabs);
|
||||
selectedTabsView.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
selectedTabsAdapter = new ChoseTabsFragment.SelectedTabsAdapter();
|
||||
|
||||
|
||||
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(getItemTouchCallback());
|
||||
itemTouchHelper.attachToRecyclerView(usedTabsView);
|
||||
usedAdapter.setOnItemSelectedListener(itemTouchHelper);
|
||||
itemTouchHelper.attachToRecyclerView(selectedTabsView);
|
||||
selectedTabsAdapter.setOnItemSelectedListener(itemTouchHelper);
|
||||
|
||||
usedTabsView.setAdapter(usedAdapter);
|
||||
selectedTabsView.setAdapter(selectedTabsAdapter);
|
||||
}
|
||||
|
||||
private void saveChanges() {
|
||||
StringBuilder save = new StringBuilder();
|
||||
if(usedTabs.size()==0) {
|
||||
if(selectedTabs.size()==0) {
|
||||
save = new StringBuilder("0");
|
||||
} else {
|
||||
for(String s: usedTabs) {
|
||||
for(String s: selectedTabs) {
|
||||
save.append(s);
|
||||
save.append("\n");
|
||||
}
|
||||
|
|
@ -107,26 +94,30 @@ public class ContentSettingsMain extends Fragment {
|
|||
private void initUsedTabs() {
|
||||
String save = android.preference.PreferenceManager.getDefaultSharedPreferences(getContext()).getString("saveUsedTabs", "1\n2\n4\n");
|
||||
String tabs[] = save.trim().split("\n");
|
||||
usedTabs.addAll(Arrays.asList(tabs));
|
||||
selectedTabs.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);
|
||||
availableTabs[0] = getString(R.string.blank_page_summary);
|
||||
availableTabs[1] = getString(R.string.kiosk_page_summary);
|
||||
availableTabs[2] = getString(R.string.subscription_page_summary);
|
||||
availableTabs[3] = getString(R.string.feed_page_summary);
|
||||
availableTabs[4] = getString(R.string.tab_bookmarks);
|
||||
availableTabs[5] = getString(R.string.title_activity_history);
|
||||
availableTabs[6] = getString(R.string.channel_page_summary);
|
||||
}
|
||||
|
||||
private void initButton(View rootView) {
|
||||
FloatingActionButton fab = rootView.findViewById(R.id.floatingActionButton);
|
||||
fab.setImageResource(ThemeHelper.getIconByAttr(R.attr.ic_add, getContext()));
|
||||
fab.setOnClickListener(v -> {
|
||||
ContentSettingsMainDialog contentSettingsMainDialog = new ContentSettingsMainDialog();
|
||||
contentSettingsMainDialog.setOnAddListener(ContentSettingsMain.this::addTab);
|
||||
contentSettingsMainDialog.show(getFragmentManager(), "select_channel");
|
||||
Dialog.OnClickListener onClickListener = (dialog, which) -> addTab(which);
|
||||
|
||||
new AddTabsDialog(getContext(),
|
||||
getString(R.string.tab_chose),
|
||||
availableTabs,
|
||||
onClickListener)
|
||||
.show();
|
||||
});
|
||||
|
||||
TypedValue typedValue = new TypedValue();
|
||||
|
|
@ -138,23 +129,21 @@ public class ContentSettingsMain extends Fragment {
|
|||
|
||||
private void addTab(int position) {
|
||||
if(position!=6) {
|
||||
usedTabs.add(String.valueOf(position));
|
||||
usedAdapter.notifyDataSetChanged();
|
||||
selectedTabs.add(String.valueOf(position));
|
||||
selectedTabsAdapter.notifyDataSetChanged();
|
||||
saveChanges();
|
||||
} else {
|
||||
SelectChannelFragment selectChannelFragment = new SelectChannelFragment();
|
||||
selectChannelFragment.setOnSelectedLisener((String url, String name, int service) -> {
|
||||
usedTabs.add(position+"\t"+url+"\t"+name+"\t"+service);
|
||||
usedAdapter.notifyDataSetChanged();
|
||||
selectedTabs.add(position+"\t"+url+"\t"+name+"\t"+service);
|
||||
selectedTabsAdapter.notifyDataSetChanged();
|
||||
saveChanges();
|
||||
});
|
||||
selectChannelFragment.show(getFragmentManager(), "select_channel");
|
||||
}
|
||||
}
|
||||
|
||||
public class UsedAdapter extends RecyclerView.Adapter<ContentSettingsMain.UsedAdapter.TabViewHolder>{
|
||||
// ... code from gist
|
||||
|
||||
public class SelectedTabsAdapter extends RecyclerView.Adapter<ChoseTabsFragment.SelectedTabsAdapter.TabViewHolder>{
|
||||
private ItemTouchHelper itemTouchHelper;
|
||||
|
||||
public void setOnItemSelectedListener(ItemTouchHelper mItemTouchHelper) {
|
||||
|
|
@ -162,29 +151,29 @@ public class ContentSettingsMain extends Fragment {
|
|||
}
|
||||
|
||||
public void swapItems(int fromPosition, int toPosition) {
|
||||
String temp = usedTabs.get(fromPosition);
|
||||
usedTabs.set(fromPosition, usedTabs.get(toPosition));
|
||||
usedTabs.set(toPosition, temp);
|
||||
String temp = selectedTabs.get(fromPosition);
|
||||
selectedTabs.set(fromPosition, selectedTabs.get(toPosition));
|
||||
selectedTabs.set(toPosition, temp);
|
||||
notifyItemMoved(fromPosition, toPosition);
|
||||
saveChanges();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContentSettingsMain.UsedAdapter.TabViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
public ChoseTabsFragment.SelectedTabsAdapter.TabViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
|
||||
LayoutInflater inflater = LayoutInflater.from(getContext());
|
||||
View view = inflater.inflate(R.layout.dialog_contentsettingtab, parent, false);
|
||||
return new ContentSettingsMain.UsedAdapter.TabViewHolder(view);
|
||||
View view = inflater.inflate(R.layout.viewholder_chose_tabs, parent, false);
|
||||
return new ChoseTabsFragment.SelectedTabsAdapter.TabViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ContentSettingsMain.UsedAdapter.TabViewHolder holder, int position) {
|
||||
public void onBindViewHolder(@NonNull ChoseTabsFragment.SelectedTabsAdapter.TabViewHolder holder, int position) {
|
||||
holder.bind(position, holder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return usedTabs.size();
|
||||
return selectedTabs.size();
|
||||
}
|
||||
|
||||
class TabViewHolder extends RecyclerView.ViewHolder {
|
||||
|
|
@ -203,21 +192,21 @@ public class ContentSettingsMain extends Fragment {
|
|||
view = itemView;
|
||||
}
|
||||
|
||||
void bind(int position, ContentSettingsMain.UsedAdapter.TabViewHolder holder) {
|
||||
void bind(int position, ChoseTabsFragment.SelectedTabsAdapter.TabViewHolder holder) {
|
||||
|
||||
handle.setImageResource(ThemeHelper.getIconByAttr(R.attr.drag_handle, getContext()));
|
||||
handle.setOnTouchListener(getOnTouchListener(holder));
|
||||
|
||||
view.setOnLongClickListener(getOnLongClickListener(holder));
|
||||
|
||||
if(usedTabs.get(position).startsWith("6\t")) {
|
||||
String channelInfo[] = usedTabs.get(position).split("\t");
|
||||
if(selectedTabs.get(position).startsWith("6\t")) {
|
||||
String channelInfo[] = selectedTabs.get(position).split("\t");
|
||||
String channelName = "";
|
||||
if(channelInfo.length==4) channelName = channelInfo[2];
|
||||
String textToSet = allTabs[6]+": "+channelName;
|
||||
String textToSet = availableTabs[6]+": "+channelName;
|
||||
text.setText(textToSet);
|
||||
} else {
|
||||
text.setText(allTabs[Integer.parseInt(usedTabs.get(position))]);
|
||||
text.setText(availableTabs[Integer.parseInt(selectedTabs.get(position))]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -234,7 +223,7 @@ public class ContentSettingsMain extends Fragment {
|
|||
private View.OnLongClickListener getOnLongClickListener(TabViewHolder holder) {
|
||||
return (view) -> {
|
||||
int position = holder.getAdapterPosition();
|
||||
usedTabs.remove(position);
|
||||
selectedTabs.remove(position);
|
||||
notifyItemRemoved(position);
|
||||
saveChanges();
|
||||
return false;
|
||||
|
|
@ -261,13 +250,13 @@ public class ContentSettingsMain extends Fragment {
|
|||
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source,
|
||||
RecyclerView.ViewHolder target) {
|
||||
if (source.getItemViewType() != target.getItemViewType() ||
|
||||
usedAdapter == null) {
|
||||
selectedTabsAdapter == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final int sourceIndex = source.getAdapterPosition();
|
||||
final int targetIndex = target.getAdapterPosition();
|
||||
usedAdapter.swapItems(sourceIndex, targetIndex);
|
||||
selectedTabsAdapter.swapItems(sourceIndex, targetIndex);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -1,123 +0,0 @@
|
|||
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.CardView;
|
||||
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 org.schabi.newpipe.database.subscription.SubscriptionEntity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ContentSettingsMainDialog extends DialogFragment {
|
||||
|
||||
|
||||
public AllAdapter allAdapter;
|
||||
|
||||
public String[] allTabs = new String[7];
|
||||
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.dialog_contentsettingsadd, container);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View rootView, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(rootView, savedInstanceState);
|
||||
|
||||
tabNames();
|
||||
|
||||
RecyclerView allTabs = rootView.findViewById(R.id.allTabs);
|
||||
allTabs.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
allAdapter = new AllAdapter();
|
||||
allTabs.setAdapter(allAdapter);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public interface OnAddListener {
|
||||
void onAddListener(int position);
|
||||
}
|
||||
ContentSettingsMainDialog.OnAddListener onAddListener = null;
|
||||
public void setOnAddListener(ContentSettingsMainDialog.OnAddListener listener) {
|
||||
onAddListener = listener;
|
||||
}
|
||||
|
||||
private void addTab(int position) {
|
||||
if(onAddListener != null) {
|
||||
onAddListener.onAddListener(position);
|
||||
}
|
||||
dismiss();
|
||||
}
|
||||
|
||||
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;
|
||||
View view;
|
||||
|
||||
public TabViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
|
||||
text = itemView.findViewById(R.id.tabName);
|
||||
view = itemView.findViewById(R.id.layoutCard);
|
||||
}
|
||||
|
||||
void bind(int position) {
|
||||
text.setText(allTabs[position]);
|
||||
((CardView) view).setCardElevation(0);
|
||||
view.setOnClickListener(v -> {
|
||||
addTab(position);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue