Skip to content

Commit

Permalink
#86 chore : merge complete
Browse files Browse the repository at this point in the history
  • Loading branch information
yy0ung committed Nov 23, 2023
2 parents 27c1578 + 2b40663 commit 41585fb
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 74 deletions.
17 changes: 0 additions & 17 deletions Aos/.idea/deploymentTargetDropDown.xml

This file was deleted.

17 changes: 17 additions & 0 deletions Aos/app/src/main/java/com/avengers/nibobnebob/app/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,29 @@ import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import com.avengers.nibobnebob.BuildConfig
import com.avengers.nibobnebob.presentation.util.Constants.APP_NAME
import com.navercorp.nid.NaverIdLoginSDK
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class App : Application(){

override fun onCreate() {
super.onCreate()
initializeNaverSDK()
}

companion object{
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = APP_NAME)
}

private fun initializeNaverSDK(){
NaverIdLoginSDK.initialize(
applicationContext,
BuildConfig.NAVER_LOGIN_CLIENT_ID,
BuildConfig.NAVER_LOGIN_CLIENT_SECRET,
APP_NAME)
NaverIdLoginSDK.showDevelopersLog(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class AccessTokenInterceptor @Inject constructor(private val dataStoreManager: D
}
Log.d("토큰 테스트",accessToken.toString())
val builder: Request.Builder = chain.request().newBuilder()
accessToken.let {
builder.addHeader(AUTHORIZATION,"$BEARER $accessToken")
accessToken?.takeIf { it.isNotEmpty() }?.let {
builder.addHeader(AUTHORIZATION, "$BEARER $it")
}
return chain.proceed(builder.build())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import retrofit2.Response
enum class StatusCode{
EMPTY,
ERROR,
EXCEPTION
EXCEPTION,
ERROR_AUTH,
ERROR_NONE
}

sealed class BaseState<out T> {
Expand All @@ -23,7 +25,11 @@ suspend fun <T> runRemote(block: suspend () -> Response<T>): BaseState<T> {
} ?: BaseState.Error(StatusCode.EMPTY, "응답이 비어있습니다")
} else {
val errorData = Gson().fromJson(response.errorBody()?.string(), BaseState.Error::class.java)
BaseState.Error(StatusCode.ERROR, errorData.message)
when(response.code()){
401 -> BaseState.Error(StatusCode.ERROR_AUTH, errorData.message)
404 -> BaseState.Error(StatusCode.ERROR_NONE, errorData.message)
else -> BaseState.Error(StatusCode.ERROR, errorData.message)
}
}
} catch (e: Exception) {
BaseState.Error(StatusCode.EXCEPTION, e.message.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.avengers.nibobnebob.data.model.response.BaseResponse
import com.avengers.nibobnebob.data.model.response.NaverLoginResponse
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.Header
import retrofit2.http.POST

interface IntroApi {
Expand All @@ -15,5 +16,7 @@ interface IntroApi {
): Response<Unit>

@POST("api/auth/social-login")
suspend fun loginNaver(): Response<BaseResponse<NaverLoginResponse>>
suspend fun loginNaver(
@Header("Authorization")token : String
): Response<BaseResponse<NaverLoginResponse>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ interface IntroRepository {
body: DetailSignupRequest
): Flow<BaseState<Unit>>

fun loginNaver(): Flow<BaseState<BaseResponse<NaverLoginResponse>>>
fun loginNaver(
token: String
): Flow<BaseState<BaseResponse<NaverLoginResponse>>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.avengers.nibobnebob.data.model.response.BaseResponse
import com.avengers.nibobnebob.data.model.response.NaverLoginResponse
import com.avengers.nibobnebob.data.model.runRemote
import com.avengers.nibobnebob.data.remote.IntroApi
import com.avengers.nibobnebob.presentation.util.Constants.ACCESS
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject
Expand All @@ -19,8 +20,8 @@ class IntroRepositoryImpl @Inject constructor(
emit(result)
}

override fun loginNaver(): Flow<BaseState<BaseResponse<NaverLoginResponse>>> = flow {
val result = runRemote { api.loginNaver() }
override fun loginNaver(token : String): Flow<BaseState<BaseResponse<NaverLoginResponse>>> = flow {
val result = runRemote { api.loginNaver("$ACCESS $token") }
emit(result)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import com.avengers.nibobnebob.presentation.ui.intro.IntroActivity
import com.avengers.nibobnebob.presentation.ui.intro.IntroViewModel
import com.avengers.nibobnebob.presentation.ui.main.MainActivity
import com.navercorp.nid.NaverIdLoginSDK
import com.navercorp.nid.oauth.NidOAuthLogin
import com.navercorp.nid.oauth.OAuthLoginCallback
import com.navercorp.nid.profile.NidProfileCallback
import com.navercorp.nid.profile.data.NidProfileResponse
import dagger.hilt.android.AndroidEntryPoint


Expand All @@ -31,7 +34,6 @@ class LoginFragment : BaseFragment<FragmentLoginBinding>(R.layout.fragment_login
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.vm = viewModel
naverInitialize()
initEventObserver()

binding.btnNaver.setOnClickListener {
Expand All @@ -53,14 +55,6 @@ class LoginFragment : BaseFragment<FragmentLoginBinding>(R.layout.fragment_login
}
}

private fun naverInitialize(){
NaverIdLoginSDK.initialize(
requireContext(),
BuildConfig.NAVER_LOGIN_CLIENT_ID,
BuildConfig.NAVER_LOGIN_CLIENT_SECRET, TAG)
NaverIdLoginSDK.showDevelopersLog(true)
}

private fun naverLogin(){
val oAuthLoginCallback = object : OAuthLoginCallback{
override fun onError(errorCode: Int, message: String) {
Expand All @@ -75,20 +69,36 @@ class LoginFragment : BaseFragment<FragmentLoginBinding>(R.layout.fragment_login

override fun onSuccess() {
val token = NaverIdLoginSDK.getAccessToken().toString()
viewModel.loginNaver(token)
NidOAuthLogin().callProfileApi(object : NidProfileCallback<NidProfileResponse>{
override fun onError(errorCode: Int, message: String) {
onFailure(errorCode, message)
}
override fun onFailure(httpStatus: Int, message: String) {
val errorCode = NaverIdLoginSDK.getLastErrorCode().code
val errorDescription = NaverIdLoginSDK.getLastErrorDescription()
Log.d(TAG,"errorCode:$errorCode, errorDesc:$errorDescription")
}

override fun onSuccess(result: NidProfileResponse) {
viewModel.naverEmail.value = result.profile?.email.toString()
viewModel.loginNaver(token)
}
})
}
}
NaverIdLoginSDK.authenticate(requireContext(), oAuthLoginCallback)
}

private fun NavController.toDetailSignup(){
val action = LoginFragmentDirections.actionLoginFragmentToDetailSignupFragment()
val action = LoginFragmentDirections.actionLoginFragmentToDetailSignupFragment(
email = viewModel.naverEmail.value
)
this.navigate(action)
}

private fun NavController.toMainActivity(){
val intent = Intent(context, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
(activity as IntroActivity).finish()
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.avengers.nibobnebob.presentation.ui.intro.login

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.avengers.nibobnebob.app.DataStoreManager
import com.avengers.nibobnebob.data.model.BaseState
import com.avengers.nibobnebob.data.model.StatusCode
import com.avengers.nibobnebob.data.repository.IntroRepository
import com.avengers.nibobnebob.presentation.ui.intro.login.model.UiLoginData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -32,12 +35,13 @@ class LoginViewModel @Inject constructor(
private val _events = MutableSharedFlow<LoginEvent>()
val events = _events.asSharedFlow()

private val _uiState = MutableStateFlow(CommonRequest())
private val _uiState = MutableStateFlow(UiLoginData())
val uiState = _uiState.asStateFlow()

val email = MutableStateFlow("")
val password = MutableStateFlow("")
val autoLogin = MutableStateFlow(false)
val naverEmail = MutableStateFlow("")

init {
observeEmail()
Expand Down Expand Up @@ -70,26 +74,22 @@ class LoginViewModel @Inject constructor(

fun loginNaver(token : String){
viewModelScope.launch {
dataStoreManager.putAccessToken(token)
introRepository.loginNaver().onEach {
when(it){
introRepository.loginNaver(token).onEach { state ->
when(state){
is BaseState.Success -> {
dataStoreManager.putAutoLogin(true)
dataStoreManager.putAccessToken(it.data.body.accessToken.toString())
dataStoreManager.putRefreshToken(it.data.body.refreshToken.toString())

dataStoreManager.putAccessToken(state.data.body.accessToken.toString())
dataStoreManager.putRefreshToken(state.data.body.refreshToken.toString())
_events.emit(LoginEvent.NavigateToMain)
}
is BaseState.Error -> {
_events.emit(LoginEvent.NavigateToDetailSignup)
// when(it.statusCode){
// 401 -> {
// Log.d(TAG,"401이 뜰일이 있나..?")
// }
// 404 -> {
// _events.emit(LoginEvent.NavigateToDetailSignup)
// }
// }
when(state.statusCode){
StatusCode.ERROR_AUTH-> {Log.d(TAG,"토큰 오류")}
StatusCode.ERROR_NONE ->{ _events.emit(LoginEvent.NavigateToDetailSignup)}
else ->{
Log.d(TAG,"오류 Exception")
}
}
}
}
}.launchIn(viewModelScope)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.avengers.nibobnebob.presentation.ui.intro.login.model

data class UiLoginData(
val email : String ="",
val password : String =""
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ class SplashActivity : BaseActivity<ActivitySplashBinding>(ActivitySplashBinding
repeatOnStarted {
viewModel.events.collect {
when (it) {
is SplashViewModel.NavigationEvent.NavigateToIntro -> moveToIntroActivity()
is SplashViewModel.NavigationEvent.NavigateToMain -> moveToMainActivity()
is SplashUiEvent.NavigateToIntro -> toIntroActivity()
is SplashUiEvent.NavigateToMain -> toMainActivity()
}
}
}
}

private fun moveToMainActivity() {
private fun toMainActivity() {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}

private fun moveToIntroActivity() {
private fun toIntroActivity() {
val intent = Intent(this, IntroActivity::class.java)
startActivity(intent)
finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,31 @@ import com.avengers.nibobnebob.presentation.base.BaseActivityViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

sealed class SplashUiEvent {
data object NavigateToMain : SplashUiEvent()
data object NavigateToIntro : SplashUiEvent()
}

@HiltViewModel
class SplashViewModel @Inject constructor(
private val networkManager: NetworkManager,
private val dataStoreManager: DataStoreManager,
) : BaseActivityViewModel(networkManager) {

sealed class NavigationEvent {
data object NavigateToMain : NavigationEvent()
data object NavigateToIntro : NavigationEvent()
}

private val TAG = "SplashViewModelDebug"
private val _events = MutableSharedFlow<NavigationEvent>()
val events: SharedFlow<NavigationEvent> get() = _events
private val _events = MutableSharedFlow<SplashUiEvent>()
val events: SharedFlow<SplashUiEvent> = _events.asSharedFlow()

fun getAutoLogin() {
viewModelScope.launch {
dataStoreManager.getAutoLogin().collect { autoLogin ->
dataStoreManager.getAccessToken().collect { accessToken ->
if (autoLogin == true && accessToken != "") {
_events.emit(NavigationEvent.NavigateToMain)
_events.emit(SplashUiEvent.NavigateToMain)
} else {
_events.emit(NavigationEvent.NavigateToIntro)
_events.emit(SplashUiEvent.NavigateToIntro)
}
}
}
Expand Down

0 comments on commit 41585fb

Please sign in to comment.