-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from intive-FDV/feature/search_tv_show_fix
Feature/search tv show
- Loading branch information
Showing
24 changed files
with
848 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
app/src/main/java/com/intive/tmdbandroid/datasource/TVShowSearchSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.intive.tmdbandroid.datasource | ||
|
||
import androidx.paging.PagingSource | ||
import com.intive.tmdbandroid.entity.ResultTVShowsEntity | ||
import androidx.paging.PagingState | ||
import com.intive.tmdbandroid.datasource.network.Service | ||
import com.intive.tmdbandroid.model.TVShow | ||
import kotlinx.coroutines.flow.collect | ||
|
||
class TVShowSearchSource(private val service: Service, private val query: String) : PagingSource<Int, TVShow>() { | ||
companion object { | ||
const val DEFAULT_PAGE_INDEX = 1 | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, TVShow> { | ||
return try { | ||
val pageNumber = params.key ?: DEFAULT_PAGE_INDEX | ||
lateinit var response: ResultTVShowsEntity | ||
service.getTvShowByTitle(query, pageNumber).collect { response = it } | ||
|
||
val prevKey = if (pageNumber > DEFAULT_PAGE_INDEX) pageNumber - 1 else null | ||
val nextKey = if (response.TVShows.isNotEmpty()) pageNumber + 1 else null | ||
|
||
LoadResult.Page( | ||
data = response.toTVShowList(), | ||
prevKey = prevKey, | ||
nextKey = nextKey | ||
) | ||
} catch (e: Exception) { | ||
LoadResult.Error(e) | ||
} | ||
} | ||
|
||
override fun getRefreshKey(state: PagingState<Int, TVShow>): Int? { | ||
return state.anchorPosition?.let { | ||
state.closestPageToPosition(it)?.prevKey?.plus(1) | ||
?: state.closestPageToPosition(it)?.nextKey?.minus(1) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
app/src/main/java/com/intive/tmdbandroid/search/ui/SearchFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package com.intive.tmdbandroid.search.ui | ||
|
||
import android.content.Context | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.view.inputmethod.InputMethodManager | ||
import android.widget.SearchView | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.navigation.fragment.findNavController | ||
import androidx.navigation.ui.AppBarConfiguration | ||
import androidx.navigation.ui.setupWithNavController | ||
import androidx.paging.PagingData | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import com.intive.tmdbandroid.common.State | ||
import com.intive.tmdbandroid.databinding.FragmentSearchBinding | ||
import com.intive.tmdbandroid.model.TVShow | ||
import com.intive.tmdbandroid.search.ui.adapters.TVShowSearchAdapter | ||
import com.intive.tmdbandroid.search.viewmodel.SearchViewModel | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.flow.collectLatest | ||
|
||
@AndroidEntryPoint | ||
class SearchFragment: Fragment() { | ||
private val viewModel: SearchViewModel by viewModels() | ||
|
||
private val clickListener = { tvShow: TVShow -> | ||
val action = SearchFragmentDirections.actionSearchFragmentToTVShowDetail(tvShow.id) | ||
findNavController().navigate(action) | ||
} | ||
|
||
private val searchAdapter = TVShowSearchAdapter(clickListener) | ||
|
||
private var searchViewQuery: String = "" | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
val binding = FragmentSearchBinding.inflate(inflater, container, false) | ||
binding.layoutProgressbar.progressBar.visibility = View.GONE | ||
setupToolbar(binding) | ||
binding.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { | ||
|
||
override fun onQueryTextChange(newText: String): Boolean { | ||
return false | ||
} | ||
|
||
override fun onQueryTextSubmit(query: String): Boolean { | ||
if (query.isNotEmpty()){ | ||
searchAdapter.query = query | ||
binding.searchView.clearFocus() | ||
viewModel.search(query) | ||
searchViewQuery = query | ||
subscribeViewModel(binding) | ||
return true | ||
} | ||
return false | ||
} | ||
}) | ||
initViews(binding) | ||
if(searchViewQuery.isEmpty()){ | ||
binding.searchView.requestFocus() | ||
val imm = binding.searchView.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager | ||
binding.searchView.postDelayed( { | ||
imm.showSoftInput(binding.searchView, InputMethodManager.SHOW_IMPLICIT) | ||
}, 50) | ||
} | ||
else{ | ||
binding.layoutSearchHint.hintContainer.visibility = View.GONE | ||
} | ||
return binding.root | ||
} | ||
|
||
private fun setupToolbar(binding: FragmentSearchBinding) { | ||
val navController = findNavController() | ||
val appBarConfiguration = AppBarConfiguration(navController.graph) | ||
val toolbar = binding.fragmentSearchToolbar | ||
toolbar.setupWithNavController(navController, appBarConfiguration) | ||
} | ||
|
||
fun subscribeViewModel(binding: FragmentSearchBinding){ | ||
searchAdapter.notifyItemChanged(0) | ||
searchAdapter.differ.addLoadStateListener { loadState -> | ||
if(loadState.append.endOfPaginationReached){ | ||
if (searchAdapter.itemCount < 1 + 1) { | ||
binding.layoutEmpty.root.visibility = View.VISIBLE | ||
} else binding.layoutEmpty.root.visibility = View.GONE | ||
} | ||
} | ||
lifecycleScope.launchWhenStarted { | ||
viewModel.uiState.collectLatest { resultTVShow -> | ||
|
||
when (resultTVShow) { | ||
is State.Success<PagingData<TVShow>> -> { | ||
binding.layoutSearchHint.hintContainer.visibility = View.GONE | ||
binding.layoutProgressbar.progressBar.visibility = View.GONE | ||
searchAdapter.submitData(resultTVShow.data) | ||
} | ||
is State.Error -> { | ||
binding.layoutError.errorContainer.visibility = View.VISIBLE | ||
binding.layoutSearchHint.hintContainer.visibility = View.GONE | ||
binding.layoutProgressbar.progressBar.visibility = View.GONE | ||
} | ||
is State.Loading -> { | ||
binding.layoutSearchHint.hintContainer.visibility = View.GONE | ||
binding.layoutProgressbar.progressBar.visibility = View.VISIBLE | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun initViews(binding: FragmentSearchBinding) { | ||
val resultsList = binding.searchResults | ||
|
||
resultsList.apply { | ||
layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) | ||
adapter = searchAdapter | ||
} | ||
} | ||
} |
Oops, something went wrong.