Skip to content

Commit

Permalink
[feat/auto_login]: Splash와 Token 비동기 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
dogdduddy committed Sep 10, 2023
1 parent 6de9c96 commit b6d9629
Showing 1 changed file with 39 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package org.gdsc.presentation.login
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.gdsc.domain.usecase.token.GetRefreshTokenUseCase
import org.gdsc.domain.usecase.token.PostRefreshTokenUseCase
Expand All @@ -18,8 +18,6 @@ import javax.inject.Inject
@AndroidEntryPoint
class SplashActivity : AppCompatActivity() {

val handle: Handler = Handler(Looper.getMainLooper())

@Inject
lateinit var verifyAccessTokenUseCase: VerifyAccessTokenUseCase

Expand All @@ -29,44 +27,48 @@ class SplashActivity : AppCompatActivity() {
@Inject
lateinit var getRefreshTokenUseCase: GetRefreshTokenUseCase

private val DELAY_TIME = 2000L

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
setToFullPage()

handle.postDelayed({

lifecycleScope.launch {
val isAccessTokenValid = verifyAccessTokenUseCase.invoke()

// 정상이면 그냥 리프레쉬
if (isAccessTokenValid) {
postRefreshTokenUseCase.invoke(getRefreshTokenUseCase.invoke())
val intent = Intent(applicationContext, MainActivity::class.java)
startActivity(intent)
finish()

// access Token이 만료되었다면
} else {
// refresh 시도
val isRefreshSuccess = postRefreshTokenUseCase.invoke(getRefreshTokenUseCase.invoke())

// refresh 성공했다면
if (isRefreshSuccess) {
val intent = Intent(applicationContext, MainActivity::class.java)
startActivity(intent)
finish()
// refresh 실패했다면
} else {
val intent = Intent(applicationContext, LoginActivity::class.java)
startActivity(intent)
finish()
}

}
}

}, 2000)
lifecycleScope.launch(Dispatchers.Main) {

val validateTokenJob = validateToken()

delay(DELAY_TIME)

if(validateTokenJob)
moveToMain()
else
moveToLogin()
}
}

private fun moveToMain() {
startActivity(Intent(applicationContext,MainActivity::class.java))
finish()
}

private fun moveToLogin() {
startActivity(Intent(applicationContext,LoginActivity::class.java))
finish()
}

private suspend fun validateToken(): Boolean {
val isAccessTokenValid = verifyAccessTokenUseCase.invoke()


return if (isAccessTokenValid) { // 정상이면 그냥 리프레쉬
postRefreshTokenUseCase.invoke(getRefreshTokenUseCase.invoke())
true
} else {
// refresh 시도
val isRefreshSuccess = postRefreshTokenUseCase.invoke(getRefreshTokenUseCase.invoke())
isRefreshSuccess
}
}

private fun setToFullPage() {
Expand Down

0 comments on commit b6d9629

Please sign in to comment.