auto paste links in add url page

This commit is contained in:
AmirHossein Abdolmotallebi 2024-08-16 04:23:16 +03:30
parent efe381d555
commit b51f91aa8c
3 changed files with 64 additions and 9 deletions

View File

@ -5,6 +5,7 @@
### Added
- support follow system Dark/Light mode
- auto paste link (if any) from clipboard when opening add url page
### Changed

View File

@ -26,12 +26,15 @@ import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.*
import androidx.compose.ui.input.pointer.PointerIcon
import androidx.compose.ui.input.pointer.pointerHoverIcon
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.*
import androidx.compose.ui.window.*
import com.abdownloadmanager.desktop.utils.mvi.HandleEffects
import ir.amirab.downloader.monitor.ProcessingDownloadItemState
import ir.amirab.downloader.utils.OnDuplicateStrategy
import java.awt.MouseInfo
@ -47,13 +50,30 @@ fun AddDownloadPage(
.padding(top = 8.dp, bottom = 16.dp)
) {
val credentials by component.credentials.collectAsState()
fun setLink(link: String) {
component.setCredentials(
credentials.copy(link = link)
)
}
val linkFocus = remember { FocusRequester() }
LaunchedEffect(Unit) {
component.onPageOpen()
linkFocus.requestFocus()
}
HandleEffects(component) {
when (it) {
is AddSingleDownloadPageEffects.SuggestUrl -> {
setLink(it.link)
}
}
}
UrlTextField(
text = credentials.link,
setText = {
component.setCredentials(
credentials.copy(link = it)
)
setLink(it)
},
modifier = Modifier.focusRequester(linkFocus)
)
Row(
) {
@ -144,7 +164,7 @@ private fun ShowSolutionsOnDuplicateDownload(component: AddSingleDownloadCompone
state = state,
resizeable = false,
) {
LaunchedEffect(window){
LaunchedEffect(window) {
window.moveSafe(
MouseInfo.getPointerInfo().location.run {
DpOffset(
@ -381,7 +401,7 @@ fun ConfigActionsButtons(component: AddSingleDownloadComponent) {
MyIcons.settings,
"Settings",
indicateActive = component.showMoreSettings,
requiresAttention =responseInfo?.requireBasicAuth?:false
requiresAttention = responseInfo?.requireBasicAuth ?: false
) {
component.showMoreSettings = true
}
@ -476,7 +496,7 @@ fun RenderFileTypeAndSize(
) {
WithContentAlpha(1f) {
if (fileInfo != null) {
if (fileInfo.requiresAuth){
if (fileInfo.requiresAuth) {
MyIcon(
MyIcons.lock,
null,
@ -540,12 +560,13 @@ private fun UrlTextField(
text: String,
setText: (String) -> Unit,
errorText: String? = null,
modifier: Modifier = Modifier,
) {
AddDownloadPageTextField(
text,
setText,
"Download link",
modifier = Modifier.fillMaxWidth(),
modifier = modifier.fillMaxWidth(),
end = {
MyTextFieldIcon(MyIcons.paste) {
setText(ClipboardUtil.read()

View File

@ -8,6 +8,9 @@ import com.abdownloadmanager.desktop.pages.settings.configurable.StringConfigura
import com.abdownloadmanager.desktop.repository.AppRepository
import com.abdownloadmanager.desktop.utils.*
import androidx.compose.runtime.*
import com.abdownloadmanager.desktop.utils.mvi.ContainsEffects
import com.abdownloadmanager.desktop.utils.mvi.supportEffects
import com.abdownloadmanager.utils.extractors.linkextractor.DownloadCredentialFromStringExtractor
import com.arkivanov.decompose.ComponentContext
import ir.amirab.downloader.connection.DownloaderClient
import ir.amirab.downloader.downloaditem.DownloadCredentials
@ -18,10 +21,17 @@ import ir.amirab.downloader.queue.QueueManager
import ir.amirab.downloader.utils.OnDuplicateStrategy
import ir.amirab.downloader.utils.orDefault
import ir.amirab.util.flow.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
sealed interface AddSingleDownloadPageEffects {
data class SuggestUrl(val link: String) : AddSingleDownloadPageEffects
}
class AddSingleDownloadComponent(
ctx: ComponentContext,
val onRequestClose: () -> Unit,
@ -30,7 +40,8 @@ class AddSingleDownloadComponent(
val openExistingDownload: (Long) -> Unit,
id: String,
) : AddDownloadComponent(ctx, id),
KoinComponent {
KoinComponent,
ContainsEffects<AddSingleDownloadPageEffects> by supportEffects() {
private val appSettings: AppRepository by inject()
private val client: DownloaderClient by inject()
@ -76,6 +87,28 @@ class AddSingleDownloadComponent(
.launchIn(scope)
}
private var wasOpened = false
fun onPageOpen() {
if (wasOpened) return
scope.launch {
withContext(Dispatchers.Default) {
// don't paste of link already exists
// maybe a link already added by browser extension etc.
if (credentials.value == DownloadCredentials.empty()) {
fillLinkIfThereIsALinkInClipboard()
}
}
}
wasOpened = true
}
private fun fillLinkIfThereIsALinkInClipboard() {
val possibleLinks = ClipboardUtil.read() ?: return
val downloadLinks = DownloadCredentialFromStringExtractor.extract(possibleLinks)
if (downloadLinks.size == 1) {
sendEffect(AddSingleDownloadPageEffects.SuggestUrl(downloadLinks[0].link))
}
}
private val length: StateFlow<Long?> = downloadChecker.length
val canAddResult = downloadChecker.canAddToDownloadResult.asStateFlow()
@ -88,7 +121,7 @@ class AddSingleDownloadComponent(
val canAddToDownloads = combineStateFlows(
canAdd, isDuplicate, onDuplicateStrategy, isLinkLoading
) { canAdd, isDuplicate, onDuplicateStrategy, isLinkLoading ->
if (isLinkLoading){
if (isLinkLoading) {
// link is loading wait for it...
return@combineStateFlows false
}