├── .gitignore
├── .idea
├── caches
│ └── build_file_checksums.ser
├── codeStyles
│ └── Project.xml
├── compiler.xml
├── encodings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── boonya
│ │ └── ben
│ │ └── callingwebservice
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── boonya
│ │ │ └── ben
│ │ │ └── callingwebservice
│ │ │ ├── Const.kt
│ │ │ ├── StarWarSpeciesApplication.kt
│ │ │ ├── api
│ │ │ ├── Apis.kt
│ │ │ └── Swapi.kt
│ │ │ ├── di
│ │ │ ├── ApiModule.kt
│ │ │ ├── AppComponent.kt
│ │ │ ├── ComponentInjector.kt
│ │ │ └── SpeciesRepositoryModule.kt
│ │ │ ├── model
│ │ │ ├── Species.kt
│ │ │ └── SpeciesList.kt
│ │ │ └── species
│ │ │ ├── SpeciesActivity.kt
│ │ │ ├── SpeciesListAdapter.kt
│ │ │ ├── SpeciesListViewModel.kt
│ │ │ ├── SpeciesRepository.kt
│ │ │ ├── SpeciesRepositoryImpl.kt
│ │ │ └── SpeciesViewHolder.kt
│ └── res
│ │ ├── layout
│ │ ├── activity_species.xml
│ │ └── species_item.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── boonya
│ └── ben
│ └── callingwebservice
│ ├── ExampleUnitTest.java
│ └── SpeciesListViewModelTest.kt
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/caches/build_file_checksums.ser:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BenBoonya/calling-webservice/b026e47865a4a60c384c2b242930f5e1d73f5736/.idea/caches/build_file_checksums.ser
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
17 |
18 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # calling-webservice
2 | This repository is created as a sample project for this blog post.
3 |
4 | https://blog.oozou.com/calling-web-service-with-android-architecture-component-8de864800a93
5 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android'
3 | apply plugin: 'kotlin-android-extensions'
4 | apply plugin: 'kotlin-kapt'
5 |
6 | android {
7 | compileSdkVersion 27
8 | buildToolsVersion '27.0.3'
9 | defaultConfig {
10 | applicationId "boonya.ben.callingwebservice"
11 | minSdkVersion 15
12 | targetSdkVersion 27
13 | versionCode 1
14 | versionName "1.0"
15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16 | }
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | }
24 |
25 | dependencies {
26 | implementation fileTree(dir: 'libs', include: ['*.jar'])
27 | androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
28 | exclude group: 'com.android.support', module: 'support-annotations'
29 | })
30 |
31 | implementation 'com.android.support:appcompat-v7:27.1.1'
32 | implementation 'com.android.support:design:27.1.1'
33 | implementation 'com.android.support:cardview-v7:27.1.1'
34 | implementation 'com.android.support.constraint:constraint-layout:1.1.2'
35 | implementation 'com.squareup.retrofit2:retrofit:2.3.0'
36 | implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
37 | implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-experimental-adapter:1.0.0'
38 | implementation 'android.arch.lifecycle:runtime:1.1.1'
39 | implementation 'android.arch.lifecycle:extensions:1.1.1'
40 |
41 | //test library
42 | testImplementation 'junit:junit:4.12'
43 | testImplementation 'org.mockito:mockito-core:2.12.0'
44 | testImplementation 'android.arch.core:core-testing:1.1.1'
45 |
46 | // dependency injection
47 | implementation 'com.google.dagger:dagger:2.15'
48 | compileOnly 'javax.annotation:jsr250-api:1.0'
49 | kapt "com.google.dagger:dagger-compiler:2.15"
50 |
51 | //kotlin
52 | implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
53 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
54 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.23.4'
55 |
56 | annotationProcessor 'android.arch.lifecycle:compiler:1.1.1'
57 | }
58 | repositories {
59 | mavenCentral()
60 | }
61 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/oozou/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/boonya/ben/callingwebservice/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("boonya.ben.callingwebservice", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
14 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/Const.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice
2 |
3 | /**
4 | * Created by oozou on 7/20/2017 AD.
5 | */
6 | class Const {
7 | companion object {
8 | val BASE_URL = "http://swapi.co/api/"
9 | }
10 | }
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/StarWarSpeciesApplication.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice
2 |
3 | import android.app.Application
4 | import boonya.ben.callingwebservice.di.ComponentInjector
5 |
6 | /**
7 | * Created by oozou on 7/20/2017 AD.
8 | */
9 | class StarWarSpeciesApplication : Application() {
10 |
11 | override fun onCreate() {
12 | super.onCreate()
13 |
14 | ComponentInjector.init()
15 | }
16 | }
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/api/Apis.kt:
--------------------------------------------------------------------------------
1 | package com.ben.boonya.architecturecomponentdemo.common.api
2 |
3 | import com.google.gson.GsonBuilder
4 | import retrofit2.Retrofit
5 | import retrofit2.converter.gson.GsonConverterFactory
6 |
7 | /**
8 | * Created by Boonya Kitpitak on 6/16/17.
9 | */
10 | object Apis {
11 | public fun getStarWarApi(): Swapi {
12 | val gson = GsonBuilder ().create()
13 | val retrofit = Retrofit.Builder()
14 | .baseUrl("http://swapi.co/api/")
15 | .addConverterFactory(GsonConverterFactory.create(gson))
16 | .build()
17 | return retrofit.create(Swapi::class.java)
18 | }
19 | }
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/api/Swapi.kt:
--------------------------------------------------------------------------------
1 | package com.ben.boonya.architecturecomponentdemo.common.api
2 |
3 | import boonya.ben.callingwebservice.model.SpeciesList
4 | import retrofit2.Call
5 | import retrofit2.http.GET
6 | import retrofit2.http.Path
7 | import retrofit2.http.Query
8 |
9 | /**
10 | * Created by Boonya Kitpitak on 6/16/17.
11 | */
12 | interface Swapi {
13 | @GET("species/")
14 | fun getSpecies(): Call
15 | }
16 |
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/di/ApiModule.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice.di
2 |
3 | import com.ben.boonya.architecturecomponentdemo.common.api.Swapi
4 | import com.google.gson.GsonBuilder
5 | import dagger.Module
6 | import dagger.Provides
7 | import retrofit2.Retrofit
8 | import retrofit2.converter.gson.GsonConverterFactory
9 | import javax.inject.Singleton
10 |
11 | /**
12 | * Created by oozou on 7/20/2017 AD.
13 | */
14 | @Module
15 | class ApiModule(val baseUrl: String) {
16 | @Provides @Singleton
17 | fun provideApiService(): Swapi {
18 | val gson = GsonBuilder().create()
19 |
20 | return Retrofit.Builder()
21 | .baseUrl(baseUrl)
22 | .addConverterFactory(GsonConverterFactory.create(gson))
23 | .build().create(Swapi::class.java)
24 | }
25 | }
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/di/AppComponent.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice.di
2 |
3 | import boonya.ben.callingwebservice.species.SpeciesListViewModel
4 | import dagger.Component
5 | import javax.inject.Singleton
6 |
7 | /**
8 | * Created by oozou on 7/20/2017 AD.
9 | */
10 |
11 | @Singleton
12 | @Component(
13 | modules = arrayOf(
14 | ApiModule::class,
15 | SpeciesRepositoryModule::class
16 | )
17 | )
18 | interface AppComponent {
19 |
20 | fun inject(speciesListViewModel: SpeciesListViewModel)
21 |
22 | }
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/di/ComponentInjector.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice.di
2 |
3 | import boonya.ben.callingwebservice.Const
4 |
5 | class ComponentInjector {
6 |
7 | companion object {
8 |
9 | lateinit var component: AppComponent
10 |
11 | fun init() {
12 | component = DaggerAppComponent.builder()
13 | .apiModule(ApiModule(Const.BASE_URL))
14 | .speciesRepositoryModule(SpeciesRepositoryModule())
15 | .build()
16 | }
17 | }
18 | }
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/di/SpeciesRepositoryModule.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice.di
2 |
3 | import boonya.ben.callingwebservice.species.SpeciesRepository
4 | import boonya.ben.callingwebservice.species.SpeciesRepositoryImpl
5 | import com.ben.boonya.architecturecomponentdemo.common.api.Swapi
6 | import dagger.Module
7 | import dagger.Provides
8 | import javax.inject.Singleton
9 |
10 | /**
11 | * Created by oozou on 7/20/2017 AD.
12 | */
13 | @Module
14 | class SpeciesRepositoryModule {
15 |
16 | @Provides @Singleton
17 | fun providePostRepository(apiService: Swapi): SpeciesRepository = SpeciesRepositoryImpl(apiService)
18 | }
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/model/Species.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice.model
2 |
3 | import com.google.gson.annotations.SerializedName
4 |
5 | /**
6 | * Created by oozou on 7/12/2017 AD.
7 | */
8 | data class Species(val name : String, val classification : String, val language : String, @SerializedName("average_lifespan") val lifeSpan : String)
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/model/SpeciesList.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice.model
2 |
3 | import com.google.gson.annotations.SerializedName
4 |
5 | /**
6 | * Created by oozou on 7/12/2017 AD.
7 | */
8 | data class SpeciesList(@SerializedName("results") val speciesList: List, val next: String?)
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/species/SpeciesActivity.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice.species
2 |
3 | import android.arch.lifecycle.*
4 | import android.support.v7.app.AppCompatActivity
5 | import android.os.Bundle
6 | import android.support.design.widget.Snackbar
7 | import android.support.v7.widget.LinearLayoutManager
8 | import android.view.View
9 | import boonya.ben.callingwebservice.R
10 | import boonya.ben.callingwebservice.di.ComponentInjector
11 | import boonya.ben.callingwebservice.model.Species
12 | import kotlinx.android.synthetic.main.activity_species.*
13 |
14 | class SpeciesActivity : AppCompatActivity(), LifecycleRegistryOwner {
15 |
16 | private val registry = LifecycleRegistry(this)
17 | private lateinit var viewModel: SpeciesListViewModel
18 | private lateinit var adapter: SpeciesListAdapter
19 |
20 | override fun getLifecycle(): LifecycleRegistry = registry
21 |
22 | override fun onCreate(savedInstanceState: Bundle?) {
23 | super.onCreate(savedInstanceState)
24 | setContentView(R.layout.activity_species)
25 | setSupportActionBar(toolbar)
26 | title = "STARWAR SPECIES"
27 | viewModel = createViewModel()
28 | adapter = SpeciesListAdapter(viewModel)
29 | rvSpecies.apply {
30 | setHasFixedSize(true)
31 | layoutManager = LinearLayoutManager(this@SpeciesActivity)
32 | adapter = this@SpeciesActivity.adapter
33 | }
34 | attachObserver()
35 | viewModel.getSpecies()
36 | }
37 |
38 | private fun attachObserver() {
39 | viewModel.isLoading.observe(this, Observer {
40 | it?.let { showLoadingDialog(it) }
41 | })
42 | viewModel.apiError.observe(this, Observer {
43 | it?.let { Snackbar.make(rvSpecies, it.localizedMessage, Snackbar.LENGTH_LONG).show() }
44 | })
45 | viewModel.speciesResponse.observe(this, Observer> {
46 | it?.let { adapter.notifyDataSetChanged() }
47 | })
48 |
49 | }
50 |
51 | private fun createViewModel(): SpeciesListViewModel =
52 | ViewModelProviders.of(this).get(SpeciesListViewModel::class.java).also {
53 | ComponentInjector.component.inject(it)
54 | }
55 |
56 | private fun showLoadingDialog(show: Boolean) {
57 | if (show) progressBar.visibility = View.VISIBLE else progressBar.visibility = View.GONE
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/species/SpeciesListAdapter.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice.species
2 |
3 | import android.support.v7.widget.RecyclerView
4 | import android.view.LayoutInflater
5 | import android.view.ViewGroup
6 | import boonya.ben.callingwebservice.R
7 |
8 | /**
9 | * Created by oozou on 7/12/2017 AD.
10 | */
11 | class SpeciesListAdapter(var speciesListViewModel: SpeciesListViewModel) : RecyclerView.Adapter() {
12 |
13 | override fun onBindViewHolder(holder: SpeciesViewHolder, position: Int) {
14 | speciesListViewModel.getSpeciesAt(position)?.let {
15 | holder.apply {
16 | name.text = it.name
17 | classification.text = it.classification
18 | language.text = it.language
19 | lifeSpan.text = it.lifeSpan
20 | }
21 | }
22 | }
23 |
24 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SpeciesViewHolder {
25 | return SpeciesViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.species_item, parent, false))
26 | }
27 |
28 | override fun getItemCount(): Int {
29 | return speciesListViewModel.getSpeciesSize()
30 | }
31 |
32 | }
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/species/SpeciesListViewModel.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice.species
2 |
3 | import android.arch.lifecycle.MutableLiveData
4 | import android.arch.lifecycle.ViewModel
5 | import boonya.ben.callingwebservice.model.Species
6 | import javax.inject.Inject
7 |
8 | /**
9 | * Created by oozou on 7/12/2017 AD.
10 | */
11 | class SpeciesListViewModel : ViewModel() {
12 |
13 | @Inject
14 | lateinit var repository: SpeciesRepository
15 |
16 | var isLoading = MutableLiveData()
17 |
18 | var apiError = MutableLiveData()
19 |
20 | var speciesResponse = MutableLiveData>()
21 |
22 | fun getSpecies() {
23 | isLoading.value = true
24 | repository.getSpecies(
25 | {
26 | speciesResponse.value = it
27 | isLoading.value = false
28 | },
29 |
30 | {
31 | apiError.value = it
32 | isLoading.value = false
33 | })
34 | }
35 |
36 |
37 | /**
38 | * Adapter Callback
39 | */
40 |
41 | fun getSpeciesAt(position: Int): Species? {
42 | if (position < getSpeciesSize()) {
43 | return speciesResponse.value?.get(position)
44 | } else {
45 | return null
46 | }
47 | }
48 |
49 | fun getSpeciesSize(): Int {
50 | speciesResponse.value?.let {
51 | return it.size
52 | }
53 | return 0
54 | }
55 |
56 | }
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/species/SpeciesRepository.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice.species
2 |
3 | import boonya.ben.callingwebservice.model.Species
4 |
5 | /**
6 | * Created by oozou on 7/20/2017 AD.
7 | */
8 | interface SpeciesRepository {
9 |
10 | fun getSpecies(successHandler: (List?) -> Unit, failureHandler: (Throwable?) -> Unit)
11 |
12 | }
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/species/SpeciesRepositoryImpl.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice.species
2 |
3 | import boonya.ben.callingwebservice.model.Species
4 | import boonya.ben.callingwebservice.model.SpeciesList
5 | import com.ben.boonya.architecturecomponentdemo.common.api.Swapi
6 | import retrofit2.Call
7 | import retrofit2.Callback
8 | import retrofit2.Response
9 |
10 | /**
11 | * Created by oozou on 7/12/2017 AD.
12 | */
13 |
14 | class SpeciesRepositoryImpl(val apiService: Swapi) : SpeciesRepository {
15 |
16 | override fun getSpecies(successHandler: (List?) -> Unit, failureHandler: (Throwable?) -> Unit) {
17 | apiService.getSpecies().enqueue(object : Callback {
18 | override fun onResponse(call: Call?, response: Response?) {
19 | response?.body()?.let {
20 | successHandler(it.speciesList)
21 | }
22 | }
23 |
24 | override fun onFailure(call: Call?, t: Throwable?) {
25 | failureHandler(t)
26 | }
27 | })
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/app/src/main/java/boonya/ben/callingwebservice/species/SpeciesViewHolder.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice.species
2 |
3 | import android.support.v7.widget.RecyclerView
4 | import android.view.View
5 | import android.widget.TextView
6 | import boonya.ben.callingwebservice.R
7 |
8 | /**
9 | * Created by oozou on 7/12/2017 AD.
10 | */
11 | class SpeciesViewHolder(view: View) : RecyclerView.ViewHolder(view) {
12 | val name by lazy { view.findViewById(R.id.name) as TextView }
13 | val classification by lazy { view.findViewById(R.id.classification) as TextView }
14 | val language by lazy { view.findViewById(R.id.language) as TextView }
15 | val lifeSpan by lazy { view.findViewById(R.id.lifeSpan) as TextView }
16 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_species.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
15 |
22 |
23 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/species_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
18 |
19 |
25 |
26 |
32 |
33 |
39 |
40 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BenBoonya/calling-webservice/b026e47865a4a60c384c2b242930f5e1d73f5736/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BenBoonya/calling-webservice/b026e47865a4a60c384c2b242930f5e1d73f5736/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BenBoonya/calling-webservice/b026e47865a4a60c384c2b242930f5e1d73f5736/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BenBoonya/calling-webservice/b026e47865a4a60c384c2b242930f5e1d73f5736/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BenBoonya/calling-webservice/b026e47865a4a60c384c2b242930f5e1d73f5736/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BenBoonya/calling-webservice/b026e47865a4a60c384c2b242930f5e1d73f5736/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BenBoonya/calling-webservice/b026e47865a4a60c384c2b242930f5e1d73f5736/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BenBoonya/calling-webservice/b026e47865a4a60c384c2b242930f5e1d73f5736/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BenBoonya/calling-webservice/b026e47865a4a60c384c2b242930f5e1d73f5736/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BenBoonya/calling-webservice/b026e47865a4a60c384c2b242930f5e1d73f5736/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | CallingWebService
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/test/java/boonya/ben/callingwebservice/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/test/java/boonya/ben/callingwebservice/SpeciesListViewModelTest.kt:
--------------------------------------------------------------------------------
1 | package boonya.ben.callingwebservice
2 |
3 | import android.arch.core.executor.testing.InstantTaskExecutorRule
4 | import android.arch.lifecycle.MutableLiveData
5 | import android.arch.lifecycle.Observer
6 | import boonya.ben.callingwebservice.species.SpeciesListViewModel
7 | import boonya.ben.callingwebservice.species.SpeciesRepositoryImpl
8 | import boonya.ben.callingwebservice.model.Species
9 | import boonya.ben.callingwebservice.model.SpeciesList
10 | import boonya.ben.callingwebservice.species.SpeciesRepository
11 | import org.junit.Before
12 | import org.junit.Rule
13 | import org.junit.Test
14 | import org.mockito.ArgumentMatchers
15 | import org.mockito.Mock
16 | import org.mockito.Mockito.*
17 | import org.mockito.MockitoAnnotations
18 |
19 | /**
20 | * Created by oozou on 7/19/2017 AD.
21 | */
22 | class SpeciesListViewModelTest {
23 |
24 | @Rule @JvmField
25 | val instantExecutorRule = InstantTaskExecutorRule()
26 |
27 | lateinit var speciesListViewModel: SpeciesListViewModel
28 |
29 | val mockedSpecies = listOf(
30 | Species("Name1", "Classification1", "Lang1", "LifeSpan1"),
31 | Species("Name2", "Classification2", "Lang2", "LifeSpan2"),
32 | Species("Name3", "Classification3", "Lang3", "LifeSpan3"))
33 |
34 | @Before
35 | fun setup() {
36 | MockitoAnnotations.initMocks(this)
37 | speciesListViewModel = SpeciesListViewModel().apply {
38 | repository = object : SpeciesRepository {
39 | override fun getSpecies(successHandler: (List?) -> Unit, failureHandler: (Throwable?) -> Unit) {
40 | successHandler(mockedSpecies)
41 | }
42 | }
43 | }
44 | }
45 |
46 | @Test
47 | fun getSpecies() {
48 | val speciesResponse = MutableLiveData>()
49 | val observer = mock(Observer::class.java)
50 | speciesListViewModel.speciesResponse = speciesResponse
51 |
52 | speciesResponse.observeForever(observer as Observer>)
53 |
54 | speciesListViewModel.getSpecies()
55 |
56 | // verify(speciesRepo).getSpecies(ArgumentMatchers.any(), ArgumentMatchers.any())
57 | //verifyNoMoreInteractions(speciesRepo)
58 | verify(observer).onChanged(mockedSpecies)
59 | verifyNoMoreInteractions(observer)
60 | }
61 |
62 |
63 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | ext.kotlin_version = '1.2.51'
5 | repositories {
6 | jcenter()
7 | maven {
8 | url 'https://maven.google.com/'
9 | name 'Google'
10 | }
11 | }
12 | dependencies {
13 | classpath 'com.android.tools.build:gradle:3.1.3'
14 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
15 |
16 | // NOTE: Do not place your application dependencies here; they belong
17 | // in the individual module build.gradle files
18 | }
19 | }
20 |
21 | allprojects {
22 | repositories {
23 | jcenter()
24 | maven { url "https://maven.google.com" }
25 | maven {
26 | url 'https://maven.google.com/'
27 | name 'Google'
28 | }
29 | }
30 | }
31 |
32 | task clean(type: Delete) {
33 | delete rootProject.buildDir
34 | }
35 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BenBoonya/calling-webservice/b026e47865a4a60c384c2b242930f5e1d73f5736/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Jul 10 12:03:34 ICT 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------