11package com.troplo.privateuploader.components.chat
22
3+ import android.widget.Toast
4+ import androidx.compose.foundation.clickable
35import androidx.compose.foundation.layout.Arrangement
46import androidx.compose.foundation.layout.Column
57import androidx.compose.foundation.layout.WindowInsets
68import androidx.compose.foundation.layout.padding
79import androidx.compose.material.icons.Icons
10+ import androidx.compose.material.icons.filled.ExitToApp
811import androidx.compose.material.icons.filled.Settings
912import androidx.compose.material3.ExperimentalMaterial3Api
1013import androidx.compose.material3.Icon
@@ -14,9 +17,18 @@ import androidx.compose.material3.Text
1417import androidx.compose.material3.rememberModalBottomSheetState
1518import androidx.compose.runtime.Composable
1619import androidx.compose.runtime.MutableState
20+ import androidx.compose.runtime.mutableStateOf
21+ import androidx.compose.runtime.remember
1722import androidx.compose.ui.Modifier
23+ import androidx.compose.ui.platform.LocalContext
1824import androidx.compose.ui.unit.dp
25+ import androidx.lifecycle.ViewModel
26+ import androidx.lifecycle.viewModelScope
27+ import com.troplo.privateuploader.api.TpuApi
28+ import com.troplo.privateuploader.components.core.dialogs.DeleteConfirmDialog
1929import com.troplo.privateuploader.data.model.Chat
30+ import kotlinx.coroutines.Dispatchers
31+ import kotlinx.coroutines.launch
2032
2133@OptIn(ExperimentalMaterial3Api ::class )
2234@Composable
@@ -28,6 +40,14 @@ fun ChatActions(
2840 val bottomSheetState = rememberModalBottomSheetState(
2941 skipPartiallyExpanded = true
3042 )
43+ val viewModel = remember { ChatActionsViewModel () }
44+ val leaveChat = remember { mutableStateOf(false ) }
45+
46+ if (leaveChat.value) {
47+ DeleteConfirmDialog (open = leaveChat, onConfirm = {
48+ viewModel.leaveChat(chat.value?.association?.id ? : 0 )
49+ }, title = " chat" , name = chat.value?.name, terminology = " Leave" )
50+ }
3151
3252 ModalBottomSheet (
3353 onDismissRequest = { openBottomSheet.value = false },
@@ -38,15 +58,64 @@ fun ChatActions(
3858 modifier = Modifier .padding(bottom = 8 .dp),
3959 verticalArrangement = Arrangement .spacedBy(2 .dp)
4060 ) {
41- ListItem (
42- headlineContent = { Text (" Settings" ) },
43- leadingContent = {
44- Icon (
45- Icons .Default .Settings ,
46- contentDescription = " Settings icon"
47- )
48- }
49- )
61+ if (chat.value?.type == " group" && (chat.value?.association?.rank == " admin" || chat.value?.association?.rank == " owner" )) {
62+ val context = LocalContext .current
63+ ListItem (
64+ headlineContent = { Text (" Group Settings" ) },
65+ modifier = Modifier .clickable {
66+ Toast .makeText(context, " Coming soon to mobile!" , Toast .LENGTH_SHORT ).show()
67+ },
68+ leadingContent = {
69+ Icon (
70+ Icons .Default .Settings ,
71+ contentDescription = " Settings icon"
72+ )
73+ }
74+ )
75+ }
76+
77+ if (chat.value?.type == " group" ) {
78+ ListItem (
79+ headlineContent = { Text (" Leave Group" ) },
80+ supportingContent = {
81+ Text (" You will not be able to rejoin unless invited back." )
82+ },
83+ leadingContent = {
84+ Icon (
85+ Icons .Default .ExitToApp ,
86+ contentDescription = " Leave icon"
87+ )
88+ },
89+ modifier = Modifier .clickable {
90+ leaveChat.value = true
91+ }
92+ )
93+ } else {
94+ ListItem (
95+ headlineContent = { Text (" Leave DM" ) },
96+ supportingContent = {
97+ Text (" The recipient will not be able to contact you unless you re-initiate the DM." )
98+ },
99+ leadingContent = {
100+ Icon (
101+ Icons .Default .ExitToApp ,
102+ contentDescription = " Leave icon"
103+ )
104+ }
105+ )
106+ }
107+ }
108+ }
109+ }
110+
111+ class ChatActionsViewModel : ViewModel () {
112+ val loading = mutableStateOf(false )
113+ fun leaveChat (id : Int ) {
114+ loading.value = true
115+ viewModelScope.launch(Dispatchers .IO ) {
116+ val response = TpuApi .retrofitService.leaveChat(id).execute()
117+
118+ loading.value = false
50119 }
51120 }
52121}
0 commit comments