Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update and optimize UsingRetrofit module #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions UsingRetrofit/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
buildFeatures {
viewBinding true
}

compileSdkVersion compileSdkVer
defaultConfig {
applicationId "com.developers.usingretrofit"
Expand All @@ -21,7 +24,7 @@ android {
}
}
buildTypes.each {
it.buildConfigField('String', "TV_KEY", API_KEY)
it.buildConfigField('String', "TV_KEY", "\"API_KEY\"")
}
}

Expand All @@ -38,7 +41,7 @@ dependencies {
implementation "com.squareup.retrofit2:converter-gson:$retrofitConverterGsonVer"

implementation "androidx.recyclerview:recyclerview:$supportVer"
implementation "androidx.cardview:cardview:$supportVer"
implementation "androidx.cardview:cardview:1.0.0"

implementation "com.squareup.picasso:picasso:$picassoVersion"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.developers.usingretrofit

import com.developers.usingretrofit.model.MovieResult
import com.developers.usingretrofit.utils.Constants
import com.developers.usingretrofit.utils.BASE_URL
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
Expand All @@ -22,9 +22,9 @@ interface ApiInterface {
fun create(): ApiInterface {

val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(Constants.BASE_URL)
.build()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build()

return retrofit.create(ApiInterface::class.java);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package com.developers.usingretrofit

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import com.developers.usingretrofit.BuildConfig.TV_KEY
import com.developers.usingretrofit.adapter.MovieAdapter
import com.developers.usingretrofit.databinding.ActivityMainBinding
import com.developers.usingretrofit.model.MovieResult
import kotlinx.android.synthetic.main.activity_main.*
import com.developers.usingretrofit.utils.lazyUI
import com.developers.usingretrofit.utils.showMessage
import com.developers.usingretrofit.utils.viewBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MainActivity : AppCompatActivity() {

private val binding by viewBinding(ActivityMainBinding::inflate)

private val apiCall by lazyUI(ApiInterface::create)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val apiCall = ApiInterface.create()
apiCall.getMovies(BuildConfig.TV_KEY, 1).enqueue(object : Callback<MovieResult> {

apiCall.getMovies(TV_KEY, 1).enqueue(object : Callback<MovieResult> {
override fun onFailure(call: Call<MovieResult>?, t: Throwable?) {
showError(t?.message)
}
Expand All @@ -36,11 +42,6 @@ class MainActivity : AppCompatActivity() {
})
}

private fun showError(message: String?) {
toast(message.toString())
}

fun Context.toast(msg: String) {
Toast.makeText(applicationContext, msg, Toast.LENGTH_SHORT).show()
}
private fun showError(message: String?) =
showMessage(message.toString())
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,56 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.developers.usingretrofit.R
import com.developers.usingretrofit.databinding.ListRowBinding
import com.developers.usingretrofit.model.Result
import com.developers.usingretrofit.utils.Constants
import com.developers.usingretrofit.utils.BASE_URL
import com.developers.usingretrofit.utils.viewBinding
import com.squareup.picasso.Callback
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.list_row.view.*

/**
* Created by Amanjeet Singh on 30/11/17.
*/
class MovieAdapter(val context: Context, private val resultList: List<Result>?) : RecyclerView.Adapter<MovieAdapter.MyViewHolder>() {
class MovieAdapter(val context: Context, private val resultList: List<Result>?) :
RecyclerView.Adapter<MovieAdapter.MyViewHolder>() {

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
override fun onBindViewHolder(holder: MyViewHolder, position: Int) =
holder.bindItems(resultList?.get(position))
}

override fun getItemCount(): Int {
return resultList?.size!!
}
override fun getItemCount(): Int =
resultList?.size ?: 0

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.list_row, parent, false)
return MyViewHolder(view)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder =
MyViewHolder(LayoutInflater.from(context).inflate(R.layout.list_row, parent, false))


class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

private val binding by viewBinding(ListRowBinding::bind)

fun bindItems(result: Result?) {
itemView.movie_title.text = result?.title
val posterUri = Uri.parse(Constants.IMAGE_BASE_URL).buildUpon()
.appendEncodedPath(result?.posterPath)
.build()
itemView.progress_bar.visibility = View.VISIBLE
Picasso.with(itemView.context).load(posterUri.toString())
.into(itemView.image_view_movie, object : Callback {

override fun onError() {
//Show Error here
}

override fun onSuccess() {
itemView.progress_bar.visibility = View.GONE
}

})
result?.let {
binding.apply {
movieTitle.text = it.title
binding.progressBar.visibility = View.VISIBLE

val posterUri = Uri.parse(BASE_URL).buildUpon()
.appendEncodedPath(it.posterPath)
.build()

Picasso.with(itemView.context).load(posterUri.toString())
.into(imageViewMovie, object : Callback {

override fun onError() {
//Show Error here
}

override fun onSuccess() {
progressBar.visibility = View.GONE
}
})
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ package com.developers.usingretrofit.utils
/**
* Created by Amanjeet Singh on 29/11/17.
*/
class Constants {

companion object {
@JvmField
val BASE_URL = "https://api.themoviedb.org/3/movie/";
@JvmField
val IMAGE_BASE_URL = "http://image.tmdb.org/t/p/w185/"
}

}
const val BASE_URL = "https://api.themoviedb.org/3/movie/"
const val IMAGE_BASE_URL = "http://image.tmdb.org/t/p/w185/"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.developers.usingretrofit.utils

fun <T> lazyUI(initializer: () -> T): Lazy<T> =
lazy(LazyThreadSafetyMode.NONE) { initializer.invoke() }
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.developers.usingretrofit.utils

import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding

inline fun <T : ViewBinding> AppCompatActivity.viewBinding(
crossinline bindingInflater: (LayoutInflater) -> T
) =
lazyUI {
bindingInflater(layoutInflater)
}

inline fun <T : ViewBinding> RecyclerView.ViewHolder.viewBinding(
crossinline bindingBinder: (View) -> T
) =
lazyUI {
bindingBinder(itemView)
}
8 changes: 4 additions & 4 deletions UsingRetrofit/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
apply from: '../dependencies.gradle'
apply from: rootProject.projectDir.absolutePath + '/dependencies.gradle'

repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:$androidPluginVer"
Expand All @@ -19,12 +19,12 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

subprojects {
apply from: '../../dependencies.gradle'
apply from: rootProject.projectDir.absolutePath + '/dependencies.gradle'
}

task clean(type: Delete) {
Expand Down
52 changes: 52 additions & 0 deletions UsingRetrofit/dependencies.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
ext{

kotlin_version = "1.5.20"
constraintLayoutVersion = "2.0.4"
compileSdkVer = 30
targetSdkVer = 30
androidPluginVer = "4.2.0"
buildToolsVer = "1.3.0"
minSdkVer = 19
supportVer = "1.2.1"
coreKtxVersion = "1.1.0"
designVersion= "1.0.0"

//Test dependencies
junitVer = "4.13.1"
androidTestRunnerVer = "1.4.0"
espressoCoreVersion = "3.4.0"

//Rx
rxJavaAndroidVersion = "2.1.0"
rxJavaVersion = "2.2.2"
rxJavaRetrofitAdapterVer = "2.3.0"
rxBindingVer = "2.1.1"
rxAdapterRetrofitVer = "2.3.0"

apolloPluginVersion = "0.3.2"
ankoVersion = "0.10.5"
gmsPlayVersionMaps = "17.0.0"
retrofitVersion = "2.6.0"
retrofitConverterGsonVer = "2.6.0"
roomVersion = "1.1.1"
kotlinCoroutinesAdapterVer = "0.9.2"
coroutinesCoreVer = "1.3.1"
coroutinesAndroidVer = "1.3.1"
dataBindingVer = "3.3.1"
jacksonKotlinModuleVer = "2.9.0"
picassoVersion = "2.5.2"
retrofitJacksonConverter = "2.3.0"
kodeInVersion = "4.1.0"
koinVersion = "0.8.2"
kotlinTestVersion = "2.0.7"
googlePlayServicesVer = "18.0.0"
moshiVersion = "1.5.0"
moshiRetrofitConverterVer = "2.0.0"
daggerVersion = "2.9"
realmVersion = "4.3.0"
timberVersion = "4.7.1"
firebaseJobDispatcherVer = "0.7.0"
sugarVersion = "1.5"
archComponentsVersion = "1.1.1"
pagingLibraryVersion = "1.0.1"
}