From d2ac0cd13dfc43af962c524d1706c744b3f9949b Mon Sep 17 00:00:00 2001 From: AlinaStepanova Date: Fri, 15 Jan 2021 23:12:34 +0200 Subject: [PATCH] fix bug with query after screen rotation --- .../moviefinder/ui/find_detail/FindDetailFragment.kt | 4 ++-- .../moviefinder/ui/find_detail/FindDetailViewModel.kt | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/avs/moviefinder/ui/find_detail/FindDetailFragment.kt b/app/src/main/java/com/avs/moviefinder/ui/find_detail/FindDetailFragment.kt index e4bc2db..ec81daf 100644 --- a/app/src/main/java/com/avs/moviefinder/ui/find_detail/FindDetailFragment.kt +++ b/app/src/main/java/com/avs/moviefinder/ui/find_detail/FindDetailFragment.kt @@ -12,9 +12,9 @@ import androidx.databinding.DataBindingUtil import androidx.lifecycle.ViewModelProvider import com.avs.moviefinder.R import com.avs.moviefinder.data.dto.Movie +import com.avs.moviefinder.data.network.ErrorType import com.avs.moviefinder.databinding.FragmentFindDetailBinding import com.avs.moviefinder.di.ViewModelFactory -import com.avs.moviefinder.data.network.ErrorType import com.avs.moviefinder.ui.BaseFragment import com.avs.moviefinder.ui.MOVIE_EXTRA_TAG import com.avs.moviefinder.ui.movie.MovieActivity @@ -62,7 +62,7 @@ class FindDetailFragment : BaseFragment() { binding.findDetailViewModel = findDetailViewModel binding.lifecycleOwner = this val query = arguments?.getString(this::class.java.simpleName) - findDetailViewModel.onQuerySubmitted(query) + findDetailViewModel.searchInitialQuery(query) val adapter = BaseMoviesAdapter( MovieListener( { movie -> startMovieActivityForResult(movie) }, diff --git a/app/src/main/java/com/avs/moviefinder/ui/find_detail/FindDetailViewModel.kt b/app/src/main/java/com/avs/moviefinder/ui/find_detail/FindDetailViewModel.kt index cf65410..1fc7892 100644 --- a/app/src/main/java/com/avs/moviefinder/ui/find_detail/FindDetailViewModel.kt +++ b/app/src/main/java/com/avs/moviefinder/ui/find_detail/FindDetailViewModel.kt @@ -44,6 +44,7 @@ class FindDetailViewModel @Inject constructor( get() = _updateMovieIndex private var _moviesDB = MutableLiveData>() private var _query = MutableLiveData() + private var _initialQuery = MutableLiveData() private var rxBusDisposable: Disposable? = null private var apiDisposable: Disposable? = null private val dbDisposable = CompositeDisposable() @@ -124,13 +125,20 @@ class FindDetailViewModel @Inject constructor( } } - fun onQuerySubmitted(query: String?) { + private fun onQuerySubmitted(query: String?) { if (query != null) { dbDisposable.add(databaseManager.getAllMovies()) _query.value = query } } + fun searchInitialQuery(query: String?) { + if (_initialQuery.value == null) { + _initialQuery.value = query + onQuerySubmitted(query) + } + } + fun subscribeToEvents() { unsubscribeFromEvents() rxBusDisposable = rxBus.events.subscribe { event -> handleServerResponse(event) }