-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate related items fragment to Jetpack Compose
- Loading branch information
1 parent
e3c1a3c
commit aac96cc
Showing
19 changed files
with
648 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 35 additions & 170 deletions
205
app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemsFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,176 +1,41 @@ | ||
package org.schabi.newpipe.fragments.list.videos; | ||
|
||
import android.content.SharedPreferences; | ||
import android.os.Bundle; | ||
import android.view.LayoutInflater; | ||
import android.view.Menu; | ||
import android.view.MenuInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.preference.PreferenceManager; | ||
|
||
import org.schabi.newpipe.R; | ||
import org.schabi.newpipe.databinding.RelatedItemsHeaderBinding; | ||
import org.schabi.newpipe.error.UserAction; | ||
import org.schabi.newpipe.extractor.InfoItem; | ||
import org.schabi.newpipe.extractor.ListExtractor; | ||
import org.schabi.newpipe.extractor.stream.StreamInfo; | ||
import org.schabi.newpipe.fragments.list.BaseListInfoFragment; | ||
import org.schabi.newpipe.info_list.ItemViewMode; | ||
import org.schabi.newpipe.ktx.ViewUtils; | ||
|
||
import java.io.Serializable; | ||
import java.util.function.Supplier; | ||
|
||
import io.reactivex.rxjava3.core.Single; | ||
|
||
public class RelatedItemsFragment extends BaseListInfoFragment<InfoItem, RelatedItemsInfo> | ||
implements SharedPreferences.OnSharedPreferenceChangeListener { | ||
private static final String INFO_KEY = "related_info_key"; | ||
|
||
private RelatedItemsInfo relatedItemsInfo; | ||
|
||
/*////////////////////////////////////////////////////////////////////////// | ||
// Views | ||
//////////////////////////////////////////////////////////////////////////*/ | ||
private RelatedItemsHeaderBinding headerBinding; | ||
public static RelatedItemsFragment getInstance(final StreamInfo info) { | ||
final RelatedItemsFragment instance = new RelatedItemsFragment(); | ||
instance.setInitialData(info); | ||
return instance; | ||
} | ||
public RelatedItemsFragment() { | ||
super(UserAction.REQUESTED_STREAM); | ||
} | ||
/*////////////////////////////////////////////////////////////////////////// | ||
// LifeCycle | ||
//////////////////////////////////////////////////////////////////////////*/ | ||
@Override | ||
public View onCreateView(@NonNull final LayoutInflater inflater, | ||
@Nullable final ViewGroup container, | ||
@Nullable final Bundle savedInstanceState) { | ||
return inflater.inflate(R.layout.fragment_related_items, container, false); | ||
} | ||
@Override | ||
public void onDestroyView() { | ||
headerBinding = null; | ||
super.onDestroyView(); | ||
} | ||
@Override | ||
protected Supplier<View> getListHeaderSupplier() { | ||
if (relatedItemsInfo == null || relatedItemsInfo.getRelatedItems() == null) { | ||
return null; | ||
} | ||
headerBinding = RelatedItemsHeaderBinding | ||
.inflate(activity.getLayoutInflater(), itemsList, false); | ||
final SharedPreferences pref = PreferenceManager | ||
.getDefaultSharedPreferences(requireContext()); | ||
final boolean autoplay = pref.getBoolean(getString(R.string.auto_queue_key), false); | ||
headerBinding.autoplaySwitch.setChecked(autoplay); | ||
headerBinding.autoplaySwitch.setOnCheckedChangeListener((compoundButton, b) -> | ||
PreferenceManager.getDefaultSharedPreferences(requireContext()).edit() | ||
.putBoolean(getString(R.string.auto_queue_key), b).apply()); | ||
return headerBinding::getRoot; | ||
} | ||
@Override | ||
protected Single<ListExtractor.InfoItemsPage<InfoItem>> loadMoreItemsLogic() { | ||
return Single.fromCallable(ListExtractor.InfoItemsPage::emptyPage); | ||
} | ||
/*////////////////////////////////////////////////////////////////////////// | ||
// Contract | ||
//////////////////////////////////////////////////////////////////////////*/ | ||
@Override | ||
protected Single<RelatedItemsInfo> loadResult(final boolean forceLoad) { | ||
return Single.fromCallable(() -> relatedItemsInfo); | ||
} | ||
@Override | ||
public void showLoading() { | ||
super.showLoading(); | ||
if (headerBinding != null) { | ||
headerBinding.getRoot().setVisibility(View.INVISIBLE); | ||
} | ||
} | ||
@Override | ||
public void handleResult(@NonNull final RelatedItemsInfo result) { | ||
super.handleResult(result); | ||
if (headerBinding != null) { | ||
headerBinding.getRoot().setVisibility(View.VISIBLE); | ||
} | ||
ViewUtils.slideUp(requireView(), 120, 96, 0.06f); | ||
} | ||
/*////////////////////////////////////////////////////////////////////////// | ||
// Utils | ||
//////////////////////////////////////////////////////////////////////////*/ | ||
@Override | ||
public void setTitle(final String title) { | ||
// Nothing to do - override parent | ||
} | ||
@Override | ||
public void onCreateOptionsMenu(@NonNull final Menu menu, | ||
@NonNull final MenuInflater inflater) { | ||
// Nothing to do - override parent | ||
} | ||
private void setInitialData(final StreamInfo info) { | ||
super.setInitialData(info.getServiceId(), info.getUrl(), info.getName()); | ||
if (this.relatedItemsInfo == null) { | ||
this.relatedItemsInfo = new RelatedItemsInfo(info); | ||
} | ||
} | ||
@Override | ||
public void onSaveInstanceState(@NonNull final Bundle outState) { | ||
super.onSaveInstanceState(outState); | ||
outState.putSerializable(INFO_KEY, relatedItemsInfo); | ||
} | ||
@Override | ||
protected void onRestoreInstanceState(@NonNull final Bundle savedState) { | ||
super.onRestoreInstanceState(savedState); | ||
final Serializable serializable = savedState.getSerializable(INFO_KEY); | ||
if (serializable instanceof RelatedItemsInfo) { | ||
this.relatedItemsInfo = (RelatedItemsInfo) serializable; | ||
} | ||
} | ||
@Override | ||
public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, | ||
final String key) { | ||
if (headerBinding != null && getString(R.string.auto_queue_key).equals(key)) { | ||
headerBinding.autoplaySwitch.setChecked(sharedPreferences.getBoolean(key, false)); | ||
package org.schabi.newpipe.fragments.list.videos | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.ui.platform.ComposeView | ||
import androidx.core.os.bundleOf | ||
import androidx.fragment.app.Fragment | ||
import org.schabi.newpipe.extractor.stream.StreamInfo | ||
import org.schabi.newpipe.ktx.serializable | ||
import org.schabi.newpipe.ui.components.video.RelatedItems | ||
import org.schabi.newpipe.ui.theme.AppTheme | ||
import org.schabi.newpipe.util.KEY_INFO | ||
|
||
class RelatedItemsFragment : Fragment() { | ||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
return ComposeView(requireContext()).apply { | ||
setContent { | ||
AppTheme { | ||
Surface(color = MaterialTheme.colorScheme.background) { | ||
RelatedItems(requireArguments().serializable<StreamInfo>(KEY_INFO)!!) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected ItemViewMode getItemViewMode() { | ||
ItemViewMode mode = super.getItemViewMode(); | ||
// Only list mode is supported. Either List or card will be used. | ||
if (mode != ItemViewMode.LIST && mode != ItemViewMode.CARD) { | ||
mode = ItemViewMode.LIST; | ||
companion object { | ||
@JvmStatic | ||
fun getInstance(info: StreamInfo) = RelatedItemsFragment().apply { | ||
arguments = bundleOf(KEY_INFO to info) | ||
} | ||
return mode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.