Skip to content

Commit

Permalink
feat: 북마크에서 로그인이 필요하면 로그인 화면을 띄운다
Browse files Browse the repository at this point in the history
  • Loading branch information
SeongHoonC committed May 2, 2024
1 parent 1c12ad6 commit 89d7eed
Show file tree
Hide file tree
Showing 20 changed files with 199 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package com.festago.festago.presentation.ui.home.bookmarklist.artistbookmark

sealed interface ArtistBookmarkEvent {
class ShowArtistDetail(val artist: ArtistBookmarkUiState) : ArtistBookmarkEvent
object ShowSignIn : ArtistBookmarkEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.navigation.fragment.findNavController
import com.festago.festago.presentation.databinding.FragmentArtistBookmarkBinding
import com.festago.festago.presentation.ui.artistdetail.ArtistDetailArgs
import com.festago.festago.presentation.ui.home.bookmarklist.BookmarkListFragmentDirections
import com.festago.festago.presentation.ui.signin.SignInActivity
import com.festago.festago.presentation.util.repeatOnStarted
import com.festago.festago.presentation.util.safeNavigate
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -44,31 +45,29 @@ class ArtistBookmarkFragment : Fragment() {
binding.uiState = vm.uiState.value

binding.refreshListener = { vm.fetchBookmarkList() }
binding.loginListener = { vm.logIn() }

artistBookmarkAdapter = ArtistBookmarkAdapter()
binding.rvArtistBookmarkList.adapter = artistBookmarkAdapter
}

private fun initObserve() {
repeatOnStarted(this) {
repeatOnStarted(viewLifecycleOwner) {
vm.uiState.collect { uiState ->
binding.uiState = uiState
when (uiState) {
is ArtistBookmarkListUiState.Loading -> {
// Handle loading
}
is ArtistBookmarkListUiState.NotLoggedIn,
is ArtistBookmarkListUiState.Loading,
is ArtistBookmarkListUiState.Error,
-> Unit

is ArtistBookmarkListUiState.Success -> {
artistBookmarkAdapter.submitList(uiState.artistBookmarks)
}

is ArtistBookmarkListUiState.Error -> {
// Handle error
}
}
}
}
repeatOnStarted(this) {
repeatOnStarted(viewLifecycleOwner) {
vm.uiEvent.collect { event ->
when (event) {
is ArtistBookmarkEvent.ShowArtistDetail -> {
Expand All @@ -80,6 +79,10 @@ class ArtistBookmarkFragment : Fragment() {
),
)
}

is ArtistBookmarkEvent.ShowSignIn -> {
startActivity(SignInActivity.getIntent(requireContext()))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.festago.festago.presentation.ui.home.bookmarklist.artistbookmark

interface ArtistBookmarkListUiState {
object NotLoggedIn : ArtistBookmarkListUiState
sealed interface ArtistBookmarkListUiState {
class NotLoggedIn(val logIn: () -> Unit) : ArtistBookmarkListUiState

object Loading : ArtistBookmarkListUiState

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.festago.festago.domain.model.bookmark.ArtistBookmark
import com.festago.festago.domain.repository.BookmarkRepository
import com.festago.festago.domain.repository.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -16,6 +17,7 @@ import javax.inject.Inject
@HiltViewModel
class ArtistBookmarkViewModel @Inject constructor(
private val bookmarkRepository: BookmarkRepository,
private val userRepository: UserRepository,
) : ViewModel() {
private val _uiState =
MutableStateFlow<ArtistBookmarkListUiState>(ArtistBookmarkListUiState.Loading)
Expand All @@ -27,6 +29,12 @@ class ArtistBookmarkViewModel @Inject constructor(
fun fetchBookmarkList() {
viewModelScope.launch {
_uiState.value = ArtistBookmarkListUiState.Loading

if (!userRepository.isSigned()) {
_uiState.value = ArtistBookmarkListUiState.NotLoggedIn(::logIn)
return@launch
}

bookmarkRepository.getArtistBookmarks().onSuccess { artistBookmarks ->
_uiState.value = ArtistBookmarkListUiState.Success(
artistBookmarks.map { it.toUiState() },
Expand All @@ -37,6 +45,12 @@ class ArtistBookmarkViewModel @Inject constructor(
}
}

fun logIn() {
viewModelScope.launch {
_uiEvent.emit(ArtistBookmarkEvent.ShowSignIn)
}
}

private fun ArtistBookmark.toUiState(): ArtistBookmarkUiState {
return ArtistBookmarkUiState(
id = artist.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import com.festago.festago.presentation.ui.home.bookmarklist.festivalbookmark.ui
sealed interface FestivalBookmarkEvent {
class ShowFestivalDetail(val festival: FestivalBookmarkItemUiState) : FestivalBookmarkEvent
class ShowArtistDetail(val artist: ArtistUiState) : FestivalBookmarkEvent
object ShowSignIn : FestivalBookmarkEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.festago.festago.presentation.ui.festivaldetail.FestivalDetailArgs
import com.festago.festago.presentation.ui.home.bookmarklist.BookmarkListFragmentDirections
import com.festago.festago.presentation.ui.home.bookmarklist.festivalbookmark.adapater.FestivalBookmarkViewAdapter
import com.festago.festago.presentation.ui.home.bookmarklist.festivalbookmark.uistate.FestivalBookmarkUiState
import com.festago.festago.presentation.ui.signin.SignInActivity
import com.festago.festago.presentation.util.repeatOnStarted
import com.festago.festago.presentation.util.safeNavigate
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -50,13 +51,15 @@ class FestivalBookmarkFragment : Fragment() {
binding.uiState = vm.uiState.value

binding.refreshListener = { vm.fetchBookmarkList() }
binding.loginListener = { vm.logIn() }
}

private fun initObserve() {
repeatOnStarted(this) {
repeatOnStarted(viewLifecycleOwner) {
vm.uiState.collect { uiState ->
binding.uiState = uiState
when (uiState) {
is FestivalBookmarkUiState.NotLoggedIn,
is FestivalBookmarkUiState.Loading,
is FestivalBookmarkUiState.Error,
-> Unit
Expand All @@ -67,8 +70,8 @@ class FestivalBookmarkFragment : Fragment() {
}
}

repeatOnStarted(this) {
vm.uiEvent.collect { event ->
repeatOnStarted(viewLifecycleOwner) {
vm.event.collect { event ->
when (event) {
is FestivalBookmarkEvent.ShowFestivalDetail -> {
findNavController().safeNavigate(
Expand All @@ -85,6 +88,10 @@ class FestivalBookmarkFragment : Fragment() {
),
)
}

is FestivalBookmarkEvent.ShowSignIn -> {
startActivity(SignInActivity.getIntent(requireContext()))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.festago.festago.domain.model.artist.Artist
import com.festago.festago.domain.model.bookmark.FestivalBookmark
import com.festago.festago.domain.model.bookmark.FestivalBookmarkOrder
import com.festago.festago.domain.repository.BookmarkRepository
import com.festago.festago.domain.repository.UserRepository
import com.festago.festago.presentation.ui.home.bookmarklist.festivalbookmark.artistadapter.ArtistUiState
import com.festago.festago.presentation.ui.home.bookmarklist.festivalbookmark.uistate.FestivalBookmarkItemUiState
import com.festago.festago.presentation.ui.home.bookmarklist.festivalbookmark.uistate.FestivalBookmarkUiState
Expand All @@ -21,19 +22,29 @@ import javax.inject.Inject
@HiltViewModel
class FestivalBookmarkViewModel @Inject constructor(
private val bookmarkRepository: BookmarkRepository,
private val userRepository: UserRepository,
) : ViewModel() {
private val _uiState =
MutableStateFlow<FestivalBookmarkUiState>(FestivalBookmarkUiState.Loading)
val uiState: StateFlow<FestivalBookmarkUiState> = _uiState.asStateFlow()

private val _uiEvent = MutableSharedFlow<FestivalBookmarkEvent>()
val uiEvent = _uiEvent.asSharedFlow()
private val _event = MutableSharedFlow<FestivalBookmarkEvent>()
val event = _event.asSharedFlow()

fun fetchBookmarkList() {
viewModelScope.launch {
_uiState.value = FestivalBookmarkUiState.Loading

if (!userRepository.isSigned()) {
_uiState.value = FestivalBookmarkUiState.NotLoggedIn
return@launch
}

val bookmarkIds = bookmarkRepository.getFestivalBookmarkIds()
.getOrElse { _uiState.value = FestivalBookmarkUiState.Error; return@launch }
.getOrElse {
_uiState.value = FestivalBookmarkUiState.Error
return@launch
}

if (bookmarkIds.isEmpty()) {
_uiState.value = FestivalBookmarkUiState.Success(emptyList())
Expand All @@ -50,6 +61,12 @@ class FestivalBookmarkViewModel @Inject constructor(
}
}

fun logIn() {
viewModelScope.launch {
_event.emit(FestivalBookmarkEvent.ShowSignIn)
}
}

private fun FestivalBookmark.toUiState(): FestivalBookmarkItemUiState {
return FestivalBookmarkItemUiState(
id = festival.id,
Expand All @@ -60,7 +77,7 @@ class FestivalBookmarkViewModel @Inject constructor(
artists = festival.artists.map { it.toUiState() },
onFestivalDetail = { festival ->
viewModelScope.launch {
_uiEvent.emit(FestivalBookmarkEvent.ShowFestivalDetail(festival))
_event.emit(FestivalBookmarkEvent.ShowFestivalDetail(festival))
}
},
)
Expand All @@ -73,7 +90,7 @@ class FestivalBookmarkViewModel @Inject constructor(
imageUrl = imageUrl,
onArtistDetail = { artist ->
viewModelScope.launch {
_uiEvent.emit(FestivalBookmarkEvent.ShowArtistDetail(artist))
_event.emit(FestivalBookmarkEvent.ShowArtistDetail(artist))
}
},
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.festago.festago.presentation.ui.home.bookmarklist.festivalbookmark.uistate

interface FestivalBookmarkUiState {
sealed interface FestivalBookmarkUiState {
object NotLoggedIn : FestivalBookmarkUiState
object Loading : FestivalBookmarkUiState

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package com.festago.festago.presentation.ui.home.bookmarklist.schoolbookmark

sealed interface SchoolBookmarkEvent {
class ShowSchoolDetail(val school: SchoolBookmarkUiState) : SchoolBookmarkEvent
object ShowSignIn : SchoolBookmarkEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.navigation.fragment.findNavController
import com.festago.festago.presentation.databinding.FragmentSchoolBookmarkBinding
import com.festago.festago.presentation.ui.home.bookmarklist.BookmarkListFragmentDirections
import com.festago.festago.presentation.ui.schooldetail.SchoolDetailArgs
import com.festago.festago.presentation.ui.signin.SignInActivity
import com.festago.festago.presentation.util.repeatOnStarted
import com.festago.festago.presentation.util.safeNavigate
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -44,31 +45,28 @@ class SchoolBookmarkFragment : Fragment() {
binding.uiState = vm.uiState.value

binding.refreshListener = { vm.fetchBookmarkList() }

binding.loginListener = { vm.logIn() }
schoolBookmarkAdapter = SchoolBookmarkAdapter()
binding.rvSchoolBookmarkList.adapter = schoolBookmarkAdapter
}

private fun initObserve() {
repeatOnStarted(this) {
repeatOnStarted(viewLifecycleOwner) {
vm.uiState.collect { uiState ->
binding.uiState = uiState
when (uiState) {
is SchoolBookmarkListUiState.Loading -> {
// Handle loading
}
is SchoolBookmarkListUiState.Loading,
is SchoolBookmarkListUiState.Error,
is SchoolBookmarkListUiState.NotLoggedIn,
-> Unit

is SchoolBookmarkListUiState.Success -> {
schoolBookmarkAdapter.submitList(uiState.schoolBookmarks)
}

is SchoolBookmarkListUiState.Error -> {
// Handle error
}
}
}
}
repeatOnStarted(this) {
repeatOnStarted(viewLifecycleOwner) {
vm.uiEvent.collect { event ->
when (event) {
is SchoolBookmarkEvent.ShowSchoolDetail -> {
Expand All @@ -78,6 +76,10 @@ class SchoolBookmarkFragment : Fragment() {
),
)
}

is SchoolBookmarkEvent.ShowSignIn -> {
startActivity(SignInActivity.getIntent(requireContext()))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.festago.festago.presentation.ui.home.bookmarklist.schoolbookmark

interface SchoolBookmarkListUiState {
sealed interface SchoolBookmarkListUiState {
object NotLoggedIn : SchoolBookmarkListUiState
object Loading : SchoolBookmarkListUiState

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.festago.festago.domain.model.bookmark.SchoolBookmark
import com.festago.festago.domain.repository.BookmarkRepository
import com.festago.festago.domain.repository.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -16,6 +17,7 @@ import javax.inject.Inject
@HiltViewModel
class SchoolBookmarkViewModel @Inject constructor(
private val bookmarkRepository: BookmarkRepository,
private val userRepository: UserRepository,
) : ViewModel() {
private val _uiState =
MutableStateFlow<SchoolBookmarkListUiState>(SchoolBookmarkListUiState.Loading)
Expand All @@ -27,6 +29,10 @@ class SchoolBookmarkViewModel @Inject constructor(
fun fetchBookmarkList() {
viewModelScope.launch {
_uiState.value = SchoolBookmarkListUiState.Loading
if (!userRepository.isSigned()) {
_uiState.value = SchoolBookmarkListUiState.NotLoggedIn
return@launch
}
bookmarkRepository.getSchoolBookmarks().onSuccess { schoolBookmarks ->
_uiState.value = SchoolBookmarkListUiState.Success(
schoolBookmarks.map { it.toUiState() },
Expand All @@ -37,6 +43,12 @@ class SchoolBookmarkViewModel @Inject constructor(
}
}

fun logIn() {
viewModelScope.launch {
_uiEvent.emit(SchoolBookmarkEvent.ShowSignIn)
}
}

private fun SchoolBookmark.toUiState(): SchoolBookmarkUiState {
return SchoolBookmarkUiState(
id = school.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class SignInActivity : AppCompatActivity() {
}

private fun navigateToHome() {
finishAffinity()
startActivity(HomeActivity.getIntent(this))
finish()
}

private fun handleSignInFailure() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
visibility="@{uiState.shouldShowEmpty}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fragmentl_bookmark_artist_bookmark"
android:text="@string/bookmark_list_tv_artist_bookmark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
Loading

0 comments on commit 89d7eed

Please sign in to comment.