Skip to content

Commit bdb7534

Browse files
committed
add: Add chat feature module
1 parent 460a435 commit bdb7534

13 files changed

Lines changed: 230 additions & 0 deletions

File tree

feature/chat/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

feature/chat/build.gradle.kts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
plugins {
2+
alias(libs.plugins.koin.library)
3+
alias(libs.plugins.koin.hilt)
4+
alias(libs.plugins.koin.library.orbit)
5+
alias(libs.plugins.kotlin.android)
6+
id("kotlin-parcelize")
7+
}
8+
9+
android {
10+
namespace = "in.koreatech.koin.feature.chat"
11+
12+
composeOptions {
13+
kotlinCompilerExtensionVersion = "1.5.10"
14+
}
15+
16+
buildFeatures {
17+
compose = true
18+
}
19+
}
20+
21+
dependencies {
22+
implementation(project(":core"))
23+
implementation(project(":domain"))
24+
implementation(project(":core:onboarding"))
25+
implementation(project(":core:designsystem"))
26+
implementation(project(":core:analytics"))
27+
28+
29+
implementation(libs.core.ktx)
30+
implementation(libs.appcompat)
31+
implementation(libs.material)
32+
33+
implementation(platform(libs.compose.bom))
34+
implementation(libs.bundles.compose.m3)
35+
36+
implementation(libs.timber)
37+
38+
implementation(libs.krossbow.stomp.core)
39+
40+
testImplementation(libs.junit)
41+
androidTestImplementation(libs.ext.junit)
42+
androidTestImplementation(libs.espresso.core)
43+
}

feature/chat/consumer-rules.pro

Whitespace-only changes.

feature/chat/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" >
3+
4+
<application>
5+
<activity
6+
android:name=".ui.room.ChatRoomActivity"
7+
android:exported="false" />
8+
</application>
9+
10+
</manifest>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package `in`.koreatech.koin.feature.chat.ui.room
2+
3+
import androidx.compose.foundation.layout.Box
4+
import androidx.compose.foundation.layout.padding
5+
import androidx.compose.material3.ExperimentalMaterial3Api
6+
import androidx.compose.material3.Scaffold
7+
import androidx.compose.runtime.Composable
8+
import androidx.compose.ui.Modifier
9+
import androidx.compose.ui.res.stringResource
10+
import androidx.hilt.navigation.compose.hiltViewModel
11+
import `in`.koreatech.koin.core.designsystem.component.topbar.KoinTopAppBar
12+
13+
@OptIn(ExperimentalMaterial3Api::class)
14+
@Composable
15+
fun ChatRoom(
16+
articleId: Int,
17+
viewModel: ChatRoomViewModel = hiltViewModel()
18+
) {
19+
Scaffold(
20+
topBar = {
21+
KoinTopAppBar(
22+
title = "Chat Room" //TODO: Replace later
23+
)
24+
}
25+
) { contentPadding ->
26+
Box(
27+
modifier = Modifier.padding(contentPadding)
28+
) {
29+
30+
}
31+
}
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package `in`.koreatech.koin.feature.chat.ui.room
2+
3+
import android.os.Bundle
4+
import androidx.activity.ComponentActivity
5+
import androidx.activity.compose.setContent
6+
import androidx.activity.enableEdgeToEdge
7+
import dagger.hilt.android.AndroidEntryPoint
8+
import `in`.koreatech.koin.core.designsystem.theme.KoinTheme
9+
import `in`.koreatech.koin.feature.chat.ui.room.ChatRoomViewModel.Companion.ARTICLE_ID
10+
11+
@AndroidEntryPoint
12+
class ChatRoomActivity : ComponentActivity() {
13+
override fun onCreate(savedInstanceState: Bundle?) {
14+
super.onCreate(savedInstanceState)
15+
enableEdgeToEdge()
16+
setContent {
17+
KoinTheme {
18+
ChatRoom(
19+
articleId = intent.getIntExtra(ARTICLE_ID, 0)
20+
)
21+
}
22+
}
23+
}
24+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package `in`.koreatech.koin.feature.chat.ui.room
2+
3+
sealed class ChatRoomSideEffect {
4+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package `in`.koreatech.koin.feature.chat.ui.room
2+
3+
import android.net.Uri
4+
5+
data class ChatRoomState (
6+
val articleId: Int = 0,
7+
val chatRoomId: Int = 0,
8+
val userId: Int = 0,
9+
val articleTitle: String = "",
10+
val chatPartnerProfileImage: Uri? = null,
11+
)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package `in`.koreatech.koin.feature.chat.ui.room
2+
3+
import android.net.Uri
4+
import androidx.lifecycle.SavedStateHandle
5+
import androidx.lifecycle.ViewModel
6+
import androidx.lifecycle.viewModelScope
7+
import dagger.assisted.Assisted
8+
import dagger.assisted.AssistedFactory
9+
import dagger.assisted.AssistedInject
10+
import dagger.hilt.android.lifecycle.HiltViewModel
11+
import `in`.koreatech.koin.domain.repository.ChatRepository
12+
import `in`.koreatech.koin.domain.usecase.chat.ChatWSConnectUseCase
13+
import `in`.koreatech.koin.domain.usecase.chat.ChatWSDisconnectUseCase
14+
import kotlinx.coroutines.flow.collectLatest
15+
import kotlinx.coroutines.launch
16+
import org.hildan.krossbow.stomp.LostReceiptException
17+
import org.hildan.krossbow.websocket.reconnection.WebSocketReconnectionException
18+
import org.orbitmvi.orbit.ContainerHost
19+
import org.orbitmvi.orbit.syntax.simple.intent
20+
import org.orbitmvi.orbit.syntax.simple.reduce
21+
import org.orbitmvi.orbit.viewmodel.container
22+
import timber.log.Timber
23+
24+
@HiltViewModel
25+
class ChatRoomViewModel @AssistedInject constructor(
26+
@Assisted private val savedStateHandle: SavedStateHandle,
27+
private val chatRepository: ChatRepository,
28+
private val chatWSConnectUseCase: ChatWSConnectUseCase,
29+
private val chatWSDisconnectUseCase: ChatWSDisconnectUseCase
30+
) : ViewModel(), ContainerHost<ChatRoomState, ChatRoomSideEffect> {
31+
override val container = container<ChatRoomState, ChatRoomSideEffect>(ChatRoomState())
32+
33+
@AssistedFactory
34+
interface Factory {
35+
fun create(savedStateHandle: SavedStateHandle): ChatRoomViewModel
36+
}
37+
38+
init {
39+
savedStateHandle.get<Int>(ARTICLE_ID)?.let {
40+
getChatRoom(it)
41+
}
42+
}
43+
44+
private fun getChatRoom(articleId: Int) = viewModelScope.launch {
45+
chatRepository.getChatRoomFromArticleId(articleId).collectLatest {
46+
intent {
47+
reduce {
48+
state.copy(
49+
articleId = it.articleId,
50+
chatRoomId = it.chatRoomId,
51+
userId = it.userId,
52+
articleTitle = it.articleTitle,
53+
chatPartnerProfileImage = Uri.parse(it.chatPartnerProfileImage)
54+
)
55+
}
56+
}
57+
chatWSConnectUseCase().onFailure { error ->
58+
if (error is WebSocketReconnectionException) {
59+
// Handle reconnection error
60+
Timber.d("${error.message}")
61+
}
62+
}
63+
}
64+
}
65+
66+
fun disconnectWS() = viewModelScope.launch {
67+
chatWSDisconnectUseCase().onFailure {
68+
// Sometimes the server closes the connection too quickly to send a RECEIPT, which is not really an error
69+
// So, we can ignore LostReceiptException
70+
// http://stomp.github.io/stomp-specification-1.2.html#Connection_Lingering
71+
if (it !is LostReceiptException) {
72+
Timber.e(it)
73+
}
74+
}
75+
}
76+
77+
companion object {
78+
const val ARTICLE_ID = "article_id"
79+
}
80+
}

0 commit comments

Comments
 (0)