Skip to content

Commit 675dbd3

Browse files
authored
Merge pull request #13036 from dustdfg/kotlin_misc_refactor
Misc small kotlin based refactors
2 parents eec3ac1 + 4f0e62e commit 675dbd3

File tree

9 files changed

+17
-27
lines changed

9 files changed

+17
-27
lines changed

app/src/androidTest/java/org/schabi/newpipe/database/FeedDAOTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import org.schabi.newpipe.extractor.channel.ChannelInfo
2222
import org.schabi.newpipe.extractor.stream.StreamType
2323
import java.io.IOException
2424
import java.time.OffsetDateTime
25-
import kotlin.streams.toList
2625

2726
class FeedDAOTest {
2827
private lateinit var db: AppDatabase

app/src/main/java/org/schabi/newpipe/DownloaderImpl.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,7 @@ public Response execute(@NonNull final Request request)
161161

162162
String responseBodyToReturn = null;
163163
try (ResponseBody body = response.body()) {
164-
if (body != null) {
165-
responseBodyToReturn = body.string();
166-
}
164+
responseBodyToReturn = body.string();
167165
}
168166

169167
final String latestUrl = response.request().url().toString();

app/src/main/java/org/schabi/newpipe/database/Migrations.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package org.schabi.newpipe.database
88

99
import android.util.Log
1010
import androidx.room.migration.Migration
11-
import androidx.sqlite.db.SupportSQLiteDatabase
1211
import org.schabi.newpipe.MainActivity
1312

1413
object Migrations {

app/src/main/java/org/schabi/newpipe/error/ErrorInfo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class ErrorInfo private constructor(
163163
private fun getServiceName(serviceId: Int?) =
164164
// not using getNameOfServiceById since we want to accept a nullable serviceId and we
165165
// want to default to SERVICE_NONE
166-
ServiceList.all()?.firstOrNull { it.serviceId == serviceId }?.serviceInfo?.name
166+
ServiceList.all().firstOrNull { it.serviceId == serviceId }?.serviceInfo?.name
167167
?: SERVICE_NONE
168168

169169
fun throwableToStringList(throwable: Throwable) = arrayOf(throwable.stackTraceToString())

app/src/main/java/org/schabi/newpipe/ktx/View.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ fun View.animate(
4141
execOnEnd: Runnable? = null
4242
) {
4343
if (DEBUG) {
44-
val id = try {
45-
resources.getResourceEntryName(id)
46-
} catch (e: Exception) {
47-
id.toString()
48-
}
44+
val id = runCatching { resources.getResourceEntryName(id) }.getOrDefault(id.toString())
4945
val msg = String.format(
5046
"%8s → [%s:%s] [%s %s:%s] execOnEnd=%s", enterOrExit,
5147
javaClass.simpleName, id, animationType, duration, delay, execOnEnd

app/src/main/java/org/schabi/newpipe/local/feed/FeedDatabaseManager.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,13 @@ class FeedDatabaseManager(context: Context) {
8585
items: List<StreamInfoItem>,
8686
oldestAllowedDate: OffsetDateTime = FEED_OLDEST_ALLOWED_DATE
8787
) {
88-
val itemsToInsert = ArrayList<StreamInfoItem>()
89-
loop@ for (streamItem in items) {
90-
val uploadDate = streamItem.uploadDate
91-
92-
itemsToInsert += when {
93-
uploadDate == null && streamItem.streamType == StreamType.LIVE_STREAM -> streamItem
94-
uploadDate != null && uploadDate.offsetDateTime() >= oldestAllowedDate -> streamItem
95-
else -> continue@loop
88+
val itemsToInsert = items.mapNotNull { stream ->
89+
val uploadDate = stream.uploadDate
90+
91+
when {
92+
uploadDate == null && stream.streamType == StreamType.LIVE_STREAM -> stream
93+
uploadDate != null && uploadDate.offsetDateTime() >= oldestAllowedDate -> stream
94+
else -> null
9695
}
9796
}
9897

app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import androidx.core.app.NotificationManagerCompat
1515
import androidx.core.app.PendingIntentCompat
1616
import androidx.core.content.ContextCompat
1717
import androidx.core.content.getSystemService
18+
import androidx.core.net.toUri
1819
import androidx.preference.PreferenceManager
1920
import com.squareup.picasso.Picasso
2021
import com.squareup.picasso.Target
@@ -181,8 +182,7 @@ class NotificationHelper(val context: Context) {
181182
val manager = context.getSystemService<NotificationManager>()!!
182183
val enabled = manager.areNotificationsEnabled()
183184
val channel = manager.getNotificationChannel(channelId)
184-
val importance = channel?.importance
185-
enabled && channel != null && importance != NotificationManager.IMPORTANCE_NONE
185+
enabled && channel?.importance != NotificationManager.IMPORTANCE_NONE
186186
} else {
187187
NotificationManagerCompat.from(context).areNotificationsEnabled()
188188
}
@@ -212,7 +212,7 @@ class NotificationHelper(val context: Context) {
212212
context.startActivity(intent)
213213
} else {
214214
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
215-
intent.data = Uri.parse("package:" + context.packageName)
215+
intent.data = "package:${context.packageName}".toUri()
216216
context.startActivity(intent)
217217
}
218218
}

app/src/main/java/org/schabi/newpipe/local/feed/notifications/ScheduleOptions.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.schabi.newpipe.local.feed.notifications
33
import android.content.Context
44
import androidx.preference.PreferenceManager
55
import org.schabi.newpipe.R
6+
import org.schabi.newpipe.ktx.getStringSafe
67
import java.util.concurrent.TimeUnit
78

89
/**
@@ -20,11 +21,9 @@ data class ScheduleOptions(
2021
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
2122
return ScheduleOptions(
2223
interval = TimeUnit.SECONDS.toMillis(
23-
preferences.getString(
24+
preferences.getStringSafe(
2425
context.getString(R.string.streams_notifications_interval_key),
25-
null
26-
)?.toLongOrNull() ?: context.getString(
27-
R.string.streams_notifications_interval_default
26+
context.getString(R.string.streams_notifications_interval_default)
2827
).toLong()
2928
),
3029
isRequireNonMeteredNetwork = preferences.getString(

app/src/main/java/org/schabi/newpipe/local/playlist/ExportPlaylist.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@ private val linkHandler: YoutubeStreamLinkHandlerFactory = YoutubeStreamLinkHand
6565
*/
6666
private fun getYouTubeId(url: String): String? {
6767

68-
return try { linkHandler.getId(url) } catch (e: ParsingException) { null }
68+
return runCatching { linkHandler.getId(url) }.getOrNull()
6969
}

0 commit comments

Comments
 (0)