Skip to content

Commit 47c80b2

Browse files
DokySpfetiu
authored andcommitted
Bug fix. get and set ip address [#28]
- state와 stateflow 분리 - AppInitUseCase 생성 후, 앱 실행 시 초기 작업 코드 구성
1 parent 7178557 commit 47c80b2

File tree

4 files changed

+42
-33
lines changed

4 files changed

+42
-33
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.foke.together.domain.interactor
2+
3+
import kotlinx.coroutines.flow.firstOrNull
4+
import javax.inject.Inject
5+
6+
class AppInitUseCase @Inject constructor(
7+
private val getExternalCameraIPUseCase: GetExternalCameraIPUseCase,
8+
private val setExternalCameraIPUseCase: SetExternalCameraIPUseCase,
9+
) {
10+
suspend operator fun invoke() {
11+
getExternalCameraIPUseCase().firstOrNull()?.run {
12+
setExternalCameraIPUseCase(this)
13+
}
14+
}
15+
}

presenter/src/main/java/com/foke/together/presenter/screen/SettingScreen.kt

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import androidx.compose.foundation.layout.size
88
import androidx.compose.foundation.shape.RoundedCornerShape
99
import androidx.compose.material.icons.Icons
1010
import androidx.compose.material.icons.automirrored.filled.ArrowBack
11-
import androidx.compose.material.icons.automirrored.filled.FactCheck
12-
import androidx.compose.material.icons.filled.CheckCircle
1311
import androidx.compose.material3.*
1412
import androidx.compose.runtime.*
1513
import androidx.compose.material3.MaterialTheme
@@ -25,7 +23,6 @@ import com.foke.together.presenter.theme.FourCutTogetherTheme
2523
import androidx.hilt.navigation.compose.hiltViewModel
2624
import com.foke.together.domain.interactor.entity.CameraSourceType
2725
import com.foke.together.presenter.viewmodel.SettingViewModel
28-
import com.foke.together.util.AppPolicy
2926
import kotlinx.coroutines.flow.map
3027

3128
private const val CameraSourceTypeError = -1
@@ -38,9 +35,6 @@ fun SettingScreen(
3835
val cameraSelectedIndex by remember {
3936
viewModel.cameraSourceType.map { CameraSourceType.entries.indexOf(it) }
4037
}.collectAsState(CameraSourceTypeError)
41-
val cameraIPAddress by remember {
42-
viewModel.cameraIPAddress.map { it.address }
43-
}.collectAsState(initial = AppPolicy.DEFAULT_EXTERNAL_CAMERA_IP)
4438
val cameraTypeList = CameraSourceType.entries.map { it.name }
4539

4640
FourCutTogetherTheme {
@@ -152,27 +146,13 @@ fun SettingScreen(
152146
width = Dimension.wrapContent
153147
height = Dimension.wrapContent
154148
},
155-
value = cameraIPAddress,
156-
onValueChange = { viewModel.setCameraIPAddress(it) },
149+
value = viewModel.cameraIPAddressState,
150+
onValueChange = {
151+
viewModel.cameraIPAddressState = it
152+
viewModel.setCameraIPAddress(it)
153+
},
157154
label = { Text(text = "IP Address") },
158155
)
159-
IconButton(
160-
modifier = Modifier.constrainAs(IPButton){
161-
top.linkTo(IPAdrress.top)
162-
start.linkTo(IPAdrress.end)
163-
end.linkTo(parent.end)
164-
bottom.linkTo(IPAdrress.bottom)
165-
width = Dimension.wrapContent
166-
height = Dimension.fillToConstraints
167-
},
168-
onClick = popBackStack
169-
) {
170-
Icon(
171-
imageVector = Icons.Filled.CheckCircle,
172-
contentDescription = "IPButton",
173-
tint = MaterialTheme.colorScheme.primary
174-
)
175-
}
176156
}
177157
}
178158
}

presenter/src/main/java/com/foke/together/presenter/viewmodel/HomeViewModel.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.foke.together.presenter.viewmodel
22

33
import androidx.lifecycle.ViewModel
44
import androidx.lifecycle.viewModelScope
5+
import com.foke.together.domain.interactor.AppInitUseCase
56
import com.foke.together.domain.interactor.web.GetCurrentUserInformationUseCase
67
import com.foke.together.domain.interactor.web.SignInUseCase
78
import com.foke.together.util.AppLog
@@ -15,11 +16,14 @@ import javax.inject.Inject
1516
class HomeViewModel @Inject constructor(
1617
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
1718
private val signInUseCase: SignInUseCase,
18-
private val getCurrentUserInformationUseCase: GetCurrentUserInformationUseCase
19-
19+
private val getCurrentUserInformationUseCase: GetCurrentUserInformationUseCase,
20+
private val appInitUseCase: AppInitUseCase
2021
): ViewModel() {
2122
init {
2223
viewModelScope.launch(ioDispatcher) {
24+
// init external camera ip address
25+
appInitUseCase()
26+
2327
// TODO: this is test code. remove later
2428
signInUseCase(
2529

presenter/src/main/java/com/foke/together/presenter/viewmodel/SettingViewModel.kt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
package com.foke.together.presenter.viewmodel
22

3+
import androidx.compose.runtime.getValue
4+
import androidx.compose.runtime.mutableStateOf
5+
import androidx.compose.runtime.setValue
36
import androidx.lifecycle.ViewModel
47
import androidx.lifecycle.viewModelScope
58
import com.foke.together.domain.interactor.GetCameraSourceTypeUseCase
69
import com.foke.together.domain.interactor.GetExternalCameraIPUseCase
7-
import com.foke.together.domain.interactor.InitExternalCameraIPUseCase
810
import com.foke.together.domain.interactor.SetCameraSourceTypeUseCase
911
import com.foke.together.domain.interactor.SetExternalCameraIPUseCase
1012
import com.foke.together.domain.interactor.entity.CameraSourceType
1113
import com.foke.together.domain.interactor.entity.ExternalCameraIP
1214
import com.foke.together.util.AppLog
15+
import com.foke.together.util.di.IODispatcher
1316
import dagger.hilt.android.lifecycle.HiltViewModel
14-
import kotlinx.coroutines.flow.MutableStateFlow
17+
import kotlinx.coroutines.CoroutineDispatcher
1518
import kotlinx.coroutines.flow.SharingStarted
16-
import kotlinx.coroutines.flow.asStateFlow
19+
import kotlinx.coroutines.flow.collectLatest
1720
import kotlinx.coroutines.flow.shareIn
1821
import kotlinx.coroutines.launch
1922
import javax.inject.Inject
2023

2124
@HiltViewModel
2225
class SettingViewModel @Inject constructor(
26+
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
2327
getCameraSourceTypeUseCase: GetCameraSourceTypeUseCase,
2428
private val setCameraSourceTypeUseCase: SetCameraSourceTypeUseCase,
2529
getExternalCameraIPUseCase: GetExternalCameraIPUseCase,
@@ -30,13 +34,20 @@ class SettingViewModel @Inject constructor(
3034
started = SharingStarted.WhileSubscribed(5000),
3135
replay = 1
3236
)
33-
34-
// TODO: need to change usecase
3537
val cameraIPAddress = getExternalCameraIPUseCase().shareIn(
3638
scope = viewModelScope,
3739
started = SharingStarted.WhileSubscribed(5000),
3840
replay = 1
3941
)
42+
var cameraIPAddressState by mutableStateOf("1234")
43+
44+
init {
45+
viewModelScope.launch(ioDispatcher) {
46+
cameraIPAddress.collectLatest {
47+
cameraIPAddressState = it.address
48+
}
49+
}
50+
}
4051

4152
fun setCameraSourceType(index: Int){
4253
setCameraSourceType(CameraSourceType.entries[index])
@@ -51,7 +62,6 @@ class SettingViewModel @Inject constructor(
5162

5263
fun setCameraIPAddress(address: String){
5364
viewModelScope.launch {
54-
// TODO: add usecase
5565
setExternalCameraIPUseCase(ExternalCameraIP(address))
5666
}
5767
}

0 commit comments

Comments
 (0)