Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 화면 이동 로직 추가 #111

Merged
merged 5 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions app/src/main/java/com/project/meongcare/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MainActivity : AppCompatActivity() {
symptomViewModel = ViewModelProvider(this)[SymptomViewModel::class.java]

activityMainBinding.run {
// autoLogin()
autoLogin()

bottomNavigationViewMain.background = null
bottomNavigationViewMain.menu.getItem(1).isEnabled = false
Expand Down Expand Up @@ -219,11 +219,16 @@ class MainActivity : AppCompatActivity() {

fun autoLogin() {
lifecycleScope.launch {
userPreferences.email.collect { email ->
if (email == null) {
// OnBoardingFragment로 교체
lifecycleScope.launch {
val accessToken = userPreferences.getAccessToken()
val refreshToken = userPreferences.getRefreshToken()

if (accessToken.isNullOrEmpty() && refreshToken.isNullOrEmpty()) {
activityMainBinding.fragmentContainerView.findNavController().navigate(R.id.onBoardingFragment)
} else if (accessToken.isNullOrEmpty() && !refreshToken.isNullOrEmpty()) {
activityMainBinding.fragmentContainerView.findNavController().navigate(R.id.loginFragment)
} else {
// HomeFragment로 교체
activityMainBinding.fragmentContainerView.findNavController().navigate(R.id.homeFragment)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class ProfileFragment : Fragment(), PhotoMenuListener {
} else {
Log.i("Logout-kakao", "로그아웃 성공, SDK에서 토큰 삭제됨")
userPreferences.setProvider(null)
userPreferences.setAccessToken(null)
findNavController().navigate(R.id.action_profileFragment_to_loginFragment)
}
}
Expand All @@ -190,6 +191,7 @@ class ProfileFragment : Fragment(), PhotoMenuListener {
private fun naverLogout() {
NaverIdLoginSDK.logout()
userPreferences.setProvider(null)
userPreferences.setAccessToken(null)
findNavController().navigate(R.id.action_profileFragment_to_loginFragment)
}

Expand All @@ -208,6 +210,7 @@ class ProfileFragment : Fragment(), PhotoMenuListener {
if (task.isSuccessful) {
Log.d("Logout-google", "로그아웃 성공")
userPreferences.setProvider(null)
userPreferences.setAccessToken(null)
findNavController().navigate(R.id.action_profileFragment_to_loginFragment)
} else {
Log.e("Logout-google", "로그아웃 실패")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class SettingFragment : Fragment() {
settingViewModel.userDeleteResponse.observe(viewLifecycleOwner) { response ->
if (response != null && response == 200) {
userPreferences.setProvider(null)
userPreferences.setEmail(null)
userPreferences.setAccessToken(null)
userPreferences.setRefreshToken(null)
findNavController().navigate(R.id.action_settingFragment_to_loginFragment)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand All @@ -34,21 +35,33 @@ class UserPreferences
}
}

private suspend fun editEmail(email: String) {
private suspend fun editEmail(email: String?) {
context.userDataStore.edit { preferences ->
preferences[preferenceKeyEmail] = email
if (email == null) {
preferences.remove(preferenceKeyEmail)
} else {
preferences[preferenceKeyEmail] = email
}
}
}

private suspend fun editAccessToken(accessToken: String) {
private suspend fun editAccessToken(accessToken: String?) {
context.userDataStore.edit { preferences ->
preferences[preferenceKeyAccessToken] = accessToken
if (accessToken == null) {
preferences.remove(preferenceKeyAccessToken)
} else {
preferences[preferenceKeyAccessToken] = accessToken
}
}
}

private suspend fun editRefreshToken(refreshToken: String) {
private suspend fun editRefreshToken(refreshToken: String?) {
context.userDataStore.edit { preferences ->
preferences[preferenceKeyRefreshToken] = refreshToken
if (refreshToken == null) {
preferences.remove(preferenceKeyRefreshToken)
} else {
preferences[preferenceKeyRefreshToken] = refreshToken
}
}
}

Expand All @@ -58,19 +71,19 @@ class UserPreferences
}
}

fun setEmail(email: String) {
fun setEmail(email: String?) {
CoroutineScope(Dispatchers.IO).launch {
editEmail(email)
}
}

fun setAccessToken(accessToken: String) {
fun setAccessToken(accessToken: String?) {
CoroutineScope(Dispatchers.IO).launch {
editAccessToken(accessToken)
}
}

fun setRefreshToken(refreshToken: String) {
fun setRefreshToken(refreshToken: String?) {
CoroutineScope(Dispatchers.IO).launch {
editRefreshToken(refreshToken)
}
Expand All @@ -96,4 +109,13 @@ class UserPreferences
context.userDataStore.data.map { preferences ->
preferences[preferenceKeyRefreshToken]
}

// first() : 저장된 데이터 중 가장 최신 시점의 데이터를 반환
suspend fun getAccessToken(): String {
return context.userDataStore.data.first()[preferenceKeyAccessToken] ?: ""
}

suspend fun getRefreshToken(): String {
return context.userDataStore.data.first()[preferenceKeyRefreshToken] ?: ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
Expand All @@ -33,6 +34,7 @@ import com.project.meongcare.login.model.data.repository.FirebaseCloudMessagingS
import com.project.meongcare.login.model.entities.LoginRequest
import com.project.meongcare.login.viewmodel.LoginViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import javax.inject.Inject

Expand Down Expand Up @@ -63,10 +65,16 @@ class LoginFragment : Fragment() {

loginViewModel.loginResponse.observe(viewLifecycleOwner) { loginResponse ->
if (loginResponse != null) {
userPreferences.setAccessToken(loginResponse.accessToken)
userPreferences.setRefreshToken(loginResponse.refreshToken)

findNavController().navigate(R.id.action_loginFragment_to_dogAddOnBoardingFragment)
lifecycleScope.launch {
val refreshToken = userPreferences.getRefreshToken()
userPreferences.setAccessToken(loginResponse.accessToken)
userPreferences.setRefreshToken(loginResponse.refreshToken)
if (refreshToken.isNullOrEmpty()) {
findNavController().navigate(R.id.action_loginFragment_to_dogAddOnBoardingFragment)
} else {
findNavController().navigate(R.id.action_loginFragment_to_homeFragment)
}
}
} else {
Log.d("Login-viewmodel", "통신 실패")
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<action
android:id="@+id/action_loginFragment_to_dogAddOnBoardingFragment"
app:destination="@id/dogAddOnBoardingFragment" />
<action
android:id="@+id/action_loginFragment_to_homeFragment"
app:destination="@id/homeFragment" />
</fragment>

<fragment
Expand Down