Skip to content

Commit 34867bb

Browse files
author
Anton Azaryan
committed
Migrate :countries and :flag-assets to Kotlin
Add simple tests and instrumented tests Add Dependencies file
1 parent 5c6cecd commit 34867bb

File tree

23 files changed

+361
-240
lines changed

23 files changed

+361
-240
lines changed

build.gradle

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
buildscript {
2-
2+
33
repositories {
44
google()
55
jcenter()
6+
mavenCentral()
7+
maven { url "https://jitpack.io" }
68
}
79

810
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.0.1'
11+
classpath "com.android.tools.build:gradle:$Version.androidGradle"
12+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$Version.kotlin"
1013
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
1114
}
1215
}
@@ -15,6 +18,8 @@ allprojects {
1518
repositories {
1619
google()
1720
jcenter()
21+
mavenCentral()
22+
maven { url 'https://jitpack.io' }
1823
}
1924
}
2025

buildSrc/build.gradle.kts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
plugins {
2+
`kotlin-dsl`
3+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import org.gradle.api.JavaVersion
2+
3+
object General {
4+
val sourceCompatibility = JavaVersion.VERSION_1_8
5+
val targetCompatibility = JavaVersion.VERSION_1_8
6+
7+
const val TestInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
8+
}
9+
10+
object Version {
11+
12+
// project
13+
const val minSdk = 21
14+
const val targetSdk = 28
15+
const val buildToolsVersion = "28.0.3"
16+
const val compileSdkVersion = 28
17+
const val androidGradle = "3.2.1"
18+
19+
// kotlin
20+
const val kotlin = "1.2.71" // https://kotlinlang.org/
21+
22+
// serialization
23+
const val jackson = "2.9.7" //https://github.com/FasterXML/jackson-core
24+
25+
// tests
26+
const val jUnit = "4.12"
27+
const val androidJUnit = "1.0.0"
28+
const val androidTestRunner = "1.0.0-rc01"
29+
const val espresso = "3.1.0-alpha4"
30+
}
31+
32+
object Deps {
33+
const val kotlinStdlib = "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${Version.kotlin}"
34+
const val jacksonCore = "com.fasterxml.jackson.core:jackson-core:${Version.jackson}"
35+
36+
// Tests
37+
const val jUnit = "junit:junit:${Version.jUnit}"
38+
const val androidJUnit = "androidx.test.ext:junit:${Version.androidJUnit}"
39+
const val androidTestRunner = "androidx.test:runner:${Version.androidTestRunner}"
40+
const val espressoCore = "androidx.test.espresso:espresso-core:${Version.espresso}"
41+
}

countries/build.gradle

+32-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
1-
apply plugin: 'java-library'
1+
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android'
23
apply plugin: 'maven'
34

45
group = 'com.github.jetruby'
56

67
dependencies {
7-
implementation fileTree(dir: 'libs', include: ['*.jar'])
8-
implementation "com.fasterxml.jackson.core:jackson-core:$rootProject.ext.jacksonCoreVersion"
8+
implementation Deps.kotlinStdlib
9+
implementation Deps.jacksonCore
10+
testImplementation Deps.jUnit
911
}
1012

11-
task sourcesJar(type: Jar, dependsOn: classes) {
12-
classifier = 'sources'
13-
from sourceSets.main.allSource
14-
}
13+
android {
14+
compileSdkVersion Version.compileSdkVersion
15+
buildToolsVersion Version.buildToolsVersion
16+
17+
defaultConfig {
18+
minSdkVersion Version.minSdk
19+
targetSdkVersion Version.targetSdk
20+
21+
testInstrumentationRunner General.TestInstrumentationRunner
22+
}
1523

16-
artifacts {
17-
archives sourcesJar
24+
buildTypes {
25+
26+
release {
27+
minifyEnabled false
28+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
29+
}
30+
}
31+
32+
compileOptions {
33+
sourceCompatibility General.sourceCompatibility
34+
targetCompatibility General.targetCompatibility
35+
}
1836
}
1937

20-
sourceCompatibility = "1.8"
21-
targetCompatibility = "1.8"
38+
// build a jar with source files
39+
task sourcesJar(type: Jar) {
40+
from android.sourceSets.main.java.srcDirs
41+
classifier = 'sources'
42+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<manifest package="com.jetruby.common.countries"/>

countries/src/main/java/com/jetruby/common/countries/Countries.java

-83
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.jetruby.common.countries
2+
3+
import android.content.Context
4+
import com.fasterxml.jackson.core.JsonFactory
5+
import com.fasterxml.jackson.core.JsonParser
6+
import com.fasterxml.jackson.core.JsonToken
7+
import com.jetruby.common.countries.Country.Companion.EMPTY
8+
import java.io.IOException
9+
import java.io.InputStreamReader
10+
import java.util.Comparator
11+
12+
13+
data class Country(
14+
var name: String = "",
15+
var countryCode: String = "",
16+
var phoneCode: String = ""
17+
) {
18+
19+
companion object {
20+
val EMPTY = Country()
21+
fun china() = Country("China", "CN", "86")
22+
fun usa() = Country("United States", "US", "1")
23+
fun comparator(): Comparator<Country> =
24+
Comparator { c1, c2 -> c1.countryCode.compareTo(c2.countryCode) }
25+
}
26+
27+
}
28+
29+
@Throws(IOException::class)
30+
fun countryList(context: Context): List<Country> = parseList(
31+
JsonFactory().createParser(
32+
InputStreamReader(context.resources.openRawResource(R.raw.countries))
33+
)
34+
)
35+
36+
/* INTERNAL */
37+
38+
internal class MutableCountry {
39+
var name: String = ""
40+
var countryCode: String = ""
41+
var phoneCode: String = ""
42+
}
43+
44+
@Throws(IOException::class)
45+
internal fun parse(jsonParser: JsonParser): Country {
46+
val instance = MutableCountry()
47+
if (jsonParser.currentToken == null) {
48+
jsonParser.nextToken()
49+
}
50+
if (jsonParser.currentToken != JsonToken.START_OBJECT) {
51+
jsonParser.skipChildren()
52+
return EMPTY
53+
}
54+
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
55+
val fieldName = jsonParser.currentName
56+
jsonParser.nextToken()
57+
parseField(instance, fieldName, jsonParser)
58+
jsonParser.skipChildren()
59+
}
60+
return Country(instance.name, instance.countryCode, instance.phoneCode)
61+
}
62+
63+
@Throws(IOException::class)
64+
internal fun parseField(instance: MutableCountry, fieldName: String, jsonParser: JsonParser) {
65+
when (fieldName) {
66+
"country_code" -> instance.countryCode = jsonParser.getValueAsString(null)
67+
"name" -> instance.name = jsonParser.getValueAsString(null)
68+
"phone_code" -> instance.phoneCode = jsonParser.getValueAsString(null)
69+
}
70+
}
71+
72+
@Throws(IOException::class)
73+
internal fun parseList(jsonParser: JsonParser): List<Country> {
74+
jsonParser.nextToken()
75+
val list: MutableList<Country> = mutableListOf()
76+
if (jsonParser.currentToken == JsonToken.START_ARRAY) {
77+
while (jsonParser.nextToken() != JsonToken.END_ARRAY)
78+
list.add(parse(jsonParser))
79+
}
80+
return list
81+
}

countries/src/main/java/com/jetruby/common/countries/Country.java

-97
This file was deleted.

0 commit comments

Comments
 (0)