Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show link dialog on long press #651

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.os.Environment
import android.provider.MediaStore
import android.util.Log
import android.util.Patterns
import android.webkit.MimeTypeMap
import android.widget.Toast
import androidx.browser.customtabs.CustomTabsIntent
import androidx.compose.foundation.ExperimentalFoundationApi
Expand Down Expand Up @@ -58,7 +59,9 @@ import com.jerboa.ui.components.home.SiteViewModel
import com.jerboa.ui.components.person.UserTab
import com.jerboa.ui.theme.SMALL_PADDING
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.ocpsoft.prettytime.PrettyTime
import java.io.IOException
import java.io.InputStream
Expand Down Expand Up @@ -881,6 +884,23 @@ fun saveBitmap(
}
}

suspend fun saveImage(url: String, context: Context) {
Toast.makeText(context, "Saving image...", Toast.LENGTH_SHORT).show()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stringResource.


val fileName = Uri.parse(url).pathSegments.last()

val extension = MimeTypeMap.getFileExtensionFromUrl(url)
val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)

withContext(Dispatchers.IO) {
URL(url).openStream().use {
saveBitmap(context, it, mimeType, fileName)
}
}

Toast.makeText(context, "Saved image", Toast.LENGTH_SHORT).show()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Saved image" should be a stringResource

}

@OptIn(ExperimentalComposeUiApi::class)
fun Modifier.onAutofill(vararg autofillType: AutofillType, onFill: (String) -> Unit): Modifier = composed {
val autofillNode = AutofillNode(
Expand Down
94 changes: 94 additions & 0 deletions app/src/main/java/com/jerboa/ui/components/common/Dialogs.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package com.jerboa.ui.components.common

import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.OpenInBrowser
import androidx.compose.material.icons.filled.Share
import androidx.compose.material.icons.outlined.BarChart
import androidx.compose.material.icons.outlined.Bookmarks
import androidx.compose.material.icons.outlined.BrightnessLow
import androidx.compose.material.icons.outlined.ContentCopy
import androidx.compose.material.icons.outlined.Download
import androidx.compose.material.icons.outlined.FormatListNumbered
import androidx.compose.material.icons.outlined.List
import androidx.compose.material.icons.outlined.LocalFireDepartment
Expand All @@ -20,21 +27,30 @@ import androidx.compose.material.icons.outlined.NewReleases
import androidx.compose.material.icons.outlined.Public
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Divider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import com.jerboa.PostViewMode
import com.jerboa.R
import com.jerboa.UnreadOrAll
import com.jerboa.datatypes.ListingType
import com.jerboa.datatypes.SortType
import com.jerboa.db.AppSettingsViewModel
import com.jerboa.isImage
import com.jerboa.saveImage
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.HttpUrl.Companion.toHttpUrl
Expand Down Expand Up @@ -321,3 +337,81 @@ fun ShowChangelog(appSettingsViewModel: AppSettingsViewModel) {
}
}
}

@Composable
fun PostLinkOptionsDialog(
url: String,
onDismissRequest: () -> Unit,
onOpenInBrowser: () -> Unit,
) {
val isImage = isImage(url)

val clipboardManager = LocalClipboardManager.current
val context = LocalContext.current
val coroutineScope = rememberCoroutineScope()

AlertDialog(
onDismissRequest = onDismissRequest,
confirmButton = {},
text = {
Column {
Text(
text = url,
modifier = Modifier
.padding(bottom = 15.dp),
color = MaterialTheme.colorScheme.primary,
)

IconAndTextDrawerItem(
icon = Icons.Filled.OpenInBrowser,
text = stringResource(R.string.link_dialog_open_in_browser),
onClick = {
onOpenInBrowser()
onDismissRequest()
},
)
IconAndTextDrawerItem(
icon = Icons.Filled.Share,
text = stringResource(R.string.link_dialog_share_link),
onClick = {
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, url)
type = "text/plain"
}

val shareIntent = Intent.createChooser(sendIntent, null)
ContextCompat.startActivity(context, shareIntent, null)

onDismissRequest()
},
)
IconAndTextDrawerItem(
icon = Icons.Outlined.ContentCopy,
text = stringResource(R.string.link_dialog_copy_link),
onClick = {
clipboardManager.setText(AnnotatedString(url))
Toast.makeText(context, context.resources.getString(R.string.link_dialog_toast_url_copied), Toast.LENGTH_SHORT).show()
onDismissRequest()
},
)

if (isImage) {
Divider(modifier = Modifier.padding(vertical = 20.dp))

IconAndTextDrawerItem(
icon = Icons.Outlined.Download,
text = stringResource(R.string.link_dialog_download_image),
onClick = {
coroutineScope.launch {
saveImage(url, context)
}

onDismissRequest()
},
)
}
}
},
)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.jerboa.ui.components.common

import android.content.Context
import android.net.Uri
import android.os.Build.VERSION.SDK_INT
import android.webkit.MimeTypeMap
import android.widget.Toast
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
Expand Down Expand Up @@ -43,13 +39,10 @@ import coil.ImageLoader
import coil.compose.rememberAsyncImagePainter
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import com.jerboa.saveBitmap
import kotlinx.coroutines.Dispatchers
import com.jerboa.saveImage
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.engawapg.lib.zoomable.rememberZoomState
import net.engawapg.lib.zoomable.zoomable
import java.net.URL

const val backFadeTime = 300

Expand Down Expand Up @@ -134,31 +127,14 @@ fun ImageViewerDialog(url: String, onBackRequest: () -> Unit) {

BarIcon(icon = Icons.Outlined.Download, name = "Download") {
coroutineScope.launch {
SaveImage(url, context)
saveImage(url, context)
}
}
}
}
}
}

suspend fun SaveImage(url: String, context: Context) {
Toast.makeText(context, "Saving image...", Toast.LENGTH_SHORT).show()

val fileName = Uri.parse(url).pathSegments.last()

val extension = MimeTypeMap.getFileExtensionFromUrl(url)
val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)

withContext(Dispatchers.IO) {
URL(url).openStream().use {
saveBitmap(context, it, mimeType, fileName)
}
}

Toast.makeText(context, "Saved image", Toast.LENGTH_SHORT).show()
}

@Composable
@Preview
fun ImageActivityPreview() {
Expand Down
Loading