Skip to content

Commit 8f0af20

Browse files
committed
Initial commit
0 parents  commit 8f0af20

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1288
-0
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/compiler.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'kotlin-android'
4+
id 'kotlin-kapt'
5+
}
6+
7+
android {
8+
compileSdkVersion 31
9+
defaultConfig {
10+
applicationId "com.example.kotlinapidemo"
11+
minSdkVersion 23
12+
targetSdkVersion 31
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
kotlinOptions {
30+
jvmTarget = '1.8'
31+
}
32+
}
33+
34+
dependencies {
35+
implementation fileTree(dir: 'libs', include: ['*.jar'])
36+
//noinspection GradleDependency
37+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
38+
implementation 'androidx.appcompat:appcompat:1.3.1'
39+
implementation 'androidx.core:core-ktx:1.6.0'
40+
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
41+
implementation 'androidx.recyclerview:recyclerview:1.2.1'
42+
testImplementation 'junit:junit:4.13.2'
43+
testImplementation 'org.mockito:mockito-inline:2.13.0'
44+
testImplementation 'org.mockito:mockito-junit-jupiter:{mockitoversion}'
45+
androidTestImplementation 'androidx.test:runner:1.4.0'
46+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
47+
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
48+
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
49+
implementation 'com.squareup.retrofit2:retrofit:2.8.1'
50+
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
51+
implementation 'com.squareup.retrofit2:converter-gson:2.8.1'
52+
implementation 'com.ryanharter.auto.value:auto-value-gson-annotations:0.7.0'
53+
implementation 'com.jakewharton.auto.value:auto-value-annotations:1.5'
54+
implementation 'com.google.code.gson:gson:2.8.6'
55+
kapt 'com.google.auto.value:auto-value:1.5.3'
56+
kapt 'com.ryanharter.auto.value:auto-value-parcel:0.2.6'
57+
kapt 'com.ryanharter.auto.value:auto-value-gson:0.7.0'
58+
kapt 'com.squareup.auto.value:auto-value-redacted:1.0.1'
59+
implementation 'com.google.android.material:material:1.4.0'
60+
implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
61+
}

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.kotlinapidemo
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.example.kotlinapidemo", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.kotlinapidemo">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
7+
8+
<application
9+
android:allowBackup="true"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:roundIcon="@mipmap/ic_launcher_round"
13+
android:supportsRtl="true"
14+
android:theme="@style/Theme.KotlinApiDemo">
15+
<activity android:name=".view.MainActivity" android:exported="true">
16+
<intent-filter>
17+
<action android:name="android.intent.action.MAIN" />
18+
19+
<category android:name="android.intent.category.LAUNCHER" />
20+
</intent-filter>
21+
</activity>
22+
</application>
23+
24+
</manifest>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.example.kotlinapidemo.data
2+
3+
import android.net.Uri
4+
import com.google.gson.GsonBuilder
5+
import retrofit2.Retrofit
6+
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
7+
import retrofit2.converter.gson.GsonConverterFactory
8+
import retrofit2.converter.scalars.ScalarsConverterFactory
9+
10+
object ApiClient {
11+
private var client: PokemonApi? = null
12+
fun api(): PokemonApi? {
13+
return client ?: initializeClient()
14+
}
15+
16+
private fun initializeClient() : PokemonApi? {
17+
var gson = GsonBuilder()
18+
gson.registerTypeAdapter(Uri::class.java, UriTypeAdapter())
19+
var gsonConverterFactory = GsonConverterFactory.create(gson.create())
20+
client = Retrofit.Builder().baseUrl("https://pokeapi.co/api/v2/")
21+
.client(HttpClient.client)
22+
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
23+
.addConverterFactory(gsonConverterFactory)
24+
.addConverterFactory(ScalarsConverterFactory.create())
25+
.build()
26+
.create(PokemonApi::class.java)
27+
return client
28+
}
29+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.kotlinapidemo.data
2+
3+
import okhttp3.OkHttpClient
4+
5+
class HttpClient {
6+
var okHttpClient : OkHttpClient = OkHttpClient().newBuilder().build()
7+
private object Instance {
8+
val instanceClient : OkHttpClient = HttpClient().okHttpClient
9+
}
10+
companion object {
11+
val client : OkHttpClient by lazy { Instance.instanceClient }
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.example.kotlinapidemo.data
2+
3+
import com.example.kotlinapidemo.data.model.PokemonDetail
4+
import com.example.kotlinapidemo.data.model.PokemonListResult
5+
import io.reactivex.Single
6+
import retrofit2.adapter.rxjava2.Result
7+
import retrofit2.http.GET
8+
import retrofit2.http.Path
9+
10+
interface PokemonApi {
11+
12+
@GET("pokemon")
13+
fun pokemonList() : Single<Result<PokemonListResult>>
14+
15+
@GET("pokemon/{name}")
16+
fun getDetailByName(@Path("name") name : String) : Single<Result<PokemonDetail>>
17+
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example.kotlinapidemo.data
2+
3+
import android.net.Uri
4+
import com.google.gson.*
5+
import java.lang.reflect.Type
6+
7+
/*Serialization means to convert an object into that string, and
8+
deserialization is its inverse operation (convert string -> object)*/
9+
10+
class UriTypeAdapter : JsonSerializer<Uri>,
11+
JsonDeserializer<Uri> {
12+
@Throws(JsonParseException::class)
13+
override fun deserialize(
14+
json: JsonElement,
15+
typeOfT: Type,
16+
context: JsonDeserializationContext
17+
): Uri {
18+
return Uri.parse(json.asString)
19+
}
20+
21+
override fun serialize(
22+
src: Uri,
23+
typeOfSrc: Type,
24+
context: JsonSerializationContext
25+
): JsonElement {
26+
return JsonPrimitive(src.toString())
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.kotlinapidemo.data.model
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
data class PokemonDetail (
6+
@SerializedName("name") val name: String,
7+
@SerializedName("weight") val weight: Int,
8+
@SerializedName("height") val height: Int
9+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.example.kotlinapidemo.data.model
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
data class PokemonListEntryResult(
6+
@SerializedName("name") val name: String,
7+
@SerializedName("url") val url: String
8+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.kotlinapidemo.data.model
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
data class PokemonListResult(
6+
@SerializedName("count") val count: String,
7+
@SerializedName("next") val next: String,
8+
@SerializedName("previous") val previous: String,
9+
@SerializedName("results") val results: List<PokemonListEntryResult>
10+
)

0 commit comments

Comments
 (0)