Skip to content

Commit

Permalink
Add support to copy to paste rs
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Nov 22, 2024
1 parent e56358c commit 2957a65
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import android.view.View
import android.view.ViewGroup
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import dev.brahmkshatriya.echo.EchoApplication.Companion.appVersion
import dev.brahmkshatriya.echo.R
import dev.brahmkshatriya.echo.common.helpers.ContinuationCallback.Companion.await
import dev.brahmkshatriya.echo.databinding.FragmentExceptionBinding
import dev.brahmkshatriya.echo.extensions.ExtensionLoadingException
import dev.brahmkshatriya.echo.extensions.InvalidExtensionListException
Expand All @@ -27,7 +29,13 @@ import dev.brahmkshatriya.echo.viewmodels.PlayerViewModel
import dev.brahmkshatriya.echo.viewmodels.UiViewModel.Companion.applyBackPressCallback
import dev.brahmkshatriya.echo.viewmodels.UiViewModel.Companion.applyContentInsets
import dev.brahmkshatriya.echo.viewmodels.UiViewModel.Companion.applyInsets
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.Serializable
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import java.net.UnknownHostException
import java.nio.channels.UnresolvedAddressException

Expand Down Expand Up @@ -71,8 +79,11 @@ class ExceptionFragment : Fragment() {
binding.exceptionMessage.setOnMenuItemClickListener { item ->
when (item.itemId) {
R.id.exception_copy -> {
with(requireContext()) {
copyToClipboard(throwable.title, "```\n${throwable.causedBy}\n```")
lifecycleScope.launch {
val pasteLink = getPasteLink(throwable).getOrElse {
throwable.causedBy
}
requireContext().copyToClipboard("Error", pasteLink)
}
true
}
Expand All @@ -83,6 +94,17 @@ class ExceptionFragment : Fragment() {
}

companion object {

private val client = OkHttpClient()
suspend fun getPasteLink(throwable: ExceptionDetails) = withContext(Dispatchers.IO) {
val details = throwable.causedBy
val request = Request.Builder()
.url("https://paste.rs")
.post(details.toRequestBody())
.build()
runCatching { client.newCall(request).await().body.string() }
}

fun Context.copyToClipboard(label: String?, string: String) {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText(label, string)
Expand All @@ -99,6 +121,7 @@ class ExceptionFragment : Fragment() {
throwable.name,
throwable.requiredExtensions.joinToString(", ")
)

is InvalidExtensionListException -> getString(R.string.invalid_extension_list)
is AppException -> throwable.run {
when (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class ItemFragment : Fragment() {

//I have no idea why this doesn't update, if title already exists
binding.toolBar.post {
binding.toolBar.title = it.title.trim()
runCatching { binding.toolBar.title = it.title.trim() }
}

it.cover.loadWithThumb(binding.cover, item.cover, it.placeHolder())
Expand Down

0 comments on commit 2957a65

Please sign in to comment.