Skip to content

Commit

Permalink
[FEATURE/#23] feat login
Browse files Browse the repository at this point in the history
  • Loading branch information
chattymin committed Jul 30, 2024
1 parent 23dad70 commit f04dbee
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion data/src/main/java/com/kkkk/data/service/AuthService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ interface AuthService {

@POST("api/v1/login")
suspend fun postLogin(
@Header("") deviceTag: String,
@Header("Authorization") deviceTag: String,
): BaseResponse<AuthTokenDto>
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.kkkk.presentation.main.onboarding.splash

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import androidx.activity.viewModels
Expand All @@ -11,6 +9,7 @@ import com.kkkk.core.base.BaseActivity
import com.kkkk.core.extension.navigateToScreenClear
import com.kkkk.core.extension.setNavigationBarColorFromResource
import com.kkkk.core.extension.setStatusBarColorFromResource
import com.kkkk.core.extension.toast
import com.kkkk.presentation.main.onboarding.onbarding.OnboardingActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
Expand All @@ -22,25 +21,32 @@ import kr.genti.presentation.databinding.ActivitySplashBinding
class SplashActivity : BaseActivity<ActivitySplashBinding>(R.layout.activity_splash) {
private val viewModel by viewModels<SplashViewModel>()

@SuppressLint("HardwareIds")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setStatusBarColor()
setNavigationBarColor()
observeUserState()

val androidId: String = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)

println("Android ID: $androidId")
login()
}

private fun setStatusBarColor() = setStatusBarColorFromResource(R.color.purple_50)
private fun setNavigationBarColor() = setNavigationBarColorFromResource(R.color.purple_50)

private fun observeUserState() {
viewModel.userState.flowWithLifecycle(lifecycle).onEach { state ->
navigateToScreenClear<OnboardingActivity>()
if (state) {
navigateToScreenClear<OnboardingActivity>()
} else {
toast(getString(R.string.error_msg))
}
}.launchIn(lifecycleScope)
}

private fun login() {
viewModel.setAndroidId(getDeviceTag())
}

private fun getDeviceTag(): String =
Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@ package com.kkkk.presentation.main.onboarding.splash

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.kkkk.domain.repository.AuthRepository
import com.kkkk.domain.repository.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
class SplashViewModel @Inject constructor(

private val authRepository: AuthRepository,
private val userRepository: UserRepository,
) : ViewModel() {
private val _userState = MutableSharedFlow<Boolean>()
val userState: SharedFlow<Boolean>
get() = _userState

init {
initSplash()
}

private fun initSplash() {
fun setAndroidId(deviceTag: String) {
viewModelScope.launch {
delay(DELAY_TIME)
_userState.emit(true) // TODO: 추후 검증 로직이 생길 경우 변경 예정
authRepository.login(deviceTag)
.onSuccess { response ->
userRepository.setTokens(response.accessToken, response.refreshToken)
userRepository.setDeviceToken(deviceTag)
_userState.emit(true)
}.onFailure(Timber::e)
}
}

companion object {
private const val DELAY_TIME = 2000L
}
}

0 comments on commit f04dbee

Please sign in to comment.