Skip to content
This repository has been archived by the owner on May 24, 2019. It is now read-only.

Commit

Permalink
Merge pull request #14 from isabelcosta/#5-screen-most-popular-shows
Browse files Browse the repository at this point in the history
[PR] [feature #5] Screen listing most popular shows
  • Loading branch information
isabelcosta authored Nov 16, 2017
2 parents d14105d + 2008cc1 commit 725333a
Show file tree
Hide file tree
Showing 28 changed files with 497 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.isabelcosta.moviesapp.adapters

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.isabelcosta.moviesapp.R
import com.example.isabelcosta.moviesapp.data.models.MostPopularShowsListItemResponseData
import com.example.isabelcosta.moviesapp.ui.activities.BaseActivity
import com.example.isabelcosta.moviesapp.utils.getFullImageUrl
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.item_most_popular_shows.view.*

class MostPopularShowsAdapter(
private val context: BaseActivity,
private val showsList: List<MostPopularShowsListItemResponseData>
) : RecyclerView.Adapter<MostPopularShowsAdapter.MostPopularShowsViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MostPopularShowsViewHolder {
val v = LayoutInflater.from(context).inflate(R.layout.item_most_popular_shows, parent, false)
return MostPopularShowsViewHolder(v)
}

override fun onBindViewHolder(holder: MostPopularShowsViewHolder?, position: Int) {
val item = showsList[position]
val itemView = holder?.itemView ?: return

itemView.mostPopularShowsItemTitle.text = item.originalName
itemView.mostPopularShowsItemRating.text = item.voteAverage.toString()
val imageFullPath = getFullImageUrl(item.posterPath)
Picasso.with(context).load(imageFullPath).into(itemView.mostPopularShowsItemPosterImage)

// itemView.setOnClickListener()
}

override fun getItemCount(): Int {
return showsList.size
}

class MostPopularShowsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ class NowPlayingMoviesAdapter(
val item = moviesList[position]
val itemView = holder?.itemView ?: return

// itemView.nowPlayingMoviesItemTitle.text = item.movieTitle
// itemView.nowPlayingMoviesItemDescription.text = item.overview
itemView.nowPlayingMoviesItemTitle.text = item.movieTitle
itemView.nowPlayingMoviesItemDescription.text = item.overview
val imageFullPath = getFullImageUrl(item.posterPath)
Picasso.with(context).load(imageFullPath).into(itemView.nowPlayingMoviesItemPosterImage)

itemView.setOnClickListener { openDetailFunction(item.id) }
itemView.setOnClickListener { openDetailFunction(item.movieId) }
}

override fun getItemCount(): Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.isabelcosta.moviesapp.data.models

import com.google.gson.annotations.SerializedName

data class MostPopularShowsListResponseData(
@SerializedName("page") val page: Int, //1
@SerializedName("total_results") val totalResults: Int, //20022
@SerializedName("total_pages") val totalPages: Int, //1002
@SerializedName("results") val results: List<MostPopularShowsListItemResponseData>
)

data class MostPopularShowsListItemResponseData(
@SerializedName("original_name") val originalName: String, //The Walking Dead
@SerializedName("genre_ids") val genreIds: List<Int>,
@SerializedName("name") val name: String, //The Walking Dead
@SerializedName("popularity") val popularity: Double, //232.631301
@SerializedName("origin_country") val originCountry: List<String>,
@SerializedName("vote_count") val voteCount: Int, //2608
@SerializedName("first_air_date") val firstAirDate: String, //2010-10-31
@SerializedName("backdrop_path") val backdropPath: String, ///xVzvD5BPAU4HpleFSo8QOdHkndo.jpg
@SerializedName("original_language") val originalLanguage: String, //en
@SerializedName("movieId") val showId: Int, //1402
@SerializedName("vote_average") val voteAverage: Double, //7.4
@SerializedName("overview") val overview: String, //Sheriff's deputy Rick Grimes awakens from a coma to find a post-apocalyptic world dominated by flesh-eating zombies. He sets out to find his family and encounters many other survivors along the way.
@SerializedName("poster_path") val posterPath: String ///vxuoMW6YBt6UsxvMfRNwRl9LtWS.jpg
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ data class MovieDetailResponseData(
@SerializedName("budget") val budget: Int, //35000000
@SerializedName("genres") val genres: List<GenreResponseData>,
@SerializedName("homepage") val homepage: String, //http://itthemovie.com/
@SerializedName("id") val id: Int, //346364
@SerializedName("id") val movieId: Int, //346364
@SerializedName("imdb_id") val imdbId: String, //tt1396484
@SerializedName("original_language") val originalLanguage: String, //en
@SerializedName("original_title") val originalTitle: String, //It
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data class NowPlayingListDatesResponseData(

data class NowPlayingListItemResponseData(
@SerializedName("vote_count") val voteCount: Int,
@SerializedName("id") val id: Int,
@SerializedName("id") val movieId: Int,
@SerializedName("video") val video: Boolean,
@SerializedName("vote_average") val voteAverage: Float,
@SerializedName("title") val movieTitle: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.isabelcosta.moviesapp.data.source

import com.example.isabelcosta.moviesapp.data.models.MostPopularShowsListResponseData

interface ITvDataSource {

interface GetMostPopularShows {
fun onSuccessGetMostPopularShows(tvShows: MostPopularShowsListResponseData)
fun onFailGetMostPopularShows()
}

interface GetTvShowDetail {
fun onSuccessGetTvShowDetail()
fun onFailGetTvShowDetail()
}

fun getMostPopularShows(presenter: ITvDataSource.GetMostPopularShows)
fun getTvShowDetail(presenter: ITvDataSource.GetTvShowDetail, tvShowId: Int)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.isabelcosta.moviesapp.data.source.remote

import com.example.isabelcosta.moviesapp.data.models.MostPopularShowsListResponseData
import com.example.isabelcosta.moviesapp.data.remote.RemoteDataSource
import com.example.isabelcosta.moviesapp.data.source.ITvDataSource
import com.example.isabelcosta.moviesapp.data.source.remote.service.TvAPIService
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class TvRemoteDataSource : RemoteDataSource(), ITvDataSource {

private val service: TvAPIService = retrofit.create(TvAPIService::class.java)

override fun getMostPopularShows(presenter: ITvDataSource.GetMostPopularShows) {

val call = service.getPopularTvShows(apiKey)

call.enqueue(object : Callback<MostPopularShowsListResponseData> {
override fun onResponse(call: Call<MostPopularShowsListResponseData>?, response: Response<MostPopularShowsListResponseData>?) {

val responseBody = response?.body()
if (responseBody != null) {
presenter.onSuccessGetMostPopularShows(responseBody)
} else {
presenter.onFailGetMostPopularShows()
}
}

override fun onFailure(call: Call<MostPopularShowsListResponseData>?, t: Throwable?) {
presenter.onFailGetMostPopularShows()
}
})
}

override fun getTvShowDetail(presenter: ITvDataSource.GetTvShowDetail, tvShowId: Int) {
presenter.onSuccessGetTvShowDetail()
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package com.example.isabelcosta.moviesapp.data.source.remote.service

import com.example.isabelcosta.moviesapp.data.models.MostPopularShowsListResponseData
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query

interface TvAPIService {

companion object {

// Endpoint parts
const val TV = "tv"
const val POPULAR = "popular"

// Path parameters
const val TV_ID = "{tv_id}"
const val TV_ID = "tv_id"

// Query string
const val API_KEY = "api_key"
}

@GET("$TV/$POPULAR")
fun getPopularTvShows(@Query(API_KEY) apiKey: String): Call<MostPopularShowsListResponseData>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.isabelcosta.moviesapp.presenters

import com.example.isabelcosta.moviesapp.data.models.MostPopularShowsListResponseData
import com.example.isabelcosta.moviesapp.data.source.ITvDataSource
import com.example.isabelcosta.moviesapp.data.source.remote.TvRemoteDataSource
import com.example.isabelcosta.moviesapp.ui.callbacks.IMostPopularShowsUiCallback

class MostPopularShowsPresenter(private val uiCallback: IMostPopularShowsUiCallback)
: ITvDataSource.GetMostPopularShows {

private val remoteDataSource = TvRemoteDataSource()

fun getMostPopularShows() {
remoteDataSource.getMostPopularShows(this)
}

override fun onSuccessGetMostPopularShows(tvShows: MostPopularShowsListResponseData) {
uiCallback.onShowMostPopularShows(tvShows)
}

override fun onFailGetMostPopularShows() {
uiCallback.onFetchFailMostPopularShows()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.Gravity
import com.example.isabelcosta.moviesapp.R
import com.example.isabelcosta.moviesapp.ui.fragments.BaseFragment
import com.example.isabelcosta.moviesapp.ui.fragments.MostPopularShowsFragment
import com.example.isabelcosta.moviesapp.ui.fragments.NowPlayingMoviesFragment
import com.example.isabelcosta.moviesapp.utils.addFragmentToActivity
import com.example.isabelcosta.moviesapp.utils.replaceActivityFragment
Expand All @@ -14,16 +15,18 @@ class MainActivity : BaseActivity() {

private val fragmentsMenuMap: HashMap<Int, BaseFragment<MainActivity>> = hashMapOf(
R.id.menuNowPlayingMovies to NowPlayingMoviesFragment.newInstance(),
R.id.menuPopularTvShows to NowPlayingMoviesFragment.newInstance(),
R.id.menuPopularTvShows to MostPopularShowsFragment.newInstance(),
R.id.menuSearchMovies to NowPlayingMoviesFragment.newInstance()
)

private val fragmentsMenuTitles: HashMap<Int, Int> = hashMapOf (
R.id.menuNowPlayingMovies to R.string.menu_now_playing_movies,
R.id.menuPopularTvShows to R.string.menu_popular_tv_shows,
R.id.menuSearchMovies to R.string.menu_search_movies
R.id.menuNowPlayingMovies to R.string.screen_title_now_playing_movies,
R.id.menuPopularTvShows to R.string.screen_title_popular_tv_shows,
R.id.menuSearchMovies to R.string.screen_title_search_movies
)

private var selectedId: Int = R.id.menuNowPlayingMovies

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -38,23 +41,23 @@ class MainActivity : BaseActivity() {

private fun initMainScreen() {
replaceFragment(NowPlayingMoviesFragment.newInstance())
mainToolbar.setTitle(R.string.menu_now_playing_movies)
mainToolbar.setTitle(R.string.screen_title_now_playing_movies)
}

private fun populateSideMenu() {

mainActivityNavigationView.setNavigationItemSelectedListener {
val selectedFragment = fragmentsMenuMap[it.itemId]
selectedId = it.itemId
val selectedFragment = fragmentsMenuMap[selectedId]

mainActivityDrawerLayout.closeDrawers()

selectedFragment?.let {
replaceFragment(it)
mainToolbar.setTitle(fragmentsMenuTitles[selectedId]!!)
mainActivityNavigationView.setCheckedItem(selectedId)
}

mainToolbar.setTitle(fragmentsMenuTitles[it.itemId]!!)
mainActivityNavigationView.setCheckedItem(it.itemId)

true
}
}
Expand All @@ -64,6 +67,37 @@ class MainActivity : BaseActivity() {
mainToolbar.setNavigationOnClickListener { mainActivityDrawerLayout.openDrawer(Gravity.START) }
}

fun setToolbarTitle(titleRscId: Int) = mainToolbar.setTitle(titleRscId)

private fun toolbarSet(showMenuIcon: Boolean){

if (showMenuIcon) {
mainToolbar.setNavigationIcon(R.drawable.ic_menu_black_24dp)
mainToolbar.setNavigationOnClickListener { mainActivityDrawerLayout.openDrawer(Gravity.START) }
} else {
mainToolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp)
mainToolbar.setNavigationOnClickListener { onBackPressed() }
}
}

fun replaceFragment(fragment: BaseFragment<MainActivity>) = replaceActivityFragment(fragmentManager, R.id.mainFrameLayout, fragment)
fun addFragment(fragment: BaseFragment<MainActivity>) = addFragmentToActivity(fragmentManager, R.id.mainFrameLayout, fragment)

fun navigateToFragmentToolbarSet(isNextFragmentInitial: Boolean) {
toolbarSet(isNextFragmentInitial)
}

override fun onBackPressed() {

val count = fragmentManager.backStackEntryCount

if (count == 0) {
super.onBackPressed()
} else {
navigateToFragmentToolbarSet(true)
setToolbarTitle(fragmentsMenuTitles[selectedId]!!)
fragmentManager.popBackStack()
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.isabelcosta.moviesapp.ui.callbacks

import com.example.isabelcosta.moviesapp.data.models.MostPopularShowsListResponseData

interface IMostPopularShowsUiCallback {

fun onShowMostPopularShows(tvShows: MostPopularShowsListResponseData)
fun onFetchFailMostPopularShows()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ abstract class BaseFragment<S : BaseActivity> : Fragment() {
}

protected abstract fun getLayoutResourceId() : Int

protected abstract fun getTitleResourceId() : Int
}
Loading

0 comments on commit 725333a

Please sign in to comment.