Skip to content

Commit

Permalink
Splash 사용자 등록여부 검사 오류 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
moon0900 committed Nov 29, 2024
1 parent 98f3965 commit b6afff5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 38 deletions.
63 changes: 42 additions & 21 deletions app/src/main/java/com/dna/beyoureyes/SplashActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ package com.dna.beyoureyes
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.daimajia.androidanimations.library.Techniques
import com.daimajia.androidanimations.library.YoYo
import com.dna.beyoureyes.databinding.ActivitySplashBinding
import com.dna.beyoureyes.model.FirebaseHelper
import com.dna.beyoureyes.model.UserInfo
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class SplashActivity : AppCompatActivity() {

Expand Down Expand Up @@ -43,25 +45,44 @@ class SplashActivity : AppCompatActivity() {
.playOn(binding.appName)

// Check if user is signed in (non-null) and update UI accordingly.
val currentUser = auth.currentUser

if (currentUser != null && FirebaseHelper.receiveUserData()) { // 앱 이용한 적 있는 유저
//Toast.makeText(this@SplashActivity, "이미 가입한 유저", Toast.LENGTH_LONG).show()
userId = currentUser.uid
AppUser.id = userId
Log.d("GOOGLE : ", AppUser.id.toString())
//FirebaseHelper.receiveUserData()
//Toast.makeText(this@SplashActivity, userId, Toast.LENGTH_LONG).show()
Handler().postDelayed({ startActivity(Intent(this, MainActivity::class.java)); finish(); }, 3 * 1000)
}
else { // 최초 접속
//Toast.makeText(this@SplashActivity, "가입안한 유저", Toast.LENGTH_LONG).show()
signInAnonymously()
Handler().postDelayed({ startActivity(Intent(this, OnboardingActivity::class.java)); finish(); }, 3 * 1000)
lifecycleScope.launch {
val currentUser = auth.currentUser
if (currentUser != null) {
val hasData = FirebaseHelper.receiveUserData(currentUser) // suspend 함수로 변경
Log.d("SPLASH", hasData.toString())
if (hasData) {
//Toast.makeText(this@SplashActivity, "이미 가입한 유저", Toast.LENGTH_LONG).show()
userId = currentUser.uid
AppUser.id = userId
Log.d("GOOGLE : ", AppUser.id.toString())
//FirebaseHelper.receiveUserData()
//Toast.makeText(this@SplashActivity, userId, Toast.LENGTH_LONG).show()
delay(3000) // 3초 지연
withContext(Dispatchers.Main) {
startActivity(Intent(this@SplashActivity, MainActivity::class.java))
finish()
}
} else {
// 최초 접속 (데이터 없음) - 익명 로그인 후 온보딩으로 이동
signInAnonymously()
delay(3000) // 3초 지연
withContext(Dispatchers.Main) {
startActivity(Intent(this@SplashActivity, OnboardingActivity::class.java))
finish()
}
}
} else {
// 최초 접속 (로그인 안됨) - 익명 로그인 후 온보딩으로 이동
//Toast.makeText(this@SplashActivity, "가입안한 유저", Toast.LENGTH_LONG).show()
signInAnonymously()
delay(3000) // 3초 지연
withContext(Dispatchers.Main) {
startActivity(Intent(this@SplashActivity, OnboardingActivity::class.java))
finish()
}
}
updateUI(currentUser) // UI 업데이트
}
updateUI(currentUser)


}
// [END on_start_check_user]

Expand Down
35 changes: 18 additions & 17 deletions app/src/main/java/com/dna/beyoureyes/model/FirebaseHelper.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.dna.beyoureyes.model

import android.util.Log
import androidx.camera.core.processing.SurfaceProcessorNode.In
import com.dna.beyoureyes.AppUser
import com.google.firebase.Timestamp
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import java.io.Serializable
import kotlinx.coroutines.tasks.await

class FirebaseHelper {
companion object {
Expand Down Expand Up @@ -48,19 +48,18 @@ class FirebaseHelper {
}
}

fun receiveUserData() : Boolean {
var hasData = true
suspend fun receiveUserData(currentUser: FirebaseUser): Boolean {
val db = Firebase.firestore
if (AppUser.id != null) {
db.collection("userInfo")
.whereEqualTo("userId", AppUser.id)
.get()
.addOnSuccessListener { info ->
if (info.isEmpty) {
Log.d("FIREBASE : ", "NO DATA FOUND")
hasData = false
return@addOnSuccessListener
}
return try {
val info = db.collection("userInfo")
.whereEqualTo("userId", currentUser.uid)
.get()
.await()
if (info.isEmpty) {
Log.d("RECEIVE_USER_DATA", "NO DATA FOUND")
false
} else {
Log.d("RECEIVE_USER_DATA", "DATA FOUND")
for (document in info) {
val userName = document.data.get("userName") as String
val userGender = document.data.get("userGender") as Long
Expand All @@ -70,10 +69,12 @@ class FirebaseHelper {
val profile = document.data.get("userProfile") as String
AppUser.setInfo(userName, userGender.toInt(), userBirth, userDisease, userAllergy, profile)
}
true
}
}
else { hasData = false }
return hasData
} catch (exception: Exception) {
Log.d("RECEIVE_USER_DATA", "Error getting documents: ", exception)
false
}
}
}
}

0 comments on commit b6afff5

Please sign in to comment.