Skip to content

Commit

Permalink
fix: Splash 화면 SplashActivity.kt 로 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
ddiyooong committed Jan 1, 2024
1 parent 43c860a commit b0ec36b
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 124 deletions.
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,18 @@

<activity
android:name=".presentation.main.MainActivity"
android:theme="@style/Theme.Mocacong"
android:exported="true">
</activity>

<activity
android:name=".presentation.login.LoginActivity"
android:theme="@style/Theme.Mocacong"
android:exported="true" >
</activity>

<activity
android:name=".presentation.splash.SplashActivity"
android:theme="@style/SplashActivityTheme"
android:exported="true" >
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.konkuk.mocacong.presentation.login

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import androidx.lifecycle.Lifecycle
Expand All @@ -10,15 +9,9 @@ import androidx.lifecycle.repeatOnLifecycle
import com.konkuk.mocacong.R
import com.konkuk.mocacong.databinding.ActivityLoginBinding
import com.konkuk.mocacong.presentation.base.BaseActivity
import com.konkuk.mocacong.presentation.main.MainActivity
import com.konkuk.mocacong.remote.repositories.LoginRepository
import com.konkuk.mocacong.util.TokenManager
import com.konkuk.mocacong.util.ViewModelFactory
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class LoginActivity : BaseActivity<ActivityLoginBinding>() {
override val TAG: String = "LoginActivity"
Expand All @@ -43,46 +36,9 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>() {
}
}

override fun onCreate(savedInstanceState: Bundle?) {
override fun initViewModel() {
viewModel =
ViewModelProvider(this, ViewModelFactory(LoginRepository()))[LoginViewModel::class.java]
checkToken()
super.onCreate(savedInstanceState)
}

private fun checkToken() {
lifecycleScope.launch {
val refreshToken = withContext(Dispatchers.Default) {
TokenManager.getRefreshToken().first()
}
if (refreshToken.isNullOrBlank()) {
return@launch
}

val response = withContext(Dispatchers.IO) {
viewModel.postRefresh(refreshToken)
}
response.byState(
onSuccess = {
CoroutineScope(Dispatchers.Default).launch {
TokenManager.saveAccessToken(it.accessToken)
startNextActivity(MainActivity::class.java)
}
},
onFailure = {
when(it.code){
1021->{
setTheme(R.style.Theme_Mocacong)
return@byState
}
1022->{
startNextActivity(MainActivity::class.java)
}
}
}
)
}

}

override fun afterViewCreated() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.konkuk.mocacong.remote.apis.KakaoRequest
import com.konkuk.mocacong.remote.models.request.OAuthRequest
import com.konkuk.mocacong.remote.models.request.ReIssueRequest
import com.konkuk.mocacong.remote.models.response.CheckDuplicateResponse
import com.konkuk.mocacong.remote.models.response.KakaoLoginResponse
import com.konkuk.mocacong.remote.repositories.LoginRepository
Expand All @@ -34,7 +33,6 @@ class LoginViewModel(val repository: LoginRepository) : ViewModel() {
var kakaoID: String = ""
var kakaoEmail: String = ""

suspend fun postRefresh(token: String) = repository.refresh(ReIssueRequest(token))

fun postKakaoLogin() = viewModelScope.launch(Dispatchers.IO) {
val kakaoRequest = KakaoRequest(email = kakaoEmail, platformId = kakaoID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CafeDetailUiModel(
val reviews: ReviewsUiModel,
val commentsCount: Int,
val comments: List<Comment?>,
val images : List<CafeImage?>
val images: List<CafeImage?>
) {
val studyTypeString: String = when (studyType) {
"solo" -> {
Expand Down Expand Up @@ -55,7 +55,7 @@ class CafeDetailUiModel(
cafeResponse.comments.forEachIndexed { index, comment ->
comments[index] = comment
}
val images = MutableList<CafeImage?>(5){null}
val images = MutableList<CafeImage?>(5) { CafeImage(0, null, false) }
cafeResponse.cafeImages.forEachIndexed { index, cafeImage ->
images[index] = cafeImage
}
Expand Down
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)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@ package com.konkuk.mocacong.remote.repositories

import com.konkuk.mocacong.remote.apis.AuthAPI
import com.konkuk.mocacong.remote.apis.KakaoRequest
import com.konkuk.mocacong.remote.apis.TokenAPI
import com.konkuk.mocacong.remote.models.request.OAuthRequest
import com.konkuk.mocacong.remote.models.request.ReIssueRequest
import com.konkuk.mocacong.remote.models.response.CheckDuplicateResponse
import com.konkuk.mocacong.remote.models.response.KakaoLoginResponse
import com.konkuk.mocacong.remote.models.response.ReIssueResponse
import com.konkuk.mocacong.util.ApiState
import com.konkuk.mocacong.util.RetrofitClient

class LoginRepository : BaseRepository() {
val api = RetrofitClient.create(AuthAPI::class.java)
val tokenApi = RetrofitClient.create(TokenAPI::class.java)

suspend fun refresh(reIssueRequest: ReIssueRequest): ApiState<ReIssueResponse> =
makeRequest { tokenApi.updateAccessToken(reIssueRequest) }

suspend fun postKakaoLogin(kakaoRequest: KakaoRequest): ApiState<KakaoLoginResponse> =
makeRequest { api.kakaoLoginPost(kakaoRequest) }
Expand Down
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) }

}
5 changes: 0 additions & 5 deletions app/src/main/java/com/konkuk/mocacong/util/Exceptions.kt

This file was deleted.

This file was deleted.

12 changes: 0 additions & 12 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,12 @@
<item name="colorAccent">@color/ssook</item>
<item name="android:textColorHighlight">@color/ssook</item>


<!--font style-->
<item name="android:textViewStyle">@style/customTextViewFontStyle</item>
<item name="android:buttonStyle">@style/customButtonFontStyle</item>
<item name="android:editTextStyle">@style/customEditTextFontStyle</item>
<item name="android:radioButtonStyle">@style/customRadioButtonFontStyle</item>
<item name="android:checkboxStyle">@style/customCheckboxFontStyle</item>

</style>

<style name="Theme.Mocacong.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<style name="Theme.Mocacong.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="Theme.Mocacong.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />


</resources>

0 comments on commit b0ec36b

Please sign in to comment.