Skip to content

Commit 503f195

Browse files
committed
Move LongPressable builders to LongPressable class
1 parent f77e3db commit 503f195

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import org.schabi.newpipe.local.playlist.RemotePlaylistManager;
5050
import org.schabi.newpipe.player.playqueue.PlayQueue;
5151
import org.schabi.newpipe.player.playqueue.PlaylistPlayQueue;
52+
import org.schabi.newpipe.ui.components.menu.LongPressAction;
53+
import org.schabi.newpipe.ui.components.menu.LongPressable;
5254
import org.schabi.newpipe.util.ExtractorHelper;
5355
import org.schabi.newpipe.util.Localization;
5456
import org.schabi.newpipe.util.NavigationHelper;
@@ -152,7 +154,11 @@ private PlayQueue getPlayQueueStartingAt(final StreamInfoItem infoItem) {
152154
@Override
153155
protected void showInfoItemDialog(final StreamInfoItem item) {
154156
activity.addContentView(
155-
getLongPressMenuView(requireContext(), item),
157+
getLongPressMenuView(
158+
requireContext(),
159+
LongPressable.from(item),
160+
LongPressAction.buildActionList(item, false)
161+
),
156162
new ViewGroup.LayoutParams(
157163
ViewGroup.LayoutParams.MATCH_PARENT,
158164
ViewGroup.LayoutParams.MATCH_PARENT

app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressAction.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ data class LongPressAction(
100100
)
101101
}
102102

103+
@JvmStatic
103104
fun buildActionList(
104105
item: StreamInfoItem,
105106
isKodiEnabled: Boolean,

app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenu.kt

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,41 +64,30 @@ import androidx.compose.ui.tooling.preview.datasource.LoremIpsum
6464
import androidx.compose.ui.unit.dp
6565
import coil3.compose.AsyncImage
6666
import org.schabi.newpipe.R
67-
import org.schabi.newpipe.extractor.stream.StreamInfoItem
6867
import org.schabi.newpipe.ktx.popFirst
6968
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.EnqueueNext
7069
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.ShowChannelDetails
7170
import org.schabi.newpipe.ui.theme.AppTheme
7271
import org.schabi.newpipe.ui.theme.customColors
7372
import org.schabi.newpipe.util.Either
7473
import org.schabi.newpipe.util.Localization
75-
import org.schabi.newpipe.util.image.ImageStrategy
7674
import java.time.OffsetDateTime
7775

7876
fun getLongPressMenuView(
7977
context: Context,
80-
item: StreamInfoItem,
78+
longPressable: LongPressable,
79+
longPressActions: List<LongPressAction>,
8180
): ComposeView {
8281
return ComposeView(context).apply {
8382
setContent {
84-
LongPressMenu(
85-
longPressable = LongPressable(
86-
title = item.name,
87-
url = item.url?.takeIf { it.isNotBlank() },
88-
thumbnailUrl = ImageStrategy.choosePreferredImage(item.thumbnails),
89-
uploader = item.uploaderName?.takeIf { it.isNotBlank() },
90-
uploaderUrl = item.uploaderUrl?.takeIf { it.isNotBlank() },
91-
viewCount = item.viewCount.takeIf { it >= 0 },
92-
uploadDate = item.uploadDate?.let { Either.right(it.offsetDateTime()) }
93-
?: item.textualUploadDate?.let { Either.left(it) },
94-
decoration = item.duration.takeIf { it >= 0 }?.let {
95-
LongPressable.Decoration.Duration(it)
96-
},
97-
),
98-
onDismissRequest = { (this.parent as ViewGroup).removeView(this) },
99-
longPressActions = LongPressAction.buildActionList(item, false),
100-
onEditActions = {},
101-
)
83+
AppTheme {
84+
LongPressMenu(
85+
longPressable = longPressable,
86+
onDismissRequest = { (this.parent as ViewGroup).removeView(this) },
87+
longPressActions = longPressActions,
88+
onEditActions = {},
89+
)
90+
}
10291
}
10392
}
10493
}
@@ -157,7 +146,6 @@ fun LongPressMenu(
157146
.weight((buttonsPerRow - rowIndex).toFloat()),
158147
)
159148
break
160-
161149
} else if (actionIndex >= 0) {
162150
val action = actions[actionIndex]
163151
LongPressMenuButton(
@@ -174,7 +162,6 @@ fun LongPressMenu(
174162
.weight(1F),
175163
)
176164
rowIndex += 1
177-
178165
} else if (headerWidthInButtons >= buttonsPerRow) {
179166
// this branch is taken if the header is going to fit on one line
180167
// (i.e. on phones in portrait)
@@ -189,7 +176,6 @@ fun LongPressMenu(
189176
.weight(headerWidthInButtons.toFloat()),
190177
)
191178
rowIndex += headerWidthInButtons
192-
193179
} else {
194180
// this branch is taken if the header will have some buttons to its
195181
// right (i.e. on tablets or on phones in landscape)
@@ -203,7 +189,6 @@ fun LongPressMenu(
203189
.weight(headerWidthInButtons.toFloat()),
204190
)
205191
rowIndex += headerWidthInButtons
206-
207192
}
208193
actionIndex += 1
209194
}

app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressable.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.schabi.newpipe.ui.components.menu
22

33
import androidx.compose.runtime.Stable
4+
import org.schabi.newpipe.extractor.stream.StreamInfoItem
5+
import org.schabi.newpipe.extractor.stream.StreamType
46
import org.schabi.newpipe.util.Either
7+
import org.schabi.newpipe.util.image.ImageStrategy
58
import java.time.OffsetDateTime
69

710
@Stable
@@ -20,4 +23,27 @@ data class LongPressable(
2023
data object Live : Decoration
2124
data class Playlist(val itemCount: Long) : Decoration
2225
}
26+
27+
companion object {
28+
@JvmStatic
29+
fun from(item: StreamInfoItem) = LongPressable(
30+
title = item.name,
31+
url = item.url?.takeIf { it.isNotBlank() },
32+
thumbnailUrl = ImageStrategy.choosePreferredImage(item.thumbnails),
33+
uploader = item.uploaderName?.takeIf { it.isNotBlank() },
34+
uploaderUrl = item.uploaderUrl?.takeIf { it.isNotBlank() },
35+
viewCount = item.viewCount.takeIf { it >= 0 },
36+
uploadDate = item.uploadDate?.let { Either.right(it.offsetDateTime()) }
37+
?: item.textualUploadDate?.let { Either.left(it) },
38+
decoration = if (item.streamType == StreamType.LIVE_STREAM ||
39+
item.streamType == StreamType.AUDIO_LIVE_STREAM
40+
) {
41+
LongPressable.Decoration.Live
42+
} else {
43+
item.duration.takeIf { it >= 0 }?.let {
44+
LongPressable.Decoration.Duration(it)
45+
}
46+
},
47+
)
48+
}
2349
}

0 commit comments

Comments
 (0)