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

Commit

Permalink
Update Retrofit *Call* callbacks
Browse files Browse the repository at this point in the history
Uses Retrofit *Call* callbacks vs discrete types/suspend functions
Use the buildFeatures block to enable  dataBinding.
Update deprecated ViewModelProviders.
Use viewModelScope instead of setting up own scope
  • Loading branch information
android-dev-lxl committed Aug 13, 2020
1 parent 3568dd9 commit 00f31b5
Show file tree
Hide file tree
Showing 22 changed files with 104 additions and 233 deletions.
32 changes: 14 additions & 18 deletions MarsRealEstateFinal/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,44 @@ apply plugin: "androidx.navigation.safeargs"

android {
compileSdkVersion 30
dataBinding {
enabled = true
}
defaultConfig {
applicationId "com.example.android.marsrealestate"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
androidExtensions {
experimental = true

buildFeatures {
dataBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$version_kotlin"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$version_kotlin"

// Constraint Layout
implementation "androidx.constraintlayout:constraintlayout:$version_constraint_layout"

// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$version_lifecycle_extensions"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$version_lifecycle"

// Navigation
implementation "android.arch.navigation:navigation-fragment-ktx:$version_navigation"
Expand All @@ -65,20 +70,11 @@ dependencies {
implementation "androidx.core:core-ktx:$version_core"

// Moshi
implementation "com.squareup.moshi:moshi:$version_moshi"
implementation "com.squareup.moshi:moshi-kotlin:$version_moshi"

// Retrofit with Moshi Converter
implementation "com.squareup.retrofit2:retrofit:$version_retrofit"
implementation "com.squareup.retrofit2:converter-moshi:$version_retrofit"

// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$version_kotlin_coroutines"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$version_kotlin_coroutines"

// Retrofit Coroutines Support
implementation "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:$version_retrofit_coroutines_adapter"

// Glide
implementation "com.github.bumptech.glide:glide:$version_glide"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.ViewModelProvider
import com.example.android.marsrealestate.databinding.FragmentDetailBinding

/**
Expand All @@ -35,11 +35,11 @@ class DetailFragment : Fragment() {

val application = requireNotNull(activity).application
val binding = FragmentDetailBinding.inflate(inflater)
binding.setLifecycleOwner(this)
binding.lifecycleOwner = this

val marsProperty = DetailFragmentArgs.fromBundle(arguments!!).selectedProperty
val viewModelFactory = DetailViewModelFactory(marsProperty, application)
binding.viewModel = ViewModelProviders.of(
binding.viewModel = ViewModelProvider(
this, viewModelFactory).get(DetailViewModel::class.java)

return binding.root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@

package com.example.android.marsrealestate.network

import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
import kotlinx.coroutines.Deferred
import retrofit2.http.Query

enum class MarsApiFilter(val value: String) {
SHOW_RENT("rent"),
SHOW_BUY("buy"),
SHOW_ALL("all") }

private const val BASE_URL = " https://android-kotlin-fun-mars-server.appspot.com/"
private const val BASE_URL = "https://android-kotlin-fun-mars-server.appspot.com/"

/**
* Build the Moshi object that Retrofit will be using, making sure to add the Kotlin adapter for
Expand All @@ -47,7 +45,6 @@ private val moshi = Moshi.Builder()
*/
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.baseUrl(BASE_URL)
.build()

Expand All @@ -56,15 +53,12 @@ private val retrofit = Retrofit.Builder()
*/
interface MarsApiService {
/**
* Returns a Coroutine [Deferred] [List] of [MarsProperty] which can be fetched with await() if
* in a Coroutine scope.
* Returns a Coroutine [List] of [MarsProperty] which can be fetched in a Coroutine scope.
* The @GET annotation indicates that the "realestate" endpoint will be requested with the GET
* HTTP method
*/
@GET("realestate")
fun getProperties(@Query("filter") type: String):
// The Coroutine Call Adapter allows us to return a Deferred, a Job with a result
Deferred<List<MarsProperty>>
suspend fun getProperties(@Query("filter") type: String): List<MarsProperty>
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import android.os.Bundle
import android.view.*
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.example.android.marsrealestate.R
import com.example.android.marsrealestate.databinding.FragmentOverviewBinding
Expand All @@ -36,7 +36,7 @@ class OverviewFragment : Fragment() {
* Lazily initialize our [OverviewViewModel].
*/
private val viewModel: OverviewViewModel by lazy {
ViewModelProviders.of(this).get(OverviewViewModel::class.java)
ViewModelProvider(this).get(OverviewViewModel::class.java)
}

/**
Expand All @@ -46,10 +46,9 @@ class OverviewFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val binding = FragmentOverviewBinding.inflate(inflater)
//val binding = GridViewItemBinding.inflate(inflater)

// Allows Data Binding to Observe LiveData with the lifecycle of this Fragment
binding.setLifecycleOwner(this)
binding.lifecycleOwner = this

// Giving the binding access to the OverviewViewModel
binding.viewModel = viewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import androidx.lifecycle.ViewModel
import com.example.android.marsrealestate.network.MarsApi
import com.example.android.marsrealestate.network.MarsApiFilter
import com.example.android.marsrealestate.network.MarsProperty
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch

enum class MarsApiStatus { LOADING, ERROR, DONE }
Expand Down Expand Up @@ -55,11 +53,6 @@ class OverviewViewModel : ViewModel() {
val navigateToSelectedProperty: LiveData<MarsProperty>
get() = _navigateToSelectedProperty

// Create a Coroutine scope using a job to be able to cancel when needed
private var viewModelJob = Job()

// the Coroutine runs using the Main (UI) dispatcher
private val coroutineScope = CoroutineScope(viewModelJob + Dispatchers.Main)

/**
* Call getMarsRealEstateProperties() on init so we can display status immediately.
Expand All @@ -75,31 +68,18 @@ class OverviewViewModel : ViewModel() {
* @param filter the [MarsApiFilter] that is sent as part of the web server request
*/
private fun getMarsRealEstateProperties(filter: MarsApiFilter) {
coroutineScope.launch {
// Get the Deferred object for our Retrofit request
var getPropertiesDeferred = MarsApi.retrofitService.getProperties(filter.value)
viewModelScope.launch {
_status.value = MarsApiStatus.LOADING
try {
_status.value = MarsApiStatus.LOADING
// this will run on a thread managed by Retrofit
val listResult = getPropertiesDeferred.await()
_properties.value = MarsApi.retrofitService.getProperties(filter.value)
_status.value = MarsApiStatus.DONE
_properties.value = listResult
} catch (e: Exception) {
_status.value = MarsApiStatus.ERROR
_properties.value = ArrayList()
}
}
}

/**
* When the [ViewModel] is finished, we cancel our coroutine [viewModelJob], which tells the
* Retrofit service to stop.
*/
override fun onCleared() {
super.onCleared()
viewModelJob.cancel()
}

/**
* Updates the data set filter for the web services by querying the data with the new filter
* by calling [getMarsRealEstateProperties]
Expand Down
12 changes: 6 additions & 6 deletions MarsRealEstateFinal/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ buildscript {
// navigation where the versions of the plugin needs to be the same as the version of the
// library defined in the app Gradle file
version_android_gradle_plugin = '4.0.1'
version_core = "1.0.1"
version_core = "1.3.1"
version_constraint_layout = "1.1.3"
version_glide = "4.8.0"
version_kotlin = "1.3.72"
version_kotlin_coroutines = "1.1.0"
version_lifecycle_extensions = "2.0.0"
version_moshi = "1.8.0"
version_kotlin_coroutines = "1.3.7"
version_lifecycle = "2.2.0"
version_moshi = "1.9.3"
version_navigation = "1.0.0"
version_retrofit = "2.5.0"
version_retrofit = "2.9.0"
version_retrofit_coroutines_adapter = "0.9.2"
version_recyclerview = "1.0.0"
version_recyclerview = "1.1.0"
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
android.databinding.enableV2=true
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
37 changes: 17 additions & 20 deletions MarsRealEstateGrid/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,46 @@ apply plugin: 'kotlin-kapt'
apply plugin: "androidx.navigation.safeargs"

android {
compileSdkVersion 28
dataBinding {
enabled = true
}
compileSdkVersion 30
defaultConfig {
applicationId "com.example.android.marsrealestate"
minSdkVersion 19
targetSdkVersion 28
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
androidExtensions {
experimental = true

buildFeatures {
dataBinding true
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$version_kotlin"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$version_kotlin"

// Constraint Layout
implementation "androidx.constraintlayout:constraintlayout:$version_constraint_layout"

// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$version_lifecycle_extensions"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$version_lifecycle"

// Navigation
implementation "android.arch.navigation:navigation-fragment-ktx:$version_navigation"
Expand All @@ -65,20 +71,11 @@ dependencies {
implementation "androidx.core:core-ktx:$version_core"

// Moshi
implementation "com.squareup.moshi:moshi:$version_moshi"
implementation "com.squareup.moshi:moshi-kotlin:$version_moshi"

// Retrofit with Moshi Converter
implementation "com.squareup.retrofit2:retrofit:$version_retrofit"
implementation "com.squareup.retrofit2:converter-moshi:$version_retrofit"

// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$version_kotlin_coroutines"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$version_kotlin_coroutines"

// Retrofit Coroutines Support
implementation "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:$version_retrofit_coroutines_adapter"

// Glide
implementation "com.github.bumptech.glide:glide:$version_glide"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.example.android.marsrealestate.databinding.FragmentDetailBinding

/**
Expand All @@ -34,7 +33,7 @@ class DetailFragment : Fragment() {
@Suppress("UNUSED_VARIABLE")
val application = requireNotNull(activity).application
val binding = FragmentDetailBinding.inflate(inflater)
binding.setLifecycleOwner(this)
binding.lifecycleOwner = this
return binding.root
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package com.example.android.marsrealestate.detail
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.ViewModel
import com.example.android.marsrealestate.detail.DetailFragment
import com.example.android.marsrealestate.network.MarsProperty

/**
Expand Down
Loading

0 comments on commit 00f31b5

Please sign in to comment.