diff --git a/app/src/main/java/io/nekohasekai/sfa/compose/screen/configuration/NewProfileViewModel.kt b/app/src/main/java/io/nekohasekai/sfa/compose/screen/configuration/NewProfileViewModel.kt index 5c6b92f..4be44d1 100644 --- a/app/src/main/java/io/nekohasekai/sfa/compose/screen/configuration/NewProfileViewModel.kt +++ b/app/src/main/java/io/nekohasekai/sfa/compose/screen/configuration/NewProfileViewModel.kt @@ -9,7 +9,6 @@ import io.nekohasekai.sfa.R import io.nekohasekai.sfa.bg.UpdateProfileWork import io.nekohasekai.sfa.database.Profile import io.nekohasekai.sfa.database.ProfileManager -import io.nekohasekai.sfa.database.Settings import io.nekohasekai.sfa.database.TypedProfile import io.nekohasekai.sfa.utils.HTTPClient import kotlinx.coroutines.Dispatchers @@ -258,13 +257,8 @@ class NewProfileViewModel(application: Application) : AndroidViewModel(applicati Libbox.checkConfig(configContent) configFile.writeText(configContent) - // Create profile in database - ProfileManager.create(profile) - - // If no profile is currently selected, select this one - if (Settings.selectedProfile == -1L) { - Settings.selectedProfile = profile.id - } + // Create profile in database and select it + ProfileManager.create(profile, andSelect = true) return profile } @@ -297,13 +291,8 @@ class NewProfileViewModel(application: Application) : AndroidViewModel(applicati configFile.writeText(configContent) - // Create profile in database - ProfileManager.create(profile) - - // If no profile is currently selected, select this one - if (Settings.selectedProfile == -1L) { - Settings.selectedProfile = profile.id - } + // Create profile in database and select it + ProfileManager.create(profile, andSelect = true) // Reconfigure updater if auto-update is enabled if (state.autoUpdate) { diff --git a/app/src/main/java/io/nekohasekai/sfa/compose/screen/configuration/ProfileImportHandler.kt b/app/src/main/java/io/nekohasekai/sfa/compose/screen/configuration/ProfileImportHandler.kt index e203866..46899a1 100644 --- a/app/src/main/java/io/nekohasekai/sfa/compose/screen/configuration/ProfileImportHandler.kt +++ b/app/src/main/java/io/nekohasekai/sfa/compose/screen/configuration/ProfileImportHandler.kt @@ -8,7 +8,6 @@ import io.nekohasekai.libbox.ProfileContent import io.nekohasekai.sfa.R import io.nekohasekai.sfa.database.Profile import io.nekohasekai.sfa.database.ProfileManager -import io.nekohasekai.sfa.database.Settings import io.nekohasekai.sfa.database.TypedProfile import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -178,13 +177,8 @@ class ProfileImportHandler(private val context: Context) { configFile.writeText(content.config) typedProfile.path = configFile.path - // Create profile in database - ProfileManager.create(profile) - - // If no profile is currently selected, select this one - if (Settings.selectedProfile == -1L) { - Settings.selectedProfile = profile.id - } + // Create profile in database and select it + ProfileManager.create(profile, andSelect = true) return ImportResult.Success(profile) } @@ -213,12 +207,8 @@ class ProfileImportHandler(private val context: Context) { configFile.writeText("{}") typedProfile.path = configFile.path - ProfileManager.create(profile) - - // If no profile is currently selected, select this one - if (Settings.selectedProfile == -1L) { - Settings.selectedProfile = profile.id - } + // Create profile in database and select it + ProfileManager.create(profile, andSelect = true) return ImportResult.Success(profile) } @@ -324,8 +314,8 @@ class ProfileImportHandler(private val context: Context) { configFile.writeText(jsonContent) typedProfile.path = configFile.path - // Create profile in database - ProfileManager.create(profile) + // Create profile in database and select it + ProfileManager.create(profile, andSelect = true) ImportResult.Success(profile) } catch (e: Exception) { diff --git a/app/src/main/java/io/nekohasekai/sfa/database/ProfileManager.kt b/app/src/main/java/io/nekohasekai/sfa/database/ProfileManager.kt index 7ba4629..a3b64eb 100644 --- a/app/src/main/java/io/nekohasekai/sfa/database/ProfileManager.kt +++ b/app/src/main/java/io/nekohasekai/sfa/database/ProfileManager.kt @@ -47,8 +47,11 @@ object ProfileManager { return instance.profileDao().get(id) } - suspend fun create(profile: Profile): Profile { + suspend fun create(profile: Profile, andSelect: Boolean = false): Profile { profile.id = instance.profileDao().insert(profile) + if (andSelect) { + Settings.selectedProfile = profile.id + } for (callback in callbacks.toList()) { callback() }