Skip to content

Commit

Permalink
Merge pull request #176 from DavayPosmotrim/168_2_add_saving_history_…
Browse files Browse the repository at this point in the history
…in_closed_session

168 2 add saving history in closed session
  • Loading branch information
GoetzDeBouville authored Oct 27, 2024
2 parents 84e3805 + d26065a commit 54012bd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import android.widget.FrameLayout
import androidx.lifecycle.lifecycleScope
import com.davay.android.core.domain.models.MovieDetails
import com.davay.android.databinding.FragmentMatchBottomSheetBinding
import com.davay.android.feature.sessionsmatched.presentation.animation.AnimationMatchDialog
import com.davay.android.feature.sessionsmatched.presentation.animation.AnimationMatchDialogImpl
import com.davay.android.feature.match.presentation.animation.AnimationMatchDialog
import com.davay.android.feature.match.presentation.animation.AnimationMatchDialogImpl
import com.davay.android.utils.MovieDetailsHelper
import com.davay.android.utils.MovieDetailsHelperImpl
import com.google.android.material.bottomsheet.BottomSheetBehavior
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.davay.android.feature.sessionsmatched.presentation.animation
package com.davay.android.feature.match.presentation.animation

import android.view.View

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.davay.android.feature.sessionsmatched.presentation.animation
package com.davay.android.feature.match.presentation.animation

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.davay.android.databinding.FragmentSelectMovieBinding
import com.davay.android.di.AppComponentHolder
import com.davay.android.di.ScreenComponent
import com.davay.android.extensions.SwipeDirection
import com.davay.android.feature.coincidences.presentation.CoincidencesFragmentDirections
import com.davay.android.feature.match.presentation.MatchBottomSheetFragment
import com.davay.android.feature.selectmovie.di.DaggerSelectMovieFragmentComponent
import com.davay.android.feature.selectmovie.presentation.adapters.MovieCardAdapter
Expand Down Expand Up @@ -108,7 +107,12 @@ class SelectMovieFragment :
viewLifecycleOwner.lifecycleScope.launch {
viewModel.sessionStatusState.collect { state ->
when (state) {
SessionStatus.CLOSED -> showConfirmDialogAtSessionClosedStatus()
SessionStatus.CLOSED -> {
if (!viewModel.isLeaveSessionPressed) {
showConfirmDialogAtSessionClosedStatus()
}
}

SessionStatus.ROULETTE -> showConfirmDialogAndNavigateToRoulette()
else -> {}
}
Expand Down Expand Up @@ -210,23 +214,8 @@ class SelectMovieFragment :
title = getString(R.string.leave_session_title),
message = getString(R.string.select_movies_leave_session_dialog_message),
yesAction = {
viewModel.leaveSessionAndNavigateToHistory()
}
)
dialog.show(parentFragmentManager, null)
}

/**
* Метод вызывается у юзеров, у которых из сессии вышел участник
* !добавить сохранение сессии в БД тут и в showDialogAndNavigateToHistorySessions()
*/
@Suppress("Detekt.UnusedPrivateMember")
private fun showConfirmDialogAndNavigateToHistorySessions() {
val dialog = MainDialogFragment.newInstance(
title = getString(R.string.leave_session_title),
message = getString(R.string.leave_session_dialog_message_session_complited),
showConfirmBlock = true,
yesAction = {
binding.progressBar.isVisible = true
viewModel.leaveSessionPressed()
viewModel.leaveSessionAndNavigateToHistory()
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.davay.android.feature.selectmovie.domain.LikeMovieInteractor
import com.davay.android.feature.selectmovie.presentation.models.MovieMatchState
import com.davay.android.feature.selectmovie.presentation.models.SelectMovieState
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
Expand Down Expand Up @@ -54,8 +55,11 @@ class SelectMovieViewModel @Inject constructor(

private var totalMovieIds = 0
private var loadedMovies = mutableSetOf<MovieDetails>()
var isLeaveSessionPressed = false
private set

init {
Log.e(TAG, "SelectMovieViewModel init")
initializeMovieList()
subscribeStates()
getMatchesCount()
Expand Down Expand Up @@ -254,28 +258,38 @@ class SelectMovieViewModel @Inject constructor(
}

fun leaveSessionAndNavigateToHistory() {
val action =
SelectMovieFragmentDirections.actionSelectMovieFragmentToMatchedSessionListFragment()
disconnect()
navigate(action)
}

private fun disconnect() {
viewModelScope.launch(Dispatchers.IO) {
val sessionId = commonWebsocketInteractor.sessionId
runSafelyUseCase(
useCaseFlow = leaveSessionUseCase.execute(sessionId),
onSuccess = {},
onFailure = { error ->
if (BuildConfig.DEBUG) {
Log.e(TAG, "Error on leave session $sessionId, error -> $error")
runCatching {
leaveSessionUseCase.execute(sessionId).collect { result ->
when (result) {
is Result.Success -> unsubscribeAndNavigate()
is Result.Error -> unsubscribeAndNavigate()
}
}
)
commonWebsocketInteractor.unsubscribeWebsockets()
}.onFailure {
if (BuildConfig.DEBUG) {
Log.e(TAG, "Error on leave session ${it.localizedMessage}")
}
val action =
SelectMovieFragmentDirections.actionSelectMovieFragmentToMatchedSessionListFragment()
navigate(action)
}
}
}

private suspend fun unsubscribeAndNavigate() {
val action =
SelectMovieFragmentDirections.actionSelectMovieFragmentToMatchedSessionListFragment()
delay(DELAY_300MS)
commonWebsocketInteractor.unsubscribeWebsockets()
navigate(action)
}

fun leaveSessionPressed() {
isLeaveSessionPressed = true
}

private companion object {
/**
* Размер подгрузки фильмов, при изменении так же учитывать значение в SelectMovieRepositoryImpl.
Expand All @@ -284,6 +298,7 @@ class SelectMovieViewModel @Inject constructor(
*/
const val PRELOAD_SIZE = 5
val TAG: String = SelectMovieViewModel::class.java.simpleName
private const val DELAY_300MS = 300L
}

}

0 comments on commit 54012bd

Please sign in to comment.