Skip to content

Commit

Permalink
[FEAT]#30: 학교 정보 가져오기
Browse files Browse the repository at this point in the history
  • Loading branch information
flash159483 committed Jul 17, 2024
1 parent ac54c9e commit 05ffb5d
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.danggeun.common.di

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.Dispatchers
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object CoroutineModule {
@Provides
@Singleton
fun provideIoDispatcher() = Dispatchers.IO

@Provides
@Singleton
fun provideMainDispatcher() = Dispatchers.Main
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bff.wespot.model.auth

data class School(
val id: Int,
val name: String,
val address: String,
val type: String,
)
1 change: 1 addition & 0 deletions feature/auth/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ dependencies {
implementation(libs.bundles.orbit)
implementation(libs.junit)
implementation(libs.androidx.junit)
implementation(libs.timber)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import com.ramcosta.composedestinations.DestinationsNavHost
import com.ramcosta.composedestinations.navigation.dependency
import com.ramcosta.composedestinations.navigation.navigate
import com.ramcosta.composedestinations.rememberNavHostEngine
import dagger.hilt.android.AndroidEntryPoint
import org.orbitmvi.orbit.compose.collectSideEffect

@AndroidEntryPoint
class AuthActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fun SchoolScreen(
)
}

if (state.schoolSearchList.isEmpty()) {
if (state.schoolList.isEmpty()) {
Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) {
WSTextButton(
text = stringResource(id = R.string.no_school_found),
Expand All @@ -113,7 +113,7 @@ fun SchoolScreen(
}

LazyColumn {
items(state.schoolSearchList, key = { school ->
items(state.schoolList, key = { school ->
school.id
}) { school ->
SchoolListItem(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.bff.wespot.auth.state

import com.bff.wespot.model.SchoolItem
import com.bff.wespot.model.auth.School

sealed class AuthAction {
data class OnSchoolSearchChanged(val text: String) : AuthAction()

data class OnSchoolSelected(val school: SchoolItem) : AuthAction()
data class OnSchoolSelected(val school: School) : AuthAction()

data class OnGradeChanged(val grade: Int) : AuthAction()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.bff.wespot.auth.state

import com.bff.wespot.model.SchoolItem
import com.bff.wespot.model.auth.School

data class AuthUiState(
val schoolName: String = "",
val schoolList: List<SchoolItem> = listOf(SchoolItem("1", "2", "3")),
val schoolSearchList: List<SchoolItem> = listOf(SchoolItem("1", "2", "3")),
val selectedSchool: SchoolItem? = null,
val schoolList: List<School> = emptyList(),
val selectedSchool: School? = null,
val grade: Int = -1,
val gradeBottomSheet: Boolean = true,
val classNumber: Int = -1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,37 @@ import com.bff.wespot.auth.state.AuthAction
import com.bff.wespot.auth.state.AuthSideEffect
import com.bff.wespot.auth.state.AuthUiState
import com.bff.wespot.auth.state.NavigationAction
import com.bff.wespot.domain.repository.auth.AuthRepository
import com.bff.wespot.domain.usecase.KakaoLoginUseCase
import com.bff.wespot.model.SchoolItem
import com.bff.wespot.model.auth.School
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
import org.orbitmvi.orbit.ContainerHost
import org.orbitmvi.orbit.syntax.simple.intent
import org.orbitmvi.orbit.syntax.simple.postSideEffect
import org.orbitmvi.orbit.syntax.simple.reduce
import org.orbitmvi.orbit.viewmodel.container
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
class AuthViewModel @Inject constructor(
private val kakaoLoginUseCase: KakaoLoginUseCase,
private val authRepository: AuthRepository,
private val dispatcher: CoroutineDispatcher,
) : ViewModel(), ContainerHost<AuthUiState, AuthSideEffect> {
override val container = container<AuthUiState, AuthSideEffect>(AuthUiState())

private val userInput = MutableStateFlow("")

init {
monitorUserInput()
}

fun onAction(action: AuthAction) {
when (action) {
is AuthAction.OnSchoolSearchChanged -> handleSchoolSearchChanged(action.text)
Expand All @@ -45,19 +59,42 @@ class AuthViewModel @Inject constructor(

private fun handleSchoolSearchChanged(text: String) = intent {
reduce {
userInput.value = text
state.copy(
schoolName = text,
schoolSearchList = state.schoolList.filter {
it.name.contains(
text,
ignoreCase = true,
)
},
)
}
}

private fun handleSchoolSelected(school: SchoolItem) = intent {
private fun monitorUserInput() {
viewModelScope.launch {
userInput
.debounce(INPUT_DEBOUNCE_TIME)
.distinctUntilChanged()
.collect {
fetchSchoolList(it)
}
}
}

private fun fetchSchoolList(search: String) = intent {
viewModelScope.launch(dispatcher) {
Timber.d("enter")
authRepository.getSchoolList(search)
.onSuccess {
reduce {
state.copy(
schoolList = it,
)
}
}
.onFailure {
Timber.e(it)
}
}
}

private fun handleSchoolSelected(school: School) = intent {
reduce {
state.copy(
selectedSchool = school,
Expand Down Expand Up @@ -111,21 +148,30 @@ class AuthViewModel @Inject constructor(
is NavigationAction.NavigateToGradeScreen -> AuthSideEffect.NavigateToGradeScreen(
navigate.edit,
)

is NavigationAction.NavigateToSchoolScreen -> AuthSideEffect.NavigateToSchoolScreen(
navigate.edit,
)

is NavigationAction.NavigateToClassScreen -> AuthSideEffect.NavigateToClassScreen(
navigate.edit,
)

is NavigationAction.NavigateToGenderScreen -> AuthSideEffect.NavigateToGenderScreen(
navigate.edit,
)

is NavigationAction.NavigateToNameScreen -> AuthSideEffect.NavigateToNameScreen(
navigate.edit,
)

NavigationAction.NavigateToEditScreen -> AuthSideEffect.NavigateToEditScreen
NavigationAction.NavigateToCompleteScreen -> AuthSideEffect.NavigateToCompleteScreen
}
postSideEffect(sideEffect)
}

companion object {
private const val INPUT_DEBOUNCE_TIME = 500L
}
}

0 comments on commit 05ffb5d

Please sign in to comment.