Skip to content

Commit

Permalink
[feature] 회원, 비회원 기능 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
yuni-ju committed Jan 10, 2025
1 parent ce24415 commit 6228244
Show file tree
Hide file tree
Showing 19 changed files with 213 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,29 @@ class CreatePickViewModel @Inject constructor(
val musicVideo = fetchMusicVideoUseCase(song)

/* 등록 결과 - pick ID 담긴 Result */
val user = getCurrentUserUseCase()
val createResult = createPickUseCase(
Pick(
id = "",
song = song,
comment = _comment.value,
createdAt = "",
createdBy = Creator(
userId = user.userId,
userName = user.userName
),
location = LocationPoint(lastLocation!!.latitude, lastLocation!!.longitude),
musicVideoUrl = musicVideo?.previewUrl ?: "",
musicVideoThumbnailUrl = musicVideo?.thumbnailUrl ?: ""
getCurrentUserUseCase()?.let { user ->
val createResult = createPickUseCase(
Pick(
id = "",
song = song,
comment = _comment.value,
createdAt = "",
createdBy = Creator(
userId = user.userId,
userName = user.userName
),
location = LocationPoint(lastLocation!!.latitude, lastLocation!!.longitude),
musicVideoUrl = musicVideo?.previewUrl ?: "",
musicVideoThumbnailUrl = musicVideo?.thumbnailUrl ?: ""
)
)
)

createResult.onSuccess { pickId ->
_createPickUiState.emit(CreateUiState.Success(pickId))
}.onFailure {
/* TODO: Firestore 등록 실패처리 */
_createPickUiState.emit(CreateUiState.Error)
Log.d("CreatePickViewModel", createResult.exceptionOrNull()?.message.toString())
createResult.onSuccess { pickId ->
_createPickUiState.emit(CreateUiState.Success(pickId))
}.onFailure {
/* TODO: Firestore 등록 실패처리 */
_createPickUiState.emit(CreateUiState.Error)
Log.d("CreatePickViewModel", createResult.exceptionOrNull()?.message.toString())
}
}
}
}
Expand Down
59 changes: 25 additions & 34 deletions app/src/main/java/com/squirtles/musicroad/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ import com.squirtles.musicroad.main.navigations.MainNavGraph
import com.squirtles.musicroad.main.navigations.MainNavigationActions
import com.squirtles.musicroad.ui.theme.MusicRoadTheme
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeout

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
Expand Down Expand Up @@ -93,40 +91,33 @@ class MainActivity : AppCompatActivity() {
private fun setKeepOnScreenCondition(splashScreen: SplashScreen) {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
try {
withTimeout(30_000L) {
mainViewModel.loadingState.collect { state ->
when (state) {
is LoadingState.Loading -> {
splashScreen.setKeepOnScreenCondition { true }
}

is LoadingState.Success -> {
Log.d("MainActivity", "Success: ${state.userId}")
splashScreen.setKeepOnScreenCondition { false }
cancel()
}

is LoadingState.NetworkError -> {
showToast(getString(R.string.main_network_error_message))
finish()
}

is LoadingState.UserNotFoundError -> {
showToast(getString(R.string.main_user_not_found_message))
finish()
}

is LoadingState.CreatedUserError -> {
showToast(getString(R.string.main_create_user_fail_message))
finish()
}
}
mainViewModel.loadingState.collect { state ->
when (state) {
is LoadingState.Loading -> {
splashScreen.setKeepOnScreenCondition { true }
}

is LoadingState.Success -> {
Log.d("MainActivity", "Success: ${state.userId}")
splashScreen.setKeepOnScreenCondition { false }
cancel()
}

is LoadingState.NetworkError -> {
showToast(getString(R.string.main_network_error_message))
finish()
}

is LoadingState.UserNotFoundError -> {
showToast(getString(R.string.main_user_not_found_message))
finish()
}

is LoadingState.CreatedUserError -> {
showToast(getString(R.string.main_create_user_fail_message))
finish()
}
}
} catch (e: TimeoutCancellationException) {
showToast(getString(R.string.main_network_error_message))
finish()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import javax.inject.Inject

sealed class LoadingState {
data object Loading : LoadingState()
data class Success(val userId: String) : LoadingState()
data class Success(val userId: String?) : LoadingState()
data class NetworkError(val error: String) : LoadingState()
data class CreatedUserError(val error: String) : LoadingState()
data class UserNotFoundError(val error: String) : LoadingState()
Expand All @@ -38,9 +38,10 @@ class MainViewModel @Inject constructor(
init {
viewModelScope.launch {
_localUserId.collect { localUid ->
if (localUid == null) {
createUser()
if (localUid == null) { // 비회원 상태
_loadingState.emit(LoadingState.Success(null))
} else {
// TODO 구글 로그인 -> 자동 로그인
fetchUser(localUid)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object ProfileDestination {
const val SETTING_PROFILE_ROUTE = "setting/profile"
const val SETTING_NOTIFICATION_ROUTE = "setting/notification"

fun profile(userId: String) = "$PROFILE_ROUTE/$userId"
fun profile(userId: String?) = "$PROFILE_ROUTE/$userId"
fun favoritePicks(userId: String) = "$FAVORITE_PICKS_ROUTE/$userId"
fun myPicks(userId: String) = "$MY_PICKS_ROUTE/$userId"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,13 @@ fun MainNavGraph(

composable(
route = ProfileDestination.profile("{userId}"),
arguments = listOf(navArgument("userId") { type = NavType.StringType })
arguments = listOf(navArgument("userId") {
type = NavType.StringType
nullable = true
defaultValue = null
})
) { backStackEntry ->
val userId = backStackEntry.arguments?.getString("userId") ?: ""

val userId = backStackEntry.arguments?.getString("userId")
ProfileScreen(
userId = userId,
onBackClick = { navController.navigateUp() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MainNavigationActions(navController: NavHostController) {
}
}

val navigateToProfile: (String) -> Unit = { userId ->
val navigateToProfile: (String?) -> Unit = { userId ->
navController.navigate(ProfileDestination.profile(userId)) {
launchSingleTop = true
}
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/java/com/squirtles/musicroad/map/MapScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fun MapScreen(
playerServiceViewModel: PlayerServiceViewModel,
onFavoriteClick: (String) -> Unit,
onCenterClick: () -> Unit,
onUserInfoClick: (String) -> Unit,
onUserInfoClick: (String?) -> Unit,
onPickSummaryClick: (String) -> Unit,
) {
val nearPicks by mapViewModel.nearPicks.collectAsStateWithLifecycle()
Expand Down Expand Up @@ -127,11 +127,17 @@ fun MapScreen(
modifier = Modifier.padding(bottom = 16.dp),
lastLocation = lastLocation,
onFavoriteClick = {
onFavoriteClick(mapViewModel.getUserId())
mapViewModel.getUserId()?.let { userId ->
onFavoriteClick(userId)
}
// TODO 로그인 안내 메시지
},
onCenterClick = {
onCenterClick()
mapViewModel.saveCurLocationForced()
mapViewModel.getUserId()?.let {
onCenterClick()
mapViewModel.saveCurLocationForced()
}
// TODO 로그인 안내 메시지
},
onUserInfoClick = {
onUserInfoClick(mapViewModel.getUserId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MapViewModel @Inject constructor(
// Firestore 데이터 쿼리 작업 최소화 및 위치데이터 공유 용도
val lastLocation: StateFlow<Location?> = fetchLastLocationUseCase()

fun getUserId() = getCurrentUserUseCase().userId
fun getUserId() = getCurrentUserUseCase()?.userId

fun setLastCameraPosition(cameraPosition: CameraPosition) {
_lastCameraPosition = cameraPosition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fun ClusterBottomSheet(
onDismissRequest: () -> Unit,
modifier: Modifier = Modifier,
clusterPickList: List<Pick>?,
userId: String,
userId: String?,
calculateDistance: (Double, Double) -> String,
onClickItem: (String) -> Unit
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import com.squirtles.musicroad.ui.theme.MusicRoadTheme
@Composable
fun InfoWindow(
pick: Pick,
userId: String,
userId: String?,
navigateToPick: (String) -> Unit,
calculateDistance: (Double, Double) -> String
) {
Expand Down
35 changes: 20 additions & 15 deletions app/src/main/java/com/squirtles/musicroad/pick/DetailPickScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ fun DetailPickScreen(
DETAIL_PICK_TAB -> {
DetailPick(
pick = pick,
isCreatedBySelf = isCreatedBySelf,
currentUserId = pickViewModel.getUserId(),
isFavorite = isFavorite,
userId = pick.createdBy.userId,
userName = pick.createdBy.userName,
pickUserId = pick.createdBy.userId,
pickUserName = pick.createdBy.userName,
favoriteCount = favoriteCount,
isMusicVideoAvailable = isMusicVideoAvailable,
onProfileClick = onProfileClick,
Expand Down Expand Up @@ -257,10 +257,10 @@ fun DetailPickScreen(
// Show default pick
DetailPick(
pick = DEFAULT_PICK,
isCreatedBySelf = false,
currentUserId = null,
isFavorite = false,
userId = "",
userName = "",
pickUserId = "",
pickUserName = "",
favoriteCount = 0,
isMusicVideoAvailable = false,
playerServiceViewModel = playerServiceViewModel,
Expand Down Expand Up @@ -303,17 +303,18 @@ fun DetailPickScreen(
@Composable
private fun DetailPick(
pick: Pick,
isCreatedBySelf: Boolean,
isFavorite: Boolean,
userId: String,
userName: String,
currentUserId: String?,
pickUserId: String,
pickUserName: String,
favoriteCount: Int,
isMusicVideoAvailable: Boolean,
playerServiceViewModel: PlayerServiceViewModel,
onProfileClick: (String) -> Unit,
onBackClick: () -> Unit,
onActionClick: () -> Unit
) {
val isCreatedBySelf = remember { currentUserId == pickUserId }
val scrollState = rememberScrollState()
val dynamicBackgroundColor = Color(pick.song.bgColor)
val onDynamicBackgroundColor = if (dynamicBackgroundColor.luminance() >= 0.5f) Black else White
Expand Down Expand Up @@ -347,14 +348,18 @@ private fun DetailPick(
modifier = Modifier.statusBarsPadding(),
isCreatedBySelf = isCreatedBySelf,
isFavorite = isFavorite,
userId = userId,
userName = userName,
userId = pickUserId,
userName = pickUserName,
onDynamicBackgroundColor = onDynamicBackgroundColor,
onProfileClick = onProfileClick,
onBackClick = {
onBackClick()
},
onActionClick = { onActionClick() }
onActionClick = {
if (currentUserId != null) {
onActionClick()
}
}
)
}
) { innerPadding ->
Expand Down Expand Up @@ -461,10 +466,10 @@ fun Context.showShortToast(message: String) {
private fun DetailPickPreview() {
DetailPick(
pick = DEFAULT_PICK,
isCreatedBySelf = false,
currentUserId = null,
isFavorite = false,
userId = "",
userName = "짱구",
pickUserId = "",
pickUserName = "짱구",
favoriteCount = 0,
isMusicVideoAvailable = true,
onProfileClick = {},
Expand Down
Loading

0 comments on commit 6228244

Please sign in to comment.