Fix profile path

This commit is contained in:
世界 2023-11-07 22:16:15 +08:00
parent 273ddc7266
commit fb705ed8a4
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 14 additions and 8 deletions

View file

@ -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?
}
}

View file

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

View file

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