-
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.
fix: Splash 화면 SplashActivity.kt 로 분리
- Loading branch information
Showing
10 changed files
with
115 additions
and
124 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
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
90 changes: 90 additions & 0 deletions
90
app/src/main/java/com/konkuk/mocacong/presentation/splash/SplashActivity.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,90 @@ | ||
package com.konkuk.mocacong.presentation.splash | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.widget.Toast | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.lifecycle.lifecycleScope | ||
import com.konkuk.mocacong.presentation.login.LoginActivity | ||
import com.konkuk.mocacong.presentation.main.MainActivity | ||
import com.konkuk.mocacong.remote.models.request.ReIssueRequest | ||
import com.konkuk.mocacong.remote.repositories.TokenRepository | ||
import com.konkuk.mocacong.util.TokenManager | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.flow.first | ||
|
||
@SuppressLint("CustomSplashScreen") | ||
class SplashActivity : AppCompatActivity() { | ||
val TAG = "Splash" | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
checkToken() | ||
} | ||
|
||
private val repository = TokenRepository() | ||
|
||
private fun checkToken() = lifecycleScope.launch { | ||
val startTime = System.currentTimeMillis().toInt() | ||
|
||
Log.d(TAG, "checkToken 들어옴") | ||
val refreshToken = withContext(Dispatchers.Default) { | ||
TokenManager.getRefreshToken().first() | ||
} | ||
if (refreshToken.isNullOrBlank()) { | ||
Log.d(TAG, "refresh token is NULL") | ||
gotoActivity(LoginActivity::class.java, startTime) | ||
return@launch | ||
} | ||
|
||
val response = withContext(Dispatchers.IO) { | ||
postRefresh(refreshToken) | ||
} | ||
response.byState( | ||
onSuccess = { | ||
CoroutineScope(Dispatchers.Default).launch { | ||
TokenManager.saveAccessToken(it.accessToken) | ||
gotoActivity(MainActivity::class.java, startTime) | ||
} | ||
}, | ||
onFailure = { | ||
when (it.code) { | ||
1021 -> { | ||
gotoActivity(LoginActivity::class.java, startTime) | ||
} | ||
1022 -> { | ||
gotoActivity(MainActivity::class.java, startTime) | ||
} | ||
else -> { | ||
Toast.makeText( | ||
this@SplashActivity, | ||
"${it.code} 오류로 로그인에 실패하였습니다. 다시 로그인해주세요.", | ||
Toast.LENGTH_SHORT | ||
).show() | ||
gotoActivity(LoginActivity::class.java, startTime) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
|
||
private suspend fun postRefresh(token: String) = repository.refresh(ReIssueRequest(token)) | ||
|
||
|
||
private fun gotoActivity(activity: Class<*>?, startTime: Int) = lifecycleScope.launch { | ||
val endTime = System.currentTimeMillis() // 종료 시간 기록 | ||
val elapsedTime = endTime - startTime // 경과 시간 계산 | ||
|
||
if (elapsedTime < 2000) { | ||
delay(2000 - elapsedTime) // 2초 동안 기다리기 | ||
} | ||
|
||
val intent = Intent(this@SplashActivity, activity) | ||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | ||
startActivity(intent) | ||
} | ||
|
||
|
||
} |
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
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/konkuk/mocacong/remote/repositories/TokenRepository.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,15 @@ | ||
package com.konkuk.mocacong.remote.repositories | ||
|
||
import com.konkuk.mocacong.remote.apis.TokenAPI | ||
import com.konkuk.mocacong.remote.models.request.ReIssueRequest | ||
import com.konkuk.mocacong.remote.models.response.ReIssueResponse | ||
import com.konkuk.mocacong.util.ApiState | ||
import com.konkuk.mocacong.util.RetrofitClient | ||
|
||
class TokenRepository: BaseRepository() { | ||
val api = RetrofitClient.create(TokenAPI::class.java) | ||
|
||
suspend fun refresh(reIssueRequest: ReIssueRequest): ApiState<ReIssueResponse> = | ||
makeRequest { api.updateAccessToken(reIssueRequest) } | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
51 changes: 0 additions & 51 deletions
51
app/src/main/java/com/konkuk/mocacong/util/TokenExceptionHandler.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