diff --git a/app/src/main/java/org/schabi/newpipe/database/feed/model/FeedGroupEntity.kt b/app/src/main/java/org/schabi/newpipe/database/feed/model/FeedGroupEntity.kt index ddefd590b..edb91406a 100644 --- a/app/src/main/java/org/schabi/newpipe/database/feed/model/FeedGroupEntity.kt +++ b/app/src/main/java/org/schabi/newpipe/database/feed/model/FeedGroupEntity.kt @@ -24,5 +24,7 @@ data class FeedGroupEntity( const val ID = "uid" const val NAME = "name" const val ICON = "icon_id" + + const val GROUP_ALL_ID = -1L } } \ No newline at end of file diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedDatabaseManager.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedDatabaseManager.kt index 253e1c0fe..08e661580 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedDatabaseManager.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedDatabaseManager.kt @@ -42,10 +42,11 @@ class FeedDatabaseManager(context: Context) { fun database() = database - fun asStreamItems(groupId: Long = -1): Flowable> { - val streams = - if (groupId >= 0) feedTable.getAllStreamsFromGroup(groupId) - else feedTable.getAllStreams() + fun asStreamItems(groupId: Long = FeedGroupEntity.GROUP_ALL_ID): Flowable> { + val streams = when (groupId) { + FeedGroupEntity.GROUP_ALL_ID -> feedTable.getAllStreams() + else -> feedTable.getAllStreamsFromGroup(groupId) + } return streams.map> { val items = ArrayList(it.size) @@ -56,15 +57,14 @@ class FeedDatabaseManager(context: Context) { fun outdatedSubscriptions(outdatedThreshold: Date) = feedTable.getAllOutdated(outdatedThreshold) - fun notLoadedCount(groupId: Long = -1): Flowable { - return if (groupId != -1L) { - feedTable.notLoadedCountForGroup(groupId) - } else { - feedTable.notLoadedCount() + fun notLoadedCount(groupId: Long = FeedGroupEntity.GROUP_ALL_ID): Flowable { + return when (groupId) { + FeedGroupEntity.GROUP_ALL_ID -> feedTable.notLoadedCount() + else -> feedTable.notLoadedCountForGroup(groupId) } } - fun outdatedSubscriptionsForGroup(groupId: Long = -1, outdatedThreshold: Date) = + fun outdatedSubscriptionsForGroup(groupId: Long = FeedGroupEntity.GROUP_ALL_ID, outdatedThreshold: Date) = feedTable.getAllOutdatedForGroup(groupId, outdatedThreshold) fun markAsOutdated(subscriptionId: Long) = feedTable @@ -148,10 +148,9 @@ class FeedDatabaseManager(context: Context) { } fun oldestSubscriptionUpdate(groupId: Long): Flowable> { - return if (groupId == -1L) { - feedTable.oldestSubscriptionUpdateFromAll() - } else { - feedTable.oldestSubscriptionUpdate(groupId) + return when (groupId) { + FeedGroupEntity.GROUP_ALL_ID -> feedTable.oldestSubscriptionUpdateFromAll() + else -> feedTable.oldestSubscriptionUpdate(groupId) } } diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt index 3abe01850..b16a9dfb7 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt @@ -20,7 +20,6 @@ package org.schabi.newpipe.local.feed import android.content.Intent -import android.os.Build import android.os.Bundle import android.os.Parcelable import android.view.* @@ -30,6 +29,7 @@ import icepick.State import kotlinx.android.synthetic.main.error_retry.* import kotlinx.android.synthetic.main.fragment_feed.* import org.schabi.newpipe.R +import org.schabi.newpipe.database.feed.model.FeedGroupEntity import org.schabi.newpipe.fragments.list.BaseListFragment import org.schabi.newpipe.local.feed.service.FeedLoadService import org.schabi.newpipe.report.UserAction @@ -41,7 +41,7 @@ class FeedFragment : BaseListFragment() { private lateinit var viewModel: FeedViewModel @State @JvmField var listState: Parcelable? = null - private var groupId = -1L + private var groupId = FeedGroupEntity.GROUP_ALL_ID private var groupName = "" private var oldestSubscriptionUpdate: Calendar? = null @@ -53,7 +53,7 @@ class FeedFragment : BaseListFragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - groupId = arguments?.getLong(KEY_GROUP_ID, -1) ?: -1 + groupId = arguments?.getLong(KEY_GROUP_ID, FeedGroupEntity.GROUP_ALL_ID) ?: FeedGroupEntity.GROUP_ALL_ID groupName = arguments?.getString(KEY_GROUP_NAME) ?: "" } @@ -279,7 +279,7 @@ class FeedFragment : BaseListFragment() { const val KEY_GROUP_NAME = "ARG_GROUP_NAME" @JvmStatic - fun newInstance(groupId: Long = -1, groupName: String? = null): FeedFragment { + fun newInstance(groupId: Long = FeedGroupEntity.GROUP_ALL_ID, groupName: String? = null): FeedFragment { val feedFragment = FeedFragment() feedFragment.arguments = Bundle().apply { diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt index 0e887f581..1a7757b8b 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt @@ -8,6 +8,7 @@ import io.reactivex.Flowable import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.functions.Function4 import io.reactivex.schedulers.Schedulers +import org.schabi.newpipe.database.feed.model.FeedGroupEntity import org.schabi.newpipe.extractor.stream.StreamInfoItem import org.schabi.newpipe.local.feed.service.FeedEventManager import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.* @@ -15,8 +16,8 @@ import org.schabi.newpipe.util.DEFAULT_THROTTLE_TIMEOUT import java.util.* import java.util.concurrent.TimeUnit -class FeedViewModel(applicationContext: Context, val groupId: Long = -1) : ViewModel() { - class Factory(val context: Context, val groupId: Long = -1) : ViewModelProvider.Factory { +class FeedViewModel(applicationContext: Context, val groupId: Long = FeedGroupEntity.GROUP_ALL_ID) : ViewModel() { + class Factory(val context: Context, val groupId: Long = FeedGroupEntity.GROUP_ALL_ID) : ViewModelProvider.Factory { @Suppress("UNCHECKED_CAST") override fun create(modelClass: Class): T { return FeedViewModel(context.applicationContext, groupId) as T diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt index db88aa5e7..4ff63400b 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt @@ -40,6 +40,7 @@ import org.reactivestreams.Subscriber import org.reactivestreams.Subscription import org.schabi.newpipe.MainActivity.DEBUG import org.schabi.newpipe.R +import org.schabi.newpipe.database.feed.model.FeedGroupEntity import org.schabi.newpipe.extractor.ListInfo import org.schabi.newpipe.extractor.exceptions.ReCaptchaException import org.schabi.newpipe.extractor.stream.StreamInfoItem @@ -109,7 +110,7 @@ class FeedLoadService : Service() { setupNotification() val defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this) - val groupId = intent.getLongExtra(EXTRA_GROUP_ID, -1) + val groupId = intent.getLongExtra(EXTRA_GROUP_ID, FeedGroupEntity.GROUP_ALL_ID) val useFeedExtractor = defaultSharedPreferences .getBoolean(getString(R.string.feed_use_dedicated_fetch_method_key), false) @@ -156,7 +157,7 @@ class FeedLoadService : Service() { } } - private fun startLoading(groupId: Long = -1, useFeedExtractor: Boolean, thresholdOutdatedMinutes: Int) { + private fun startLoading(groupId: Long = FeedGroupEntity.GROUP_ALL_ID, useFeedExtractor: Boolean, thresholdOutdatedMinutes: Int) { feedResultsHolder = ResultsHolder() val outdatedThreshold = Calendar.getInstance().apply { @@ -164,7 +165,7 @@ class FeedLoadService : Service() { }.time val subscriptions = when (groupId) { - -1L -> feedDatabaseManager.outdatedSubscriptions(outdatedThreshold) + FeedGroupEntity.GROUP_ALL_ID -> feedDatabaseManager.outdatedSubscriptions(outdatedThreshold) else -> feedDatabaseManager.outdatedSubscriptionsForGroup(groupId, outdatedThreshold) } diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt index 318a501c0..58175d5cd 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt @@ -22,6 +22,7 @@ import io.reactivex.disposables.CompositeDisposable import kotlinx.android.synthetic.main.dialog_title.view.* import kotlinx.android.synthetic.main.fragment_subscription.* import org.schabi.newpipe.R +import org.schabi.newpipe.database.feed.model.FeedGroupEntity import org.schabi.newpipe.extractor.channel.ChannelInfoItem import org.schabi.newpipe.fragments.BaseStateFragment import org.schabi.newpipe.local.subscription.SubscriptionViewModel.* @@ -200,7 +201,7 @@ class SubscriptionFragment : BaseStateFragment() { } carouselAdapter.setOnItemLongClickListener { item, _ -> if (item is FeedGroupCardItem) { - if (item.groupId == -1L) { + if (item.groupId == FeedGroupEntity.GROUP_ALL_ID) { return@setOnItemLongClickListener false } } diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialogViewModel.kt b/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialogViewModel.kt index 249461935..575c71f45 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialogViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialogViewModel.kt @@ -16,8 +16,8 @@ import org.schabi.newpipe.local.subscription.FeedGroupIcon import org.schabi.newpipe.local.subscription.SubscriptionManager -class FeedGroupDialogViewModel(applicationContext: Context, val groupId: Long = -1) : ViewModel() { - class Factory(val context: Context, val groupId: Long = -1) : ViewModelProvider.Factory { +class FeedGroupDialogViewModel(applicationContext: Context, val groupId: Long = FeedGroupEntity.GROUP_ALL_ID) : ViewModel() { + class Factory(val context: Context, val groupId: Long = FeedGroupEntity.GROUP_ALL_ID) : ViewModelProvider.Factory { @Suppress("UNCHECKED_CAST") override fun create(modelClass: Class): T { return FeedGroupDialogViewModel(context.applicationContext, groupId) as T diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/item/FeedGroupCardItem.kt b/app/src/main/java/org/schabi/newpipe/local/subscription/item/FeedGroupCardItem.kt index e0e06b181..a757dc5b3 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/item/FeedGroupCardItem.kt +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/item/FeedGroupCardItem.kt @@ -8,14 +8,17 @@ import org.schabi.newpipe.database.feed.model.FeedGroupEntity import org.schabi.newpipe.local.subscription.FeedGroupIcon data class FeedGroupCardItem( - val groupId: Long = -1, + val groupId: Long = FeedGroupEntity.GROUP_ALL_ID, val name: String, val icon: FeedGroupIcon ) : Item() { constructor (feedGroupEntity: FeedGroupEntity) : this(feedGroupEntity.uid, feedGroupEntity.name, feedGroupEntity.icon) override fun getId(): Long { - return if (groupId == -1L) super.getId() else groupId + return when (groupId) { + FeedGroupEntity.GROUP_ALL_ID -> super.getId() + else -> groupId + } } override fun getLayout(): Int = R.layout.feed_group_card_item diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index 2de8dc2bd..b6f73dac7 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -23,6 +23,7 @@ import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.R; import org.schabi.newpipe.RouterActivity; import org.schabi.newpipe.about.AboutActivity; +import org.schabi.newpipe.database.feed.model.FeedGroupEntity; import org.schabi.newpipe.download.DownloadActivity; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; @@ -344,7 +345,7 @@ public class NavigationHelper { } public static void openFeedFragment(FragmentManager fragmentManager) { - openFeedFragment(fragmentManager, -1, null); + openFeedFragment(fragmentManager, FeedGroupEntity.GROUP_ALL_ID, null); } public static void openFeedFragment(FragmentManager fragmentManager, long groupId, @Nullable String groupName) {