Use takeWhile() extension.

This commit is contained in:
Isira Seneviratne 2023-01-28 14:14:53 +05:30
parent c5e21b5f18
commit 99f031f155
2 changed files with 1 additions and 19 deletions

View File

@ -1,15 +0,0 @@
package com.github.libretube.extensions
/**
* Returns a list of all items until the given condition is fulfilled
* @param predicate The condition which needs to be searched for
* @return a list of all items before the first true condition
*/
fun <T> List<T>.filterUntil(predicate: (T) -> Boolean): List<T>? {
val items = mutableListOf<T>()
this.forEach {
if (predicate(it)) return items
items.add(it)
}
return null
}

View File

@ -18,7 +18,6 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PUSH_CHANNEL_ID import com.github.libretube.constants.PUSH_CHANNEL_ID
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.filterUntil
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.ui.activities.MainActivity import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.views.TimePickerPreference import com.github.libretube.ui.views.TimePickerPreference
@ -107,9 +106,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
} }
// filter the new videos until the last seen video in the feed // filter the new videos until the last seen video in the feed
val newStreams = videoFeed.filterUntil { val newStreams = videoFeed.takeWhile { it.url!!.toID() != lastSeenStreamId }
it.url!!.toID() == lastSeenStreamId
} ?: return@runBlocking
// return if the previous video didn't get found // return if the previous video didn't get found
if (newStreams.isEmpty()) return@runBlocking if (newStreams.isEmpty()) return@runBlocking