Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed May 26, 2024
1 parent 4da7e62 commit a0a4616
Showing 1 changed file with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,50 @@ fun rememberClipboardData(): State<List<Uri>> {
return clip
}

@Composable
fun rememberClipboardText(): State<String> {
val settingsState = LocalSettingsState.current
val allowPaste = settingsState.allowAutoClipboardPaste

val context = LocalContext.current
val clipboardManager = remember(context) {
context.getSystemService<ClipboardManager>()
}

val clip = remember {
mutableStateOf(
if (allowPaste) {
clipboardManager.clipText()
} else ""
)
}.apply {
value = if (allowPaste) {
clipboardManager.clipText()
} else ""
}

val callback = remember {
ClipboardManager.OnPrimaryClipChangedListener {
if (allowPaste) {
clip.value = clipboardManager.clipText()
}
}
}
DisposableEffect(clipboardManager, allowPaste) {
if (allowPaste) {
clipboardManager?.addPrimaryClipChangedListener(callback)
}
onDispose {
clipboardManager?.removePrimaryClipChangedListener(callback)
}
}
return clip
}

fun ClipboardManager?.clipList(): List<Uri> = this?.primaryClip?.clipList() ?: emptyList()

fun ClipboardManager?.clipText(): String = this?.primaryClip?.getItemAt(0)?.text?.toString() ?: ""

fun ClipData.clipList() = List(
size = itemCount,
init = {
Expand Down

0 comments on commit a0a4616

Please sign in to comment.