Skip to content

Commit

Permalink
Merge pull request #157 from KimMooHyeon/feature#153
Browse files Browse the repository at this point in the history
๋กœ๊ทธ์ธ ํ™”๋ฉด Compose ์ „ํ™˜
  • Loading branch information
KimMooHyeon authored Jun 10, 2023
2 parents 533505a + 6bf9b13 commit b1e79fb
Show file tree
Hide file tree
Showing 17 changed files with 352 additions and 12 deletions.
14 changes: 13 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ android {
buildConfigField "String", "DATABASE_URL", localProperties["database.url"]
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
resConfigs "ko-rKR", "ko"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
Expand All @@ -46,9 +49,18 @@ android {
composeOptions {
kotlinCompilerExtensionVersion '1.4.7'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}

dependencies {
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation platform('androidx.compose:compose-bom:2022.10.00')
implementation 'androidx.compose.ui:ui-graphics'
androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
def nav_version = "2.5.3"
def hilt_version = "2.44"
def room_version = "2.4.3"
Expand Down Expand Up @@ -109,7 +121,7 @@ dependencies {
implementation "androidx.paging:paging-compose:$pagingComposeVersion"

// Exoplayer
implementation 'com.google.android.exoplayer:exoplayer:2.18.1'
implementation 'com.google.android.exoplayer:exoplayer:2.18.7'
// Coordinator-layout
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0'
// ffmpeg
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
android:name=".presentation.uploadfilm.UploadFilmActivity"
android:exported="false" />
<activity
android:name=".presentation.login.LoginActivity"
android:name=".presentation.login.LoginComposeActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>(R.layout.activity_login

override fun initView() {
setGoogleLogin()
setObserveLoginResult()
//setObserveLoginResult()
}

override fun onStart() {
Expand All @@ -52,7 +52,7 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>(R.layout.activity_login
}
}

private fun setObserveLoginResult() {
/* private fun setObserveLoginResult() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collectLatest { state ->
Expand All @@ -75,7 +75,7 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>(R.layout.activity_login
}
}
}
}
}*/

private fun showSnackBarMessage(message: String) {
Snackbar.make(findViewById(android.R.id.content), message, Snackbar.LENGTH_SHORT).show()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package com.boostcamp.dailyfilm.presentation.login

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.boostcamp.dailyfilm.R
import com.boostcamp.dailyfilm.presentation.calendar.CalendarActivity
import com.boostcamp.dailyfilm.presentation.util.UiState
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class LoginComposeActivity : ComponentActivity() {
private val viewModel by viewModels<LoginViewModel>()
private lateinit var activityResultLauncher: ActivityResultLauncher<Intent>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val options = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail().build()
val client = GoogleSignIn.getClient(this, options)

activityResultLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result: ActivityResult ->
if (result.resultCode == RESULT_OK) {
val task = GoogleSignIn.getSignedInAccountFromIntent(result.data)
try {
val account = task.getResult(ApiException::class.java)
viewModel.requestLogin(account.idToken!!)
} catch (e: ApiException) {
showSnackBarMessage(this, getString(R.string.failed_google_login))
}
}
}
setContent {
LoginUI(viewModel, client, activityResultLauncher)
}
}
}

@Composable
fun LoginUI(
viewModel: LoginViewModel,
client: GoogleSignInClient,
activityResultLauncher: ActivityResultLauncher<Intent>
) {
val activity = LocalContext.current as Activity

val uiState = viewModel.uiState.collectAsStateWithLifecycle()
var isLoading by rememberSaveable {
mutableStateOf(false)
}
LoginView(client, activityResultLauncher)
ProgressLoading(isLoading = isLoading)
when (val result = uiState.value.getContentIfNotHandled()) {
is UiState.Uninitialized -> {
autoLogin(activity)
}

is UiState.Loading -> {
isLoading = true
}

is UiState.Success -> {
isLoading = false
activity.startActivity(
Intent(
LocalContext.current,
CalendarActivity::class.java
)
)
activity.finish()
}

is UiState.Failure -> {
isLoading = false
result.throwable.message?.let {
showSnackBarMessage(activity, it)
}
}

else -> {}
}
}

@Composable
private fun LoginView(
client: GoogleSignInClient,
activityResultLauncher: ActivityResultLauncher<Intent>
) {
val backgroundColor = if (isSystemInDarkTheme()) {
colorResource(id = R.color.light_blue)
} else {
Color.White
}
val contentsColor = if (isSystemInDarkTheme()) {
Color.White
} else {
colorResource(id = R.color.dark_gray)
}
Column {
Logo()
LoginButton(activityResultLauncher, client, backgroundColor, contentsColor)
}
}

@Composable
fun ProgressLoading(isLoading: Boolean) {
if (isLoading) {
Surface(
color = Color.Black.copy(alpha = 0.2f),
modifier = Modifier
.fillMaxSize()
.background(Color.Black.copy(alpha = 0.2f))

) {
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
CircularProgressIndicator(
Modifier.size(150.dp),
color = colorResource(id = R.color.dark_gray),
strokeWidth = 8.dp
)
}
}
}
}

@Composable
fun LoginButton(
activityResultLauncher: ActivityResultLauncher<Intent>,
client: GoogleSignInClient,
backgroundColor: Color,
contentColor: Color
) {

Button(
onClick = {
activityResultLauncher.launch(client.signInIntent)
},
modifier = Modifier
.fillMaxWidth()
.height(48.dp)
.padding(start = 48.dp, end = 48.dp),
shape = RoundedCornerShape(6.dp),
colors = ButtonDefaults.buttonColors(
containerColor = backgroundColor,
contentColor = contentColor
),
border = BorderStroke(2.dp, colorResource(id = R.color.dark_gray))
) {
Image(
painter = painterResource(id = R.drawable.btn_google_login),
contentDescription = stringResource(R.string.google_logo)
)
Text(text = stringResource(R.string.sign_in_with_google), modifier = Modifier.padding(6.dp))
}
}

@Composable
fun Logo() {
Image(
painter = painterResource(id = R.mipmap.app_logo),
contentDescription = stringResource(R.string.dailyfilm_logo),
modifier = Modifier
.fillMaxWidth()
.height(350.dp),
alignment = Alignment.Center
)

}

private fun autoLogin(content: Activity) {
GoogleSignIn.getLastSignedInAccount(content)?.let {
content.startActivity(Intent(content, CalendarActivity::class.java))
content.finish()
}
}

private fun showSnackBarMessage(context: Activity, message: String) {
Snackbar.make(context.findViewById(android.R.id.content), message, Snackbar.LENGTH_SHORT).show()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,38 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.boostcamp.dailyfilm.data.login.LoginRepository
import com.boostcamp.dailyfilm.data.model.Result
import com.boostcamp.dailyfilm.presentation.util.Event
import com.boostcamp.dailyfilm.presentation.util.UiState
import com.google.firebase.auth.FirebaseUser
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class LoginViewModel @Inject constructor(
private val loginRepository: LoginRepository
) : ViewModel() {

private val _uiState = MutableStateFlow<UiState<FirebaseUser?>>(UiState.Uninitialized)
private val _uiState = MutableStateFlow<Event<UiState<FirebaseUser?>>>(Event(UiState.Uninitialized))
val uiState = _uiState.asStateFlow()

fun requestLogin(idToken: String) {
_uiState.value = UiState.Loading
event(UiState.Loading)
loginRepository.requestLogin(idToken).onEach { result ->
when (result) {
is Result.Success -> {
_uiState.value = UiState.Success(result.data)
event(UiState.Success(result.data))
}
is Result.Error -> {
_uiState.value = UiState.Failure(result.exception)
event(UiState.Failure(result.exception))
}
}
}.launchIn(viewModelScope)
}
private fun event(event: UiState<FirebaseUser?>) {
viewModelScope.launch {
_uiState.emit(Event(event))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.boostcamp.dailyfilm.presentation.selectvideo
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.boostcamp.dailyfilm.data.model.VideoItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.boostcamp.dailyfilm.R
import com.boostcamp.dailyfilm.presentation.login.LoginActivity
import com.boostcamp.dailyfilm.presentation.login.LoginComposeActivity
import com.boostcamp.dailyfilm.presentation.settings.SettingsEvent
import com.boostcamp.dailyfilm.presentation.settings.SettingsViewModel
import com.boostcamp.dailyfilm.presentation.ui.theme.DailyFilmTheme
Expand Down Expand Up @@ -212,7 +213,7 @@ fun navigateToLogin(context: Context) {
context.startActivity(
Intent(
context,
LoginActivity::class.java,
LoginComposeActivity::class.java,
).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
},
Expand Down
Loading

0 comments on commit b1e79fb

Please sign in to comment.