From c4f0917b052f99d86bee13efd29e9986aaa15dea Mon Sep 17 00:00:00 2001 From: Damontecres Date: Sat, 16 May 2026 17:11:33 -0400 Subject: [PATCH] Add forgotten file --- .../wholphin/services/UserSwitchListener.kt | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 app/src/main/java/com/github/damontecres/wholphin/services/UserSwitchListener.kt diff --git a/app/src/main/java/com/github/damontecres/wholphin/services/UserSwitchListener.kt b/app/src/main/java/com/github/damontecres/wholphin/services/UserSwitchListener.kt new file mode 100644 index 00000000..e8ac6e0d --- /dev/null +++ b/app/src/main/java/com/github/damontecres/wholphin/services/UserSwitchListener.kt @@ -0,0 +1,106 @@ +package com.github.damontecres.wholphin.services + +import android.content.Context +import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.app.AppCompatDelegate +import androidx.core.os.LocaleListCompat +import androidx.lifecycle.asFlow +import androidx.lifecycle.lifecycleScope +import com.github.damontecres.wholphin.BuildConfig +import com.github.damontecres.wholphin.data.SeerrServerDao +import com.github.damontecres.wholphin.data.ServerRepository +import com.github.damontecres.wholphin.data.model.JellyfinUser +import com.github.damontecres.wholphin.data.model.SeerrAuthMethod +import com.github.damontecres.wholphin.ui.launchDefault +import com.github.damontecres.wholphin.ui.launchIO +import dagger.hilt.android.qualifiers.ActivityContext +import dagger.hilt.android.scopes.ActivityScoped +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.supervisorScope +import timber.log.Timber +import javax.inject.Inject + +/** + * Listens for JF user switching in the app to also switch other settings like Seerr user/server + */ +@ActivityScoped +class UserSwitchListener + @Inject + constructor( + @param:ActivityContext private val context: Context, + private val serverRepository: ServerRepository, + private val seerrServerRepository: SeerrServerRepository, + private val seerrServerDao: SeerrServerDao, + private val seerrApi: SeerrApi, + private val homeSettingsService: HomeSettingsService, + ) { + init { + context as AppCompatActivity + context.lifecycleScope.launchDefault { + serverRepository.currentUser.asFlow().collect { user -> + Timber.d("New user") + seerrServerRepository.clear() + homeSettingsService.currentSettings.update { HomePageResolvedSettings.EMPTY } + if (user != null) { + switchUser(user) + } + } + } + } + + private suspend fun switchUser(user: JellyfinUser) = + supervisorScope { + // Switch the locale to either the user's choice or the system default (empty) + val localeList = + user.uiLanguage?.let { LocaleListCompat.forLanguageTags(it) } + ?: LocaleListCompat.getEmptyLocaleList() + Timber.i("Switching locale to %s", localeList) + AppCompatDelegate.setApplicationLocales(localeList) + + // Check for home settings + launchIO { + homeSettingsService.loadCurrentSettings(user.id) + } + if (BuildConfig.DISCOVER_ENABLED) { + // Check for seerr server + launchIO { + seerrServerDao + .getUsersByJellyfinUser(user.rowId) + .lastOrNull() + ?.let { seerrUser -> + val server = + seerrServerDao.getServer(seerrUser.serverId)?.server + if (server != null) { + Timber.i("Found a seerr user & server") + try { + seerrApi.update(server.url, seerrUser.credential) + val userConfig = + if (seerrUser.authMethod != SeerrAuthMethod.API_KEY) { + seerrLogin( + seerrApi.api, + seerrUser.authMethod, + seerrUser.username, + seerrUser.password, + ) + } else { + seerrApi.api.usersApi.authMeGet() + } + seerrServerRepository.set( + server, + seerrUser, + userConfig, + ) + } catch (ex: Exception) { + Timber.w( + ex, + "Error logging into %s", + server.url, + ) + seerrServerRepository.error(server, seerrUser, ex) + } + } + } + } + } + } + }