Skip to content

Commit

Permalink
migration : 단일 Event 소비를 위한 Event래핑 추가, Loading 프로그래스 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
KimMooHyeon committed Jun 5, 2023
1 parent 9ceffe4 commit 6bf9b13
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 82 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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
Expand All @@ -10,42 +12,48 @@ 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.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.boostcamp.dailyfilm.R
import com.boostcamp.dailyfilm.presentation.calendar.CalendarActivity
import com.boostcamp.dailyfilm.presentation.util.LottieDialogFragment
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
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

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

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -64,93 +72,104 @@ class LoginComposeActivity : ComponentActivity() {
val account = task.getResult(ApiException::class.java)
viewModel.requestLogin(account.idToken!!)
} catch (e: ApiException) {
showSnackBarMessage("Failed Google Login")
showSnackBarMessage(this, getString(R.string.failed_google_login))
}
}
}
setContent {
LoginView(client)
LoginUI(viewModel, client, activityResultLauncher)
}
setObserveLoginResult()
}
}

@Composable
private fun LoginView(client: GoogleSignInClient) {
val backgroundColor = if (isSystemInDarkTheme()) {
Color(0xFF4285F4)
} else {
Color(0xFFFFFFFF)
}
val contentsColor = if (isSystemInDarkTheme()) {
Color(0xFFFFFFFF)
} else {
Color(0xFF454647)
@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)
}
Column {
Logo()
LoginButton(activityResultLauncher, client, backgroundColor, contentsColor)

is UiState.Loading -> {
isLoading = true
}
}

override fun onStart() {
super.onStart()
GoogleSignIn.getLastSignedInAccount(this)?.let {
startActivity(Intent(this, CalendarActivity::class.java))
finish()
is UiState.Success -> {
isLoading = false
activity.startActivity(
Intent(
LocalContext.current,
CalendarActivity::class.java
)
)
activity.finish()
}
}

private fun setObserveLoginResult() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collectLatest { state ->
when (state) {
is UiState.Uninitialized -> {
return@collectLatest
}

is UiState.Success -> {
loadingDialogFragment.hideProgressDialog()
startActivity(
Intent(
this@LoginComposeActivity,
CalendarActivity::class.java
)
)
}

is UiState.Loading -> {
// loadingDialogFragment.showProgressDialog(supportFragmentManager)
}

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

private fun showSnackBarMessage(message: String) {
Snackbar.make(findViewById(android.R.id.content), message, Snackbar.LENGTH_SHORT).show()
else -> {}
}

}


@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 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
Expand All @@ -160,25 +179,50 @@ fun LoginButton(
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, Color(0xC1494747)),
elevation = ButtonDefaults.buttonElevation()
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

0 comments on commit 6bf9b13

Please sign in to comment.