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

🖼️ ISSUE-119 Share Note as image #269

Merged
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
9 changes: 9 additions & 0 deletions .idea/NotyKT.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions noty-android/app/composeapp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.ch8n.fileprovider"
Copy link
Owner

Choose a reason for hiding this comment

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

Let's make it app-specific:

Suggested change
android:authorities="com.ch8n.fileprovider"
android:authorities="${applicationId}.FileProvider"

android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
Comment on lines +33 to +41
Copy link
Owner

Choose a reason for hiding this comment

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

@ch8n I guess we don't need a provider since we are just sharing images from intent.

Copy link
Owner

Choose a reason for hiding this comment

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

means in simpleapp module, we didn't use this. So can we make it common across both?


<activity
android:name=".ui.MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|navigation|screenSize|screenLayout"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
package dev.shreyaspatil.noty.composeapp.component.action

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
Expand Down Expand Up @@ -49,6 +55,39 @@ fun ShareAction(onClick: () -> Unit) {
)
}

data class ShareActionItem(
val label: String,
val onActionClick: () -> Unit,
)

@Composable
fun ShareDropdown(
expanded: Boolean,
shareActions: List<ShareActionItem>,
onDismissRequest: () -> Unit
) {
DropdownMenu(
expanded = expanded,
onDismissRequest = onDismissRequest,
modifier = Modifier
.wrapContentHeight()
.width(100.dp)
) {
shareActions.forEachIndexed { index, shareAction ->
DropdownMenuItem(
onClick = {
shareAction.onActionClick.invoke()
onDismissRequest.invoke()
}
) {
Row {
Text(text = shareAction.label)
}
}
}
}
}

@Composable
fun ThemeSwitchAction(onToggle: () -> Unit) {
val icon = painterResource(R.drawable.ic_day)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package dev.shreyaspatil.noty.composeapp.ui.screens

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.widget.Toast
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
Expand All @@ -41,6 +44,7 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -53,15 +57,21 @@ import androidx.navigation.NavHostController
import dev.shreyaspatil.noty.composeapp.R
import dev.shreyaspatil.noty.composeapp.component.action.DeleteAction
import dev.shreyaspatil.noty.composeapp.component.action.ShareAction
import dev.shreyaspatil.noty.composeapp.component.action.ShareActionItem
import dev.shreyaspatil.noty.composeapp.component.action.ShareDropdown
import dev.shreyaspatil.noty.composeapp.component.dialog.FailureDialog
import dev.shreyaspatil.noty.composeapp.component.text.NoteField
import dev.shreyaspatil.noty.composeapp.component.text.NoteTitleField
import dev.shreyaspatil.noty.composeapp.utils.CaptureBitmap
import dev.shreyaspatil.noty.composeapp.utils.ShowToast
import dev.shreyaspatil.noty.composeapp.utils.saveImage
import dev.shreyaspatil.noty.composeapp.utils.shareImageUri
import dev.shreyaspatil.noty.core.ui.UIDataState
import dev.shreyaspatil.noty.utils.validator.NoteValidator
import dev.shreyaspatil.noty.view.viewmodel.NoteDetailViewModel
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.launch

@ExperimentalAnimationApi
@InternalCoroutinesApi
Expand All @@ -81,6 +91,7 @@ fun NoteDetailsScreen(
if (note != null) {
var titleText by remember { mutableStateOf(note.title) }
var noteText by remember { mutableStateOf(note.note) }
var snapShot: () -> Bitmap? = { null }

Scaffold(
topBar = {
Expand Down Expand Up @@ -109,35 +120,74 @@ fun NoteDetailsScreen(
contentColor = MaterialTheme.colors.onPrimary,
elevation = 0.dp,
actions = {
var dropdownExpanded by remember { mutableStateOf(false) }
val coroutineScope = rememberCoroutineScope()
DeleteAction(onClick = { viewModel.deleteNote() })
ShareAction(onClick = { shareNote(activity, titleText, noteText) })
ShareAction(onClick = {
dropdownExpanded = true
})
ShareDropdown(
expanded = dropdownExpanded,
onDismissRequest = {
dropdownExpanded = false
},
shareActions = listOf(
ShareActionItem(
label = "Text",
onActionClick = {
shareNote(activity, titleText, noteText)
}
),
ShareActionItem(
label = "Image",
onActionClick = {
coroutineScope.launch {
val bitmap = snapShot.invoke()
if (bitmap == null) {
Toast.makeText(
activity,
"Something Went Wrong!",
Toast.LENGTH_SHORT
).show()
return@launch
}
shareNoteImage(bitmap, activity)
}
}
),
)
)
}
)
},
content = {
Column(
Modifier.scrollable(
rememberScrollState(),
orientation = Orientation.Vertical
).padding(16.dp)
) {
NoteTitleField(
modifier = Modifier
.fillMaxWidth()
.background(MaterialTheme.colors.background),
value = titleText,
onTextChange = { titleText = it }
)
snapShot = CaptureBitmap {
Column(
Modifier
.scrollable(
rememberScrollState(),
orientation = Orientation.Vertical
)
.padding(16.dp)
) {
NoteTitleField(
modifier = Modifier
.fillMaxWidth()
.background(MaterialTheme.colors.background),
value = titleText,
onTextChange = { titleText = it }
)

NoteField(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
.padding(top = 8.dp)
.background(MaterialTheme.colors.background),
value = noteText,
onTextChange = { noteText = it }
)
NoteField(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
.padding(top = 8.dp)
.background(MaterialTheme.colors.background),
value = noteText,
onTextChange = { noteText = it }
)
}
}
},
floatingActionButton = {
Expand Down Expand Up @@ -186,3 +236,16 @@ fun shareNote(activity: Activity, title: String, note: String) {

activity.startActivity(Intent.createChooser(intent, null))
}

suspend fun shareNoteImage(bitmap: Bitmap, context: Context) {
val uri = saveImage(bitmap, context)
if (uri != null) {
shareImageUri(context, uri)
} else {
Toast.makeText(
context,
"uri is null",
Toast.LENGTH_SHORT
).show()
}
}
Loading