diff --git a/app/src/main/java/io/nekohasekai/sfa/database/Profile.kt b/app/src/main/java/io/nekohasekai/sfa/database/Profile.kt index 95deec2..57c1d69 100644 --- a/app/src/main/java/io/nekohasekai/sfa/database/Profile.kt +++ b/app/src/main/java/io/nekohasekai/sfa/database/Profile.kt @@ -49,9 +49,12 @@ class Profile( @Query("DELETE FROM profiles") fun clear() - @Query("SELECT MAX(userOrder) + 1 FROM profiles") + @Query("SELECT MAX(userOrder) + 1 FROM profiles") fun nextOrder(): Long? + @Query("SELECT MAX(id) + 1 FROM profiles") + fun nextFileID(): Long? + } } 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 ef33421..881b6f1 100644 --- a/app/src/main/java/io/nekohasekai/sfa/database/ProfileManager.kt +++ b/app/src/main/java/io/nekohasekai/sfa/database/ProfileManager.kt @@ -37,6 +37,11 @@ object ProfileManager { return instance.profileDao().nextOrder() ?: 0 } + suspend fun nextFileID(): Long { + return instance.profileDao().nextFileID() ?: 1 + } + + suspend fun get(id: Long): Profile? { return instance.profileDao().get(id) } diff --git a/app/src/main/java/io/nekohasekai/sfa/ui/profile/NewProfileActivity.kt b/app/src/main/java/io/nekohasekai/sfa/ui/profile/NewProfileActivity.kt index 936f15d..5c3272e 100644 --- a/app/src/main/java/io/nekohasekai/sfa/ui/profile/NewProfileActivity.kt +++ b/app/src/main/java/io/nekohasekai/sfa/ui/profile/NewProfileActivity.kt @@ -142,12 +142,15 @@ class NewProfileActivity : AbstractActivity() { val typedProfile = TypedProfile() val profile = Profile(name = binding.name.text, typed = typedProfile) profile.userOrder = ProfileManager.nextOrder() + val fileID = ProfileManager.nextFileID() + val configDirectory = File(filesDir, "configs").also { it.mkdirs() } + val configFile = File(configDirectory, "$fileID.json") + typedProfile.path = configFile.path when (binding.type.text) { TypedProfile.Type.Local.name -> { typedProfile.type = TypedProfile.Type.Local - val configDirectory = File(filesDir, "configs").also { it.mkdirs() } - val configFile = File(configDirectory, "${profile.userOrder}.json") + when (binding.fileSourceMenu.text) { FileSource.CreateNew.formatted -> { configFile.writeText("{}") @@ -166,23 +169,18 @@ class NewProfileActivity : AbstractActivity() { } else { error("unsupported source: $sourceURL") } - Libbox.checkConfig(content) configFile.writeText(content) } } - typedProfile.path = configFile.path } TypedProfile.Type.Remote.name -> { typedProfile.type = TypedProfile.Type.Remote - val configDirectory = File(filesDir, "configs").also { it.mkdirs() } - val configFile = File(configDirectory, "${profile.userOrder}.json") val remoteURL = binding.remoteURL.text val content = HTTPClient().use { it.getString(remoteURL) } Libbox.checkConfig(content) configFile.writeText(content) - typedProfile.path = configFile.path typedProfile.remoteURL = remoteURL typedProfile.lastUpdated = Date() typedProfile.autoUpdate = EnabledType.valueOf(binding.autoUpdate.text).boolValue