[Android/Compose] 구글 메일 작성 구현

Jinny·2022년 8월 31일
0

Android(Compose)

목록 보기
8/12
post-thumbnail

Dialog.kt

@Composable
fun InquiryDialog(viewModel: MainViewModel, onChangeState: () -> Unit) {
    var inquiryContent by remember {
        mutableStateOf("")
    }
    AlertDialog(
        onDismissRequest = { onChangeState() },
        title = { Text(text = "문의", modifier = Modifier.fillMaxWidth(), textAlign = TextAlign.Center) },
        text = {
            Column {
```    		Spacer(modifier = Modifier.height(15.dp))
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Text(text = "문의 내용", modifier = Modifier.padding(bottom = 5.dp, end = 7.dp))
                    TextField(value = inquiryContent, onValueChange = { inquiryContent = it })
                }

            }
        },
        dismissButton = {
            TextButton(onClick = { onChangeState() }) {
                Text(text = "취소")
            }
        },
        confirmButton = {
            TextButton(
                onClick = {
                    onChangeState()
                    viewModel.sendEmail(inquiryContent)

                }) {
                Text(text = "문의")
            }
        }
    )
}

MainViewModel.kt

class MainViewModel: ViewModel() {
    private val _event = MutableSharedFlow<MainEvent>()
    val event = _event.asSharedFlow()

    var emailContent = MutableStateFlow("")

    fun sendEmail(content: String = viewModelScope.launch {
        emailContent.value = content
        _event.emit(MainEvent.SendEmail) }

    sealed class MainEvent{
        object SendEmail : MainEvent()
        object ToStartActivity : MainEvent()
    }

MainActivity

class MainActivity : ComponentActivity() {

    private val viewModel by viewModels<MainViewModel>()
    private val auth = FirebaseAuth.getInstance()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        lifecycleScope.launch {
            launch {
                viewModel.event.collect { event ->
                    when (event) {
                        MainViewModel.MainEvent.SendEmail -> sendEmail(viewModel.emailContent.value, viewModel.userEmail.value)
                    }
                }
            }
        }
    }

    private fun sendEmail(content: String){
    	val email = auth.currentUser?.email

        val managerEmail = "abc@gmail.com"
        val uri = Uri.parse("mailto:$managerEmail") // 받는 사람

        val intent = Intent(Intent.ACTION_SENDTO, uri)

        intent.putExtra(Intent.EXTRA_SUBJECT, "문의")
        intent.putExtra(Intent.EXTRA_TEXT, "문의 내용 : $content")
        intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(email))

        startActivity(intent)
    }

0개의 댓글