Skip to content

Commit

Permalink
Bug fix. get and set ip address [#28]
Browse files Browse the repository at this point in the history
- state와 stateflow 분리
- AppInitUseCase 생성 후, 앱 실행 시 초기 작업 코드 구성
  • Loading branch information
DokySp committed Oct 11, 2024
1 parent 7178557 commit 21c4053
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.foke.together.domain.interactor

import kotlinx.coroutines.flow.firstOrNull
import javax.inject.Inject

class AppInitUseCase @Inject constructor(
private val getExternalCameraIPUseCase: GetExternalCameraIPUseCase,
private val setExternalCameraIPUseCase: SetExternalCameraIPUseCase,
) {
suspend operator fun invoke() {
getExternalCameraIPUseCase().firstOrNull()?.run {
setExternalCameraIPUseCase(this)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.automirrored.filled.FactCheck
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.material3.MaterialTheme
Expand All @@ -25,7 +23,6 @@ import com.foke.together.presenter.theme.FourCutTogetherTheme
import androidx.hilt.navigation.compose.hiltViewModel
import com.foke.together.domain.interactor.entity.CameraSourceType
import com.foke.together.presenter.viewmodel.SettingViewModel
import com.foke.together.util.AppPolicy
import kotlinx.coroutines.flow.map

private const val CameraSourceTypeError = -1
Expand All @@ -38,9 +35,6 @@ fun SettingScreen(
val cameraSelectedIndex by remember {
viewModel.cameraSourceType.map { CameraSourceType.entries.indexOf(it) }
}.collectAsState(CameraSourceTypeError)
val cameraIPAddress by remember {
viewModel.cameraIPAddress.map { it.address }
}.collectAsState(initial = AppPolicy.DEFAULT_EXTERNAL_CAMERA_IP)
val cameraTypeList = CameraSourceType.entries.map { it.name }

FourCutTogetherTheme {
Expand Down Expand Up @@ -152,27 +146,13 @@ fun SettingScreen(
width = Dimension.wrapContent
height = Dimension.wrapContent
},
value = cameraIPAddress,
onValueChange = { viewModel.setCameraIPAddress(it) },
value = viewModel.cameraIPAddressState,
onValueChange = {
viewModel.cameraIPAddressState = it
viewModel.setCameraIPAddress(it)
},
label = { Text(text = "IP Address") },
)
IconButton(
modifier = Modifier.constrainAs(IPButton){
top.linkTo(IPAdrress.top)
start.linkTo(IPAdrress.end)
end.linkTo(parent.end)
bottom.linkTo(IPAdrress.bottom)
width = Dimension.wrapContent
height = Dimension.fillToConstraints
},
onClick = popBackStack
) {
Icon(
imageVector = Icons.Filled.CheckCircle,
contentDescription = "IPButton",
tint = MaterialTheme.colorScheme.primary
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.foke.together.presenter.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.foke.together.domain.interactor.AppInitUseCase
import com.foke.together.domain.interactor.web.GetCurrentUserInformationUseCase
import com.foke.together.domain.interactor.web.SignInUseCase
import com.foke.together.util.AppLog
Expand All @@ -15,11 +16,14 @@ import javax.inject.Inject
class HomeViewModel @Inject constructor(
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
private val signInUseCase: SignInUseCase,
private val getCurrentUserInformationUseCase: GetCurrentUserInformationUseCase

private val getCurrentUserInformationUseCase: GetCurrentUserInformationUseCase,
private val appInitUseCase: AppInitUseCase
): ViewModel() {
init {
viewModelScope.launch(ioDispatcher) {
// init external camera ip address
appInitUseCase()

// TODO: this is test code. remove later
signInUseCase(
"[email protected]",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package com.foke.together.presenter.viewmodel

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.foke.together.domain.interactor.GetCameraSourceTypeUseCase
import com.foke.together.domain.interactor.GetExternalCameraIPUseCase
import com.foke.together.domain.interactor.InitExternalCameraIPUseCase
import com.foke.together.domain.interactor.SetCameraSourceTypeUseCase
import com.foke.together.domain.interactor.SetExternalCameraIPUseCase
import com.foke.together.domain.interactor.entity.CameraSourceType
import com.foke.together.domain.interactor.entity.ExternalCameraIP
import com.foke.together.util.AppLog
import com.foke.together.util.di.IODispatcher
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class SettingViewModel @Inject constructor(
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
getCameraSourceTypeUseCase: GetCameraSourceTypeUseCase,
private val setCameraSourceTypeUseCase: SetCameraSourceTypeUseCase,
getExternalCameraIPUseCase: GetExternalCameraIPUseCase,
Expand All @@ -30,13 +34,20 @@ class SettingViewModel @Inject constructor(
started = SharingStarted.WhileSubscribed(5000),
replay = 1
)

// TODO: need to change usecase
val cameraIPAddress = getExternalCameraIPUseCase().shareIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000),
replay = 1
)
var cameraIPAddressState by mutableStateOf("1234")

init {
viewModelScope.launch(ioDispatcher) {
cameraIPAddress.collectLatest {
cameraIPAddressState = it.address
}
}
}

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

fun setCameraIPAddress(address: String){
viewModelScope.launch {
// TODO: add usecase
setExternalCameraIPUseCase(ExternalCameraIP(address))
}
}
Expand Down

0 comments on commit 21c4053

Please sign in to comment.