2017-09-03 08:04:18 +02:00
|
|
|
package org.schabi.newpipe.fragments.list.channel;
|
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
import static org.schabi.newpipe.ktx.TextViewUtils.animateTextColor;
|
|
|
|
import static org.schabi.newpipe.ktx.ViewUtils.animate;
|
|
|
|
import static org.schabi.newpipe.ktx.ViewUtils.animateBackgroundColor;
|
|
|
|
|
2022-10-23 11:28:34 +02:00
|
|
|
import android.content.Context;
|
2022-10-23 21:13:43 +02:00
|
|
|
import android.content.SharedPreferences;
|
2023-04-13 00:00:23 +02:00
|
|
|
import android.graphics.Color;
|
2017-09-03 08:04:18 +02:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
2023-04-13 00:00:23 +02:00
|
|
|
import android.util.TypedValue;
|
2017-09-03 08:04:18 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2023-04-13 00:00:23 +02:00
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import androidx.core.graphics.ColorUtils;
|
2022-10-23 21:13:43 +02:00
|
|
|
import androidx.preference.PreferenceManager;
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
import com.google.android.material.snackbar.Snackbar;
|
2022-10-23 21:28:54 +02:00
|
|
|
import com.google.android.material.tabs.TabLayout;
|
2023-04-13 00:00:23 +02:00
|
|
|
import com.jakewharton.rxbinding4.view.RxView;
|
2022-10-23 21:28:54 +02:00
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
import org.schabi.newpipe.R;
|
2022-10-23 11:28:34 +02:00
|
|
|
import org.schabi.newpipe.database.subscription.NotificationMode;
|
|
|
|
import org.schabi.newpipe.database.subscription.SubscriptionEntity;
|
2020-11-03 06:26:29 +01:00
|
|
|
import org.schabi.newpipe.databinding.FragmentChannelBinding;
|
2020-12-11 14:55:47 +01:00
|
|
|
import org.schabi.newpipe.error.ErrorInfo;
|
2023-04-13 00:00:23 +02:00
|
|
|
import org.schabi.newpipe.error.ErrorUtil;
|
2020-12-11 14:55:47 +01:00
|
|
|
import org.schabi.newpipe.error.UserAction;
|
2017-09-03 08:04:18 +02:00
|
|
|
import org.schabi.newpipe.extractor.channel.ChannelInfo;
|
2022-10-23 15:37:40 +02:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ContentNotSupportedException;
|
2022-11-05 00:23:03 +01:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
|
2022-10-23 10:27:35 +02:00
|
|
|
import org.schabi.newpipe.fragments.BaseStateFragment;
|
|
|
|
import org.schabi.newpipe.fragments.detail.TabAdapter;
|
2023-04-13 00:00:23 +02:00
|
|
|
import org.schabi.newpipe.ktx.AnimationType;
|
2022-10-23 11:28:34 +02:00
|
|
|
import org.schabi.newpipe.local.feed.notifications.NotificationHelper;
|
|
|
|
import org.schabi.newpipe.local.subscription.SubscriptionManager;
|
2022-11-05 00:23:03 +01:00
|
|
|
import org.schabi.newpipe.util.ChannelTabHelper;
|
2022-10-23 10:27:35 +02:00
|
|
|
import org.schabi.newpipe.util.Constants;
|
2017-09-03 08:04:18 +02:00
|
|
|
import org.schabi.newpipe.util.ExtractorHelper;
|
2023-04-13 00:00:23 +02:00
|
|
|
import org.schabi.newpipe.util.Localization;
|
2017-11-04 19:30:01 +01:00
|
|
|
import org.schabi.newpipe.util.NavigationHelper;
|
2023-04-13 00:00:23 +02:00
|
|
|
import org.schabi.newpipe.util.PicassoHelper;
|
2022-10-25 09:01:11 +02:00
|
|
|
import org.schabi.newpipe.util.StateSaver;
|
2023-04-13 00:00:23 +02:00
|
|
|
import org.schabi.newpipe.util.ThemeHelper;
|
2022-01-16 17:08:13 +01:00
|
|
|
import org.schabi.newpipe.util.external_communication.ShareUtils;
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2022-10-23 11:28:34 +02:00
|
|
|
import java.util.List;
|
2022-10-25 09:01:11 +02:00
|
|
|
import java.util.Queue;
|
2023-04-13 00:00:23 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2022-10-23 11:28:34 +02:00
|
|
|
|
2022-10-23 10:27:35 +02:00
|
|
|
import icepick.State;
|
2020-10-31 21:55:45 +01:00
|
|
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
2022-10-23 11:28:34 +02:00
|
|
|
import io.reactivex.rxjava3.core.Observable;
|
|
|
|
import io.reactivex.rxjava3.disposables.CompositeDisposable;
|
2020-10-31 21:55:45 +01:00
|
|
|
import io.reactivex.rxjava3.disposables.Disposable;
|
2023-04-13 00:00:23 +02:00
|
|
|
import io.reactivex.rxjava3.functions.Action;
|
2022-10-23 11:28:34 +02:00
|
|
|
import io.reactivex.rxjava3.functions.Consumer;
|
2023-04-13 00:00:23 +02:00
|
|
|
import io.reactivex.rxjava3.functions.Function;
|
2020-10-31 21:55:45 +01:00
|
|
|
import io.reactivex.rxjava3.schedulers.Schedulers;
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2022-10-25 09:01:11 +02:00
|
|
|
public class ChannelFragment extends BaseStateFragment<ChannelInfo>
|
|
|
|
implements StateSaver.WriteRead {
|
2023-04-13 00:00:23 +02:00
|
|
|
|
|
|
|
private static final int BUTTON_DEBOUNCE_INTERVAL = 100;
|
|
|
|
private static final String PICASSO_CHANNEL_TAG = "PICASSO_CHANNEL_TAG";
|
|
|
|
|
2022-10-23 10:27:35 +02:00
|
|
|
@State
|
|
|
|
protected int serviceId = Constants.NO_SERVICE_ID;
|
|
|
|
@State
|
|
|
|
protected String name;
|
|
|
|
@State
|
|
|
|
protected String url;
|
2021-03-27 14:37:44 +01:00
|
|
|
|
2022-10-23 10:27:35 +02:00
|
|
|
private ChannelInfo currentInfo;
|
|
|
|
private Disposable currentWorker;
|
2022-10-23 11:28:34 +02:00
|
|
|
private final CompositeDisposable disposables = new CompositeDisposable();
|
2023-04-13 00:00:23 +02:00
|
|
|
private Disposable subscribeButtonMonitor;
|
2022-10-23 11:28:34 +02:00
|
|
|
private SubscriptionManager subscriptionManager;
|
2022-10-23 21:28:54 +02:00
|
|
|
private int lastTab;
|
2023-04-13 00:00:23 +02:00
|
|
|
private boolean channelContentNotSupported = false;
|
2022-04-04 17:58:39 +02:00
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Views
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2022-10-23 10:27:35 +02:00
|
|
|
private FragmentChannelBinding binding;
|
|
|
|
private TabAdapter tabAdapter;
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
private MenuItem menuRssButton;
|
|
|
|
private MenuItem menuNotifyButton;
|
2023-04-13 21:55:37 +02:00
|
|
|
private SubscriptionEntity channelSubscription;
|
2023-04-13 00:00:23 +02:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
public static ChannelFragment getInstance(final int serviceId, final String url,
|
|
|
|
final String name) {
|
2020-08-16 10:24:58 +02:00
|
|
|
final ChannelFragment instance = new ChannelFragment();
|
2018-07-10 16:26:42 +02:00
|
|
|
instance.setInitialData(serviceId, url, name);
|
2017-09-03 08:04:18 +02:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
private void setInitialData(final int sid, final String u, final String title) {
|
2022-10-23 10:27:35 +02:00
|
|
|
this.serviceId = sid;
|
|
|
|
this.url = u;
|
|
|
|
this.name = !TextUtils.isEmpty(title) ? title : "";
|
2017-10-26 20:45:16 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
|
2020-04-02 13:51:10 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// LifeCycle
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
@Override
|
2022-10-23 10:27:35 +02:00
|
|
|
public void onCreate(final Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setHasOptionsMenu(true);
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2022-10-23 11:28:34 +02:00
|
|
|
@Override
|
2022-10-23 21:28:54 +02:00
|
|
|
public void onAttach(final @NonNull Context context) {
|
2022-10-23 11:28:34 +02:00
|
|
|
super.onAttach(context);
|
|
|
|
subscriptionManager = new SubscriptionManager(activity);
|
|
|
|
}
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public View onCreateView(@NonNull final LayoutInflater inflater,
|
|
|
|
@Nullable final ViewGroup container,
|
|
|
|
@Nullable final Bundle savedInstanceState) {
|
2022-10-23 10:27:35 +02:00
|
|
|
binding = FragmentChannelBinding.inflate(inflater, container, false);
|
|
|
|
return binding.getRoot();
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2022-10-23 10:27:35 +02:00
|
|
|
@Override // called from onViewCreated in {@link BaseFragment#onViewCreated}
|
|
|
|
protected void initViews(final View rootView, final Bundle savedInstanceState) {
|
|
|
|
super.initViews(rootView, savedInstanceState);
|
|
|
|
|
|
|
|
tabAdapter = new TabAdapter(getChildFragmentManager());
|
|
|
|
binding.viewPager.setAdapter(tabAdapter);
|
|
|
|
binding.tabLayout.setupWithViewPager(binding.viewPager);
|
2023-04-13 00:00:23 +02:00
|
|
|
|
|
|
|
binding.channelTitleView.setText(name);
|
2020-04-07 15:39:06 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
@Override
|
2023-04-13 00:00:23 +02:00
|
|
|
protected void initListeners() {
|
|
|
|
super.initListeners();
|
|
|
|
|
|
|
|
final View.OnClickListener openSubChannel = v -> {
|
|
|
|
if (!TextUtils.isEmpty(currentInfo.getParentChannelUrl())) {
|
|
|
|
try {
|
|
|
|
NavigationHelper.openChannelFragment(getFM(), currentInfo.getServiceId(),
|
|
|
|
currentInfo.getParentChannelUrl(),
|
|
|
|
currentInfo.getParentChannelName());
|
|
|
|
} catch (final Exception e) {
|
|
|
|
ErrorUtil.showUiErrorSnackbar(this, "Opening channel fragment", e);
|
|
|
|
}
|
|
|
|
} else if (DEBUG) {
|
|
|
|
Log.i(TAG, "Can't open parent channel because we got no channel URL");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
binding.subChannelAvatarView.setOnClickListener(openSubChannel);
|
|
|
|
binding.subChannelTitleView.setOnClickListener(openSubChannel);
|
2022-11-22 02:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-09-03 08:04:18 +02:00
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2022-10-23 11:28:34 +02:00
|
|
|
if (currentWorker != null) {
|
|
|
|
currentWorker.dispose();
|
|
|
|
}
|
|
|
|
disposables.clear();
|
2022-10-23 10:27:35 +02:00
|
|
|
binding = null;
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
2017-09-03 08:04:18 +02:00
|
|
|
// Menu
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
2021-06-20 00:29:18 +02:00
|
|
|
public void onCreateOptionsMenu(@NonNull final Menu menu,
|
|
|
|
@NonNull final MenuInflater inflater) {
|
2017-09-03 08:04:18 +02:00
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
2022-10-23 10:27:35 +02:00
|
|
|
inflater.inflate(R.menu.menu_channel, menu);
|
|
|
|
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "onCreateOptionsMenu() called with: "
|
|
|
|
+ "menu = [" + menu + "], inflater = [" + inflater + "]");
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
2023-04-13 21:55:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPrepareOptionsMenu(final @NonNull Menu menu) {
|
|
|
|
super.onPrepareOptionsMenu(menu);
|
2022-10-23 10:27:35 +02:00
|
|
|
menuRssButton = menu.findItem(R.id.menu_item_rss);
|
2022-10-23 11:28:34 +02:00
|
|
|
menuNotifyButton = menu.findItem(R.id.menu_item_notify);
|
2023-04-13 21:55:37 +02:00
|
|
|
updateNotifyButton(channelSubscription);
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public boolean onOptionsItemSelected(final MenuItem item) {
|
2017-09-03 08:04:18 +02:00
|
|
|
switch (item.getItemId()) {
|
2022-10-23 11:28:34 +02:00
|
|
|
case R.id.menu_item_notify:
|
|
|
|
final boolean value = !item.isChecked();
|
|
|
|
item.setEnabled(false);
|
|
|
|
setNotify(value);
|
|
|
|
break;
|
2020-02-22 10:36:10 +01:00
|
|
|
case R.id.action_settings:
|
|
|
|
NavigationHelper.openSettings(requireContext());
|
|
|
|
break;
|
2017-11-01 16:26:44 +01:00
|
|
|
case R.id.menu_item_rss:
|
2022-01-15 23:55:19 +01:00
|
|
|
if (currentInfo != null) {
|
2023-02-23 23:38:20 +01:00
|
|
|
ShareUtils.openUrlInApp(requireContext(), currentInfo.getFeedUrl());
|
2022-01-15 23:55:19 +01:00
|
|
|
}
|
2017-11-01 16:26:44 +01:00
|
|
|
break;
|
|
|
|
case R.id.menu_item_openInBrowser:
|
2019-12-02 21:34:52 +01:00
|
|
|
if (currentInfo != null) {
|
2020-02-22 10:36:10 +01:00
|
|
|
ShareUtils.openUrlInBrowser(requireContext(), currentInfo.getOriginalUrl());
|
2019-12-02 21:34:52 +01:00
|
|
|
}
|
2017-11-01 16:26:44 +01:00
|
|
|
break;
|
2018-01-29 08:01:06 +01:00
|
|
|
case R.id.menu_item_share:
|
2019-12-02 21:34:52 +01:00
|
|
|
if (currentInfo != null) {
|
2021-03-20 16:35:14 +01:00
|
|
|
ShareUtils.shareText(requireContext(), name, currentInfo.getOriginalUrl(),
|
|
|
|
currentInfo.getAvatarUrl());
|
2019-12-02 21:34:52 +01:00
|
|
|
}
|
2017-11-01 16:26:44 +01:00
|
|
|
break;
|
2017-09-03 08:04:18 +02:00
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2017-11-01 16:26:44 +01:00
|
|
|
return true;
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Channel Subscription
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
private void monitorSubscription(final ChannelInfo info) {
|
|
|
|
final Consumer<Throwable> onError = (Throwable throwable) -> {
|
|
|
|
animate(binding.channelSubscribeButton, false, 100);
|
|
|
|
showSnackBarError(new ErrorInfo(throwable, UserAction.SUBSCRIPTION_GET,
|
|
|
|
"Get subscription status", currentInfo));
|
|
|
|
};
|
|
|
|
|
|
|
|
final Observable<List<SubscriptionEntity>> observable = subscriptionManager
|
|
|
|
.subscriptionTable()
|
|
|
|
.getSubscriptionFlowable(info.getServiceId(), info.getUrl())
|
|
|
|
.toObservable();
|
|
|
|
|
|
|
|
disposables.add(observable
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(getSubscribeUpdateMonitor(info), onError));
|
|
|
|
|
|
|
|
disposables.add(observable
|
|
|
|
.map(List::isEmpty)
|
|
|
|
.distinctUntilChanged()
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(isEmpty -> updateSubscribeButton(!isEmpty), onError));
|
|
|
|
|
|
|
|
disposables.add(observable
|
|
|
|
.map(List::isEmpty)
|
|
|
|
.distinctUntilChanged()
|
|
|
|
.skip(1) // channel has just been opened
|
|
|
|
.filter(x -> NotificationHelper.areNewStreamsNotificationsEnabled(requireContext()))
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(isEmpty -> {
|
|
|
|
if (!isEmpty) {
|
|
|
|
showNotifySnackbar();
|
|
|
|
}
|
|
|
|
}, onError));
|
|
|
|
}
|
|
|
|
|
2023-04-14 10:19:58 +02:00
|
|
|
private Function<Object, Object> mapOnSubscribe(final SubscriptionEntity subscription) {
|
2023-04-13 00:00:23 +02:00
|
|
|
return (@NonNull Object o) -> {
|
2023-04-14 10:19:58 +02:00
|
|
|
subscriptionManager.insertSubscription(subscription);
|
2023-04-13 00:00:23 +02:00
|
|
|
return o;
|
|
|
|
};
|
2022-10-23 10:27:35 +02:00
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
private Function<Object, Object> mapOnUnsubscribe(final SubscriptionEntity subscription) {
|
|
|
|
return (@NonNull Object o) -> {
|
|
|
|
subscriptionManager.deleteSubscription(subscription);
|
|
|
|
return o;
|
|
|
|
};
|
|
|
|
}
|
2022-10-23 11:28:34 +02:00
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
private void updateSubscription(final ChannelInfo info) {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "updateSubscription() called with: info = [" + info + "]");
|
2022-10-23 11:28:34 +02:00
|
|
|
}
|
2023-04-13 00:00:23 +02:00
|
|
|
final Action onComplete = () -> {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "Updated subscription: " + info.getUrl());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
final Consumer<Throwable> onError = (@NonNull Throwable throwable) ->
|
|
|
|
showSnackBarError(new ErrorInfo(throwable, UserAction.SUBSCRIPTION_UPDATE,
|
|
|
|
"Updating subscription for " + info.getUrl(), info));
|
|
|
|
|
|
|
|
disposables.add(subscriptionManager.updateChannelInfo(info)
|
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(onComplete, onError));
|
|
|
|
}
|
|
|
|
|
|
|
|
private Disposable monitorSubscribeButton(final Function<Object, Object> action) {
|
|
|
|
final Consumer<Object> onNext = (@NonNull Object o) -> {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "Changed subscription status to this channel!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
final Consumer<Throwable> onError = (@NonNull Throwable throwable) ->
|
|
|
|
showSnackBarError(new ErrorInfo(throwable, UserAction.SUBSCRIPTION_CHANGE,
|
|
|
|
"Changing subscription for " + currentInfo.getUrl(), currentInfo));
|
|
|
|
|
|
|
|
/* Emit clicks from main thread unto io thread */
|
|
|
|
return RxView.clicks(binding.channelSubscribeButton)
|
|
|
|
.subscribeOn(AndroidSchedulers.mainThread())
|
|
|
|
.observeOn(Schedulers.io())
|
|
|
|
.debounce(BUTTON_DEBOUNCE_INTERVAL, TimeUnit.MILLISECONDS) // Ignore rapid clicks
|
|
|
|
.map(action)
|
|
|
|
.subscribe(onNext, onError);
|
2022-10-23 11:28:34 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
private Consumer<List<SubscriptionEntity>> getSubscribeUpdateMonitor(final ChannelInfo info) {
|
2022-10-23 11:28:34 +02:00
|
|
|
return (List<SubscriptionEntity> subscriptionEntities) -> {
|
2023-04-13 00:00:23 +02:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "subscriptionManager.subscriptionTable.doOnNext() called with: "
|
|
|
|
+ "subscriptionEntities = [" + subscriptionEntities + "]");
|
|
|
|
}
|
|
|
|
if (subscribeButtonMonitor != null) {
|
|
|
|
subscribeButtonMonitor.dispose();
|
|
|
|
}
|
|
|
|
|
2022-10-23 11:28:34 +02:00
|
|
|
if (subscriptionEntities.isEmpty()) {
|
2023-04-13 00:00:23 +02:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "No subscription to this channel!");
|
|
|
|
}
|
|
|
|
final SubscriptionEntity channel = new SubscriptionEntity();
|
|
|
|
channel.setServiceId(info.getServiceId());
|
|
|
|
channel.setUrl(info.getUrl());
|
|
|
|
channel.setData(info.getName(),
|
|
|
|
info.getAvatarUrl(),
|
|
|
|
info.getDescription(),
|
|
|
|
info.getSubscriberCount());
|
2023-04-13 21:55:37 +02:00
|
|
|
channelSubscription = null;
|
2022-10-23 11:28:34 +02:00
|
|
|
updateNotifyButton(null);
|
2023-04-14 10:19:58 +02:00
|
|
|
subscribeButtonMonitor = monitorSubscribeButton(mapOnSubscribe(channel));
|
2022-10-23 11:28:34 +02:00
|
|
|
} else {
|
2023-04-13 00:00:23 +02:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "Found subscription to this channel!");
|
|
|
|
}
|
2023-04-13 21:55:37 +02:00
|
|
|
channelSubscription = subscriptionEntities.get(0);
|
|
|
|
updateNotifyButton(channelSubscription);
|
|
|
|
subscribeButtonMonitor =
|
|
|
|
monitorSubscribeButton(mapOnUnsubscribe(channelSubscription));
|
2022-10-23 11:28:34 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
private void updateSubscribeButton(final boolean isSubscribed) {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "updateSubscribeButton() called with: "
|
|
|
|
+ "isSubscribed = [" + isSubscribed + "]");
|
|
|
|
}
|
|
|
|
|
|
|
|
final boolean isButtonVisible = binding.channelSubscribeButton.getVisibility()
|
|
|
|
== View.VISIBLE;
|
|
|
|
final int backgroundDuration = isButtonVisible ? 300 : 0;
|
|
|
|
final int textDuration = isButtonVisible ? 200 : 0;
|
|
|
|
|
|
|
|
final int subscribedBackground = ContextCompat
|
|
|
|
.getColor(activity, R.color.subscribed_background_color);
|
|
|
|
final int subscribedText = ContextCompat.getColor(activity, R.color.subscribed_text_color);
|
|
|
|
final int subscribeBackground = ColorUtils.blendARGB(ThemeHelper
|
|
|
|
.resolveColorFromAttr(activity, R.attr.colorPrimary), subscribedBackground, 0.35f);
|
|
|
|
final int subscribeText = ContextCompat.getColor(activity, R.color.subscribe_text_color);
|
|
|
|
|
|
|
|
if (isSubscribed) {
|
|
|
|
binding.channelSubscribeButton.setText(R.string.subscribed_button_title);
|
|
|
|
animateBackgroundColor(binding.channelSubscribeButton, backgroundDuration,
|
|
|
|
subscribeBackground, subscribedBackground);
|
|
|
|
animateTextColor(binding.channelSubscribeButton, textDuration, subscribeText,
|
|
|
|
subscribedText);
|
|
|
|
} else {
|
|
|
|
binding.channelSubscribeButton.setText(R.string.subscribe_button_title);
|
|
|
|
animateBackgroundColor(binding.channelSubscribeButton, backgroundDuration,
|
|
|
|
subscribedBackground, subscribeBackground);
|
|
|
|
animateTextColor(binding.channelSubscribeButton, textDuration, subscribedText,
|
|
|
|
subscribeText);
|
|
|
|
}
|
|
|
|
|
|
|
|
animate(binding.channelSubscribeButton, true, 100, AnimationType.LIGHT_SCALE_AND_ALPHA);
|
|
|
|
}
|
|
|
|
|
2022-10-23 11:28:34 +02:00
|
|
|
private void updateNotifyButton(@Nullable final SubscriptionEntity subscription) {
|
|
|
|
if (menuNotifyButton == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (subscription != null) {
|
|
|
|
menuNotifyButton.setEnabled(
|
|
|
|
NotificationHelper.areNewStreamsNotificationsEnabled(requireContext())
|
|
|
|
);
|
|
|
|
menuNotifyButton.setChecked(
|
|
|
|
subscription.getNotificationMode() == NotificationMode.ENABLED
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
menuNotifyButton.setVisible(subscription != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setNotify(final boolean isEnabled) {
|
|
|
|
disposables.add(
|
|
|
|
subscriptionManager
|
|
|
|
.updateNotificationMode(
|
|
|
|
currentInfo.getServiceId(),
|
|
|
|
currentInfo.getUrl(),
|
|
|
|
isEnabled ? NotificationMode.ENABLED : NotificationMode.DISABLED)
|
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
/**
|
|
|
|
* Show a snackbar with the option to enable notifications on new streams for this channel.
|
|
|
|
*/
|
|
|
|
private void showNotifySnackbar() {
|
|
|
|
Snackbar.make(binding.getRoot(), R.string.you_successfully_subscribed, Snackbar.LENGTH_LONG)
|
|
|
|
.setAction(R.string.get_notified, v -> setNotify(true))
|
|
|
|
.setActionTextColor(Color.YELLOW)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-23 10:27:35 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Init
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2022-10-23 10:27:35 +02:00
|
|
|
private void updateTabs() {
|
|
|
|
tabAdapter.clearAllItems();
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
if (currentInfo != null && !channelContentNotSupported) {
|
|
|
|
final Context context = requireContext();
|
|
|
|
final SharedPreferences preferences = PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(context);
|
2022-10-23 15:37:40 +02:00
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
for (final ListLinkHandler linkHandler : currentInfo.getTabs()) {
|
|
|
|
final String tab = linkHandler.getContentFilters().get(0);
|
|
|
|
if (ChannelTabHelper.showChannelTab(context, preferences, tab)) {
|
2023-04-14 10:27:25 +02:00
|
|
|
final ChannelTabFragment channelTabFragment =
|
|
|
|
ChannelTabFragment.getInstance(serviceId, linkHandler, name);
|
|
|
|
channelTabFragment.useAsFrontPage(useAsFrontPage);
|
|
|
|
tabAdapter.addFragment(channelTabFragment,
|
2023-04-13 00:00:23 +02:00
|
|
|
context.getString(ChannelTabHelper.getTranslationKey(tab)));
|
2022-10-23 15:37:40 +02:00
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
2023-04-13 00:00:23 +02:00
|
|
|
|
|
|
|
final String description = currentInfo.getDescription();
|
|
|
|
if (description != null && !description.isEmpty()
|
|
|
|
&& ChannelTabHelper.showChannelTab(
|
|
|
|
context, preferences, R.string.show_channel_tabs_about)) {
|
|
|
|
tabAdapter.addFragment(
|
|
|
|
ChannelAboutFragment.getInstance(currentInfo),
|
|
|
|
context.getString(R.string.channel_tab_about));
|
|
|
|
}
|
2020-03-31 19:20:15 +02:00
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2022-10-23 10:27:35 +02:00
|
|
|
tabAdapter.notifyDataSetUpdate();
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2022-10-23 10:27:35 +02:00
|
|
|
for (int i = 0; i < tabAdapter.getCount(); i++) {
|
|
|
|
binding.tabLayout.getTabAt(i).setText(tabAdapter.getItemTitle(i));
|
2019-05-08 19:17:54 +02:00
|
|
|
}
|
2022-10-23 21:28:54 +02:00
|
|
|
|
|
|
|
// Restore previously selected tab
|
|
|
|
final TabLayout.Tab ltab = binding.tabLayout.getTabAt(lastTab);
|
|
|
|
if (ltab != null) {
|
|
|
|
binding.tabLayout.selectTab(ltab);
|
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
|
2022-10-25 09:01:11 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// State Saving
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String generateSuffix() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeTo(final Queue<Object> objectsToSave) {
|
|
|
|
objectsToSave.add(currentInfo);
|
2023-04-13 00:00:23 +02:00
|
|
|
objectsToSave.add(binding == null ? 0 : binding.tabLayout.getSelectedTabPosition());
|
2022-10-25 09:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFrom(@NonNull final Queue<Object> savedObjects) {
|
|
|
|
currentInfo = (ChannelInfo) savedObjects.poll();
|
|
|
|
lastTab = (Integer) savedObjects.poll();
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState(final @NonNull Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
if (binding != null) {
|
|
|
|
outState.putInt("LastTab", binding.tabLayout.getSelectedTabPosition());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) {
|
|
|
|
super.onRestoreInstanceState(savedInstanceState);
|
|
|
|
lastTab = savedInstanceState.getInt("LastTab", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Contract
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2022-10-25 09:01:11 +02:00
|
|
|
@Override
|
|
|
|
protected void doInitialLoadLogic() {
|
|
|
|
if (currentInfo == null) {
|
|
|
|
startLoading(false);
|
|
|
|
} else {
|
|
|
|
handleResult(currentInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
@Override
|
2022-10-23 10:27:35 +02:00
|
|
|
public void startLoading(final boolean forceLoad) {
|
|
|
|
super.startLoading(forceLoad);
|
2020-04-13 22:40:58 +02:00
|
|
|
|
2022-10-23 10:27:35 +02:00
|
|
|
currentInfo = null;
|
|
|
|
updateTabs();
|
|
|
|
if (currentWorker != null) {
|
|
|
|
currentWorker.dispose();
|
2020-04-13 22:40:58 +02:00
|
|
|
}
|
|
|
|
|
2022-10-23 10:27:35 +02:00
|
|
|
runWorker(forceLoad);
|
2020-04-13 22:40:58 +02:00
|
|
|
}
|
|
|
|
|
2022-10-23 10:27:35 +02:00
|
|
|
private void runWorker(final boolean forceLoad) {
|
|
|
|
currentWorker = ExtractorHelper.getChannelInfo(serviceId, url, forceLoad)
|
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(result -> {
|
|
|
|
isLoading.set(false);
|
|
|
|
handleResult(result);
|
|
|
|
}, throwable -> showError(new ErrorInfo(throwable, UserAction.REQUESTED_STREAM,
|
|
|
|
url == null ? "no url" : url, serviceId)));
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
@Override
|
|
|
|
public void showLoading() {
|
|
|
|
super.showLoading();
|
|
|
|
PicassoHelper.cancelTag(PICASSO_CHANNEL_TAG);
|
|
|
|
animate(binding.channelSubscribeButton, false, 100);
|
|
|
|
}
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
@Override
|
2022-10-23 15:37:40 +02:00
|
|
|
public void handleResult(@NonNull final ChannelInfo result) {
|
|
|
|
super.handleResult(result);
|
|
|
|
currentInfo = result;
|
|
|
|
setInitialData(result.getServiceId(), result.getOriginalUrl(), result.getName());
|
2018-01-29 08:01:06 +01:00
|
|
|
|
2023-04-13 00:00:23 +02:00
|
|
|
binding.getRoot().setVisibility(View.VISIBLE);
|
|
|
|
PicassoHelper.loadBanner(result.getBannerUrl()).tag(PICASSO_CHANNEL_TAG)
|
|
|
|
.into(binding.channelBannerImage);
|
|
|
|
PicassoHelper.loadAvatar(result.getAvatarUrl()).tag(PICASSO_CHANNEL_TAG)
|
|
|
|
.into(binding.channelAvatarView);
|
|
|
|
PicassoHelper.loadAvatar(result.getParentChannelAvatarUrl()).tag(PICASSO_CHANNEL_TAG)
|
|
|
|
.into(binding.subChannelAvatarView);
|
|
|
|
|
|
|
|
binding.channelTitleView.setText(result.getName());
|
|
|
|
binding.channelSubscriberView.setVisibility(View.VISIBLE);
|
|
|
|
if (result.getSubscriberCount() >= 0) {
|
|
|
|
binding.channelSubscriberView.setText(Localization
|
|
|
|
.shortSubscriberCount(activity, result.getSubscriberCount()));
|
|
|
|
} else {
|
|
|
|
binding.channelSubscriberView.setText(R.string.subscribers_count_not_available);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(currentInfo.getParentChannelName())) {
|
|
|
|
binding.subChannelTitleView.setText(String.format(
|
|
|
|
getString(R.string.channel_created_by),
|
|
|
|
currentInfo.getParentChannelName())
|
|
|
|
);
|
|
|
|
binding.subChannelTitleView.setVisibility(View.VISIBLE);
|
|
|
|
binding.subChannelAvatarView.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (menuRssButton != null) {
|
|
|
|
menuRssButton.setVisible(!TextUtils.isEmpty(result.getFeedUrl()));
|
|
|
|
}
|
|
|
|
|
|
|
|
channelContentNotSupported = false;
|
|
|
|
for (final Throwable throwable : result.getErrors()) {
|
|
|
|
if (throwable instanceof ContentNotSupportedException) {
|
|
|
|
channelContentNotSupported = true;
|
|
|
|
showContentNotSupportedIfNeeded();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disposables.clear();
|
|
|
|
if (subscribeButtonMonitor != null) {
|
|
|
|
subscribeButtonMonitor.dispose();
|
|
|
|
}
|
|
|
|
|
2022-10-23 10:27:35 +02:00
|
|
|
updateTabs();
|
2023-04-13 00:00:23 +02:00
|
|
|
updateSubscription(result);
|
|
|
|
monitorSubscription(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showContentNotSupportedIfNeeded() {
|
|
|
|
// channelBinding might not be initialized when handleResult() is called
|
|
|
|
// (e.g. after rotating the screen, #6696)
|
|
|
|
if (!channelContentNotSupported || binding == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
binding.errorContentNotSupported.setVisibility(View.VISIBLE);
|
|
|
|
binding.channelKaomoji.setText("(︶︹︺)");
|
|
|
|
binding.channelKaomoji.setTextSize(TypedValue.COMPLEX_UNIT_SP, 45f);
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
}
|