Skip to content

Commit 7bc3567

Browse files
MGaetan89thagikura
authored andcommitted
Convert Java files to Kotlin (#328)
* Convert Java files to Kotlin * Update following @thagikura's comments
1 parent 6aa475e commit 7bc3567

File tree

8 files changed

+183
-198
lines changed

8 files changed

+183
-198
lines changed

build.gradle

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,37 @@
1717
// Top-level build file where you can add configuration options common to all sub-projects/modules.
1818

1919
buildscript {
20+
ext {
21+
minSdkVersion = 9
22+
targetSdkVersion = 25
23+
compileSdkVersion = 25
24+
buildToolsVersion = "25.0.3"
25+
androidGradlePluginVersion = "2.3.3"
26+
androidMavenGradlePluginVersion = "1.5"
27+
gradleBintrayPluginVersion = "1.6"
28+
kotlinVersion = "1.1.3"
29+
supportLibVersion = "25.3.1"
30+
espressoVersion = "2.2.2"
31+
testRunnerVersion = "0.5"
32+
junitVersion = "4.12"
33+
}
34+
2035
repositories {
2136
jcenter()
2237
maven {
2338
url 'https://maven.google.com'
2439
}
2540
}
2641
dependencies {
27-
classpath 'com.android.tools.build:gradle:2.3.3'
28-
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
29-
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
42+
classpath "com.android.tools.build:gradle:$androidGradlePluginVersion"
43+
classpath "com.github.dcendents:android-maven-gradle-plugin:$androidMavenGradlePluginVersion"
44+
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$gradleBintrayPluginVersion"
45+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
3046
// NOTE: Do not place your application dependencies here; they belong
3147
// in the individual module build.gradle files
3248
}
3349
}
3450

35-
ext {
36-
minSdkVersion = 9
37-
targetSdkVersion = 25
38-
compileSdkVersion = 25
39-
buildToolsVersion = "25.0.3"
40-
supportLibVersion = "25.3.1"
41-
espressoVersion = "2.2.2"
42-
testRunnerVersion = "0.5"
43-
junitVersion = "4.12"
44-
}
45-
4651
allprojects {
4752
repositories {
4853
jcenter()
@@ -63,9 +68,9 @@ project.ext.preDexLibs = !project.hasProperty('disablePreDex')
6368

6469
subprojects {
6570
project.plugins.whenPluginAdded { plugin ->
66-
if ("com.android.build.gradle.AppPlugin".equals(plugin.class.name)) {
71+
if ("com.android.build.gradle.AppPlugin" == plugin.class.name) {
6772
project.android.dexOptions.preDexLibraries = rootProject.ext.preDexLibs
68-
} else if ("com.android.build.gradle.LibraryPlugin".equals(plugin.class.name)) {
73+
} else if ("com.android.build.gradle.LibraryPlugin" == plugin.class.name) {
6974
project.android.dexOptions.preDexLibraries = rootProject.ext.preDexLibs
7075
}
7176
}

demo-cat-gallery/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
apply plugin: 'com.android.application'
18+
apply plugin: 'kotlin-android'
1819

1920
android {
2021
compileSdkVersion rootProject.ext.compileSdkVersion
@@ -39,8 +40,9 @@ android {
3940
}
4041

4142
dependencies {
43+
compile project(path: ":flexbox")
4244
compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
4345
compile "com.android.support:design:${rootProject.ext.supportLibVersion}"
4446
compile "com.android.support:recyclerview-v7:${rootProject.ext.supportLibVersion}"
45-
compile project(path: ":flexbox")
47+
compile "org.jetbrains.kotlin:kotlin-stdlib:${rootProject.ext.kotlinVersion}"
4648
}

demo-cat-gallery/src/main/java/com/google/android/flexbox/apps/catgallery/CatAdapter.java

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2017 Google Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.flexbox.apps.catgallery
18+
19+
import android.support.v7.widget.RecyclerView
20+
import android.view.LayoutInflater
21+
import android.view.ViewGroup
22+
23+
/**
24+
* Adapter class that handles the data set with the {@link RecyclerView.LayoutManager}
25+
*/
26+
internal class CatAdapter : RecyclerView.Adapter<CatViewHolder>() {
27+
28+
companion object {
29+
private val CAT_IMAGE_IDS = intArrayOf(
30+
R.drawable.cat_1,
31+
R.drawable.cat_2,
32+
R.drawable.cat_3,
33+
R.drawable.cat_4,
34+
R.drawable.cat_5,
35+
R.drawable.cat_6,
36+
R.drawable.cat_7,
37+
R.drawable.cat_8,
38+
R.drawable.cat_9,
39+
R.drawable.cat_10,
40+
R.drawable.cat_11,
41+
R.drawable.cat_12,
42+
R.drawable.cat_13,
43+
R.drawable.cat_14,
44+
R.drawable.cat_15,
45+
R.drawable.cat_16,
46+
R.drawable.cat_17,
47+
R.drawable.cat_18,
48+
R.drawable.cat_19
49+
)
50+
}
51+
52+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CatViewHolder {
53+
val view = LayoutInflater.from(parent.context)
54+
.inflate(R.layout.viewholder_cat, parent, false)
55+
return CatViewHolder(view)
56+
}
57+
58+
override fun onBindViewHolder(holder: CatViewHolder, position: Int) {
59+
val pos = position % CAT_IMAGE_IDS.size
60+
holder.bindTo(CAT_IMAGE_IDS[pos])
61+
}
62+
63+
override fun getItemCount() = CAT_IMAGE_IDS.size * 4
64+
}

demo-cat-gallery/src/main/java/com/google/android/flexbox/apps/catgallery/CatViewHolder.java

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2017 Google Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.flexbox.apps.catgallery
18+
19+
import android.support.annotation.DrawableRes
20+
import android.support.v7.widget.RecyclerView
21+
import android.view.View
22+
import android.widget.ImageView
23+
import com.google.android.flexbox.FlexboxLayoutManager
24+
25+
/**
26+
* ViewHolder that represents a cat image.
27+
*/
28+
internal class CatViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
29+
30+
private val imageView = itemView.findViewById(R.id.imageview) as ImageView
31+
32+
internal fun bindTo(@DrawableRes drawableRes: Int) {
33+
imageView.setImageResource(drawableRes)
34+
val lp = imageView.layoutParams
35+
if (lp is FlexboxLayoutManager.LayoutParams) {
36+
lp.flexGrow = 1f
37+
}
38+
}
39+
}

demo-cat-gallery/src/main/java/com/google/android/flexbox/apps/catgallery/MainActivity.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)