Skip to content

Commit

Permalink
Merge pull request #373 from DKU-Dgaja/an-feat(#359)-앱-실행-시-token-유효하…
Browse files Browse the repository at this point in the history
…면-로그인-생략하는-기능-구현

[AN] feat(#359): 앱 실행 시 token 유효하면 로그인 생략하는 기능 구현
  • Loading branch information
saesang authored Jul 1, 2024
2 parents 7e90e02 + c69ef26 commit e36f9b1
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ class FeedRVAdapter(val context : Context, val studyInfoList : List<StudyInfo>)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
if (studyInfoList[position].profileImageUrl.isEmpty()) {
holder.backgroundColor.setBackgroundColor(Color.parseColor(backgroundColorList[position % 6]))
} else {
} else if (studyInfoList[position].profileImageUrl != "string") {
holder.backgroundColor.setBackgroundColor(Color.parseColor(studyInfoList[position].profileImageUrl))
} else {
holder.backgroundColor.setBackgroundColor(Color.parseColor(backgroundColorList[position % 6]))
}
holder.studyName.text = studyInfoList[position].topic
holder.commitRule.text = setCommitRule(studyInfoList[position].periodType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,53 @@ import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import com.takseha.presentation.R
import com.takseha.presentation.ui.auth.LoginActivity
import com.takseha.presentation.ui.home.MainHomeActivity
import com.takseha.presentation.viewmodel.auth.SplashViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class SplashActivity : AppCompatActivity() {
private val viewModel: SplashViewModel by viewModels()
/*
TODO :
리팩토링 시 구현해야 할 것
1. splash 화면에서 info api 연동해서 role 확인 -> 자동로그인 기능 구현(로그인 및 회원가입 화면 pass 하는 로직 구현)
2. 닉네임 특수기호(이모티콘) 입력 불가하도록 하는 기능 추가
3. githubId 유효성 검사
*/

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
window.statusBarColor = ContextCompat.getColor(this, R.color.BACKGROUND)

Handler(Looper.getMainLooper()).postDelayed({
startActivity(Intent(this, LoginActivity::class.java))
finish()
}, 2000)
lifecycleScope.launch {
viewModel.checkAvailableToken()
}
lifecycleScope.launch {
viewModel.availableTokenCheck.observe(this@SplashActivity) {
setMainHome(it)
Log.d("SplashActivity", it.toString())
}
}
}

private fun setMainHome(availableTokenState: Boolean) {
if (availableTokenState) {
Handler(Looper.getMainLooper()).postDelayed({
startActivity(Intent(this, MainHomeActivity::class.java))
finish()
}, 2000)
} else {
Handler(Looper.getMainLooper()).postDelayed({
startActivity(Intent(this, LoginActivity::class.java))
finish()
}, 2000)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.takseha.presentation.viewmodel.auth

import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.takseha.common.model.SPKey
import com.takseha.common.util.SP
import com.takseha.data.repository.auth.GitudyAuthRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

class SplashViewModel(application: Application) : AndroidViewModel(application) {
private var gitudyAuthRepository: GitudyAuthRepository = GitudyAuthRepository()
private val prefs = SP(getApplication())

private val bearerToken = "Bearer ${prefs.loadPref(SPKey.ACCESS_TOKEN, "0")} ${
prefs.loadPref(SPKey.REFRESH_TOKEN, "0")
}"

private var _availableTokenCheck = MutableLiveData<Boolean>()
val availableTokenCheck : LiveData<Boolean>
get() = _availableTokenCheck

suspend fun checkAvailableToken() {
val checkTokenResponse = gitudyAuthRepository.getUserInfo(bearerToken)
Log.d("SplashViewModel", bearerToken)

if (checkTokenResponse.isSuccessful) {
val resCode = checkTokenResponse.body()!!.resCode
val resMsg = checkTokenResponse.body()!!.resMsg

if (resCode == 200 && resMsg == "OK") {
_availableTokenCheck.value = true
} else {
_availableTokenCheck.value = false
Log.e("SplashViewModel", "https status error: $resCode, $resMsg")
}
} else {
Log.e(
"SplashViewModel",
"tokenResponse status: ${checkTokenResponse.code()}\ntokenResponse message: ${checkTokenResponse.message()}"
)
}
}
}

0 comments on commit e36f9b1

Please sign in to comment.