Final declarations, naming & redundant code

* add final declarations where missing
* fix typo "onSelectedLisener" to "onSelectedListener"
* rename "baseEqual" to "baseEquals"
* replace getPlaylistsObserver code with functions pointers
* remove duplicate code in constructors
* remove useless null checks
This commit is contained in:
Roy Yosef 2020-05-03 00:14:31 +03:00 committed by Stypox
parent dfc27b2480
commit 13c0fdef08
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
7 changed files with 64 additions and 82 deletions

View File

@ -64,7 +64,7 @@ public class SelectChannelFragment extends DialogFragment {
private final ImageLoader imageLoader = ImageLoader.getInstance();
private OnSelectedLisener onSelectedLisener = null;
private OnSelectedListener onSelectedListener = null;
private OnCancelListener onCancelListener = null;
private ProgressBar progressBar;
@ -73,8 +73,8 @@ public class SelectChannelFragment extends DialogFragment {
private List<SubscriptionEntity> subscriptions = new Vector<>();
public void setOnSelectedLisener(final OnSelectedLisener listener) {
onSelectedLisener = listener;
public void setOnSelectedListener(final OnSelectedListener listener) {
onSelectedListener = listener;
}
public void setOnCancelListener(final OnCancelListener listener) {
@ -129,9 +129,9 @@ public class SelectChannelFragment extends DialogFragment {
}
private void clickedItem(final int position) {
if (onSelectedLisener != null) {
if (onSelectedListener != null) {
SubscriptionEntity entry = subscriptions.get(position);
onSelectedLisener
onSelectedListener
.onChannelSelected(entry.getServiceId(), entry.getUrl(), entry.getName());
}
dismiss();
@ -186,7 +186,7 @@ public class SelectChannelFragment extends DialogFragment {
// Interfaces
//////////////////////////////////////////////////////////////////////////*/
public interface OnSelectedLisener {
public interface OnSelectedListener {
void onChannelSelected(int serviceId, String url, String name);
}

View File

@ -52,11 +52,11 @@ public class SelectKioskFragment extends DialogFragment {
private RecyclerView recyclerView = null;
private SelectKioskAdapter selectKioskAdapter = null;
private OnSelectedLisener onSelectedLisener = null;
private OnSelectedListener onSelectedListener = null;
private OnCancelListener onCancelListener = null;
public void setOnSelectedLisener(final OnSelectedLisener listener) {
onSelectedLisener = listener;
public void setOnSelectedListener(final OnSelectedListener listener) {
onSelectedListener = listener;
}
public void setOnCancelListener(final OnCancelListener listener) {
@ -102,8 +102,8 @@ public class SelectKioskFragment extends DialogFragment {
}
private void clickedItem(final SelectKioskAdapter.Entry entry) {
if (onSelectedLisener != null) {
onSelectedLisener.onKioskSelected(entry.serviceId, entry.kioskId, entry.kioskName);
if (onSelectedListener != null) {
onSelectedListener.onKioskSelected(entry.serviceId, entry.kioskId, entry.kioskName);
}
dismiss();
}
@ -122,7 +122,7 @@ public class SelectKioskFragment extends DialogFragment {
// Interfaces
//////////////////////////////////////////////////////////////////////////*/
public interface OnSelectedLisener {
public interface OnSelectedListener {
void onKioskSelected(int serviceId, String kioskId, String kioskName);
}

View File

@ -35,10 +35,7 @@ import java.util.List;
import java.util.Vector;
import io.reactivex.Flowable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class SelectPlaylistFragment extends DialogFragment {
/**
@ -49,17 +46,18 @@ public class SelectPlaylistFragment extends DialogFragment {
private final ImageLoader imageLoader = ImageLoader.getInstance();
private OnSelectedLisener onSelectedLisener = null;
private OnSelectedListener onSelectedListener = null;
private OnCancelListener onCancelListener = null;
private ProgressBar progressBar;
private TextView emptyView;
private RecyclerView recyclerView;
private Disposable playlistsSubscriber;
private List<PlaylistLocalItem> playlists = new Vector<>();
public void setOnSelectedLisener(final OnSelectedLisener listener) {
onSelectedLisener = listener;
public void setOnSelectedListener(final OnSelectedListener listener) {
onSelectedListener = listener;
}
public void setOnCancelListener(final OnCancelListener listener) {
@ -67,13 +65,14 @@ public class SelectPlaylistFragment extends DialogFragment {
}
/*//////////////////////////////////////////////////////////////////////////
// Init
// Fragment's Lifecycle
//////////////////////////////////////////////////////////////////////////*/
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.select_playlist_fragment, container, false);
final View v =
inflater.inflate(R.layout.select_playlist_fragment, container, false);
recyclerView = v.findViewById(R.id.items_list);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
SelectPlaylistAdapter playlistAdapter = new SelectPlaylistAdapter();
@ -86,19 +85,26 @@ public class SelectPlaylistFragment extends DialogFragment {
emptyView.setVisibility(View.GONE);
final AppDatabase database = NewPipeDatabase.getInstance(this.getContext());
LocalPlaylistManager localPlaylistManager = new LocalPlaylistManager(database);
RemotePlaylistManager remotePlaylistManager = new RemotePlaylistManager(database);
final LocalPlaylistManager localPlaylistManager = new LocalPlaylistManager(database);
final RemotePlaylistManager remotePlaylistManager = new RemotePlaylistManager(database);
Flowable.combineLatest(localPlaylistManager.getPlaylists(),
playlistsSubscriber = Flowable.combineLatest(localPlaylistManager.getPlaylists(),
remotePlaylistManager.getPlaylists(), PlaylistItemsUtils::merge)
.toObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getPlaylistsObserver());
.subscribe(this::displayPlaylists, this::onError);
return v;
}
@Override
public void onDestroy() {
super.onDestroy();
if (playlistsSubscriber != null) {
playlistsSubscriber.dispose();
playlistsSubscriber = null;
}
}
/*//////////////////////////////////////////////////////////////////////////
// Handle actions
//////////////////////////////////////////////////////////////////////////*/
@ -112,17 +118,17 @@ public class SelectPlaylistFragment extends DialogFragment {
}
private void clickedItem(final int position) {
if (onSelectedLisener != null) {
LocalItem selectedItem = playlists.get(position);
if (onSelectedListener != null) {
final LocalItem selectedItem = playlists.get(position);
if (selectedItem instanceof PlaylistMetadataEntry) {
final PlaylistMetadataEntry entry = ((PlaylistMetadataEntry) selectedItem);
onSelectedLisener
onSelectedListener
.onLocalPlaylistSelected(entry.uid, entry.name);
} else if (selectedItem instanceof PlaylistRemoteEntity) {
final PlaylistRemoteEntity entry = ((PlaylistRemoteEntity) selectedItem);
onSelectedLisener.onRemotePlaylistSelected(
onSelectedListener.onRemotePlaylistSelected(
entry.getServiceId(), entry.getUrl(), entry.getName());
}
}
@ -144,26 +150,6 @@ public class SelectPlaylistFragment extends DialogFragment {
}
private Observer<List<PlaylistLocalItem>> getPlaylistsObserver() {
return new Observer<List<PlaylistLocalItem>>() {
@Override
public void onSubscribe(final Disposable d) { }
@Override
public void onNext(final List<PlaylistLocalItem> newPlaylists) {
displayPlaylists(newPlaylists);
}
@Override
public void onError(final Throwable exception) {
SelectPlaylistFragment.this.onError(exception);
}
@Override
public void onComplete() { }
};
}
/*//////////////////////////////////////////////////////////////////////////
// Error
//////////////////////////////////////////////////////////////////////////*/
@ -178,7 +164,7 @@ public class SelectPlaylistFragment extends DialogFragment {
// Interfaces
//////////////////////////////////////////////////////////////////////////*/
public interface OnSelectedLisener {
public interface OnSelectedListener {
void onLocalPlaylistSelected(long id, String name);
void onRemotePlaylistSelected(int serviceId, String url, String name);
}
@ -192,14 +178,14 @@ public class SelectPlaylistFragment extends DialogFragment {
@Override
public SelectPlaylistItemHolder onCreateViewHolder(final ViewGroup parent,
final int viewType) {
View item = LayoutInflater.from(parent.getContext())
final View item = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_playlist_mini_item, parent, false);
return new SelectPlaylistItemHolder(item);
}
@Override
public void onBindViewHolder(final SelectPlaylistItemHolder holder, final int position) {
PlaylistLocalItem selectedItem = playlists.get(position);
final PlaylistLocalItem selectedItem = playlists.get(position);
if (selectedItem instanceof PlaylistMetadataEntry) {
final PlaylistMetadataEntry entry = ((PlaylistMetadataEntry) selectedItem);

View File

@ -49,7 +49,7 @@ public class ChooseTabsFragment extends Fragment {
private TabsManager tabsManager;
private List<Tab> tabList = new ArrayList<>();
private final List<Tab> tabList = new ArrayList<>();
private ChooseTabsFragment.SelectedTabsAdapter selectedTabsAdapter;
/*//////////////////////////////////////////////////////////////////////////
@ -79,10 +79,10 @@ public class ChooseTabsFragment extends Fragment {
initButton(rootView);
RecyclerView listSelectedTabs = rootView.findViewById(R.id.selectedTabs);
final RecyclerView listSelectedTabs = rootView.findViewById(R.id.selectedTabs);
listSelectedTabs.setLayoutManager(new LinearLayoutManager(requireContext()));
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(getItemTouchCallback());
final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(getItemTouchCallback());
itemTouchHelper.attachToRecyclerView(listSelectedTabs);
selectedTabsAdapter = new SelectedTabsAdapter(requireContext(), itemTouchHelper);
@ -139,7 +139,7 @@ public class ChooseTabsFragment extends Fragment {
private void updateTitle() {
if (getActivity() instanceof AppCompatActivity) {
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
final ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(R.string.main_page_content);
}
@ -202,20 +202,20 @@ public class ChooseTabsFragment extends Fragment {
switch (type) {
case KIOSK:
SelectKioskFragment selectKioskFragment = new SelectKioskFragment();
selectKioskFragment.setOnSelectedLisener((serviceId, kioskId, kioskName) ->
selectKioskFragment.setOnSelectedListener((serviceId, kioskId, kioskName) ->
addTab(new Tab.KioskTab(serviceId, kioskId)));
selectKioskFragment.show(requireFragmentManager(), "select_kiosk");
return;
case CHANNEL:
SelectChannelFragment selectChannelFragment = new SelectChannelFragment();
selectChannelFragment.setOnSelectedLisener((serviceId, url, name) ->
selectChannelFragment.setOnSelectedListener((serviceId, url, name) ->
addTab(new Tab.ChannelTab(serviceId, url, name)));
selectChannelFragment.show(requireFragmentManager(), "select_channel");
return;
case PLAYLIST:
SelectPlaylistFragment selectPlaylistFragment = new SelectPlaylistFragment();
selectPlaylistFragment.setOnSelectedLisener(
new SelectPlaylistFragment.OnSelectedLisener() {
selectPlaylistFragment.setOnSelectedListener(
new SelectPlaylistFragment.OnSelectedListener() {
@Override
public void onLocalPlaylistSelected(final long id, final String name) {
addTab(new Tab.PlaylistTab(id, name));
@ -360,7 +360,7 @@ public class ChooseTabsFragment extends Fragment {
@Override
public ChooseTabsFragment.SelectedTabsAdapter.TabViewHolder onCreateViewHolder(
@NonNull final ViewGroup parent, final int viewType) {
View view = inflater.inflate(R.layout.list_choose_tabs, parent, false);
final View view = inflater.inflate(R.layout.list_choose_tabs, parent, false);
return new ChooseTabsFragment.SelectedTabsAdapter.TabViewHolder(view);
}
@ -417,8 +417,8 @@ public class ChooseTabsFragment extends Fragment {
.getChannelServiceId()) + "/" + tab.getTabName(requireContext());
break;
case PLAYLIST:
int serviceId = ((Tab.PlaylistTab) tab).getPlaylistServiceId();
String serviceName = serviceId == -1
final int serviceId = ((Tab.PlaylistTab) tab).getPlaylistServiceId();
final String serviceName = serviceId == -1
? getString(R.string.local)
: NewPipe.getNameOfService(serviceId);
tabName = serviceName + "/" + tab.getTabName(requireContext());

View File

@ -504,11 +504,7 @@ public abstract class Tab {
private LocalItemType playlistType;
private PlaylistTab() {
this.playlistName = "<no-name>";
this.playlistId = -1;
this.playlistType = LocalItemType.PLAYLIST_LOCAL_ITEM;
this.playlistServiceId = -1;
this.playlistUrl = "<no-url>";
this(-1, "<no-name>");
}
public PlaylistTab(final long playlistId, final String playlistName) {
@ -545,18 +541,16 @@ public abstract class Tab {
@DrawableRes
@Override
public int getTabIconRes(final Context context) {
return ThemeHelper.resolveResourceIdFromAttr(context, R.attr.ic_list);
return ThemeHelper.resolveResourceIdFromAttr(context, R.attr.ic_bookmark);
}
@Override
public Fragment getFragment(final Context context) {
if (playlistType == LocalItemType.PLAYLIST_LOCAL_ITEM) {
return LocalPlaylistFragment.getInstance(playlistId,
playlistName == null ? "" : playlistName);
return LocalPlaylistFragment.getInstance(playlistId, playlistName);
} else { // playlistType == LocalItemType.PLAYLIST_REMOTE_ITEM
return PlaylistFragment.getInstance(playlistServiceId, playlistUrl,
playlistName == null ? "" : playlistName);
return PlaylistFragment.getInstance(playlistServiceId, playlistUrl, playlistName);
}
}
@ -583,16 +577,16 @@ public abstract class Tab {
@Override
public boolean equals(final Object obj) {
boolean baseEqual = super.equals(obj)
final boolean baseEquals = super.equals(obj)
&& Objects.equals(playlistType, ((PlaylistTab) obj).playlistType)
&& Objects.equals(playlistName, ((PlaylistTab) obj).playlistName);
if (!baseEqual) {
if (!baseEquals) {
return false;
}
boolean localPlaylistEquals = playlistId == ((PlaylistTab) obj).playlistId;
boolean remotePlaylistEquals =
final boolean localPlaylistEquals = playlistId == ((PlaylistTab) obj).playlistId;
final boolean remotePlaylistEquals =
playlistServiceId == ((PlaylistTab) obj).playlistServiceId
&& Objects.equals(playlistUrl, ((PlaylistTab) obj).playlistUrl);

View File

@ -14,7 +14,7 @@ public final class PlaylistItemsUtils {
public static List<PlaylistLocalItem> merge(
final List<PlaylistMetadataEntry> localPlaylists,
final List<PlaylistRemoteEntity> remotePlaylists) {
List<PlaylistLocalItem> items = new ArrayList<>(
final List<PlaylistLocalItem> items = new ArrayList<>(
localPlaylists.size() + remotePlaylists.size());
items.addAll(localPlaylists);
items.addAll(remotePlaylists);

View File

@ -23,7 +23,9 @@
android:id="@+id/items_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/list_playlist_mini_item"></androidx.recyclerview.widget.RecyclerView>
tools:listitem="@layout/list_playlist_mini_item">
</androidx.recyclerview.widget.RecyclerView>
<TextView
@ -40,4 +42,4 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"/>
</LinearLayout>
</LinearLayout>