11package org .schabi .newpipe .info_list .holder ;
22
33import static android .text .TextUtils .isEmpty ;
4+ import static org .schabi .newpipe .util .ServiceHelper .getServiceById ;
45
56import android .graphics .Paint ;
67import android .text .Layout ;
78import android .text .method .LinkMovementMethod ;
89import android .text .style .URLSpan ;
9- import android .util .Log ;
1010import android .view .View ;
1111import android .view .ViewGroup ;
1212import android .widget .Button ;
1515import android .widget .TextView ;
1616
1717import androidx .annotation .Nullable ;
18- import androidx .appcompat .app .AppCompatActivity ;
1918import androidx .core .text .HtmlCompat ;
19+ import androidx .fragment .app .FragmentActivity ;
2020
21- import org .schabi .newpipe .MainActivity ;
2221import org .schabi .newpipe .R ;
23- import org .schabi .newpipe .error .ErrorUtil ;
2422import org .schabi .newpipe .extractor .InfoItem ;
25- import org .schabi .newpipe .extractor .NewPipe ;
26- import org .schabi .newpipe .extractor .ServiceList ;
2723import org .schabi .newpipe .extractor .StreamingService ;
2824import org .schabi .newpipe .extractor .comments .CommentsInfo ;
2925import org .schabi .newpipe .extractor .comments .CommentsInfoItem ;
30- import org .schabi .newpipe .extractor .exceptions .ExtractionException ;
3126import org .schabi .newpipe .extractor .stream .Description ;
32- import org .schabi .newpipe .fragments .list .comments .CommentRepliesFragment ;
3327import org .schabi .newpipe .info_list .InfoItemBuilder ;
3428import org .schabi .newpipe .local .history .HistoryRecordManager ;
3529import org .schabi .newpipe .util .DeviceUtils ;
4640import io .reactivex .rxjava3 .disposables .CompositeDisposable ;
4741
4842public class CommentInfoItemHolder extends InfoItemHolder {
49- private static final String TAG = "CommentIIHolder" ;
5043 private static final String ELLIPSIS = "…" ;
5144
5245 private static final int COMMENT_DEFAULT_LINES = 2 ;
@@ -125,30 +118,19 @@ public void updateFromItem(final InfoItem infoItem,
125118
126119 // setup the top row, with pinned icon, author name and comment date
127120 itemPinnedView .setVisibility (item .isPinned () ? View .VISIBLE : View .GONE );
128-
129- final String uploadDate ;
130- if (item .getUploadDate () != null ) {
131- uploadDate = Localization .relativeTime (item .getUploadDate ().offsetDateTime ());
132- } else {
133- uploadDate = item .getTextualUploadDate ();
134- }
135- itemTitleView .setText (Localization .concatenateStrings (item .getUploaderName (), uploadDate ));
121+ itemTitleView .setText (Localization .concatenateStrings (item .getUploaderName (),
122+ Localization .relativeTimeOrTextual (item .getUploadDate (),
123+ item .getTextualUploadDate (), itemBuilder .getContext ())));
136124
137125
138126 // setup bottom row, with likes, heart and replies button
139- if (item .getLikeCount () >= 0 ) {
140- itemLikesCountView .setText (
141- Localization .shortCount (
142- itemBuilder .getContext (),
143- item .getLikeCount ()));
144- } else {
145- itemLikesCountView .setText ("-" );
146- }
127+ itemLikesCountView .setText (
128+ Localization .likeCount (itemBuilder .getContext (), item .getLikeCount ()));
147129
148130 itemHeartView .setVisibility (item .isHeartedByUploader () ? View .VISIBLE : View .GONE );
149131
150132 final boolean hasReplies = item .getReplies () != null ;
151- repliesButton .setOnClickListener (hasReplies ? v -> openRepliesFragment (item ) : null );
133+ repliesButton .setOnClickListener (hasReplies ? v -> openCommentReplies (item ) : null );
152134 repliesButton .setVisibility (hasReplies ? View .VISIBLE : View .GONE );
153135 repliesButton .setText (hasReplies
154136 ? Localization .replyCount (itemBuilder .getContext (), item .getReplyCount ()) : "" );
@@ -157,14 +139,7 @@ public void updateFromItem(final InfoItem infoItem,
157139
158140
159141 // setup comment content and click listeners to expand/ellipsize it
160- try {
161- streamService = NewPipe .getService (item .getServiceId ());
162- } catch (final ExtractionException e ) {
163- // should never happen
164- ErrorUtil .showUiErrorSnackbar (itemBuilder .getContext (), "Getting StreamingService" , e );
165- Log .w (TAG , "Cannot obtain service from comment service id, defaulting to YouTube" , e );
166- streamService = ServiceList .YouTube ;
167- }
142+ streamService = getServiceById (item .getServiceId ());
168143 streamUrl = item .getUrl ();
169144 commentText = item .getCommentText ();
170145 ellipsize ();
@@ -193,19 +168,13 @@ public void updateFromItem(final InfoItem infoItem,
193168 }
194169
195170 private void openCommentAuthor (final CommentsInfoItem item ) {
196- if (isEmpty (item .getUploaderUrl ())) {
197- return ;
198- }
199- final AppCompatActivity activity = (AppCompatActivity ) itemBuilder .getContext ();
200- try {
201- NavigationHelper .openChannelFragment (
202- activity .getSupportFragmentManager (),
203- item .getServiceId (),
204- item .getUploaderUrl (),
205- item .getUploaderName ());
206- } catch (final Exception e ) {
207- ErrorUtil .showUiErrorSnackbar (activity , "Opening channel fragment" , e );
208- }
171+ NavigationHelper .openCommentAuthorIfPresent ((FragmentActivity ) itemBuilder .getContext (),
172+ item );
173+ }
174+
175+ private void openCommentReplies (final CommentsInfoItem item ) {
176+ NavigationHelper .openCommentRepliesFragment ((FragmentActivity ) itemBuilder .getContext (),
177+ (CommentsInfo ) itemBuilder .getSourceListInfo (), item );
209178 }
210179
211180 private void allowLinkFocus () {
@@ -305,17 +274,4 @@ private void linkifyCommentContentView(@Nullable final Consumer<TextView> onComp
305274 onCompletion );
306275 }
307276 }
308-
309- private void openRepliesFragment (final CommentsInfoItem commentsInfoItem ) {
310- ((MainActivity ) itemBuilder .getContext ())
311- .getSupportFragmentManager ()
312- .beginTransaction ()
313- .setCustomAnimations (R .animator .custom_fade_in , R .animator .custom_fade_out ,
314- R .animator .custom_fade_in , R .animator .custom_fade_out )
315- .replace (R .id .fragment_holder ,
316- new CommentRepliesFragment ((CommentsInfo ) itemBuilder .getSourceListInfo (),
317- commentsInfoItem ))
318- .addToBackStack (null )
319- .commit ();
320- }
321277}
0 commit comments