NewPipe/app/src/main/java/org/schabi/newpipe/fragments/list/kiosk/KioskFragment.java

164 lines
6.0 KiB
Java
Raw Normal View History

package org.schabi.newpipe.fragments.list.kiosk;
2017-09-23 17:39:04 +02:00
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
2017-09-23 17:39:04 +02:00
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
2017-09-23 17:39:04 +02:00
import org.schabi.newpipe.R;
import org.schabi.newpipe.error.ErrorInfo;
import org.schabi.newpipe.error.UserAction;
2017-09-23 17:39:04 +02:00
import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.kiosk.KioskInfo;
2018-07-15 21:21:09 +02:00
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.localization.ContentCountry;
2017-09-23 17:39:04 +02:00
import org.schabi.newpipe.fragments.list.BaseListInfoFragment;
import org.schabi.newpipe.util.ExtractorHelper;
import org.schabi.newpipe.util.KioskTranslator;
import org.schabi.newpipe.util.Localization;
2017-09-23 17:39:04 +02:00
import icepick.State;
2020-10-31 21:55:45 +01:00
import io.reactivex.rxjava3.core.Single;
2017-09-23 17:39:04 +02:00
/**
* Created by Christian Schabesberger on 23.09.17.
* <p>
2017-09-23 17:39:04 +02:00
* Copyright (C) Christian Schabesberger 2017 <chris.schabesberger@mailbox.org>
* KioskFragment.java is part of NewPipe.
* </p>
* <p>
2017-09-23 17:39:04 +02: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.
* </p>
* <p>
2017-09-23 17:39:04 +02: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.
* </p>
* <p>
2017-09-23 17:39:04 +02:00
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
* </p>
2017-09-23 17:39:04 +02:00
*/
public class KioskFragment extends BaseListInfoFragment<KioskInfo> {
@State
String kioskId = "";
String kioskTranslatedName;
@State
ContentCountry contentCountry;
2018-06-11 11:01:18 +02:00
2017-09-23 17:39:04 +02:00
/*//////////////////////////////////////////////////////////////////////////
// Views
//////////////////////////////////////////////////////////////////////////*/
public static KioskFragment getInstance(final int serviceId) throws ExtractionException {
return getInstance(serviceId, NewPipe.getService(serviceId)
.getKioskList().getDefaultKioskId());
}
public static KioskFragment getInstance(final int serviceId, final String kioskId)
throws ExtractionException {
2020-08-16 10:24:58 +02:00
final KioskFragment instance = new KioskFragment();
final StreamingService service = NewPipe.getService(serviceId);
final ListLinkHandlerFactory kioskLinkHandlerFactory = service.getKioskList()
2018-07-15 21:21:09 +02:00
.getListLinkHandlerFactoryByType(kioskId);
2017-09-23 17:39:04 +02:00
instance.setInitialData(serviceId,
2018-07-15 21:21:09 +02:00
kioskLinkHandlerFactory.fromId(kioskId).getUrl(), kioskId);
2017-10-26 15:16:35 +02:00
instance.kioskId = kioskId;
2017-09-23 17:39:04 +02:00
return instance;
}
public KioskFragment() {
super(UserAction.REQUESTED_KIOSK);
}
2017-09-23 17:39:04 +02:00
/*//////////////////////////////////////////////////////////////////////////
// LifeCycle
//////////////////////////////////////////////////////////////////////////*/
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
kioskTranslatedName = KioskTranslator.getTranslatedKioskName(kioskId, activity);
name = kioskTranslatedName;
contentCountry = Localization.getPreferredContentCountry(requireContext());
}
2017-10-26 15:16:35 +02:00
@Override
2021-10-16 21:33:45 +02:00
public void onResume() {
super.onResume();
if (!Localization.getPreferredContentCountry(requireContext()).equals(contentCountry)) {
reloadContent();
}
if (useAsFrontPage && activity != null) {
2017-10-26 15:16:35 +02:00
try {
setTitle(kioskTranslatedName);
2020-08-16 10:24:58 +02:00
} catch (final Exception e) {
showSnackBarError(new ErrorInfo(e, UserAction.UI_ERROR, "Setting kiosk title"));
2017-10-26 15:16:35 +02:00
}
}
}
2017-09-23 17:39:04 +02:00
@Override
public View onCreateView(@NonNull final LayoutInflater inflater,
@Nullable final ViewGroup container,
@Nullable final Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_kiosk, container, false);
2017-09-23 17:39:04 +02:00
}
/*//////////////////////////////////////////////////////////////////////////
// Menu
//////////////////////////////////////////////////////////////////////////*/
@Override
public void onCreateOptionsMenu(@NonNull final Menu menu,
@NonNull final MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
2020-08-16 10:24:58 +02:00
final ActionBar supportActionBar = activity.getSupportActionBar();
2017-09-26 18:16:39 +02:00
if (supportActionBar != null && useAsFrontPage) {
supportActionBar.setDisplayHomeAsUpEnabled(false);
}
}
2017-09-23 17:39:04 +02:00
/*//////////////////////////////////////////////////////////////////////////
// Load and handle
//////////////////////////////////////////////////////////////////////////*/
@Override
public Single<KioskInfo> loadResult(final boolean forceReload) {
contentCountry = Localization.getPreferredContentCountry(requireContext());
return ExtractorHelper.getKioskInfo(serviceId, url, forceReload);
2017-09-23 17:39:04 +02:00
}
@Override
public Single<ListExtractor.InfoItemsPage> loadMoreItemsLogic() {
return ExtractorHelper.getMoreKioskItems(serviceId, url, currentNextPage);
2017-09-23 17:39:04 +02:00
}
/*//////////////////////////////////////////////////////////////////////////
// Contract
//////////////////////////////////////////////////////////////////////////*/
@Override
public void handleResult(@NonNull final KioskInfo result) {
super.handleResult(result);
name = kioskTranslatedName;
2019-03-10 22:38:30 +01:00
setTitle(kioskTranslatedName);
2017-09-23 17:39:04 +02:00
}
}