Skip to content

Commit

Permalink
handle sharing images + revert force triggering onChangeUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaticHacker committed Jul 6, 2022
1 parent bcf8953 commit 94a9912
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>

</activity>
</application>
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.jerboa

import android.app.Application
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Parcelable
import android.util.Patterns
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand Down Expand Up @@ -175,12 +177,16 @@ class MainActivity : ComponentActivity() {
composable(
route = "createPost",
// TODO: Handle Image mimetype too
deepLinks = listOf(navDeepLink { mimeType = "text/plain" })
deepLinks = listOf(
navDeepLink { mimeType = "text/plain" },
navDeepLink { mimeType = "image/*" })
) {

val context = LocalContext.current
val activity = context.findActivity()
val text = activity?.intent?.getStringExtra(Intent.EXTRA_TEXT) ?: ""
val image =
activity?.intent?.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri
// url and body will be empty everytime except when there is EXTRA TEXT in the intent
var url = ""
var body = ""
Expand All @@ -198,7 +204,9 @@ class MainActivity : ComponentActivity() {
postViewModel = postViewModel,
_url = url,
_body = body,
_image = image
)
activity?.intent?.replaceExtras(Bundle())
}
composable(route = "inbox") {
InboxActivity(
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/jerboa/ui/components/common/PictrsImage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ fun PictrsBannerImage(
fun PickImage(
modifier: Modifier = Modifier,
onPickedImage: (image: Uri) -> Unit,
image: Uri? = null,
showImage: Boolean = true,
) {
val ctx = LocalContext.current
Expand All @@ -152,6 +153,24 @@ fun PickImage(
mutableStateOf<Bitmap?>(null)
}

if (image != null) {
LaunchedEffect(image) {
imageUri = image
// Set the bitmap
if (Build.VERSION.SDK_INT < 28) {
bitmap.value = MediaStore.Images
.Media.getBitmap(ctx.contentResolver, imageUri!!)
} else {
val source = ImageDecoder
.createSource(ctx.contentResolver, imageUri!!)
bitmap.value = ImageDecoder.decodeBitmap(source)
}

Log.d("jerboa", imageUri.toString())
onPickedImage(image)
}
}

val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.GetContent()
) { uri: Uri? ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fun CreatePostBody(
url: String,
onUrlChange: (url: String) -> Unit,
onPickedImage: (image: Uri) -> Unit,
image: Uri? = null,
community: CommunitySafe? = null,
navController: NavController = rememberNavController(),
formValid: (valid: Boolean) -> Unit,
Expand All @@ -97,9 +98,6 @@ fun CreatePostBody(
val nameField = validatePostName(name)
val urlField = validateUrl(url)

// To fetch suggested title when opened from deeplink
onUrlChange(url)

formValid(
!nameField.hasError &&
!urlField.hasError &&
Expand Down Expand Up @@ -154,6 +152,7 @@ fun CreatePostBody(
item {
PickImage(
onPickedImage = onPickedImage,
image = image
)
}
// TODO change this to reply text field at some point
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jerboa.ui.components.post.create

import android.net.Uri
import android.util.Log
import android.util.Patterns
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -35,6 +36,7 @@ fun CreatePostActivity(
postViewModel: PostViewModel,
_url: String,
_body: String,
_image: Uri?,
) {

Log.d("jerboa", "got to create post activity")
Expand Down Expand Up @@ -103,6 +105,7 @@ fun CreatePostActivity(
community = communityListViewModel.selectedCommunity,
formValid = { formValid = it },
suggestedTitle = createPostViewModel.suggestedTitle,
image = _image,
onPickedImage = { uri ->
val imageIs = imageInputStreamFromUri(ctx, uri)
scope.launch {
Expand Down

0 comments on commit 94a9912

Please sign in to comment.