Reload service after remote profile updated
This commit is contained in:
@@ -11,6 +11,7 @@ import androidx.work.WorkerParameters
|
|||||||
import io.nekohasekai.libbox.Libbox
|
import io.nekohasekai.libbox.Libbox
|
||||||
import io.nekohasekai.sfa.Application
|
import io.nekohasekai.sfa.Application
|
||||||
import io.nekohasekai.sfa.database.ProfileManager
|
import io.nekohasekai.sfa.database.ProfileManager
|
||||||
|
import io.nekohasekai.sfa.database.Settings
|
||||||
import io.nekohasekai.sfa.database.TypedProfile
|
import io.nekohasekai.sfa.database.TypedProfile
|
||||||
import io.nekohasekai.sfa.utils.HTTPClient
|
import io.nekohasekai.sfa.utils.HTTPClient
|
||||||
import java.io.File
|
import java.io.File
|
||||||
@@ -63,10 +64,12 @@ class UpdateProfileWork {
|
|||||||
appContext: Context, params: WorkerParameters
|
appContext: Context, params: WorkerParameters
|
||||||
) : CoroutineWorker(appContext, params) {
|
) : CoroutineWorker(appContext, params) {
|
||||||
override suspend fun doWork(): Result {
|
override suspend fun doWork(): Result {
|
||||||
|
var selectedProfileUpdated = false
|
||||||
val remoteProfiles = ProfileManager.list()
|
val remoteProfiles = ProfileManager.list()
|
||||||
.filter { it.typed.type == TypedProfile.Type.Remote && it.typed.autoUpdate }
|
.filter { it.typed.type == TypedProfile.Type.Remote && it.typed.autoUpdate }
|
||||||
if (remoteProfiles.isEmpty()) return Result.success()
|
if (remoteProfiles.isEmpty()) return Result.success()
|
||||||
var success = true
|
var success = true
|
||||||
|
val selectedProfile = Settings.selectedProfile
|
||||||
for (profile in remoteProfiles) {
|
for (profile in remoteProfiles) {
|
||||||
val lastSeconds =
|
val lastSeconds =
|
||||||
(System.currentTimeMillis() - profile.typed.lastUpdated.time) / 1000L
|
(System.currentTimeMillis() - profile.typed.lastUpdated.time) / 1000L
|
||||||
@@ -76,7 +79,13 @@ class UpdateProfileWork {
|
|||||||
try {
|
try {
|
||||||
val content = HTTPClient().use { it.getString(profile.typed.remoteURL) }
|
val content = HTTPClient().use { it.getString(profile.typed.remoteURL) }
|
||||||
Libbox.checkConfig(content)
|
Libbox.checkConfig(content)
|
||||||
File(profile.typed.path).writeText(content)
|
val file = File(profile.typed.path)
|
||||||
|
if (file.readText() != content) {
|
||||||
|
File(profile.typed.path).writeText(content)
|
||||||
|
if (profile.id == selectedProfile) {
|
||||||
|
selectedProfileUpdated = true
|
||||||
|
}
|
||||||
|
}
|
||||||
profile.typed.lastUpdated = Date()
|
profile.typed.lastUpdated = Date()
|
||||||
ProfileManager.update(profile)
|
ProfileManager.update(profile)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@@ -84,6 +93,11 @@ class UpdateProfileWork {
|
|||||||
success = false
|
success = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (selectedProfileUpdated) {
|
||||||
|
runCatching {
|
||||||
|
Libbox.newStandaloneCommandClient().serviceReload()
|
||||||
|
}
|
||||||
|
}
|
||||||
return if (success) {
|
return if (success) {
|
||||||
Result.success()
|
Result.success()
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import io.nekohasekai.sfa.bg.UpdateProfileWork
|
|||||||
import io.nekohasekai.sfa.constant.EnabledType
|
import io.nekohasekai.sfa.constant.EnabledType
|
||||||
import io.nekohasekai.sfa.database.Profile
|
import io.nekohasekai.sfa.database.Profile
|
||||||
import io.nekohasekai.sfa.database.ProfileManager
|
import io.nekohasekai.sfa.database.ProfileManager
|
||||||
|
import io.nekohasekai.sfa.database.Settings
|
||||||
import io.nekohasekai.sfa.database.TypedProfile
|
import io.nekohasekai.sfa.database.TypedProfile
|
||||||
import io.nekohasekai.sfa.databinding.ActivityEditProfileBinding
|
import io.nekohasekai.sfa.databinding.ActivityEditProfileBinding
|
||||||
import io.nekohasekai.sfa.ktx.addTextChangedListener
|
import io.nekohasekai.sfa.ktx.addTextChangedListener
|
||||||
@@ -166,10 +167,17 @@ class EditProfileActivity : AbstractActivity<ActivityEditProfileBinding>() {
|
|||||||
private fun updateProfile(view: View) {
|
private fun updateProfile(view: View) {
|
||||||
binding.progressView.isVisible = true
|
binding.progressView.isVisible = true
|
||||||
lifecycleScope.launch(Dispatchers.IO) {
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
|
var selectedProfileUpdated = false
|
||||||
try {
|
try {
|
||||||
val content = HTTPClient().use { it.getString(profile.typed.remoteURL) }
|
val content = HTTPClient().use { it.getString(profile.typed.remoteURL) }
|
||||||
Libbox.checkConfig(content)
|
Libbox.checkConfig(content)
|
||||||
File(profile.typed.path).writeText(content)
|
val file = File(profile.typed.path)
|
||||||
|
if (file.readText() != content) {
|
||||||
|
File(profile.typed.path).writeText(content)
|
||||||
|
if (profile.id == Settings.selectedProfile) {
|
||||||
|
selectedProfileUpdated = true
|
||||||
|
}
|
||||||
|
}
|
||||||
profile.typed.lastUpdated = Date()
|
profile.typed.lastUpdated = Date()
|
||||||
ProfileManager.update(profile)
|
ProfileManager.update(profile)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@@ -182,6 +190,11 @@ class EditProfileActivity : AbstractActivity<ActivityEditProfileBinding>() {
|
|||||||
DateFormat.getDateTimeInstance().format(profile.typed.lastUpdated)
|
DateFormat.getDateTimeInstance().format(profile.typed.lastUpdated)
|
||||||
binding.progressView.isVisible = false
|
binding.progressView.isVisible = false
|
||||||
}
|
}
|
||||||
|
if (selectedProfileUpdated) {
|
||||||
|
runCatching {
|
||||||
|
Libbox.newStandaloneCommandClient().serviceReload()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user