Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate comment fragments to Jetpack Compose #11060

Merged
merged 65 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
bda961a
Convert comment replies views to Jetpack Compose
Isira-Seneviratne May 12, 2024
644a345
Rename .java to .kt
Isira-Seneviratne May 12, 2024
e05d977
Use reply header composable in fragment
Isira-Seneviratne May 12, 2024
8ce9a7e
Added like count
Isira-Seneviratne May 12, 2024
56c80ce
Added missing comment features, fixed theming
Isira-Seneviratne May 17, 2024
1620668
Add comment ellipsis
Isira-Seneviratne Jun 16, 2024
341cc37
Update replies fragment to use the comment composable as well
Isira-Seneviratne Jun 18, 2024
11bb249
Improve previews, display date of comment
Isira-Seneviratne Jun 18, 2024
e30d5e4
Fixed some comment issues
Isira-Seneviratne Jun 18, 2024
1908e18
Use AnnotatedString to handle HTML parsing
Isira-Seneviratne Jun 19, 2024
e92ba8f
Add replies button
Isira-Seneviratne Jun 19, 2024
5841eaa
Set view strategy
Isira-Seneviratne Jun 20, 2024
0ec81c9
Fixed like count display
Isira-Seneviratne Jun 20, 2024
5bfb044
Fixed fragment title
Isira-Seneviratne Jun 21, 2024
be037e0
Rename .java to .kt
Isira-Seneviratne Jun 21, 2024
9c52e03
Migrate comments fragment to Jetpack Compose
Isira-Seneviratne Jun 21, 2024
9331095
Added scrollbar to comment section
Isira-Seneviratne Jun 21, 2024
b9dd707
Replace CommentRepliesFragment with bottom sheet composable, improve …
Isira-Seneviratne Jun 23, 2024
b092fe2
Replace Spacers with the horizontalArrangement parameter
Isira-Seneviratne Jun 23, 2024
5e7e14e
Handle no comments and comments disabled scenarios
Isira-Seneviratne Jun 23, 2024
909d214
Rm redundant Surface
Isira-Seneviratne Jun 26, 2024
369a46f
Improve code organization
Isira-Seneviratne Jun 28, 2024
02c5f26
Cache paging data using the cachedIn() extension
Isira-Seneviratne Jun 30, 2024
c5d94a5
Add comment view model
Isira-Seneviratne Jul 2, 2024
e72da94
Rm extra padding in header
Isira-Seneviratne Jul 5, 2024
42cb914
Replace padding modifier with verticalArrangement in comment header
Isira-Seneviratne Jul 5, 2024
1009dc4
Added loading indicator
Isira-Seneviratne Jul 6, 2024
d131d33
Rm unused method
Isira-Seneviratne Jul 8, 2024
ac1ca14
Improve comment loading smoothness
Isira-Seneviratne Jul 8, 2024
e639b02
Animate comment expand/collapse
Isira-Seneviratne Jul 9, 2024
4740e3b
Make parsed links clickable, visible
Isira-Seneviratne Jul 10, 2024
edab9a6
Fix alignment of comment message
Isira-Seneviratne Jul 12, 2024
f984b26
Fix some modifiers
Isira-Seneviratne Jul 16, 2024
ea414f5
Added DescriptionText composable
Isira-Seneviratne Jul 25, 2024
eaac7f3
Improved component organisation
Isira-Seneviratne Jul 28, 2024
e955bee
Update Kotlin to 2.0, update dependencies and fix issues
Isira-Seneviratne Jul 28, 2024
f9dae90
Always show comment thumbnails, even if placeholders
Isira-Seneviratne Aug 3, 2024
e082bca
Use nested scroll modifier
Isira-Seneviratne Aug 10, 2024
294b9cf
Rm unused declaration
Isira-Seneviratne Aug 17, 2024
3641698
Merge branch 'refs/heads/refactor' into Comments-Compose
Isira-Seneviratne Aug 23, 2024
d3a6991
Use Fragment.content extension, improve comment composables
Isira-Seneviratne Aug 26, 2024
f9340ae
Improve compose function organisation
Isira-Seneviratne Aug 27, 2024
5fffee2
Fix text color in bottom sheet
Isira-Seneviratne Aug 28, 2024
b1add13
Address code review comments
Isira-Seneviratne Aug 28, 2024
941b8eb
Implement copy on long click
Isira-Seneviratne Aug 29, 2024
4cac111
Reduce preview count
Isira-Seneviratne Aug 29, 2024
3785404
Display number of comments
Isira-Seneviratne Aug 30, 2024
62d4044
Make lazy column scrollbars red
Isira-Seneviratne Aug 30, 2024
823d4a0
Improve loading indicator positioning
Isira-Seneviratne Aug 30, 2024
5017f4f
Update dependencies
Isira-Seneviratne Sep 5, 2024
3177ca6
Avoid issues if context is a ContextWrapper
Isira-Seneviratne Sep 11, 2024
bac9f7e
Merge branch 'refactor' into pr11060
Stypox Nov 10, 2024
36ede24
Update compose bom and fix navigation compose without version
Stypox Nov 10, 2024
e6b1341
Improve Comment layout
Stypox Nov 10, 2024
802a094
Improve comment replies dialog layout
Stypox Nov 10, 2024
412e1d6
Better handle unknown values for comment & like count
Stypox Nov 10, 2024
23b3835
Fix PagingSource for comments
Stypox Nov 10, 2024
ef56dea
Fix content color in comment replies fragment
Stypox Nov 10, 2024
9d8a79b
Slightly improve comment replies header spacing
Stypox Nov 11, 2024
800961c
Unexpand bottom sheet dialog when clicking on a channel
Stypox Nov 11, 2024
a92a285
Use Icons.Default.* instead of vector assets
Stypox Nov 11, 2024
cea149f
Add .kotlin/ to gitignore
Stypox Nov 11, 2024
37d1c78
Create utilities to copy to clipboard in Compose code
Stypox Nov 11, 2024
aea2b7c
Show correct reply count in dialog
Stypox Nov 11, 2024
fdf36cb
Deduplicate and improve Scrollbar theme
Stypox Nov 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ captures/
*.class
app/debug/
app/release/
.kotlin/

# vscode / eclipse files
*.classpath
Expand Down
15 changes: 8 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.core:core-ktx:1.12.0'
implementation 'androidx.documentfile:documentfile:1.0.1'
implementation 'androidx.fragment:fragment-ktx:1.6.2'
implementation 'androidx.fragment:fragment-compose:1.8.2'
implementation "androidx.lifecycle:lifecycle-livedata-ktx:${androidxLifecycleVersion}"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${androidxLifecycleVersion}"
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.1.0'
Expand Down Expand Up @@ -293,18 +293,19 @@ dependencies {
// Date and time formatting
implementation "org.ocpsoft.prettytime:prettytime:5.0.8.Final"

// Jetpack Compose BOM group
implementation(platform('androidx.compose:compose-bom:2024.09.03'))
// Jetpack Compose
implementation(platform('androidx.compose:compose-bom:2024.10.01'))
implementation 'androidx.compose.material3:material3'
implementation 'androidx.compose.material3.adaptive:adaptive'
implementation 'androidx.activity:activity-compose'
implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose'
implementation 'androidx.compose.ui:ui-text' // Needed for parsing HTML to AnnotatedString
implementation 'androidx.compose.material:material-icons-extended'

// Jetpack Compose related dependencies
implementation 'androidx.compose.material3.adaptive:adaptive:1.0.0'
implementation 'androidx.activity:activity-compose:1.9.2'
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.8.6'
implementation 'androidx.paging:paging-compose:3.3.2'
implementation "androidx.navigation:navigation-compose:2.8.2"
implementation "androidx.navigation:navigation-compose:2.8.3"

// Coroutines interop
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-rx3:1.8.1'
Expand Down
110 changes: 13 additions & 97 deletions app/src/main/java/org/schabi/newpipe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentContainerView;
import androidx.fragment.app.FragmentManager;
import androidx.preference.PreferenceManager;

Expand All @@ -66,13 +64,11 @@
import org.schabi.newpipe.error.ErrorUtil;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance;
import org.schabi.newpipe.fragments.BackPressable;
import org.schabi.newpipe.fragments.MainFragment;
import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
import org.schabi.newpipe.fragments.list.comments.CommentRepliesFragment;
import org.schabi.newpipe.fragments.list.search.SearchFragment;
import org.schabi.newpipe.local.feed.notifications.NotificationWorker;
import org.schabi.newpipe.player.Player;
Expand Down Expand Up @@ -557,39 +553,27 @@ public void onBackPressed() {
// In case bottomSheet is not visible on the screen or collapsed we can assume that the user
// interacts with a fragment inside fragment_holder so all back presses should be
// handled by it
final var fragmentManager = getSupportFragmentManager();

if (bottomSheetHiddenOrCollapsed()) {
final FragmentManager fm = getSupportFragmentManager();
final Fragment fragment = fm.findFragmentById(R.id.fragment_holder);
final var fragment = fragmentManager.findFragmentById(R.id.fragment_holder);
// If current fragment implements BackPressable (i.e. can/wanna handle back press)
// delegate the back press to it
if (fragment instanceof BackPressable) {
if (((BackPressable) fragment).onBackPressed()) {
return;
}
} else if (fragment instanceof CommentRepliesFragment) {
// expand DetailsFragment if CommentRepliesFragment was opened
// to show the top level comments again
// Expand DetailsFragment if CommentRepliesFragment was opened
// and no other CommentRepliesFragments are on top of the back stack
// to show the top level comments again.
openDetailFragmentFromCommentReplies(fm, false);
if (fragment instanceof BackPressable backPressable && backPressable.onBackPressed()) {
return;
}

} else {
final Fragment fragmentPlayer = getSupportFragmentManager()
.findFragmentById(R.id.fragment_player_holder);
final var player = fragmentManager.findFragmentById(R.id.fragment_player_holder);
// If current fragment implements BackPressable (i.e. can/wanna handle back press)
// delegate the back press to it
if (fragmentPlayer instanceof BackPressable) {
if (!((BackPressable) fragmentPlayer).onBackPressed()) {
BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder)
.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
if (player instanceof BackPressable backPressable && !backPressable.onBackPressed()) {
BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder)
.setState(BottomSheetBehavior.STATE_COLLAPSED);
return;
}
}

if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
if (fragmentManager.getBackStackEntryCount() == 1) {
finish();
} else {
super.onBackPressed();
Expand Down Expand Up @@ -648,15 +632,9 @@ public void onRequestPermissionsResult(final int requestCode,
* </pre>
*/
private void onHomeButtonPressed() {
final FragmentManager fm = getSupportFragmentManager();
final Fragment fragment = fm.findFragmentById(R.id.fragment_holder);

if (fragment instanceof CommentRepliesFragment) {
// Expand DetailsFragment if CommentRepliesFragment was opened
// and no other CommentRepliesFragments are on top of the back stack
// to show the top level comments again.
openDetailFragmentFromCommentReplies(fm, true);
} else if (!NavigationHelper.tryGotoSearchFragment(fm)) {
final var fm = getSupportFragmentManager();

if (!NavigationHelper.tryGotoSearchFragment(fm)) {
// If search fragment wasn't found in the backstack go to the main fragment
NavigationHelper.gotoMainFragment(fm);
}
Expand Down Expand Up @@ -854,68 +832,6 @@ public void onReceive(final Context context, final Intent intent) {
}
}

private void openDetailFragmentFromCommentReplies(
@NonNull final FragmentManager fm,
final boolean popBackStack
) {
// obtain the name of the fragment under the replies fragment that's going to be popped
@Nullable final String fragmentUnderEntryName;
if (fm.getBackStackEntryCount() < 2) {
fragmentUnderEntryName = null;
} else {
fragmentUnderEntryName = fm.getBackStackEntryAt(fm.getBackStackEntryCount() - 2)
.getName();
}

// the root comment is the comment for which the user opened the replies page
@Nullable final CommentRepliesFragment repliesFragment =
(CommentRepliesFragment) fm.findFragmentByTag(CommentRepliesFragment.TAG);
@Nullable final CommentsInfoItem rootComment =
repliesFragment == null ? null : repliesFragment.getCommentsInfoItem();

// sometimes this function pops the backstack, other times it's handled by the system
if (popBackStack) {
fm.popBackStackImmediate();
}

// only expand the bottom sheet back if there are no more nested comment replies fragments
// stacked under the one that is currently being popped
if (CommentRepliesFragment.TAG.equals(fragmentUnderEntryName)) {
return;
}

final BottomSheetBehavior<FragmentContainerView> behavior = BottomSheetBehavior
.from(mainBinding.fragmentPlayerHolder);
// do not return to the comment if the details fragment was closed
if (behavior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
return;
}

// scroll to the root comment once the bottom sheet expansion animation is finished
behavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull final View bottomSheet,
final int newState) {
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
final Fragment detailFragment = fm.findFragmentById(
R.id.fragment_player_holder);
if (detailFragment instanceof VideoDetailFragment && rootComment != null) {
// should always be the case
((VideoDetailFragment) detailFragment).scrollToComment(rootComment);
}
behavior.removeBottomSheetCallback(this);
}
}

@Override
public void onSlide(@NonNull final View bottomSheet, final float slideOffset) {
// not needed, listener is removed once the sheet is expanded
}
});

behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}

private boolean bottomSheetHiddenOrCollapsed() {
final BottomSheetBehavior<FrameLayout> bottomSheetBehavior =
BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,8 @@ private void handleCookiesFromUrl(@Nullable final String url) {
final int abuseEnd = url.indexOf("+path");

try {
String abuseCookie = url.substring(abuseStart + 13, abuseEnd);
abuseCookie = Utils.decodeUrlUtf8(abuseCookie);
handleCookies(abuseCookie);
} catch (IllegalArgumentException | StringIndexOutOfBoundsException e) {
handleCookies(Utils.decodeUrlUtf8(url.substring(abuseStart + 13, abuseEnd)));
} catch (final StringIndexOutOfBoundsException e) {
if (MainActivity.DEBUG) {
Log.e(TAG, "handleCookiesFromUrl: invalid google abuse starting at "
+ abuseStart + " and ending at " + abuseEnd + " for url " + url, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
import org.schabi.newpipe.error.UserAction;
import org.schabi.newpipe.extractor.Image;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
import org.schabi.newpipe.extractor.exceptions.ContentNotSupportedException;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.stream.AudioStream;
Expand Down Expand Up @@ -881,8 +880,7 @@ private void initTabs() {
tabContentDescriptions.clear();

if (shouldShowComments()) {
pageAdapter.addFragment(
CommentsFragment.getInstance(serviceId, url, title), COMMENTS_TAB_TAG);
pageAdapter.addFragment(CommentsFragment.getInstance(serviceId, url), COMMENTS_TAB_TAG);
tabIcons.add(R.drawable.ic_comment);
tabContentDescriptions.add(R.string.comments_tab_description);
}
Expand Down Expand Up @@ -1012,20 +1010,6 @@ public void scrollToTop() {
updateTabLayoutVisibility();
}

public void scrollToComment(final CommentsInfoItem comment) {
final int commentsTabPos = pageAdapter.getItemPositionByTitle(COMMENTS_TAB_TAG);
final Fragment fragment = pageAdapter.getItem(commentsTabPos);
if (!(fragment instanceof CommentsFragment)) {
return;
}

// unexpand the app bar only if scrolling to the comment succeeded
if (((CommentsFragment) fragment).scrollToComment(comment)) {
binding.appBarLayout.setExpanded(false, false);
binding.viewPager.setCurrentItem(commentsTabPos, false);
}
}

/*//////////////////////////////////////////////////////////////////////////
// Play Utils
//////////////////////////////////////////////////////////////////////////*/
Expand Down
Loading
Loading