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

기존에 가입된 계정이면 재회원가입이 아니라 로그인이 되도록 변경 #66

Merged
merged 2 commits into from
Jan 18, 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
2 changes: 2 additions & 0 deletions app/src/main/java/com/whyranoid/walkie/KoinModules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import com.whyranoid.domain.usecase.GetUserBadgesUseCase
import com.whyranoid.domain.usecase.GetUserDetailUseCase
import com.whyranoid.domain.usecase.GetUserPostPreviewsUseCase
import com.whyranoid.domain.usecase.LikePostUseCase
import com.whyranoid.domain.usecase.RequestLoginUseCase
import com.whyranoid.domain.usecase.SignOutUseCase
import com.whyranoid.domain.usecase.UploadPostUseCase
import com.whyranoid.domain.usecase.broadcast.AddGpsListener
Expand Down Expand Up @@ -166,6 +167,7 @@ val useCaseModule = module {
single { GetMyFollowingUseCase(get(), get()) }
single { GetFollowingsPostsUseCase(get(), get()) }
single { LikePostUseCase(get(), get()) }
single { RequestLoginUseCase(get()) }
}

val databaseModule = module {
Expand Down
2 changes: 2 additions & 0 deletions data/src/main/java/com/whyranoid/data/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ object API {

const val SEND_LIKE = "api/community/send-like"

const val LOGIN = "api/walkies/login"

object WalkingControl {
const val RUNNING_START = "api/walk/start"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.whyranoid.data.datasource.account

import com.whyranoid.data.getResult
import com.whyranoid.data.model.account.SignUpRequest
import com.whyranoid.data.model.account.toLoginData
import com.whyranoid.domain.datasource.AccountDataSource
import com.whyranoid.domain.model.account.LoginData

class AccountDataSourceImpl(private val accountService: AccountService) : AccountDataSource {
override suspend fun signUp(
Expand Down Expand Up @@ -38,4 +41,13 @@ class AccountDataSourceImpl(private val accountService: AccountService) : Accoun
requireNotNull(response.body()).let { Pair(it.isDuplicated, it.nickName ?: "empty") }
}
}

override suspend fun signIn(authorId: String): Result<LoginData> {
return kotlin.runCatching {
val response = accountService.login(authorId)
response.getResult {
it.toLoginData()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.whyranoid.data.datasource.account

import com.whyranoid.data.API
import com.whyranoid.data.model.account.LoginDataResponse
import com.whyranoid.data.model.account.NickCheckResponse
import com.whyranoid.data.model.account.SignUpRequest
import com.whyranoid.data.model.account.SignUpResponse
Expand All @@ -20,4 +21,9 @@ interface AccountService {
suspend fun signUp(
@Body signUpRequest: SignUpRequest,
): Response<SignUpResponse>

@GET(API.LOGIN)
suspend fun login(
@Query("uid") uid: String,
): Response<LoginDataResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.whyranoid.data.model.account

import com.whyranoid.domain.model.account.LoginData

data class LoginDataResponse(
val walkieId: Long,
val nickname: String,
val profileImg: String,
)

fun LoginDataResponse.toLoginData() = LoginData(
walkieId = walkieId,
nickname = nickname,
profileImg = profileImg,
)
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,19 @@ class AccountRepositoryImpl(
}
}

override suspend fun signIn(): Result<Long> {
override suspend fun signIn(authorId: String, name: String): Result<Long> {
return kotlin.runCatching {
// TODO API CALL and update
0L
accountDataSource.signIn(authorId).onSuccess {
val (uid, nickname, profileImg) = it
accountDataStore.updateUId(uid)
accountDataStore.updateUserName(name)
accountDataStore.updateNickName(nickname)
profileImg?.let { url -> accountDataStore.updateProfileUrl(url) }
return@runCatching it.walkieId
}.onFailure {
// Error handling
}
return Result.failure(Exception("로그인 실패"))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.whyranoid.domain.datasource

import com.whyranoid.domain.model.account.LoginData

interface AccountDataSource {
suspend fun signUp(
nickName: String,
Expand All @@ -10,4 +12,6 @@ interface AccountDataSource {
): Result<Long>

suspend fun nickCheck(nickName: String): Result<Pair<Boolean, String>>

suspend fun signIn(authorId: String): Result<LoginData>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.whyranoid.domain.model.account

data class LoginData(
val walkieId: Long,
val nickname: String,
val profileImg: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface AccountRepository {

suspend fun getUID(): Long

suspend fun signIn(): Result<Long>
suspend fun signIn(authorId: String, name: String): Result<Long>

suspend fun singOut(): Result<Boolean>
suspend fun checkNickName(nickName: String): Result<Pair<Boolean, String>>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.whyranoid.domain.usecase

import com.whyranoid.domain.repository.AccountRepository

class RequestLoginUseCase(
private val accountRepository: AccountRepository,
) {

suspend operator fun invoke(authorId: String, name: String): Result<Long> {
return accountRepository.signIn(authorId, name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,53 @@ import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.tasks.Task
import com.whyranoid.domain.usecase.RequestLoginUseCase
import com.whyranoid.presentation.R
import com.whyranoid.presentation.theme.WalkieTheme
import com.whyranoid.presentation.theme.WalkieTypography
import kotlinx.coroutines.launch
import org.koin.androidx.compose.get

@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun SignInInitialScreen(
isDay: Boolean = true,
alreadySignUp: () -> Unit,
goToAgreeState: (authId: String, name: String, url: String?) -> Unit,
) {
val scope = rememberCoroutineScope()
val scaffoldState = rememberScaffoldState()

val requestLoginUseCase = get<RequestLoginUseCase>()

val launcher =
rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == AppCompatActivity.RESULT_OK) {
val data: Intent? = result.data
val task: Task<GoogleSignInAccount> =
GoogleSignIn.getSignedInAccountFromIntent(data)
task.getResult(ApiException::class.java)?.let { account ->
handleSignInResult(
account = account,
goToAgreeState = goToAgreeState,
) { errorMsg ->
scope.launch { scaffoldState.snackbarHostState.showSnackbar(errorMsg) }

scope.launch {
val isRegistered = requestLoginUseCase(
requireNotNull(account.id),
requireNotNull(account.displayName)
).getOrNull()

if (isRegistered != -1L && isRegistered != null) {
alreadySignUp()
} else {
handleSignInResult(
account = account,
goToAgreeState = goToAgreeState,
) { errorMsg ->
scope.launch {
scaffoldState.snackbarHostState.showSnackbar(errorMsg)
}
}
}
}

}
} else {
scope.launch {
Expand Down Expand Up @@ -157,14 +177,14 @@ private fun signInWithGoogle(
@Composable
fun DaySignInInitialScreenPreview() {
WalkieTheme {
SignInInitialScreen(true) { _, _, _ -> }
SignInInitialScreen(true, {}) { _, _, _ -> }
}
}

@Preview
@Composable
fun NightSignInInitialScreenPreview() {
WalkieTheme {
SignInInitialScreen(false) { _, _, _ -> }
SignInInitialScreen(false, {}) { _, _, _ -> }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fun SignInScreen(
val signInState = viewModel.signInState.collectAsStateWithLifecycle()

when (signInState.value) {
is SignInState.InitialState -> SignInInitialScreen(isDay = isDay) { authId, name, url ->
is SignInState.InitialState -> SignInInitialScreen(isDay = isDay, finishSignIn) { authId, name, url ->
viewModel.goToAgreeState(authId, name, url)
}

Expand Down
Loading