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

Open
wants to merge 51 commits into
base: refactor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
51 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
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
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,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 @@ -286,12 +286,12 @@ dependencies {
implementation "org.ocpsoft.prettytime:prettytime:5.0.8.Final"

// Jetpack Compose
implementation(platform('androidx.compose:compose-bom:2024.06.00'))
implementation 'androidx.compose.material3:material3:1.3.0-beta05'
implementation 'androidx.compose.material3.adaptive:adaptive:1.0.0-beta04'
implementation(platform('androidx.compose:compose-bom:2024.08.00'))
implementation 'androidx.compose.material3:material3:1.3.0-rc01'
implementation 'androidx.compose.material3.adaptive:adaptive:1.0.0-rc01'
implementation 'androidx.activity:activity-compose'
implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.ui:ui-text:1.7.0-beta07' // Needed for parsing HTML to AnnotatedString
implementation 'androidx.compose.ui:ui-text:1.7.0-rc01' // Needed for parsing HTML to AnnotatedString
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose'
implementation 'androidx.paging:paging-compose:3.3.2'
implementation 'com.github.nanihadesuka:LazyColumnScrollbar:2.2.0'
Expand Down
107 changes: 12 additions & 95 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 @@ -558,33 +554,22 @@ public void onBackPressed() {
// interacts with a fragment inside fragment_holder so all back presses should be
// handled by it
if (bottomSheetHiddenOrCollapsed()) {
final FragmentManager fm = getSupportFragmentManager();
final Fragment fragment = fm.findFragmentById(R.id.fragment_holder);
final var fm = getSupportFragmentManager();
TobiGr marked this conversation as resolved.
Show resolved Hide resolved
final var fragment = fm.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()
final var fragmentPlayer = getSupportFragmentManager()
.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 (fragmentPlayer instanceof BackPressable backPressable
&& !backPressable.onBackPressed()) {
BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder)
.setState(BottomSheetBehavior.STATE_COLLAPSED);
return;
}
}
Expand Down Expand Up @@ -648,15 +633,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 +833,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 @@ -881,8 +881,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 @@ -1014,16 +1013,15 @@ public void scrollToTop() {

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;
}
final var fragment = pageAdapter.getItem(commentsTabPos);

// TODO: Implement the scrolling with Compose.
// 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);
}
// if (fragment instanceof CommentsFragment commentsFragment &&
// commentsFragment.scrollToComment(comment)) {
// binding.appBarLayout.setExpanded(false, false);
// binding.viewPager.setCurrentItem(commentsTabPos, false);
// }
TobiGr marked this conversation as resolved.
Show resolved Hide resolved
}

/*//////////////////////////////////////////////////////////////////////////
Expand Down
Loading
Loading