2017-09-26 15:29:38 +00:00
|
|
|
package org.schabi.newpipe.settings;
|
|
|
|
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2022-02-23 20:11:25 +00:00
|
|
|
import android.widget.ImageView;
|
2017-09-26 15:29:38 +00:00
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2020-04-01 19:21:11 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2020-03-31 17:20:15 +00:00
|
|
|
import androidx.fragment.app.DialogFragment;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2017-09-26 15:29:38 +00:00
|
|
|
import org.schabi.newpipe.R;
|
|
|
|
import org.schabi.newpipe.database.subscription.SubscriptionEntity;
|
2021-12-01 08:43:24 +00:00
|
|
|
import org.schabi.newpipe.error.ErrorUtil;
|
2020-12-11 13:55:47 +00:00
|
|
|
import org.schabi.newpipe.local.subscription.SubscriptionManager;
|
2021-03-27 13:37:44 +00:00
|
|
|
import org.schabi.newpipe.util.PicassoHelper;
|
2020-04-01 19:21:11 +00:00
|
|
|
import org.schabi.newpipe.util.ThemeHelper;
|
2017-09-26 15:29:38 +00:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Vector;
|
|
|
|
|
2020-10-31 20:55:45 +00:00
|
|
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
2021-01-15 16:11:04 +00:00
|
|
|
import io.reactivex.rxjava3.core.Observer;
|
2020-10-31 20:55:45 +00:00
|
|
|
import io.reactivex.rxjava3.disposables.Disposable;
|
|
|
|
import io.reactivex.rxjava3.schedulers.Schedulers;
|
2017-09-26 15:29:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Christian Schabesberger on 26.09.17.
|
|
|
|
* SelectChannelFragment.java is part of NewPipe.
|
2020-03-31 17:20:15 +00:00
|
|
|
* <p>
|
2017-09-26 15:29:38 +00:00
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2020-03-31 17:20:15 +00:00
|
|
|
* </p>
|
|
|
|
* <p>
|
2017-09-26 15:29:38 +00:00
|
|
|
* NewPipe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2020-03-31 17:20:15 +00:00
|
|
|
* </p>
|
|
|
|
* <p>
|
2017-09-26 15:29:38 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
2020-03-31 17:20:15 +00:00
|
|
|
* </p>
|
2017-09-26 15:29:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
public class SelectChannelFragment extends DialogFragment {
|
|
|
|
|
2020-05-02 21:14:31 +00:00
|
|
|
private OnSelectedListener onSelectedListener = null;
|
2020-04-02 11:51:10 +00:00
|
|
|
private OnCancelListener onCancelListener = null;
|
|
|
|
|
|
|
|
private ProgressBar progressBar;
|
2020-03-31 17:20:15 +00:00
|
|
|
private TextView emptyView;
|
|
|
|
private RecyclerView recyclerView;
|
2020-04-02 11:51:10 +00:00
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
private List<SubscriptionEntity> subscriptions = new Vector<>();
|
2017-09-28 13:36:15 +00:00
|
|
|
|
2020-05-02 21:14:31 +00:00
|
|
|
public void setOnSelectedListener(final OnSelectedListener listener) {
|
|
|
|
onSelectedListener = listener;
|
2017-09-26 15:29:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
public void setOnCancelListener(final OnCancelListener listener) {
|
2017-09-28 13:36:15 +00:00
|
|
|
onCancelListener = listener;
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:10 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Init
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2020-04-01 19:21:11 +00:00
|
|
|
@Override
|
2020-04-06 19:07:16 +00:00
|
|
|
public void onCreate(@Nullable final Bundle savedInstanceState) {
|
2020-04-01 19:21:11 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setStyle(STYLE_NO_TITLE, ThemeHelper.getMinWidthDialogTheme(requireContext()));
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:29:38 +00:00
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container,
|
|
|
|
final Bundle savedInstanceState) {
|
2020-08-16 08:24:58 +00:00
|
|
|
final View v = inflater.inflate(R.layout.select_channel_fragment, container, false);
|
2018-08-28 17:17:14 +00:00
|
|
|
recyclerView = v.findViewById(R.id.items_list);
|
2017-09-26 15:29:38 +00:00
|
|
|
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
2020-08-16 08:24:58 +00:00
|
|
|
final SelectChannelAdapter channelAdapter = new SelectChannelAdapter();
|
2017-09-26 15:29:38 +00:00
|
|
|
recyclerView.setAdapter(channelAdapter);
|
|
|
|
|
|
|
|
progressBar = v.findViewById(R.id.progressBar);
|
|
|
|
emptyView = v.findViewById(R.id.empty_state_view);
|
|
|
|
progressBar.setVisibility(View.VISIBLE);
|
|
|
|
recyclerView.setVisibility(View.GONE);
|
|
|
|
emptyView.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
|
2020-12-11 13:55:47 +00:00
|
|
|
final SubscriptionManager subscriptionManager = new SubscriptionManager(requireContext());
|
2019-04-28 20:43:54 +00:00
|
|
|
subscriptionManager.subscriptions().toObservable()
|
2017-09-26 15:29:38 +00:00
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(getSubscriptionObserver());
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2017-09-28 13:36:15 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
2020-04-02 11:51:10 +00:00
|
|
|
// Handle actions
|
2017-09-28 13:36:15 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
2020-12-11 13:55:47 +00:00
|
|
|
public void onCancel(@NonNull final DialogInterface dialogInterface) {
|
2017-09-28 13:36:15 +00:00
|
|
|
super.onCancel(dialogInterface);
|
2020-03-31 17:20:15 +00:00
|
|
|
if (onCancelListener != null) {
|
2017-09-28 13:36:15 +00:00
|
|
|
onCancelListener.onCancel();
|
|
|
|
}
|
2017-09-26 15:29:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
private void clickedItem(final int position) {
|
2020-05-02 21:14:31 +00:00
|
|
|
if (onSelectedListener != null) {
|
2020-08-16 08:24:58 +00:00
|
|
|
final SubscriptionEntity entry = subscriptions.get(position);
|
2020-05-02 21:14:31 +00:00
|
|
|
onSelectedListener
|
2020-03-31 17:20:15 +00:00
|
|
|
.onChannelSelected(entry.getServiceId(), entry.getUrl(), entry.getName());
|
2017-09-26 15:29:38 +00:00
|
|
|
}
|
|
|
|
dismiss();
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:10 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Item handling
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
private void displayChannels(final List<SubscriptionEntity> newSubscriptions) {
|
|
|
|
this.subscriptions = newSubscriptions;
|
2017-09-28 13:36:15 +00:00
|
|
|
progressBar.setVisibility(View.GONE);
|
2020-03-31 17:20:15 +00:00
|
|
|
if (newSubscriptions.isEmpty()) {
|
2017-09-28 13:36:15 +00:00
|
|
|
emptyView.setVisibility(View.VISIBLE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
recyclerView.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:29:38 +00:00
|
|
|
private Observer<List<SubscriptionEntity>> getSubscriptionObserver() {
|
|
|
|
return new Observer<List<SubscriptionEntity>>() {
|
|
|
|
@Override
|
2020-12-11 13:55:47 +00:00
|
|
|
public void onSubscribe(@NonNull final Disposable disposable) { }
|
2017-09-26 15:29:38 +00:00
|
|
|
|
|
|
|
@Override
|
2020-12-11 13:55:47 +00:00
|
|
|
public void onNext(@NonNull final List<SubscriptionEntity> newSubscriptions) {
|
2020-03-31 17:20:15 +00:00
|
|
|
displayChannels(newSubscriptions);
|
2017-09-26 15:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-12-11 13:55:47 +00:00
|
|
|
public void onError(@NonNull final Throwable exception) {
|
2021-12-01 08:43:24 +00:00
|
|
|
ErrorUtil.showUiErrorSnackbar(SelectChannelFragment.this,
|
2020-12-11 14:07:18 +00:00
|
|
|
"Loading subscription", exception);
|
2017-09-26 15:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void onComplete() { }
|
2017-09-26 15:29:38 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:10 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Interfaces
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2020-05-02 21:14:31 +00:00
|
|
|
public interface OnSelectedListener {
|
2020-03-31 17:20:15 +00:00
|
|
|
void onChannelSelected(int serviceId, String url, String name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnCancelListener {
|
|
|
|
void onCancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
private class SelectChannelAdapter
|
|
|
|
extends RecyclerView.Adapter<SelectChannelAdapter.SelectChannelItemHolder> {
|
2020-12-11 13:55:47 +00:00
|
|
|
@NonNull
|
2017-09-26 15:29:38 +00:00
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public SelectChannelItemHolder onCreateViewHolder(final ViewGroup parent,
|
|
|
|
final int viewType) {
|
2020-08-16 08:24:58 +00:00
|
|
|
final View item = LayoutInflater.from(parent.getContext())
|
2017-09-26 15:29:38 +00:00
|
|
|
.inflate(R.layout.select_channel_item, parent, false);
|
|
|
|
return new SelectChannelItemHolder(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void onBindViewHolder(final SelectChannelItemHolder holder, final int position) {
|
2020-08-16 08:24:58 +00:00
|
|
|
final SubscriptionEntity entry = subscriptions.get(position);
|
2017-09-26 15:29:38 +00:00
|
|
|
holder.titleView.setText(entry.getName());
|
2020-08-27 20:56:41 +00:00
|
|
|
holder.view.setOnClickListener(view -> clickedItem(position));
|
2021-03-27 13:37:44 +00:00
|
|
|
PicassoHelper.loadAvatar(entry.getAvatarUrl()).into(holder.thumbnailView);
|
2017-09-26 15:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return subscriptions.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public class SelectChannelItemHolder extends RecyclerView.ViewHolder {
|
2020-03-31 17:20:15 +00:00
|
|
|
public final View view;
|
2022-02-23 20:11:25 +00:00
|
|
|
final ImageView thumbnailView;
|
2020-04-02 11:51:10 +00:00
|
|
|
final TextView titleView;
|
2020-03-31 17:20:15 +00:00
|
|
|
SelectChannelItemHolder(final View v) {
|
2017-09-26 15:29:38 +00:00
|
|
|
super(v);
|
|
|
|
this.view = v;
|
|
|
|
thumbnailView = v.findViewById(R.id.itemThumbnailView);
|
|
|
|
titleView = v.findViewById(R.id.itemTitleView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|