0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ClipboardManagerからClipboardに移行する

Posted at

compose-uiを1.8.0にアップデートしたところ、以下の警告が出るようになりました。

'interface ClipboardManager : Any' is deprecated. Use Clipboard instead, which supports suspend functions.

移行作業

単純に移行するとコード量が多いので、以下のような拡張関数を追加するのが良さそうです。

suspend fun Clipboard.setText(text: String) {
    setClipEntry(
        ClipData
            .newPlainText(text, text)
            .toClipEntry(),
    )
}

LocalClipboardManager を置換し、

-    val clipboardManager = LocalClipboardManager.current
+    val clipboard = LocalClipboard.current

suspend funを実行する形に修正します

+   val scope = rememberCoroutineScope()
    Button(
        onClick = {
-           clipboardManager.setText(AnnotatedString(it))
+           scope.launch {
+               clipboard.setText(it)
+               ...
+           }
        }
    )
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?