-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from Nexters/feature/authentication
코드 정리
- Loading branch information
Showing
8 changed files
with
133 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,46 @@ | ||
package com.keyme.app | ||
|
||
import android.os.Bundle | ||
import android.widget.Toast | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import com.keyme.app.ui.KeymeApp | ||
import com.keyme.presentation.UiEvent | ||
import com.keyme.presentation.UiEventManager | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class MainActivity : ComponentActivity() { | ||
|
||
@Inject | ||
lateinit var uiEventManager: UiEventManager | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContent { | ||
KeymeApp() | ||
} | ||
|
||
handleUiEvent() | ||
} | ||
|
||
private fun handleUiEvent() { | ||
lifecycleScope.launch { | ||
repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
uiEventManager.uiEvent.collectLatest { | ||
when (it) { | ||
is UiEvent.Toast -> { | ||
Toast.makeText(this@MainActivity, it.message, Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
presentation/src/main/java/com/keyme/presentation/UiEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.keyme.presentation | ||
|
||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
sealed interface UiEvent { | ||
class Toast(val message: String) : UiEvent | ||
} | ||
|
||
@Singleton | ||
class UiEventManager @Inject constructor() { | ||
private val _uiEvent = MutableSharedFlow<UiEvent>() | ||
val uiEvent = _uiEvent.asSharedFlow() | ||
|
||
suspend fun onEvent(event: UiEvent) { | ||
_uiEvent.emit(event) | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
presentation/src/main/java/com/keyme/presentation/UiState.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 11 additions & 27 deletions
38
presentation/src/main/java/com/keyme/presentation/signin/SignInViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,31 @@ | ||
package com.keyme.presentation.signin | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.keyme.domain.entity.onApiError | ||
import com.keyme.domain.entity.onFailure | ||
import com.keyme.domain.entity.onSuccess | ||
import com.keyme.domain.usecase.SignInUseCase | ||
import com.keyme.presentation.UiState | ||
import com.keyme.presentation.BaseViewModel | ||
import com.keyme.presentation.signin.enums.SignInStateEnum | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class SignInViewModel @Inject constructor( | ||
private val signInUseCase: SignInUseCase, | ||
) : ViewModel() { | ||
) : BaseViewModel() { | ||
|
||
private val _keymeSignInState = MutableStateFlow<UiState<SignInStateEnum>>(UiState.Loading) | ||
val keymeSignInState: StateFlow<UiState<SignInStateEnum>> = _keymeSignInState | ||
private val _keymeSignInState = MutableStateFlow(SignInStateEnum.NICKNAME) | ||
val keymeSignInState: StateFlow<SignInStateEnum> = _keymeSignInState.asStateFlow() | ||
|
||
fun signInWithKeyme( | ||
token: String, | ||
) { | ||
viewModelScope.launch { | ||
signInUseCase.invoke(token) | ||
.onSuccess { | ||
_keymeSignInState.value = UiState.Success( | ||
when { | ||
it.nickname.isNullOrBlank() -> SignInStateEnum.NICKNAME | ||
it.onboardingTestResultId == null -> SignInStateEnum.KEYME_TEST | ||
else -> SignInStateEnum.MY_DAILY | ||
}, | ||
) | ||
} | ||
.onApiError { code, message -> | ||
_keymeSignInState.value = UiState.ApiError(code, message) | ||
} | ||
.onFailure { | ||
_keymeSignInState.value = UiState.Failure(it) | ||
} | ||
apiCall(apiRequest = { signInUseCase.invoke(token) }) { | ||
_keymeSignInState.value = when { | ||
it.nickname.isNullOrBlank() -> SignInStateEnum.NICKNAME | ||
it.onboardingTestResultId == null -> SignInStateEnum.KEYME_TEST | ||
else -> SignInStateEnum.MY_DAILY | ||
} | ||
} | ||
} | ||
} |