Auto-select newly created profiles

Previously, profiles were only auto-selected when no profile was
selected. Now any new profile (created manually or imported) is
immediately selected, improving user experience.
This commit is contained in:
世界
2025-12-18 19:30:07 +08:00
parent 7b39c1dd5a
commit d76ccd0ed3
3 changed files with 14 additions and 32 deletions

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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()
}