Add profile sharing

This commit is contained in:
世界
2023-07-30 21:16:05 +08:00
parent 9575764f40
commit fe0b3fdce3
22 changed files with 302 additions and 87 deletions

View File

@@ -7,7 +7,17 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
@Suppress("RedundantSuspendModifier")
object Profiles {
object ProfileManager {
private val callbacks = mutableListOf<() -> Unit>()
fun registerCallback(callback: () -> Unit) {
callbacks.add(callback)
}
fun unregisterCallback(callback: () -> Unit) {
callbacks.remove(callback)
}
private val instance by lazy {
Application.application.getDatabasePath(Path.PROFILES_DATABASE_PATH).parentFile?.mkdirs()
@@ -27,23 +37,50 @@ object Profiles {
suspend fun create(profile: Profile): Profile {
profile.id = instance.profileDao().insert(profile)
for (callback in callbacks.toList()) {
callback()
}
return profile
}
suspend fun update(profile: Profile): Int {
return instance.profileDao().update(profile)
try {
return instance.profileDao().update(profile)
} finally {
for (callback in callbacks.toList()) {
callback()
}
}
}
suspend fun update(profiles: List<Profile>): Int {
return instance.profileDao().update(profiles)
try {
return instance.profileDao().update(profiles)
} finally {
for (callback in callbacks.toList()) {
callback()
}
}
}
suspend fun delete(profile: Profile): Int {
return instance.profileDao().delete(profile)
try {
return instance.profileDao().delete(profile)
} finally {
for (callback in callbacks.toList()) {
callback()
}
}
}
suspend fun delete(profiles: List<Profile>): Int {
return instance.profileDao().delete(profiles)
try {
return instance.profileDao().delete(profiles)
} finally {
for (callback in callbacks.toList()) {
callback()
}
}
}
suspend fun list(): List<Profile> {

View File

@@ -80,7 +80,7 @@ object Settings {
private suspend fun needVPNService(): Boolean {
val selectedProfileId = selectedProfile
if (selectedProfileId == -1L) return false
val profile = Profiles.get(selectedProfile) ?: return false
val profile = ProfileManager.get(selectedProfile) ?: return false
val content = JSONObject(File(profile.typed.path).readText())
val inbounds = content.getJSONArray("inbounds")
for (index in 0 until inbounds.length()) {