mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-19 21:20:38 +02:00
Adds Image Upload on Profile Edit pages.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.vitorpamplona.amethyst.ui.actions
|
package com.vitorpamplona.amethyst.ui.actions
|
||||||
|
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
@@ -18,6 +19,7 @@ import androidx.compose.runtime.Composable
|
|||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.text.input.KeyboardCapitalization
|
import androidx.compose.ui.text.input.KeyboardCapitalization
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
@@ -30,9 +32,14 @@ import com.vitorpamplona.amethyst.model.Account
|
|||||||
@Composable
|
@Composable
|
||||||
fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
|
fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
|
||||||
val postViewModel: NewUserMetadataViewModel = viewModel()
|
val postViewModel: NewUserMetadataViewModel = viewModel()
|
||||||
|
val context = LocalContext.current
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(Unit) {
|
||||||
postViewModel.load(account)
|
postViewModel.load(account)
|
||||||
|
|
||||||
|
postViewModel.imageUploadingError.collect { error ->
|
||||||
|
Toast.makeText(context, error, Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Dialog(
|
Dialog(
|
||||||
@@ -141,6 +148,15 @@ fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
|
|||||||
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
|
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
leadingIcon = {
|
||||||
|
UploadFromGallery(
|
||||||
|
isUploading = postViewModel.isUploadingImageForPicture,
|
||||||
|
tint = MaterialTheme.colors.onSurface.copy(alpha = 0.32f),
|
||||||
|
modifier = Modifier.padding(start = 5.dp)
|
||||||
|
) {
|
||||||
|
postViewModel.uploadForPicture(it, context)
|
||||||
|
}
|
||||||
|
},
|
||||||
singleLine = true
|
singleLine = true
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -157,6 +173,15 @@ fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
|
|||||||
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
|
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
leadingIcon = {
|
||||||
|
UploadFromGallery(
|
||||||
|
isUploading = postViewModel.isUploadingImageForBanner,
|
||||||
|
tint = MaterialTheme.colors.onSurface.copy(alpha = 0.32f),
|
||||||
|
modifier = Modifier.padding(start = 5.dp)
|
||||||
|
) {
|
||||||
|
postViewModel.uploadForBanner(it, context)
|
||||||
|
}
|
||||||
|
},
|
||||||
singleLine = true
|
singleLine = true
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@@ -1,13 +1,20 @@
|
|||||||
package com.vitorpamplona.amethyst.ui.actions
|
package com.vitorpamplona.amethyst.ui.actions
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode
|
import com.fasterxml.jackson.databind.node.ObjectNode
|
||||||
import com.vitorpamplona.amethyst.model.Account
|
import com.vitorpamplona.amethyst.model.Account
|
||||||
import com.vitorpamplona.amethyst.service.model.GitHubIdentity
|
import com.vitorpamplona.amethyst.service.model.GitHubIdentity
|
||||||
import com.vitorpamplona.amethyst.service.model.MastodonIdentity
|
import com.vitorpamplona.amethyst.service.model.MastodonIdentity
|
||||||
import com.vitorpamplona.amethyst.service.model.TwitterIdentity
|
import com.vitorpamplona.amethyst.service.model.TwitterIdentity
|
||||||
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
import java.io.StringWriter
|
import java.io.StringWriter
|
||||||
|
|
||||||
@@ -30,6 +37,10 @@ class NewUserMetadataViewModel : ViewModel() {
|
|||||||
val github = mutableStateOf("")
|
val github = mutableStateOf("")
|
||||||
val mastodon = mutableStateOf("")
|
val mastodon = mutableStateOf("")
|
||||||
|
|
||||||
|
var isUploadingImageForPicture by mutableStateOf(false)
|
||||||
|
var isUploadingImageForBanner by mutableStateOf(false)
|
||||||
|
val imageUploadingError = MutableSharedFlow<String?>()
|
||||||
|
|
||||||
fun load(account: Account) {
|
fun load(account: Account) {
|
||||||
this.account = account
|
this.account = account
|
||||||
|
|
||||||
@@ -127,4 +138,49 @@ class NewUserMetadataViewModel : ViewModel() {
|
|||||||
github.value = ""
|
github.value = ""
|
||||||
mastodon.value = ""
|
mastodon.value = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun uploadForPicture(uri: Uri, context: Context) {
|
||||||
|
upload(
|
||||||
|
uri,
|
||||||
|
context,
|
||||||
|
onUploading = {
|
||||||
|
isUploadingImageForPicture = it
|
||||||
|
},
|
||||||
|
onUploaded = {
|
||||||
|
picture.value = it
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun uploadForBanner(uri: Uri, context: Context) {
|
||||||
|
upload(
|
||||||
|
uri,
|
||||||
|
context,
|
||||||
|
onUploading = {
|
||||||
|
isUploadingImageForBanner = it
|
||||||
|
},
|
||||||
|
onUploaded = {
|
||||||
|
banner.value = it
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun upload(it: Uri, context: Context, onUploading: (Boolean) -> Unit, onUploaded: (String) -> Unit) {
|
||||||
|
onUploading(true)
|
||||||
|
|
||||||
|
ImageUploader.uploadImage(
|
||||||
|
uri = it,
|
||||||
|
contentResolver = context.contentResolver,
|
||||||
|
onSuccess = { imageUrl ->
|
||||||
|
onUploading(false)
|
||||||
|
onUploaded(imageUrl)
|
||||||
|
},
|
||||||
|
onError = {
|
||||||
|
onUploading(false)
|
||||||
|
viewModelScope.launch {
|
||||||
|
imageUploadingError.emit("Failed to upload the image / video")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user