From c45c4a1ad8d25c8ed09692e9cd37f5c10b18cc1a Mon Sep 17 00:00:00 2001 From: Kr328 <39107975+Kr328@users.noreply.github.com> Date: Tue, 14 Apr 2020 14:18:41 +0800 Subject: [PATCH] fix profile edit --- .../com/github/kr328/clash/ProfilesActivity.kt | 18 ++++++++---------- .../kr328/clash/service/ProfileProcessor.kt | 3 +++ .../kr328/clash/service/ProfileService.kt | 12 +++--------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/github/kr328/clash/ProfilesActivity.kt b/app/src/main/java/com/github/kr328/clash/ProfilesActivity.kt index 5723903..98fdab1 100644 --- a/app/src/main/java/com/github/kr328/clash/ProfilesActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/ProfilesActivity.kt @@ -28,7 +28,7 @@ class ProfilesActivity : BaseActivity(), ProfileAdapter.Callback, ProfilesMenu.C private var backgroundJob: Job? = null private val reloadMutex = Mutex() - private val editorStack = Stack() + private val editorStack = Stack() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -64,10 +64,11 @@ class ProfilesActivity : BaseActivity(), ProfileAdapter.Callback, ProfilesMenu.C override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { if (requestCode == EDITOR_REQUEST_CODE) { launch { - val id = editorStack.pop() + val profile = editorStack.pop() withProfile { - startUpdate(id) + updateMetadata(profile.id, profile) + startUpdate(profile.id) } } @@ -120,16 +121,13 @@ class ProfilesActivity : BaseActivity(), ProfileAdapter.Callback, ProfilesMenu.C startActivity(ProfileEditActivity::class.intent.putExtra("id", id)) } - private fun openEditor(id: Long) = launch { + private fun openEditor(profile: Profile) = launch { try { - if (id < 0) - throw FileNotFoundException() - val uri = withProfile { - acquireTempUri(id) + acquireTempUri(profile.id) } ?: throw FileNotFoundException() - editorStack.push(id) + editorStack.push(profile.copy(uri = Uri.parse(uri))) startActivityForResult( Intent(Intent.ACTION_VIEW) @@ -148,7 +146,7 @@ class ProfilesActivity : BaseActivity(), ProfileAdapter.Callback, ProfilesMenu.C } override fun onOpenEditor(entity: Profile) { - openEditor(entity.id) + openEditor(entity) } override fun onUpdate(entity: Profile) { diff --git a/service/src/main/java/com/github/kr328/clash/service/ProfileProcessor.kt b/service/src/main/java/com/github/kr328/clash/service/ProfileProcessor.kt index abf278f..b60e042 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ProfileProcessor.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ProfileProcessor.kt @@ -3,6 +3,7 @@ package com.github.kr328.clash.service import android.content.Context import android.net.Uri import android.webkit.URLUtil +import com.github.kr328.clash.common.utils.Log import com.github.kr328.clash.core.Clash import com.github.kr328.clash.service.data.ProfileDao import com.github.kr328.clash.service.model.Profile @@ -30,6 +31,8 @@ object ProfileProcessor { context.resolveBaseDir(metadata.id) ) + Log.d("${metadata.uri}") + val entity = if (metadata.type == Type.FILE) metadata.copy( uri = ProfileProvider.resolveUri( diff --git a/service/src/main/java/com/github/kr328/clash/service/ProfileService.kt b/service/src/main/java/com/github/kr328/clash/service/ProfileService.kt index 36bd931..e5faf08 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ProfileService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ProfileService.kt @@ -145,17 +145,11 @@ class ProfileService : BaseService() { val tempFile = service.resolveTempProfileFile(id) - file.copyTo(tempFile) + tempFile.parentFile?.mkdirs() - val uri = ProfileProvider.resolveUri(service, tempFile) + file.copyTo(tempFile, overwrite = true) - return runBlocking { - lock.withLock { - pending[id] = queryMetadataById(id)?.copy(uri = uri) ?: return@withLock - } - - uri.toString() - } + return ProfileProvider.resolveUri(service, tempFile).toString() } override fun updateMetadata(id: Long, metadata: Profile?) {