Skip to content

Commit

Permalink
Merge pull request #26 from minseonglove/feature/#8
Browse files Browse the repository at this point in the history
Feature: #8 로그인 화면 구현
  • Loading branch information
minseonglove authored Nov 15, 2022
2 parents 12b9029 + 9db9682 commit 86978fb
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 46 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ucmcCI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ jobs:
run: chmod +x gradlew
- name: Create google-service
run: echo '${{ secrets.GOOGLE_SERVICES_JSON }}' > ./data/google-services.json
- name: Create data dir
run: mkdir ./presentation/src/main/java/com/gta/presentation/secret
- name: Create Secrets
run: echo '${{ secrets.CONSTANTS }}' > ./presentation/src/main/java/com/gta/presentation/secret/Secrets.kt
- name: Build with Gradle
run: ./gradlew build
- name: Build Signed APK
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ lint/tmp/

# Mac
*.DS_Store
/presentation/src/main/java/com/gta/presentation/secret/Secrets.kt
22 changes: 0 additions & 22 deletions data/src/androidTest/java/com/gta/data/ExampleInstrumentedTest.kt

This file was deleted.

5 changes: 5 additions & 0 deletions presentation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.shobhitpuri.custombuttons:google-signin:1.1.0'

implementation platform('com.google.firebase:firebase-bom:31.0.2')
implementation 'com.google.firebase:firebase-auth-ktx'
implementation 'com.google.android.gms:play-services-auth:20.3.0'

// Test
testImplementation 'junit:junit:4.13.2'
Expand Down
4 changes: 2 additions & 2 deletions presentation/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<application>
<activity
android:name=".ui.LoginActivity"
android:name=".ui.login.LoginActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -22,4 +22,4 @@
</activity>
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.gta.presentation.di

import com.google.firebase.auth.FirebaseAuth
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import java.util.Locale
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object FirebaseAuthModule {

@Singleton
@Provides
fun provideFirebaseAuth() = FirebaseAuth.getInstance().apply {
setLanguageCode(Locale.getDefault().language)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.gta.presentation.di

import android.content.Context
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.gta.presentation.secret.FIREBASE_CLIENT_ID
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent
import dagger.hilt.android.qualifiers.ActivityContext

@Module
@InstallIn(ActivityComponent::class)
class FirebaseSigninModule {
@Provides
fun provideGoogleSignInOptions() = GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(FIREBASE_CLIENT_ID)
.requestEmail()
.build()

@Provides
fun provideGoogleSignInClient(@ActivityContext context: Context, options: GoogleSignInOptions) =
GoogleSignIn.getClient(context, options)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.gta.presentation.ui.login

import android.content.Intent
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.google.android.gms.auth.api.Auth
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.gta.presentation.databinding.ActivityLoginBinding
import com.gta.presentation.ui.MainActivity
import com.gta.presentation.ui.base.BaseActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding>(ActivityLoginBinding::inflate) {

@Inject
lateinit var googleSignInClient: GoogleSignInClient

private val viewModel: LoginViewModel by viewModels()

private val requestActivity = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == RESULT_OK) {
val authIntent = it.data ?: return@registerForActivityResult
val account = Auth.GoogleSignInApi.getSignInResultFromIntent(authIntent)?.signInAccount
viewModel.signinWithToken(account?.idToken)
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initCollector()
binding.btnLoginGoogle.setOnClickListener {
googleLogin()
}
}

override fun onResume() {
super.onResume()
viewModel.checkLoginState()
}

private fun initCollector() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.loginEvent.collectLatest { state ->
/*
로그인에 성공 했다면
1. real time db에 회원정보가 저장되어있는지 확인
2. 있으면 바로 화면 전환
3. 없으면 db에 회원정보 생성 후 화면 전환
*/
if (state) {
startMainActivity()
}
}
}
}
}

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

private fun googleLogin() {
requestActivity.launch(googleSignInClient.signInIntent)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.gta.presentation.ui.login

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.GoogleAuthProvider
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
class LoginViewModel @Inject constructor(
private val auth: FirebaseAuth
) : ViewModel() {

private val _loginEvent = MutableSharedFlow<Boolean>()
val loginEvent: SharedFlow<Boolean> get() = _loginEvent

fun checkLoginState() {
emitLoginEvent(auth.currentUser != null)
}

fun signinWithToken(token: String?) {
token ?: return
val credential = GoogleAuthProvider.getCredential(token, null)
auth.signInWithCredential(credential).addOnCompleteListener { task ->
if (task.isSuccessful) {
emitLoginEvent(true)
} else {
Timber.e(task.exception)
}
}
}

private fun emitLoginEvent(state: Boolean) {
viewModelScope.launch {
_loginEvent.emit(state)
}
}
}
9 changes: 9 additions & 0 deletions presentation/src/main/res/drawable/ic_logo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="160dp"
android:height="112dp"
android:viewportWidth="160"
android:viewportHeight="112">
<path
android:pathData="M136,40H132L104.81,6.01C103.31,4.13 101.4,2.62 99.24,1.58C97.08,0.54 94.71,0 92.31,0H38.83C32.29,0 26.41,3.98 23.98,10.06L12,40.56C5.11,42.35 0,48.55 0,56V84C0,86.21 1.79,88 4,88H16C16,101.25 26.75,112 40,112C53.26,112 64,101.25 64,88H96C96,101.25 106.75,112 120,112C133.26,112 144,101.25 144,88H156C158.21,88 160,86.21 160,84V64C160,50.74 149.26,40 136,40ZM40,100C33.38,100 28,94.62 28,88C28,81.38 33.38,76 40,76C46.62,76 52,81.38 52,88C52,94.62 46.62,100 40,100ZM58,40H29.23L38.83,16H58V40ZM70,40V16H92.31L111.51,40H70ZM120,100C113.38,100 108,94.62 108,88C108,81.38 113.38,76 120,76C126.62,76 132,81.38 132,88C132,94.62 126.62,100 120,100Z"
android:fillColor="@color/primaryColor"/>
</vector>
40 changes: 38 additions & 2 deletions presentation/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,42 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.LoginActivity">
tools:context=".ui.login.LoginActivity">

</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="@+id/iv_login_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3"
app:srcCompat="@drawable/ic_logo"
tools:ignore="ContentDescription" />

<TextView
android:id="@+id/tv_login_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/app_name"
android:textColor="@color/primaryColor"
android:textSize="@dimen/font_display_small"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="@+id/iv_login_logo"
app:layout_constraintStart_toStartOf="@+id/iv_login_logo"
app:layout_constraintTop_toBottomOf="@+id/iv_login_logo" />

<com.shobhitpuri.custombuttons.GoogleSignInButton
android:id="@+id/btn_login_google"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/padding_extra_large"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_login_logo" />


</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 86978fb

Please sign in to comment.