Modify badge API
Pass searchable as argument instead of badgeKey which is now obsolete
This commit is contained in:
@@ -47,5 +47,6 @@ dependencies {
|
||||
implementation(project(":notifications"))
|
||||
implementation(project(":preferences"))
|
||||
implementation(project(":base"))
|
||||
|
||||
implementation(project(":search"))
|
||||
implementation(project(":files"))
|
||||
}
|
||||
@@ -3,13 +3,14 @@ package de.mm20.launcher2.badges
|
||||
import android.content.Context
|
||||
import de.mm20.launcher2.badges.providers.*
|
||||
import de.mm20.launcher2.preferences.LauncherDataStore
|
||||
import de.mm20.launcher2.search.data.Searchable
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.*
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
|
||||
interface BadgeRepository {
|
||||
fun getBadge(badgeKey: String): Flow<Badge?>
|
||||
fun getBadge(searchable: Searchable): Flow<Badge?>
|
||||
}
|
||||
|
||||
internal class BadgeRepositoryImpl(private val context: Context) : BadgeRepository, KoinComponent {
|
||||
@@ -23,6 +24,7 @@ internal class BadgeRepositoryImpl(private val context: Context) : BadgeReposito
|
||||
scope.launch {
|
||||
dataStore.data.map { it.badges }.distinctUntilChanged().collectLatest {
|
||||
val providers = mutableListOf<BadgeProvider>()
|
||||
providers += WorkProfileBadgeProvider()
|
||||
if (it.notifications) {
|
||||
providers += NotificationBadgeProvider()
|
||||
}
|
||||
@@ -35,20 +37,19 @@ internal class BadgeRepositoryImpl(private val context: Context) : BadgeReposito
|
||||
if (it.suspendedApps) {
|
||||
providers += SuspendedAppsBadgeProvider()
|
||||
}
|
||||
providers += WorkProfileBadgeProvider(context)
|
||||
badgeProviders.value = providers
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getBadge(badgeKey: String): Flow<Badge?> = channelFlow {
|
||||
override fun getBadge(searchable: Searchable): Flow<Badge?> = channelFlow {
|
||||
withContext(Dispatchers.Default) {
|
||||
badgeProviders.collectLatest { providers ->
|
||||
if (providers.isEmpty()) {
|
||||
send(null)
|
||||
return@collectLatest
|
||||
}
|
||||
combine(providers.map { it.getBadge(badgeKey) }) { badges ->
|
||||
combine(providers.map { it.getBadge(searchable) }) { badges ->
|
||||
if (badges.all { it == null }) {
|
||||
return@combine null
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import de.mm20.launcher2.badges.Badge
|
||||
import de.mm20.launcher2.graphics.BadgeDrawable
|
||||
import de.mm20.launcher2.search.data.AppShortcut
|
||||
import de.mm20.launcher2.search.data.Searchable
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.channelFlow
|
||||
@@ -13,9 +15,9 @@ import kotlinx.coroutines.withContext
|
||||
class AppShortcutBadgeProvider(
|
||||
private val context: Context
|
||||
) : BadgeProvider {
|
||||
override fun getBadge(badgeKey: String): Flow<Badge?> = channelFlow {
|
||||
if (badgeKey.startsWith("shortcut://")) {
|
||||
val componentName = ComponentName.unflattenFromString(badgeKey.substring(11))
|
||||
override fun getBadge(searchable: Searchable): Flow<Badge?> = channelFlow {
|
||||
if (searchable is AppShortcut) {
|
||||
val componentName = searchable.launcherShortcut.activity
|
||||
if (componentName == null) {
|
||||
send(null)
|
||||
return@channelFlow
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.mm20.launcher2.badges.providers
|
||||
|
||||
import de.mm20.launcher2.badges.Badge
|
||||
import de.mm20.launcher2.search.data.Searchable
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface BadgeProvider {
|
||||
@@ -9,5 +10,5 @@ interface BadgeProvider {
|
||||
* BadgeRepository is waiting for values from every provider.
|
||||
* null must be emitted if no badge should be shown.
|
||||
*/
|
||||
fun getBadge(badgeKey: String): Flow<Badge?>
|
||||
fun getBadge(searchable: Searchable): Flow<Badge?>
|
||||
}
|
||||
@@ -3,17 +3,20 @@ package de.mm20.launcher2.badges.providers
|
||||
import android.util.Log
|
||||
import de.mm20.launcher2.badges.Badge
|
||||
import de.mm20.launcher2.badges.R
|
||||
import de.mm20.launcher2.search.data.File
|
||||
import de.mm20.launcher2.search.data.Searchable
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
|
||||
class CloudBadgeProvider: BadgeProvider {
|
||||
override fun getBadge(badgeKey: String): Flow<Badge?> = flow {
|
||||
when(badgeKey) {
|
||||
"gdrive://" -> emit(Badge(iconRes = R.drawable.ic_badge_gdrive))
|
||||
"onedrive://" -> emit(Badge(iconRes = R.drawable.ic_badge_onedrive))
|
||||
"owncloud://" -> emit(Badge(iconRes = R.drawable.ic_badge_owncloud))
|
||||
"nextcloud://" -> emit(Badge(iconRes = R.drawable.ic_badge_nextcloud))
|
||||
else -> emit(null)
|
||||
override fun getBadge(searchable: Searchable): Flow<Badge?> = flow {
|
||||
if (searchable is File) {
|
||||
val iconResId = searchable.providerIconRes
|
||||
if (iconResId != null) {
|
||||
emit(Badge(iconRes = iconResId))
|
||||
return@flow
|
||||
}
|
||||
}
|
||||
emit(null)
|
||||
}
|
||||
}
|
||||
+6
-3
@@ -3,6 +3,9 @@ package de.mm20.launcher2.badges.providers
|
||||
import android.app.Notification
|
||||
import de.mm20.launcher2.badges.Badge
|
||||
import de.mm20.launcher2.notifications.NotificationRepository
|
||||
import de.mm20.launcher2.search.data.Application
|
||||
import de.mm20.launcher2.search.data.LauncherApp
|
||||
import de.mm20.launcher2.search.data.Searchable
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.channelFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
@@ -13,9 +16,9 @@ import org.koin.core.component.inject
|
||||
class NotificationBadgeProvider : BadgeProvider, KoinComponent {
|
||||
private val notificationRepository: NotificationRepository by inject()
|
||||
|
||||
override fun getBadge(badgeKey: String): Flow<Badge?> = channelFlow {
|
||||
if (badgeKey.startsWith("app://")) {
|
||||
val packageName = badgeKey.substring(6)
|
||||
override fun getBadge(searchable: Searchable): Flow<Badge?> = channelFlow {
|
||||
if (searchable is Application) {
|
||||
val packageName = searchable.`package`
|
||||
notificationRepository.notifications.map {
|
||||
it.filter { it.packageName == packageName }
|
||||
}.collectLatest {
|
||||
|
||||
+6
-3
@@ -3,6 +3,9 @@ package de.mm20.launcher2.badges.providers
|
||||
import de.mm20.launcher2.applications.AppRepository
|
||||
import de.mm20.launcher2.badges.Badge
|
||||
import de.mm20.launcher2.badges.R
|
||||
import de.mm20.launcher2.search.data.Application
|
||||
import de.mm20.launcher2.search.data.LauncherApp
|
||||
import de.mm20.launcher2.search.data.Searchable
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.channelFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
@@ -12,9 +15,9 @@ import org.koin.core.component.inject
|
||||
class SuspendedAppsBadgeProvider : BadgeProvider, KoinComponent {
|
||||
private val appRepository: AppRepository by inject()
|
||||
|
||||
override fun getBadge(badgeKey: String): Flow<Badge?> = channelFlow {
|
||||
if (badgeKey.startsWith("app://")) {
|
||||
val packageName = badgeKey.substring(6)
|
||||
override fun getBadge(searchable: Searchable): Flow<Badge?> = channelFlow {
|
||||
if (searchable is Application) {
|
||||
val packageName = searchable.`package`
|
||||
appRepository.getSuspendedPackages().collectLatest {
|
||||
if (it.contains(packageName)) {
|
||||
send(
|
||||
|
||||
+10
-15
@@ -1,26 +1,21 @@
|
||||
package de.mm20.launcher2.badges.providers
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Process
|
||||
import de.mm20.launcher2.badges.Badge
|
||||
import de.mm20.launcher2.badges.R
|
||||
import de.mm20.launcher2.ktx.getSerialNumber
|
||||
import de.mm20.launcher2.search.data.AppShortcut
|
||||
import de.mm20.launcher2.search.data.LauncherApp
|
||||
import de.mm20.launcher2.search.data.Searchable
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
|
||||
class WorkProfileBadgeProvider(private val context: Context) : BadgeProvider {
|
||||
override fun getBadge(badgeKey: String): Flow<Badge?> = flow {
|
||||
if (badgeKey.startsWith("profile://")) {
|
||||
val serialNumber = badgeKey.substring(10).toLong()
|
||||
if (serialNumber != Process.myUserHandle().getSerialNumber(context)) {
|
||||
emit(
|
||||
Badge(
|
||||
iconRes = R.drawable.ic_badge_workprofile
|
||||
)
|
||||
class WorkProfileBadgeProvider : BadgeProvider {
|
||||
override fun getBadge(searchable: Searchable): Flow<Badge?> = flow {
|
||||
if (searchable is LauncherApp && !searchable.isMainProfile || searchable is AppShortcut && !searchable.isMainProfile) {
|
||||
emit(
|
||||
Badge(
|
||||
iconRes = R.drawable.ic_badge_workprofile
|
||||
)
|
||||
} else {
|
||||
emit(null)
|
||||
}
|
||||
)
|
||||
} else {
|
||||
emit(null)
|
||||
}
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 10 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 9.0 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 9.4 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 11 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 10 KiB |
Reference in New Issue
Block a user