Make pickImage work

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-09-11 17:58:52 +02:00 committed by Marcel Hibbe
parent df6d54d51c
commit fa014aab3a
No known key found for this signature in database
GPG key ID: C793F8B59F43CE7B
2 changed files with 99 additions and 23 deletions

View file

@ -13,6 +13,7 @@ import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import androidx.activity.compose.ManagedActivityResultLauncher import androidx.activity.compose.ManagedActivityResultLauncher
import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.rememberLauncherForActivityResult
@ -77,6 +78,7 @@ import com.nextcloud.talk.chat.ChatActivity
import com.nextcloud.talk.contacts.ContactsActivityCompose import com.nextcloud.talk.contacts.ContactsActivityCompose
import com.nextcloud.talk.contacts.loadImage import com.nextcloud.talk.contacts.loadImage
import com.nextcloud.talk.models.json.autocomplete.AutocompleteUser import com.nextcloud.talk.models.json.autocomplete.AutocompleteUser
import com.nextcloud.talk.utils.PickImage
import com.nextcloud.talk.utils.bundle.BundleKeys import com.nextcloud.talk.utils.bundle.BundleKeys
import javax.inject.Inject import javax.inject.Inject
@ -84,7 +86,7 @@ import javax.inject.Inject
class ConversationCreationActivity : BaseActivity() { class ConversationCreationActivity : BaseActivity() {
@Inject @Inject
lateinit var viewModelFactory: ViewModelProvider.Factory lateinit var viewModelFactory: ViewModelProvider.Factory
private lateinit var pickImage: PickImage
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -93,13 +95,16 @@ class ConversationCreationActivity : BaseActivity() {
this, this,
viewModelFactory viewModelFactory
)[ConversationCreationViewModel::class.java] )[ConversationCreationViewModel::class.java]
val conversationUser = conversationCreationViewModel.currentUser
pickImage = PickImage(this, conversationUser)
setContent { setContent {
val colorScheme = viewThemeUtils.getColorScheme(this) val colorScheme = viewThemeUtils.getColorScheme(this)
val context = LocalContext.current val context = LocalContext.current
MaterialTheme( MaterialTheme(
colorScheme = colorScheme colorScheme = colorScheme
) { ) {
ConversationCreationScreen(conversationCreationViewModel, context) ConversationCreationScreen(conversationCreationViewModel, context, pickImage)
} }
SetStatusBarColor() SetStatusBarColor()
} }
@ -125,15 +130,47 @@ private fun SetStatusBarColor() {
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun ConversationCreationScreen(conversationCreationViewModel: ConversationCreationViewModel, context: Context) { fun ConversationCreationScreen(
conversationCreationViewModel: ConversationCreationViewModel,
context: Context,
pickImage: PickImage
) {
var selectedImageUri by remember { mutableStateOf<Uri?>(null) }
val imagePickerLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
pickImage.onImagePickerResult(result.data) { uri ->
selectedImageUri = uri
}
}
}
val remoteFilePickerLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
pickImage.onSelectRemoteFilesResult(imagePickerLauncher, result.data)
}
}
val cameraLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
pickImage.onTakePictureResult(imagePickerLauncher, result.data)
}
}
val launcher = rememberLauncherForActivityResult( val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult(), contract = ActivityResultContracts.StartActivityForResult(),
onResult = { result -> onResult = { result ->
if (result.resultCode == Activity.RESULT_OK) { if (result.resultCode == Activity.RESULT_OK) {
val data = result.data val data = result.data
val selectedParticipants = data?.getParcelableArrayListExtra<AutocompleteUser>("selectedParticipants") val selectedParticipants =
?: emptyList() data?.getParcelableArrayListExtra<AutocompleteUser>("selectedParticipants")
?: emptyList()
val participants = selectedParticipants.toMutableList() val participants = selectedParticipants.toMutableList()
conversationCreationViewModel.updateSelectedParticipants(participants) conversationCreationViewModel.updateSelectedParticipants(participants)
} }
@ -162,8 +199,16 @@ fun ConversationCreationScreen(conversationCreationViewModel: ConversationCreati
.padding(paddingValues) .padding(paddingValues)
.verticalScroll(rememberScrollState()) .verticalScroll(rememberScrollState())
) { ) {
DefaultUserAvatar() DefaultUserAvatar(selectedImageUri)
UploadAvatar() UploadAvatar(
pickImage = pickImage,
onImageSelected = { uri -> selectedImageUri = uri },
imagePickerLauncher = imagePickerLauncher,
remoteFilePickerLauncher = remoteFilePickerLauncher,
cameraLauncher = cameraLauncher,
onDeleteImage = { selectedImageUri = null }
)
ConversationNameAndDescription(conversationCreationViewModel) ConversationNameAndDescription(conversationCreationViewModel)
AddParticipants(launcher, context, conversationCreationViewModel) AddParticipants(launcher, context, conversationCreationViewModel)
RoomCreationOptions(conversationCreationViewModel) RoomCreationOptions(conversationCreationViewModel)
@ -174,31 +219,51 @@ fun ConversationCreationScreen(conversationCreationViewModel: ConversationCreati
} }
@Composable @Composable
fun DefaultUserAvatar() { fun DefaultUserAvatar(selectedImageUri: Uri?) {
Box( Box(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {
AsyncImage( if (selectedImageUri != null) {
model = R.drawable.ic_circular_group, AsyncImage(
contentDescription = stringResource(id = R.string.user_avatar), model = selectedImageUri,
modifier = Modifier contentDescription = stringResource(id = R.string.user_avatar),
.size(width = 84.dp, height = 84.dp) modifier = Modifier
.padding(top = 8.dp) .size(84.dp)
) .padding(top = 8.dp)
)
} else {
AsyncImage(
model = R.drawable.ic_circular_group,
contentDescription = stringResource(id = R.string.user_avatar),
modifier = Modifier
.size(84.dp)
.padding(top = 8.dp)
)
}
} }
} }
@Composable @Composable
fun UploadAvatar() { fun UploadAvatar(
pickImage: PickImage,
onImageSelected: (Uri) -> Unit,
imagePickerLauncher: ManagedActivityResultLauncher<Intent, ActivityResult>,
remoteFilePickerLauncher: ManagedActivityResultLauncher<Intent, ActivityResult>,
cameraLauncher: ManagedActivityResultLauncher<Intent, ActivityResult>,
onDeleteImage: () -> Unit
) {
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(16.dp), .padding(16.dp),
horizontalArrangement = Arrangement.Center horizontalArrangement = Arrangement.Center
) { ) {
IconButton(onClick = { IconButton(
}) { onClick = {
pickImage.takePicture(cameraLauncher)
}
) {
Icon( Icon(
painter = painterResource(id = R.drawable.ic_baseline_photo_camera_24), painter = painterResource(id = R.drawable.ic_baseline_photo_camera_24),
contentDescription = null, contentDescription = null,
@ -207,6 +272,7 @@ fun UploadAvatar() {
} }
IconButton(onClick = { IconButton(onClick = {
pickImage.selectLocal(imagePickerLauncher)
}) { }) {
Icon( Icon(
painter = painterResource(id = R.drawable.ic_folder_multiple_image), painter = painterResource(id = R.drawable.ic_folder_multiple_image),
@ -214,9 +280,11 @@ fun UploadAvatar() {
modifier = Modifier.size(24.dp) modifier = Modifier.size(24.dp)
) )
} }
IconButton(
IconButton(onClick = { onClick = {
}) { pickImage.selectRemote(remoteFilePickerLauncher)
}
) {
Icon( Icon(
painter = painterResource(id = R.drawable.baseline_tag_faces_24), painter = painterResource(id = R.drawable.baseline_tag_faces_24),
contentDescription = null, contentDescription = null,
@ -225,6 +293,7 @@ fun UploadAvatar() {
} }
IconButton(onClick = { IconButton(onClick = {
onDeleteImage()
}) { }) {
Icon( Icon(
painter = painterResource(id = R.drawable.ic_delete_grey600_24dp), painter = painterResource(id = R.drawable.ic_delete_grey600_24dp),
@ -530,6 +599,7 @@ fun CreateConversation(conversationCreationViewModel: ConversationCreationViewMo
} }
} }
} }
class CompanionClass { class CompanionClass {
companion object { companion object {
internal val TAG = ConversationCreationActivity::class.simpleName internal val TAG = ConversationCreationActivity::class.simpleName

View file

@ -11,11 +11,13 @@ import android.util.Log
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.domain.ConversationModel import com.nextcloud.talk.models.domain.ConversationModel
import com.nextcloud.talk.models.json.autocomplete.AutocompleteUser import com.nextcloud.talk.models.json.autocomplete.AutocompleteUser
import com.nextcloud.talk.models.json.conversations.Conversation import com.nextcloud.talk.models.json.conversations.Conversation
import com.nextcloud.talk.models.json.generic.GenericMeta import com.nextcloud.talk.models.json.generic.GenericMeta
import com.nextcloud.talk.repositories.conversations.ConversationsRepositoryImpl.Companion.STATUS_CODE_OK import com.nextcloud.talk.repositories.conversations.ConversationsRepositoryImpl.Companion.STATUS_CODE_OK
import com.nextcloud.talk.users.UserManager
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -23,7 +25,8 @@ import java.io.File
import javax.inject.Inject import javax.inject.Inject
class ConversationCreationViewModel @Inject constructor( class ConversationCreationViewModel @Inject constructor(
private val repository: ConversationCreationRepository private val repository: ConversationCreationRepository,
private val userManager: UserManager
) : ViewModel() { ) : ViewModel() {
private val _selectedParticipants = MutableStateFlow<List<AutocompleteUser>>(emptyList()) private val _selectedParticipants = MutableStateFlow<List<AutocompleteUser>>(emptyList())
val selectedParticipants: StateFlow<List<AutocompleteUser>> = _selectedParticipants val selectedParticipants: StateFlow<List<AutocompleteUser>> = _selectedParticipants
@ -35,6 +38,9 @@ class ConversationCreationViewModel @Inject constructor(
private val _deleteState = MutableStateFlow<DeleteAvatarState>(DeleteAvatarState.Loading) private val _deleteState = MutableStateFlow<DeleteAvatarState>(DeleteAvatarState.Loading)
val deleteState: StateFlow<DeleteAvatarState> = _deleteState val deleteState: StateFlow<DeleteAvatarState> = _deleteState
private val _currentUser = userManager.currentUser.blockingGet()
val currentUser: User = _currentUser
fun updateSelectedParticipants(participants: List<AutocompleteUser>) { fun updateSelectedParticipants(participants: List<AutocompleteUser>) {
_selectedParticipants.value = participants _selectedParticipants.value = participants
} }