Skip to content

Commit 86869f0

Browse files
committed
Copied SelectFeedGroupFragment from SelectChannelFragment
1 parent c9155f7 commit 86869f0

1 file changed

Lines changed: 213 additions & 0 deletions

File tree

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
package org.schabi.newpipe.settings;
2+
3+
import android.content.DialogInterface;
4+
import android.os.Bundle;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.ImageView;
9+
import android.widget.ProgressBar;
10+
import android.widget.TextView;
11+
12+
import androidx.annotation.NonNull;
13+
import androidx.annotation.Nullable;
14+
import androidx.fragment.app.DialogFragment;
15+
import androidx.recyclerview.widget.LinearLayoutManager;
16+
import androidx.recyclerview.widget.RecyclerView;
17+
18+
import org.schabi.newpipe.R;
19+
import org.schabi.newpipe.database.subscription.SubscriptionEntity;
20+
import org.schabi.newpipe.error.ErrorUtil;
21+
import org.schabi.newpipe.local.subscription.SubscriptionManager;
22+
import org.schabi.newpipe.util.ThemeHelper;
23+
import org.schabi.newpipe.util.image.PicassoHelper;
24+
25+
import java.util.List;
26+
import java.util.Vector;
27+
28+
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
29+
import io.reactivex.rxjava3.core.Observer;
30+
import io.reactivex.rxjava3.disposables.Disposable;
31+
import io.reactivex.rxjava3.schedulers.Schedulers;
32+
33+
/**
34+
* Created by Christian Schabesberger on 26.09.17.
35+
* SelectChannelFragment.java is part of NewPipe.
36+
* <p>
37+
* NewPipe is free software: you can redistribute it and/or modify
38+
* it under the terms of the GNU General Public License as published by
39+
* the Free Software Foundation, either version 3 of the License, or
40+
* (at your option) any later version.
41+
* </p>
42+
* <p>
43+
* NewPipe is distributed in the hope that it will be useful,
44+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
45+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46+
* GNU General Public License for more details.
47+
* </p>
48+
* <p>
49+
* You should have received a copy of the GNU General Public License
50+
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
51+
* </p>
52+
*/
53+
54+
public class SelectFeedGroupFragment extends DialogFragment {
55+
56+
private OnSelectedListener onSelectedListener = null;
57+
private OnCancelListener onCancelListener = null;
58+
59+
private ProgressBar progressBar;
60+
private TextView emptyView;
61+
private RecyclerView recyclerView;
62+
63+
private List<SubscriptionEntity> subscriptions = new Vector<>();
64+
65+
public void setOnSelectedListener(final OnSelectedListener listener) {
66+
onSelectedListener = listener;
67+
}
68+
69+
public void setOnCancelListener(final OnCancelListener listener) {
70+
onCancelListener = listener;
71+
}
72+
73+
/*//////////////////////////////////////////////////////////////////////////
74+
// Init
75+
//////////////////////////////////////////////////////////////////////////*/
76+
77+
@Override
78+
public void onCreate(@Nullable final Bundle savedInstanceState) {
79+
super.onCreate(savedInstanceState);
80+
setStyle(STYLE_NO_TITLE, ThemeHelper.getMinWidthDialogTheme(requireContext()));
81+
}
82+
83+
@Override
84+
public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container,
85+
final Bundle savedInstanceState) {
86+
final View v = inflater.inflate(R.layout.select_channel_fragment, container, false);
87+
recyclerView = v.findViewById(R.id.items_list);
88+
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
89+
final SelectChannelAdapter channelAdapter = new SelectChannelAdapter();
90+
recyclerView.setAdapter(channelAdapter);
91+
92+
progressBar = v.findViewById(R.id.progressBar);
93+
emptyView = v.findViewById(R.id.empty_state_view);
94+
progressBar.setVisibility(View.VISIBLE);
95+
recyclerView.setVisibility(View.GONE);
96+
emptyView.setVisibility(View.GONE);
97+
98+
99+
final SubscriptionManager subscriptionManager = new SubscriptionManager(requireContext());
100+
subscriptionManager.subscriptions().toObservable()
101+
.subscribeOn(Schedulers.io())
102+
.observeOn(AndroidSchedulers.mainThread())
103+
.subscribe(getSubscriptionObserver());
104+
105+
return v;
106+
}
107+
108+
/*//////////////////////////////////////////////////////////////////////////
109+
// Handle actions
110+
//////////////////////////////////////////////////////////////////////////*/
111+
112+
@Override
113+
public void onCancel(@NonNull final DialogInterface dialogInterface) {
114+
super.onCancel(dialogInterface);
115+
if (onCancelListener != null) {
116+
onCancelListener.onCancel();
117+
}
118+
}
119+
120+
private void clickedItem(final int position) {
121+
if (onSelectedListener != null) {
122+
final SubscriptionEntity entry = subscriptions.get(position);
123+
onSelectedListener
124+
.onChannelSelected(entry.getServiceId(), entry.getUrl(), entry.getName());
125+
}
126+
dismiss();
127+
}
128+
129+
/*//////////////////////////////////////////////////////////////////////////
130+
// Item handling
131+
//////////////////////////////////////////////////////////////////////////*/
132+
133+
private void displayChannels(final List<SubscriptionEntity> newSubscriptions) {
134+
this.subscriptions = newSubscriptions;
135+
progressBar.setVisibility(View.GONE);
136+
if (newSubscriptions.isEmpty()) {
137+
emptyView.setVisibility(View.VISIBLE);
138+
return;
139+
}
140+
recyclerView.setVisibility(View.VISIBLE);
141+
142+
}
143+
144+
private Observer<List<SubscriptionEntity>> getSubscriptionObserver() {
145+
return new Observer<List<SubscriptionEntity>>() {
146+
@Override
147+
public void onSubscribe(@NonNull final Disposable disposable) { }
148+
149+
@Override
150+
public void onNext(@NonNull final List<SubscriptionEntity> newSubscriptions) {
151+
displayChannels(newSubscriptions);
152+
}
153+
154+
@Override
155+
public void onError(@NonNull final Throwable exception) {
156+
ErrorUtil.showUiErrorSnackbar(SelectFeedGroupFragment.this,
157+
"Loading subscription", exception);
158+
}
159+
160+
@Override
161+
public void onComplete() { }
162+
};
163+
}
164+
165+
/*//////////////////////////////////////////////////////////////////////////
166+
// Interfaces
167+
//////////////////////////////////////////////////////////////////////////*/
168+
169+
public interface OnSelectedListener {
170+
void onChannelSelected(int serviceId, String url, String name);
171+
}
172+
173+
public interface OnCancelListener {
174+
void onCancel();
175+
}
176+
177+
private class SelectChannelAdapter
178+
extends RecyclerView.Adapter<SelectChannelAdapter.SelectChannelItemHolder> {
179+
@NonNull
180+
@Override
181+
public SelectChannelItemHolder onCreateViewHolder(final ViewGroup parent,
182+
final int viewType) {
183+
final View item = LayoutInflater.from(parent.getContext())
184+
.inflate(R.layout.select_channel_item, parent, false);
185+
return new SelectChannelItemHolder(item);
186+
}
187+
188+
@Override
189+
public void onBindViewHolder(final SelectChannelItemHolder holder, final int position) {
190+
final SubscriptionEntity entry = subscriptions.get(position);
191+
holder.titleView.setText(entry.getName());
192+
holder.view.setOnClickListener(view -> clickedItem(position));
193+
PicassoHelper.loadAvatar(entry.getAvatarUrl()).into(holder.thumbnailView);
194+
}
195+
196+
@Override
197+
public int getItemCount() {
198+
return subscriptions.size();
199+
}
200+
201+
public class SelectChannelItemHolder extends RecyclerView.ViewHolder {
202+
public final View view;
203+
final ImageView thumbnailView;
204+
final TextView titleView;
205+
SelectChannelItemHolder(final View v) {
206+
super(v);
207+
this.view = v;
208+
thumbnailView = v.findViewById(R.id.itemThumbnailView);
209+
titleView = v.findViewById(R.id.itemTitleView);
210+
}
211+
}
212+
}
213+
}

0 commit comments

Comments
 (0)