Refactor preferences module

This commit is contained in:
MM20
2024-01-19 22:29:37 +01:00
parent 94aee6baba
commit 92ddc75060
171 changed files with 3202 additions and 2962 deletions
@@ -4,5 +4,5 @@ import org.koin.android.ext.koin.androidContext
import org.koin.dsl.module
val musicModule = module {
single<MusicService> { MusicServiceImpl(androidContext(), get()) }
single<MusicService> { MusicServiceImpl(androidContext(), get(), get()) }
}
@@ -4,21 +4,17 @@ import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.media.AudioManager
import android.media.MediaMetadata
import android.media.session.MediaController
import android.media.session.MediaSession
import android.media.session.PlaybackState.CustomAction
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import android.service.notification.StatusBarNotification
import android.view.KeyEvent
import androidx.core.app.NotificationCompat
import androidx.core.content.edit
import androidx.core.graphics.drawable.toBitmap
import coil.imageLoader
@@ -27,7 +23,7 @@ import coil.size.Scale
import de.mm20.launcher2.crashreporter.CrashReporter
import de.mm20.launcher2.notifications.Notification
import de.mm20.launcher2.notifications.NotificationRepository
import de.mm20.launcher2.preferences.LauncherDataStore
import de.mm20.launcher2.preferences.media.MediaSettings
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
@@ -47,7 +43,6 @@ import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.io.IOException
interface MusicService {
@@ -81,11 +76,11 @@ interface MusicService {
internal class MusicServiceImpl(
private val context: Context,
notificationRepository: NotificationRepository
notificationRepository: NotificationRepository,
private val settings: MediaSettings,
) : MusicService, KoinComponent {
private val scope = CoroutineScope(Job() + Dispatchers.Default)
private val dataStore: LauncherDataStore by inject()
private val preferences: SharedPreferences by lazy {
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
@@ -108,12 +103,12 @@ internal class MusicServiceImpl(
private val currentMediaController: SharedFlow<MediaController?> =
combine(
notificationRepository.notifications,
dataStore.data.map { it.musicWidget }
settings,
) { notifications, settings ->
withContext(Dispatchers.Default) {
val musicApps = getEnabledPlayerPackages(
settings.allowListList.toSet(),
settings.denyListList.toSet()
settings.allowList,
settings.denyList,
)
val sbn: Notification? = notifications.filter {
it.mediaSessionToken != null && musicApps.contains(it.packageName)