Skip to content

Commit c8772e9

Browse files
committed
feat(conversations): add conversation viewmodel
1 parent 08cdd90 commit c8772e9

File tree

9 files changed

+94
-46
lines changed

9 files changed

+94
-46
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,6 @@ dependencies {
6868

6969
// System bars customization
7070
implementation "com.google.accompanist:accompanist-systemuicontroller:0.28.0"
71+
implementation "androidx.compose.ui:ui-viewbinding:1.4.0"
72+
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1"
7173
}

app/src/main/java/com/chatgptlite/wanted/MainActivity.kt

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ package com.chatgptlite.wanted
33
import android.annotation.SuppressLint
44
import android.os.Bundle
55
import androidx.activity.ComponentActivity
6-
import androidx.compose.foundation.gestures.detectTapGestures
6+
import androidx.activity.compose.BackHandler
7+
import androidx.activity.viewModels
78
import androidx.compose.foundation.layout.*
89
import androidx.compose.material3.*
9-
import androidx.compose.runtime.Composable
10-
import androidx.compose.runtime.rememberCoroutineScope
10+
import androidx.compose.runtime.*
1111
import androidx.compose.ui.Modifier
12-
import androidx.compose.ui.input.pointer.pointerInput
1312
import androidx.compose.ui.platform.ComposeView
1413
import androidx.compose.ui.tooling.preview.Preview
1514
import androidx.core.view.WindowCompat
1615
import com.chatgptlite.wanted.ui.common.AppBar
1716
import com.chatgptlite.wanted.ui.common.AppScaffold
18-
import com.chatgptlite.wanted.ui.conversations.Conversations
17+
import com.chatgptlite.wanted.ui.conversations.Conversation
1918
import com.chatgptlite.wanted.ui.theme.ChatGPTLiteTheme
2019
import kotlinx.coroutines.launch
2120

2221

2322
class MainActivity : ComponentActivity() {
23+
private val viewModel: MainViewModel by viewModels()
2424

2525
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
2626
@OptIn(ExperimentalMaterial3Api::class)
@@ -34,16 +34,30 @@ class MainActivity : ComponentActivity() {
3434
consumeWindowInsets = false
3535
setContent {
3636
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
37+
viewModel
38+
val drawerOpen by viewModel.drawerShouldBeOpened.collectAsState()
39+
40+
if (drawerOpen) {
41+
// Open drawer and reset state in VM.
42+
LaunchedEffect(Unit) {
43+
// wrap in try-finally to handle interruption whiles opening drawer
44+
try {
45+
drawerState.open()
46+
} finally {
47+
viewModel.resetOpenDrawerAction()
48+
}
49+
}
50+
}
3751

3852
// Intercepts back navigation when the drawer is open
3953
val scope = rememberCoroutineScope()
40-
// if (drawerState.isOpen) {
41-
// BackPressHandler {
42-
// scope.launch {
43-
// drawerState.close()
44-
// }
45-
// }
46-
// }
54+
if (drawerState.isOpen) {
55+
BackHandler {
56+
scope.launch {
57+
drawerState.close()
58+
}
59+
}
60+
}
4761

4862
ChatGPTLiteTheme() {
4963
Surface(
@@ -55,9 +69,9 @@ class MainActivity : ComponentActivity() {
5569
scope.launch {
5670
drawerState.close()
5771
}
72+
73+
println(it)
5874
},
59-
onProfileClicked = {
60-
}
6175
) {
6276
Scaffold(
6377
modifier = Modifier.safeContentPadding(),
@@ -71,7 +85,7 @@ class MainActivity : ComponentActivity() {
7185
)
7286
},
7387
content = {
74-
Conversations()
88+
Conversation()
7589
},
7690
)
7791
}
@@ -83,19 +97,6 @@ class MainActivity : ComponentActivity() {
8397
}
8498
}
8599

86-
@Composable
87-
fun Greeting(name: String) {
88-
Text(text = "Hello $name!", modifier = Modifier.pointerInput(Unit) {
89-
detectTapGestures(
90-
onPress = { offset -> },
91-
onDoubleTap = { offset -> },
92-
onLongPress = { offset -> },
93-
onTap = { offset -> }
94-
)
95-
// or other similar...
96-
})
97-
}
98-
99100
@Preview(showBackground = true)
100101
@Composable
101102
fun DefaultPreview() {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.chatgptlite.wanted
2+
3+
import androidx.lifecycle.ViewModel
4+
import kotlinx.coroutines.flow.MutableStateFlow
5+
import kotlinx.coroutines.flow.asStateFlow
6+
7+
/**
8+
* Used to communicate between screens.
9+
*/
10+
class MainViewModel : ViewModel() {
11+
private val _drawerShouldBeOpened = MutableStateFlow(false)
12+
val drawerShouldBeOpened = _drawerShouldBeOpened.asStateFlow()
13+
14+
fun openDrawer() {
15+
_drawerShouldBeOpened.value = true
16+
}
17+
18+
fun resetOpenDrawerAction() {
19+
_drawerShouldBeOpened.value = false
20+
}
21+
}

app/src/main/java/com/chatgptlite/wanted/ui/common/AppBar.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import com.chatgptlite.wanted.ui.theme.BackGroundColor
2525

2626
@Composable
2727
fun AppBar(onClickMenu: () -> Unit) {
28-
2928
ChatGPTLiteTheme() {
3029
Surface(
3130
shadowElevation = 4.dp,

app/src/main/java/com/chatgptlite/wanted/ui/common/AppDrawer.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import com.chatgptlite.wanted.ui.theme.ChatGPTLiteTheme
3535

3636
@Composable
3737
fun AppDrawer(
38-
onProfileClicked: (String) -> Unit,
3938
onChatClicked: (String) -> Unit,
4039
) {
4140
val context = LocalContext.current
@@ -229,7 +228,7 @@ fun DrawerPreview() {
229228
ChatGPTLiteTheme {
230229
Surface {
231230
Column {
232-
AppDrawer({}, {})
231+
AppDrawer {}
233232
}
234233
}
235234
}
@@ -241,7 +240,7 @@ fun DrawerPreviewDark() {
241240
ChatGPTLiteTheme(darkTheme = true) {
242241
Surface {
243242
Column {
244-
AppDrawer({}, {})
243+
AppDrawer {}
245244
}
246245
}
247246
}

app/src/main/java/com/chatgptlite/wanted/ui/common/AppRoute.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import androidx.navigation.NavHostController
66
import androidx.navigation.compose.NavHost
77
import androidx.navigation.compose.composable
88
import androidx.navigation.compose.rememberNavController
9-
import com.chatgptlite.wanted.ui.conversations.Conversations
9+
import com.chatgptlite.wanted.ui.conversations.Conversation
1010

1111
@Composable
1212
fun AppRoute(
@@ -26,7 +26,7 @@ fun AppRoute(
2626
// val homeViewModel: HomeViewModel = viewModel(
2727
// factory = HomeViewModel.provideFactory(appContainer.postsRepository)
2828
// )
29-
Conversations (
29+
Conversation (
3030

3131
)
3232
}

app/src/main/java/com/chatgptlite/wanted/ui/common/AppScaffold.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import com.chatgptlite.wanted.ui.theme.ChatGPTLiteTheme
1313
@Composable
1414
fun AppScaffold (
1515
drawerState: DrawerState = rememberDrawerState(initialValue = Closed),
16-
onProfileClicked: (String) -> Unit,
1716
onChatClicked: (String) -> Unit,
1817
content: @Composable () -> Unit
1918
) {
@@ -22,7 +21,6 @@ fun AppScaffold (
2221
drawerState = drawerState,
2322
drawerContent = {
2423
AppDrawer (
25-
onProfileClicked = onProfileClicked,
2624
onChatClicked = onChatClicked
2725
)
2826
},

app/src/main/java/com/chatgptlite/wanted/ui/conversations/Conversations.kt renamed to app/src/main/java/com/chatgptlite/wanted/ui/conversations/Conversation.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package com.chatgptlite.wanted.ui.conversations
22

33
import androidx.compose.foundation.background
4-
import androidx.compose.foundation.gestures.scrollable
54
import androidx.compose.foundation.layout.*
65
import androidx.compose.foundation.lazy.LazyColumn
76
import androidx.compose.foundation.lazy.rememberLazyListState
87
import androidx.compose.foundation.rememberScrollState
98
import androidx.compose.foundation.shape.RoundedCornerShape
10-
import androidx.compose.foundation.verticalScroll
11-
import androidx.compose.material3.MaterialTheme
129
import androidx.compose.material3.Surface
1310
import androidx.compose.material3.Text
1411
import androidx.compose.runtime.Composable
@@ -20,13 +17,12 @@ import androidx.compose.ui.unit.dp
2017
import androidx.compose.ui.unit.sp
2118
import com.chatgptlite.wanted.data.fake.fakeMessages
2219
import com.chatgptlite.wanted.models.MessageModel
23-
import com.chatgptlite.wanted.ui.common.simpleVerticalScrollbar
2420
import com.chatgptlite.wanted.ui.conversations.components.TextInput
2521
import com.chatgptlite.wanted.ui.conversations.ui.theme.ChatGPTLiteTheme
2622
import com.chatgptlite.wanted.ui.theme.*
2723

2824
@Composable
29-
fun Conversations() {
25+
fun Conversation() {
3026
ChatGPTLiteTheme(darkTheme = true) {
3127
Surface(
3228
modifier = Modifier.fillMaxSize(),
@@ -49,11 +45,11 @@ fun ColumnScope.MessageList(messages: List<MessageModel>) {
4945
Modifier
5046
.fillMaxWidth()
5147
.weight(1f, false)
52-
.simpleVerticalScrollbar(
53-
state = listState,
54-
width = 4.dp,
55-
color = MaterialTheme.colorScheme.secondary,
56-
)
48+
// .simpleVerticalScrollbar(
49+
// state = listState,
50+
// width = 4.dp,
51+
// color = MaterialTheme.colorScheme.secondary,
52+
// )
5753
.padding(horizontal = 16.dp),
5854
reverseLayout = true,
5955
state = listState,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.chatgptlite.wanted.ui.conversations
2+
3+
import androidx.lifecycle.ViewModel
4+
import com.chatgptlite.wanted.models.ConversationModel
5+
import com.chatgptlite.wanted.models.MessageModel
6+
import kotlinx.coroutines.flow.MutableStateFlow
7+
import kotlinx.coroutines.flow.asStateFlow
8+
9+
/**
10+
* Used to communicate between screens.
11+
*/
12+
class ConversationViewModel : ViewModel() {
13+
private val _currentConversation: MutableStateFlow<String> = MutableStateFlow("")
14+
private val _messages: MutableStateFlow<HashMap<String, List<MessageModel>>> = MutableStateFlow(hashMapOf())
15+
private val _isFetching: MutableStateFlow<Boolean> = MutableStateFlow(false)
16+
17+
val currentConversationState = _currentConversation.asStateFlow()
18+
val messagesListState = _messages.asStateFlow()
19+
val isFetching = _isFetching.asStateFlow()
20+
21+
fun initConversation(conversation: ConversationModel) {
22+
_isFetching.value = true
23+
_currentConversation.value = conversation.id
24+
// Fetch data here
25+
26+
_isFetching.value = false
27+
}
28+
29+
fun setMessages(messages: List<MessageModel>) {
30+
_messages.value[_currentConversation.value] = messages
31+
}
32+
}

0 commit comments

Comments
 (0)