fix:背景图片选择器用GetContent以支持浏览选项 (#1630)
This commit is contained in:
1 parent
65109f2898
commit
366d547bcd
2 files changed
+23
-15
No files matched your search
@@ -6,7 +6,6 @@ import android.view.View
|
|||||||
import android.widget.FrameLayout
|
import android.widget.FrameLayout
|
||||||
import android.widget.ImageView
|
import android.widget.ImageView
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.PickVisualMediaRequest
|
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
@@ -166,14 +165,14 @@ fun ReadBookRouteScreen(
|
|||||||
}
|
}
|
||||||
|
|
||||||
val readStyleImagePicker = rememberLauncherForActivityResult(
|
val readStyleImagePicker = rememberLauncherForActivityResult(
|
||||||
ActivityResultContracts.PickVisualMedia()
|
ActivityResultContracts.GetContent()
|
||||||
) { uri ->
|
) { uri ->
|
||||||
uri?.let { viewModel.onIntent(ReadBookIntent.ReadStyleImageSelected(it)) }
|
uri?.let { viewModel.onIntent(ReadBookIntent.ReadStyleImageSelected(it)) }
|
||||||
}
|
}
|
||||||
|
|
||||||
var pendingReadStyleImageIsNight by remember { mutableStateOf(false) }
|
var pendingReadStyleImageIsNight by remember { mutableStateOf(false) }
|
||||||
val readStyleImagePickerForMode = rememberLauncherForActivityResult(
|
val readStyleImagePickerForMode = rememberLauncherForActivityResult(
|
||||||
ActivityResultContracts.PickVisualMedia()
|
ActivityResultContracts.GetContent()
|
||||||
) { uri ->
|
) { uri ->
|
||||||
uri?.let {
|
uri?.let {
|
||||||
viewModel.onIntent(ReadBookIntent.ReadStyleImageSelectedForMode(it, pendingReadStyleImageIsNight))
|
viewModel.onIntent(ReadBookIntent.ReadStyleImageSelectedForMode(it, pendingReadStyleImageIsNight))
|
||||||
@@ -368,15 +367,11 @@ fun ReadBookRouteScreen(
|
|||||||
booksDirPicker.launch(null)
|
booksDirPicker.launch(null)
|
||||||
}
|
}
|
||||||
is ReadBookEffect.OpenReadStyleImagePicker -> {
|
is ReadBookEffect.OpenReadStyleImagePicker -> {
|
||||||
readStyleImagePicker.launch(
|
readStyleImagePicker.launch("image/*")
|
||||||
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
is ReadBookEffect.OpenReadStyleImagePickerForMode -> {
|
is ReadBookEffect.OpenReadStyleImagePickerForMode -> {
|
||||||
pendingReadStyleImageIsNight = effect.isNight
|
pendingReadStyleImageIsNight = effect.isNight
|
||||||
readStyleImagePickerForMode.launch(
|
readStyleImagePickerForMode.launch("image/*")
|
||||||
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
is ReadBookEffect.OpenReadStyleImport -> {
|
is ReadBookEffect.OpenReadStyleImport -> {
|
||||||
readStyleImportPicker.launch(
|
readStyleImportPicker.launch(
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import android.content.Intent
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.provider.OpenableColumns
|
import android.provider.OpenableColumns
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.PickVisualMediaRequest
|
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.foundation.Canvas
|
import androidx.compose.foundation.Canvas
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
@@ -44,6 +43,7 @@ import androidx.compose.ui.unit.sp
|
|||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
import io.legado.app.R
|
import io.legado.app.R
|
||||||
|
import io.legado.app.constant.AppLog
|
||||||
import io.legado.app.data.entities.HighlightRule
|
import io.legado.app.data.entities.HighlightRule
|
||||||
import io.legado.app.data.repository.ReadSettingsRepository
|
import io.legado.app.data.repository.ReadSettingsRepository
|
||||||
import io.legado.app.data.repository.configNames
|
import io.legado.app.data.repository.configNames
|
||||||
@@ -63,8 +63,11 @@ import io.legado.app.ui.widget.components.settingItem.TinyDropdownSettingItem
|
|||||||
import io.legado.app.ui.widget.components.settingItem.TinySliderSettingItem
|
import io.legado.app.ui.widget.components.settingItem.TinySliderSettingItem
|
||||||
import io.legado.app.ui.widget.components.settingItem.TinySwitchSettingItem
|
import io.legado.app.ui.widget.components.settingItem.TinySwitchSettingItem
|
||||||
import io.legado.app.ui.widget.components.text.AppText
|
import io.legado.app.ui.widget.components.text.AppText
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import splitties.init.appCtx
|
import splitties.init.appCtx
|
||||||
|
import io.legado.app.utils.toastOnUi
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@@ -77,6 +80,7 @@ fun HighlightRuleEditSheet(
|
|||||||
val isNew = rule == null
|
val isNew = rule == null
|
||||||
val initial = remember(show, rule) { rule ?: HighlightRule() }
|
val initial = remember(show, rule) { rule ?: HighlightRule() }
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
val coroutineScope = rememberCoroutineScope()
|
||||||
|
|
||||||
// Rule info state
|
// Rule info state
|
||||||
var pattern by remember(show, rule) { mutableStateOf(initial.pattern) }
|
var pattern by remember(show, rule) { mutableStateOf(initial.pattern) }
|
||||||
@@ -139,9 +143,12 @@ fun HighlightRuleEditSheet(
|
|||||||
|
|
||||||
// System photo picker, with the contract's built-in fallback on older devices.
|
// System photo picker, with the contract's built-in fallback on older devices.
|
||||||
val imagePicker = rememberLauncherForActivityResult(
|
val imagePicker = rememberLauncherForActivityResult(
|
||||||
ActivityResultContracts.PickVisualMedia()
|
ActivityResultContracts.GetContent()
|
||||||
) { uri: Uri? ->
|
) { uri: Uri? ->
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
|
coroutineScope.launch {
|
||||||
|
runCatching {
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
val dir = File(appCtx.filesDir, "bg_images")
|
val dir = File(appCtx.filesDir, "bg_images")
|
||||||
if (!dir.exists()) dir.mkdirs()
|
if (!dir.exists()) dir.mkdirs()
|
||||||
val displayName = context.contentResolver.query(
|
val displayName = context.contentResolver.query(
|
||||||
@@ -169,8 +176,16 @@ fun HighlightRuleEditSheet(
|
|||||||
target.outputStream().use { output ->
|
target.outputStream().use { output ->
|
||||||
input.copyTo(output)
|
input.copyTo(output)
|
||||||
}
|
}
|
||||||
|
} ?: throw java.io.FileNotFoundException("Open input stream failed")
|
||||||
|
target.absolutePath
|
||||||
|
}
|
||||||
|
}.onSuccess { path ->
|
||||||
|
bgImage = path
|
||||||
|
}.onFailure { throwable ->
|
||||||
|
context.toastOnUi(R.string.error)
|
||||||
|
AppLog.put("选择高亮背景图失败", throwable)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
bgImage = target.absolutePath
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -385,9 +400,7 @@ fun HighlightRuleEditSheet(
|
|||||||
title = stringResource(R.string.highlight_bg_image),
|
title = stringResource(R.string.highlight_bg_image),
|
||||||
description = bgImage.ifBlank { null }?.let { File(it).name },
|
description = bgImage.ifBlank { null }?.let { File(it).name },
|
||||||
onClick = {
|
onClick = {
|
||||||
imagePicker.launch(
|
imagePicker.launch("image/*")
|
||||||
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user