Skip to content

Commit

Permalink
feat: 앱심사를 위한 로그인 페이지 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeOhHyung committed Jul 30, 2022
1 parent 3259837 commit 71b9f20
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 2 deletions.
8 changes: 8 additions & 0 deletions core/src/main/java/com/best/friends/core/BaseViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ abstract class BaseViewModel : ViewModel() {
val error: SharedFlow<String>
get() = _error

protected fun startLoading() {
_loading.value = true
}

protected fun stopLoading() {
_loading.value = false
}

protected fun sendErrorMessage(throwable: Throwable) {
sendErrorMessage(throwable.message.toString())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yapp.android2.data.remote.login

import com.yapp.android2.data.remote.RemoteDataSource
import com.yapp.android2.data.remote.request.LoginForAppReviewRequest
import com.yapp.android2.data.remote.request.PostLoginRequest
import com.yapp.android2.domain.entity.FCMToken
import com.yapp.android2.domain.entity.User
Expand All @@ -10,4 +11,6 @@ interface LoginRemoteDataSource : RemoteDataSource {
suspend fun postLogin(request: PostLoginRequest): User

suspend fun postFCMToken(request: FCMToken)

suspend fun loginForAppReview(request: LoginForAppReviewRequest): User
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yapp.android2.data.remote.login

import com.yapp.android2.data.remote.request.LoginForAppReviewRequest
import com.yapp.android2.data.remote.request.PostLoginRequest
import com.yapp.android2.data.service.LoginService
import com.yapp.android2.domain.entity.FCMToken
Expand All @@ -17,4 +18,8 @@ class LoginRemoteDataSourceImpl @Inject constructor(
override suspend fun postFCMToken(request: FCMToken) {
return loginService.postFCMToken(request)
}

override suspend fun loginForAppReview(request: LoginForAppReviewRequest): User {
return loginService.loginForAppReview(request).data ?: User.EMPTY
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.yapp.android2.data.remote.request

data class LoginForAppReviewRequest(
val email: String,
val password: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.yapp.android2.data.repository

import com.yapp.android2.data.local.login.LoginLocalDataSource
import com.yapp.android2.data.remote.login.LoginRemoteDataSource
import com.yapp.android2.data.remote.request.LoginForAppReviewRequest
import com.yapp.android2.data.remote.request.PostLoginRequest
import com.yapp.android2.domain.entity.FCMToken
import com.yapp.android2.domain.entity.User
Expand All @@ -23,6 +24,12 @@ class LoginRepositoryImpl @Inject constructor(
return loginRemoteDataSource.postLogin(request)
}

override suspend fun loginForAppReview(email: String, password: String) {
val request = LoginForAppReviewRequest(email, password)
val user = loginRemoteDataSource.loginForAppReview(request)
saveUser(user)
}

override suspend fun postFCMToken(fcmToken: String) {
val request = FCMToken(
fcmToken = fcmToken
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yapp.android2.data.service

import com.yapp.android2.data.remote.request.LoginForAppReviewRequest
import com.yapp.android2.data.remote.request.PostLoginRequest
import com.yapp.android2.domain.entity.FCMToken
import com.yapp.android2.domain.entity.User
Expand All @@ -18,4 +19,9 @@ interface LoginService : Service {
suspend fun postFCMToken(
@Body request: FCMToken
)

@POST("api/user/signin")
suspend fun loginForAppReview(
@Body request: LoginForAppReviewRequest
): ApiResponse<User>
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface LoginRepository : Repository {
providerId: String
): User

suspend fun loginForAppReview(email: String, password: String)

suspend fun postFCMToken(fcmToken: String)

fun saveUser(user: User)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,68 @@
package com.best.friends.login

import android.os.Bundle
import androidx.activity.viewModels
import com.best.friends.core.BaseActivity
import com.best.friends.login.databinding.ActivityLoginForAppReviewBinding
import com.best.friends.navigator.HomeNavigator
import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.messaging.FirebaseMessaging
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber
import javax.inject.Inject

@AndroidEntryPoint
class LoginForAppReviewActivity :
BaseActivity<ActivityLoginForAppReviewBinding>(R.layout.activity_login_for_app_review) {

private val viewModel by viewModels<LoginForAppReviewViewModel>()

@Inject
lateinit var homeNavigator: HomeNavigator

@Inject
lateinit var firebaseMessaging: FirebaseMessaging

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

initView()
observe()
initFirebase()
}

private fun initView() {
binding.viewModel = viewModel
}

private fun observe() {
viewModel.isRegisterUser.observe(this) {
if (it) {
viewModel.addFCMToken()
}
}

viewModel.isSuccess.observe(this) {
if (it) {
// 메인 화면으로 이동
val intent = homeNavigator.intent(this)
startActivity(intent)
finish()
}
}
}

private fun initFirebase() {
firebaseMessaging.token.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Timber.e(task.exception)
return@OnCompleteListener
}

// Get new FCM registration token
val token = task.result
viewModel.setFCMToken(token)
Timber.i("FCM Token: $token")
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.best.friends.login

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.distinctUntilChanged
import androidx.lifecycle.viewModelScope
import com.best.friends.core.BaseViewModel
import com.best.friends.core.extensions.Empty
import com.yapp.android2.domain.repository.login.LoginRepository
import com.yapp.android2.domain.usecase.PostFCMTokenUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
class LoginForAppReviewViewModel @Inject constructor(
private val loginRepository: LoginRepository,
private val postFCMTokenUseCase: PostFCMTokenUseCase
) : BaseViewModel() {

val email = MutableStateFlow(String.Empty)
val password = MutableStateFlow(String.Empty)

private val _fcmToken = MutableLiveData<String>()
val fcmToken: LiveData<String>
get() = _fcmToken

private val _isSuccess = MutableLiveData(false)
val isSuccess: LiveData<Boolean>
get() = _isSuccess.distinctUntilChanged()

private val _isRegisterUser = MutableLiveData(false)
val isRegisterUser: LiveData<Boolean>
get() = _isRegisterUser

fun login() {
viewModelScope.launch {
startLoading()
kotlin.runCatching {
loginRepository.loginForAppReview(email.value, password.value)
}.onSuccess {
_isRegisterUser.postValue(true)
}.onFailure {

}

stopLoading()
}
}

fun setFCMToken(token: String) {
_fcmToken.value = token
}

fun addFCMToken() {
viewModelScope.launch {
kotlin.runCatching {
postFCMTokenUseCase(requireNotNull(fcmToken.value))
}.onSuccess {
_isSuccess.postValue(true)
}.onFailure {
Timber.tag("--- LoginViewModel - FCM Token error").e("$it")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>

<variable
name="viewModel"
type="com.best.friends.login.LoginForAppReviewViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down Expand Up @@ -30,6 +37,7 @@
android:inputType="textEmailAddress"
android:paddingHorizontal="20dp"
android:paddingVertical="12dp"
android:text="@={viewModel.email}"
android:textAppearance="@style/Typography.Body3.Medium"
android:textColorHint="@color/gray2"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -48,6 +56,7 @@
android:inputType="textPassword"
android:paddingHorizontal="20dp"
android:paddingVertical="12dp"
android:text="@={viewModel.password}"
android:textAppearance="@style/Typography.Body3.Medium"
android:textColorHint="@color/gray2"
app:layout_constraintEnd_toEndOf="parent"
Expand Down Expand Up @@ -109,6 +118,7 @@

<TextView
android:id="@+id/tv_login"
android:onClick="@{() -> viewModel.login()}"
android:layout_width="0dp"
android:layout_height="46dp"
android:layout_marginHorizontal="30dp"
Expand All @@ -120,9 +130,17 @@
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_find_id"
app:layout_constraintTop_toBottomOf="@id/tv_find_id" />

/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{viewModel.loading, default=gone}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

0 comments on commit 71b9f20

Please sign in to comment.