Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor PokemonAdapter and Cleanup Gradle #46

Merged
merged 3 commits into from
Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,37 @@ android {
}
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}

kotlinOptions {
jvmTarget = "11"
}

buildFeatures {
dataBinding true
}

lintOptions {
abortOnError false
}

sourceSets {
androidTest.java.srcDirs += "src/test-common/java"
test.java.srcDirs += "src/test-common/java"
test.assets.srcDirs += files("$projectDir/schemas".toString())
}

testOptions {
unitTests {
includeAndroidResources = true
returnDefaultValues = true
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
exclude("META-INF/*.kotlin_module")
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions.freeCompilerArgs += ["-Xopt-in=kotlin.time.ExperimentalTime"]
kotlinOptions.freeCompilerArgs += ["-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi"]
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

<activity
android:name=".ui.main.MainActivity"
android:exported="true"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ class PokemonAdapter : BindingListAdapter<Pokemon, PokemonAdapter.PokemonViewHol

private var onClickedAt = 0L

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PokemonViewHolder {
val binding = parent.binding<ItemPokemonBinding>(R.layout.item_pokemon)
return PokemonViewHolder(binding).apply {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PokemonViewHolder =
parent.binding<ItemPokemonBinding>(R.layout.item_pokemon).let(::PokemonViewHolder)

override fun onBindViewHolder(holder: PokemonViewHolder, position: Int) =
holder.bindPokemon(getItem(position))

inner class PokemonViewHolder constructor(
private val binding: ItemPokemonBinding
) : RecyclerView.ViewHolder(binding.root) {

init {
binding.root.setOnClickListener {
val position = bindingAdapterPosition.takeIf { it != NO_POSITION }
?: return@setOnClickListener
Expand All @@ -45,18 +53,13 @@ class PokemonAdapter : BindingListAdapter<Pokemon, PokemonAdapter.PokemonViewHol
}
}
}
}

override fun onBindViewHolder(holder: PokemonViewHolder, position: Int) {
holder.binding.apply {
pokemon = getItem(position)
executePendingBindings()
fun bindPokemon(pokemon: Pokemon) {
binding.pokemon = pokemon
binding.executePendingBindings()
}
}

class PokemonViewHolder(val binding: ItemPokemonBinding) :
RecyclerView.ViewHolder(binding.root)

companion object {
private val diffUtil = object : DiffUtil.ItemCallback<Pokemon>() {

Expand Down
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// */
ext.versions = [
minSdk : 21,
compileSdk : 30,
compileSdk : 31,
versionCode : 6,
versionName : '1.0.5',

Expand Down