@@ -20,6 +20,7 @@ import android.text.format.DateUtils
2020import androidx.compose.foundation.background
2121import androidx.compose.foundation.clickable
2222import androidx.compose.foundation.layout.Arrangement
23+ import androidx.compose.foundation.layout.Box
2324import androidx.compose.foundation.layout.Column
2425import androidx.compose.foundation.layout.ColumnScope
2526import androidx.compose.foundation.layout.Row
@@ -30,11 +31,13 @@ import androidx.compose.foundation.layout.fillMaxWidth
3031import androidx.compose.foundation.layout.height
3132import androidx.compose.foundation.layout.padding
3233import androidx.compose.foundation.layout.requiredWidth
33- import androidx.compose.foundation.layout.safeContent
34+ import androidx.compose.foundation.layout.safeDrawing
35+ import androidx.compose.foundation.layout.size
3436import androidx.compose.foundation.layout.windowInsetsPadding
3537import androidx.compose.foundation.lazy.LazyColumn
3638import androidx.compose.foundation.lazy.LazyItemScope
3739import androidx.compose.foundation.lazy.items
40+ import androidx.compose.foundation.shape.CircleShape
3841import androidx.compose.foundation.shape.RoundedCornerShape
3942import androidx.compose.material.icons.Icons
4043import androidx.compose.material.icons.filled.Add
@@ -48,10 +51,12 @@ import androidx.compose.material3.Text
4851import androidx.compose.runtime.Composable
4952import androidx.compose.runtime.collectAsState
5053import androidx.compose.runtime.getValue
54+ import androidx.compose.runtime.setValue
5155import androidx.compose.ui.Alignment
5256import androidx.compose.ui.Modifier
5357import androidx.compose.ui.draw.clip
5458import androidx.compose.ui.res.stringResource
59+ import androidx.compose.ui.text.style.TextOverflow
5560import androidx.compose.ui.unit.dp
5661import io.shubham0204.smollmandroid.R
5762import io.shubham0204.smollmandroid.data.Chat
@@ -69,30 +74,60 @@ fun DrawerUI(
6974 modifier =
7075 Modifier
7176 .background(MaterialTheme .colorScheme.surfaceContainer)
72- .windowInsetsPadding(WindowInsets .safeContent )
77+ .windowInsetsPadding(WindowInsets .safeDrawing )
7378 .padding(8 .dp)
7479 .requiredWidth(300 .dp)
7580 .fillMaxHeight(),
7681 ) {
77- Row (modifier = Modifier .fillMaxWidth(), horizontalArrangement = Arrangement .SpaceEvenly ) {
82+ Row (
83+ modifier = Modifier .fillMaxWidth(),
84+ horizontalArrangement = Arrangement .SpaceEvenly ,
85+ ) {
7886 Button (
7987 onClick = {
8088 val chatCount = viewModel.chatsDB.getChatsCount()
81- val newChat = viewModel.chatsDB.addChat(chatName = " Untitled ${chatCount + 1 } " )
89+ val newChat =
90+ viewModel.chatsDB.addChat(chatName = " Untitled ${chatCount + 1 } " )
8291 onItemClick(newChat)
8392 },
8493 ) {
8594 Icon (Icons .Default .Add , contentDescription = " New Chat" )
86- Text (stringResource(R .string.chat_drawer_new_chat), style = MaterialTheme .typography.labelMedium)
95+ Text (
96+ stringResource(R .string.chat_drawer_new_chat),
97+ style = MaterialTheme .typography.labelMedium,
98+ )
8799 }
88100 Button (
89101 onClick = onCreateTaskClick,
90102 ) {
91103 Icon (Icons .Default .AddTask , contentDescription = " New Task" )
92- Text (stringResource(R .string.chat_drawer_new_task), style = MaterialTheme .typography.labelMedium)
104+ Text (
105+ stringResource(R .string.chat_drawer_new_task),
106+ style = MaterialTheme .typography.labelMedium,
107+ )
93108 }
94109 }
95110 Spacer (modifier = Modifier .height(16 .dp))
111+ ChatsList (
112+ viewModel,
113+ onManageTasksClick,
114+ onItemClick,
115+ )
116+ }
117+ AppAlertDialog ()
118+ }
119+ }
120+
121+ @Composable
122+ private fun ColumnScope.ChatsList (
123+ viewModel : ChatScreenViewModel ,
124+ onManageTasksClick : () -> Unit ,
125+ onItemClick : (Chat ) -> Unit ,
126+ ) {
127+ val chats by viewModel.getChats().collectAsState(emptyList())
128+ val currentChat by viewModel.currChatState.collectAsState(null )
129+ LazyColumn (modifier = Modifier .weight(1f )) {
130+ item {
96131 Row (
97132 modifier =
98133 Modifier
@@ -120,49 +155,55 @@ fun DrawerUI(
120155 style = MaterialTheme .typography.labelSmall,
121156 )
122157 Spacer (modifier = Modifier .height(8 .dp))
123- ChatsList (viewModel, onItemClick)
124158 }
125- AppAlertDialog ()
126- }
127- }
128-
129- @Composable
130- private fun ColumnScope.ChatsList (
131- viewModel : ChatScreenViewModel ,
132- onItemClick : (Chat ) -> Unit ,
133- ) {
134- val chats by viewModel.getChats().collectAsState(emptyList())
135- LazyColumn (modifier = Modifier .weight(1f )) {
136- items(chats) { chat -> ChatListItem (chat, onItemClick) }
159+ items(chats) { chat ->
160+ ChatListItem (
161+ chat,
162+ onItemClick,
163+ currentChat?.id == chat.id,
164+ )
165+ }
137166 }
138167}
139168
140169@Composable
141170private fun LazyItemScope.ChatListItem (
142171 chat : Chat ,
143172 onItemClick : (Chat ) -> Unit ,
173+ isCurrentlySelected : Boolean ,
144174) {
145175 Row (
146176 modifier =
147177 Modifier
148178 .fillMaxWidth()
149- .padding(4 .dp)
150- .background(MaterialTheme .colorScheme.surfaceContainerHighest, RoundedCornerShape (8 .dp))
151179 .padding(8 .dp)
152180 .clip(RoundedCornerShape (8 .dp))
153181 .clickable { onItemClick(chat) }
154182 .animateItem(),
155183 verticalAlignment = Alignment .CenterVertically ,
156184 ) {
157- Column (modifier = Modifier .weight(1f )) {
158- Text (
159- chat.name,
160- style = MaterialTheme .typography.labelLarge,
161- )
162- Text (
163- text = DateUtils .getRelativeTimeSpanString(chat.dateUsed.time).toString(),
164- style = MaterialTheme .typography.labelMedium,
165- )
185+ Row (verticalAlignment = Alignment .CenterVertically ) {
186+ Column (modifier = Modifier .weight(1f )) {
187+ Text (
188+ chat.name,
189+ style = MaterialTheme .typography.bodyLarge,
190+ maxLines = 1 ,
191+ overflow = TextOverflow .Ellipsis ,
192+ )
193+ Text (
194+ text = DateUtils .getRelativeTimeSpanString(chat.dateUsed.time).toString(),
195+ style = MaterialTheme .typography.labelSmall,
196+ )
197+ }
198+ if (isCurrentlySelected) {
199+ Box (
200+ modifier =
201+ Modifier
202+ .padding(start = 4 .dp)
203+ .background(MaterialTheme .colorScheme.tertiary, CircleShape )
204+ .size(10 .dp),
205+ ) { }
206+ }
166207 }
167208 }
168209}
0 commit comments