Misc small kotlin based refactors

Java file here because it uses kotlin function which returns non null
This commit is contained in:
Yevhen Babiichuk (DustDFG) 2026-01-11 13:51:34 +02:00
parent 183df8839a
commit d84852c920
9 changed files with 16 additions and 26 deletions

View file

@ -22,7 +22,6 @@ import org.schabi.newpipe.extractor.channel.ChannelInfo
import org.schabi.newpipe.extractor.stream.StreamType import org.schabi.newpipe.extractor.stream.StreamType
import java.io.IOException import java.io.IOException
import java.time.OffsetDateTime import java.time.OffsetDateTime
import kotlin.streams.toList
class FeedDAOTest { class FeedDAOTest {
private lateinit var db: AppDatabase private lateinit var db: AppDatabase

View file

@ -161,9 +161,7 @@ public final class DownloaderImpl extends Downloader {
String responseBodyToReturn = null; String responseBodyToReturn = null;
try (ResponseBody body = response.body()) { try (ResponseBody body = response.body()) {
if (body != null) { responseBodyToReturn = body.string();
responseBodyToReturn = body.string();
}
} }
final String latestUrl = response.request().url().toString(); final String latestUrl = response.request().url().toString();

View file

@ -8,7 +8,6 @@ package org.schabi.newpipe.database
import android.util.Log import android.util.Log
import androidx.room.migration.Migration import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import org.schabi.newpipe.MainActivity import org.schabi.newpipe.MainActivity
object Migrations { object Migrations {

View file

@ -163,7 +163,7 @@ class ErrorInfo private constructor(
private fun getServiceName(serviceId: Int?) = private fun getServiceName(serviceId: Int?) =
// not using getNameOfServiceById since we want to accept a nullable serviceId and we // not using getNameOfServiceById since we want to accept a nullable serviceId and we
// want to default to SERVICE_NONE // want to default to SERVICE_NONE
ServiceList.all()?.firstOrNull { it.serviceId == serviceId }?.serviceInfo?.name ServiceList.all().firstOrNull { it.serviceId == serviceId }?.serviceInfo?.name
?: SERVICE_NONE ?: SERVICE_NONE
fun throwableToStringList(throwable: Throwable) = arrayOf(throwable.stackTraceToString()) fun throwableToStringList(throwable: Throwable) = arrayOf(throwable.stackTraceToString())

View file

@ -41,11 +41,7 @@ fun View.animate(
execOnEnd: Runnable? = null execOnEnd: Runnable? = null
) { ) {
if (DEBUG) { if (DEBUG) {
val id = try { val id = runCatching { resources.getResourceEntryName(id) }.getOrDefault(id.toString())
resources.getResourceEntryName(id)
} catch (e: Exception) {
id.toString()
}
val msg = String.format( val msg = String.format(
"%8s → [%s:%s] [%s %s:%s] execOnEnd=%s", enterOrExit, "%8s → [%s:%s] [%s %s:%s] execOnEnd=%s", enterOrExit,
javaClass.simpleName, id, animationType, duration, delay, execOnEnd javaClass.simpleName, id, animationType, duration, delay, execOnEnd

View file

@ -85,14 +85,13 @@ class FeedDatabaseManager(context: Context) {
items: List<StreamInfoItem>, items: List<StreamInfoItem>,
oldestAllowedDate: OffsetDateTime = FEED_OLDEST_ALLOWED_DATE oldestAllowedDate: OffsetDateTime = FEED_OLDEST_ALLOWED_DATE
) { ) {
val itemsToInsert = ArrayList<StreamInfoItem>() val itemsToInsert = items.mapNotNull { stream ->
loop@ for (streamItem in items) { val uploadDate = stream.uploadDate
val uploadDate = streamItem.uploadDate
itemsToInsert += when { when {
uploadDate == null && streamItem.streamType == StreamType.LIVE_STREAM -> streamItem uploadDate == null && stream.streamType == StreamType.LIVE_STREAM -> stream
uploadDate != null && uploadDate.offsetDateTime() >= oldestAllowedDate -> streamItem uploadDate != null && uploadDate.offsetDateTime() >= oldestAllowedDate -> stream
else -> continue@loop else -> null
} }
} }

View file

@ -15,6 +15,7 @@ import androidx.core.app.NotificationManagerCompat
import androidx.core.app.PendingIntentCompat import androidx.core.app.PendingIntentCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.net.toUri
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.squareup.picasso.Picasso import com.squareup.picasso.Picasso
import com.squareup.picasso.Target import com.squareup.picasso.Target
@ -181,8 +182,7 @@ class NotificationHelper(val context: Context) {
val manager = context.getSystemService<NotificationManager>()!! val manager = context.getSystemService<NotificationManager>()!!
val enabled = manager.areNotificationsEnabled() val enabled = manager.areNotificationsEnabled()
val channel = manager.getNotificationChannel(channelId) val channel = manager.getNotificationChannel(channelId)
val importance = channel?.importance enabled && channel?.importance != NotificationManager.IMPORTANCE_NONE
enabled && channel != null && importance != NotificationManager.IMPORTANCE_NONE
} else { } else {
NotificationManagerCompat.from(context).areNotificationsEnabled() NotificationManagerCompat.from(context).areNotificationsEnabled()
} }
@ -212,7 +212,7 @@ class NotificationHelper(val context: Context) {
context.startActivity(intent) context.startActivity(intent)
} else { } else {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.data = Uri.parse("package:" + context.packageName) intent.data = "package:${context.packageName}".toUri()
context.startActivity(intent) context.startActivity(intent)
} }
} }

View file

@ -3,6 +3,7 @@ package org.schabi.newpipe.local.feed.notifications
import android.content.Context import android.content.Context
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.ktx.getStringSafe
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
/** /**
@ -20,11 +21,9 @@ data class ScheduleOptions(
val preferences = PreferenceManager.getDefaultSharedPreferences(context) val preferences = PreferenceManager.getDefaultSharedPreferences(context)
return ScheduleOptions( return ScheduleOptions(
interval = TimeUnit.SECONDS.toMillis( interval = TimeUnit.SECONDS.toMillis(
preferences.getString( preferences.getStringSafe(
context.getString(R.string.streams_notifications_interval_key), context.getString(R.string.streams_notifications_interval_key),
null context.getString(R.string.streams_notifications_interval_default)
)?.toLongOrNull() ?: context.getString(
R.string.streams_notifications_interval_default
).toLong() ).toLong()
), ),
isRequireNonMeteredNetwork = preferences.getString( isRequireNonMeteredNetwork = preferences.getString(

View file

@ -65,5 +65,5 @@ private val linkHandler: YoutubeStreamLinkHandlerFactory = YoutubeStreamLinkHand
*/ */
private fun getYouTubeId(url: String): String? { private fun getYouTubeId(url: String): String? {
return try { linkHandler.getId(url) } catch (e: ParsingException) { null } return runCatching { linkHandler.getId(url) }.getOrNull()
} }