|
| 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.NewPipeDatabase; |
| 19 | +import org.schabi.newpipe.R; |
| 20 | +import org.schabi.newpipe.database.AppDatabase; |
| 21 | +import org.schabi.newpipe.database.feed.model.FeedGroupEntity; |
| 22 | +import org.schabi.newpipe.error.ErrorUtil; |
| 23 | +import org.schabi.newpipe.util.ThemeHelper; |
| 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<FeedGroupEntity> feedGroups = 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_feed_group_fragment, container, false); |
| 87 | + recyclerView = v.findViewById(R.id.items_list); |
| 88 | + recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); |
| 89 | + final SelectFeedGroupAdapter feedGroupAdapter = new SelectFeedGroupAdapter(); |
| 90 | + recyclerView.setAdapter(feedGroupAdapter); |
| 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 AppDatabase database = NewPipeDatabase.getInstance(requireContext()); |
| 100 | + database.feedGroupDAO().getAll().toObservable() |
| 101 | + .subscribeOn(Schedulers.io()) |
| 102 | + .observeOn(AndroidSchedulers.mainThread()) |
| 103 | + .subscribe(getFeedGroupObserver()); |
| 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 FeedGroupEntity entry = feedGroups.get(position); |
| 123 | + onSelectedListener |
| 124 | + .onFeedGroupSelected(entry.getUid(), entry.getName(), |
| 125 | + entry.getIcon().getDrawableResource()); |
| 126 | + } |
| 127 | + dismiss(); |
| 128 | + } |
| 129 | + |
| 130 | + /*////////////////////////////////////////////////////////////////////////// |
| 131 | + // Item handling |
| 132 | + //////////////////////////////////////////////////////////////////////////*/ |
| 133 | + |
| 134 | + private void displayFeedGroups(final List<FeedGroupEntity> newFeedGroups) { |
| 135 | + this.feedGroups = newFeedGroups; |
| 136 | + progressBar.setVisibility(View.GONE); |
| 137 | + if (newFeedGroups.isEmpty()) { |
| 138 | + emptyView.setVisibility(View.VISIBLE); |
| 139 | + return; |
| 140 | + } |
| 141 | + recyclerView.setVisibility(View.VISIBLE); |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + private Observer<List<FeedGroupEntity>> getFeedGroupObserver() { |
| 146 | + return new Observer<List<FeedGroupEntity>>() { |
| 147 | + @Override |
| 148 | + public void onSubscribe(@NonNull final Disposable disposable) { } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void onNext(@NonNull final List<FeedGroupEntity> newGroups) { |
| 152 | + displayFeedGroups(newGroups); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void onError(@NonNull final Throwable exception) { |
| 157 | + ErrorUtil.showUiErrorSnackbar(SelectFeedGroupFragment.this, |
| 158 | + "Loading Feed Groups", exception); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void onComplete() { } |
| 163 | + }; |
| 164 | + } |
| 165 | + |
| 166 | + /*////////////////////////////////////////////////////////////////////////// |
| 167 | + // Interfaces |
| 168 | + //////////////////////////////////////////////////////////////////////////*/ |
| 169 | + |
| 170 | + public interface OnSelectedListener { |
| 171 | + void onFeedGroupSelected(Long groupId, String name, int icon); |
| 172 | + } |
| 173 | + |
| 174 | + public interface OnCancelListener { |
| 175 | + void onCancel(); |
| 176 | + } |
| 177 | + |
| 178 | + private class SelectFeedGroupAdapter |
| 179 | + extends RecyclerView.Adapter<SelectFeedGroupAdapter.SelectFeedGroupItemHolder> { |
| 180 | + @NonNull |
| 181 | + @Override |
| 182 | + public SelectFeedGroupItemHolder onCreateViewHolder(final ViewGroup parent, |
| 183 | + final int viewType) { |
| 184 | + final View item = LayoutInflater.from(parent.getContext()) |
| 185 | + .inflate(R.layout.select_feed_group_item, parent, false); |
| 186 | + return new SelectFeedGroupItemHolder(item); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public void onBindViewHolder(final SelectFeedGroupItemHolder holder, final int position) { |
| 191 | + final FeedGroupEntity entry = feedGroups.get(position); |
| 192 | + holder.titleView.setText(entry.getName()); |
| 193 | + holder.view.setOnClickListener(view -> clickedItem(position)); |
| 194 | + holder.thumbnailView.setImageResource(entry.getIcon().getDrawableResource()); |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public int getItemCount() { |
| 199 | + return feedGroups.size(); |
| 200 | + } |
| 201 | + |
| 202 | + public class SelectFeedGroupItemHolder extends RecyclerView.ViewHolder { |
| 203 | + public final View view; |
| 204 | + final ImageView thumbnailView; |
| 205 | + final TextView titleView; |
| 206 | + SelectFeedGroupItemHolder(final View v) { |
| 207 | + super(v); |
| 208 | + this.view = v; |
| 209 | + thumbnailView = v.findViewById(R.id.itemThumbnailView); |
| 210 | + titleView = v.findViewById(R.id.itemTitleView); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments