Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit cdb47fd

Browse files
authored
Merge pull request #961 from dlam/dlam/update-paging
Update PagingSample with ViewBinding and ActivityScenario
2 parents d41f219 + 349ea8f commit cdb47fd

File tree

4 files changed

+70
-96
lines changed

4 files changed

+70
-96
lines changed

PagingSample/app/build.gradle

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515
*/
1616

1717
apply plugin: 'com.android.application'
18-
1918
apply plugin: 'kotlin-android'
20-
21-
apply plugin: 'kotlin-android-extensions'
22-
2319
apply plugin: 'kotlin-kapt'
20+
2421
android {
2522
compileSdkVersion build_versions.compile_sdk
2623
buildToolsVersion build_versions.build_tools
@@ -32,16 +29,23 @@ android {
3229
versionName "1.0"
3330
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
3431
}
32+
33+
buildFeatures {
34+
viewBinding = true
35+
}
36+
3537
buildTypes {
3638
release {
3739
minifyEnabled false
3840
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
3941
}
4042
}
43+
4144
compileOptions {
4245
sourceCompatibility JavaVersion.VERSION_1_8
4346
targetCompatibility JavaVersion.VERSION_1_8
4447
}
48+
4549
kotlinOptions {
4650
jvmTarget = "1.8"
4751
freeCompilerArgs += ["-Xopt-in=kotlin.RequiresOptIn"]
@@ -63,8 +67,8 @@ dependencies {
6367
androidTestImplementation deps.atsl.ext_junit
6468
androidTestImplementation deps.atsl.runner
6569
androidTestImplementation deps.atsl.rules
66-
androidTestImplementation deps.room.testing
6770
androidTestImplementation deps.arch_core.testing
71+
androidTestImplementation deps.room.testing
6872

6973
kapt deps.room.compiler
7074
}

PagingSample/app/src/androidTest/java/paging/android/example/com/pagingsample/MainActivityTest.java

-81
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2017 The Android Open Source Project
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+
package paging.android.example.com.pagingsample
17+
18+
import android.content.Intent
19+
import androidx.recyclerview.widget.RecyclerView
20+
import androidx.test.core.app.ActivityScenario
21+
import androidx.test.core.app.ApplicationProvider
22+
import androidx.test.ext.junit.runners.AndroidJUnit4
23+
import org.hamcrest.CoreMatchers
24+
import org.hamcrest.MatcherAssert
25+
import org.junit.Test
26+
import org.junit.runner.RunWith
27+
import java.util.concurrent.TimeoutException
28+
29+
/**
30+
* Simply sanity test to ensure that activity launches without any issues and shows some data.
31+
*/
32+
@RunWith(AndroidJUnit4::class)
33+
class MainActivityTest {
34+
35+
@Test
36+
@Throws(InterruptedException::class, TimeoutException::class)
37+
fun showSomeResults() {
38+
val intent = Intent(ApplicationProvider.getApplicationContext(), MainActivity::class.java)
39+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
40+
val scenario = ActivityScenario.launch<MainActivity>(intent)
41+
42+
scenario.onActivity { activity ->
43+
val recyclerView: RecyclerView = activity.binding.cheeseList
44+
MatcherAssert.assertThat(recyclerView.adapter, CoreMatchers.notNullValue())
45+
MatcherAssert.assertThat(recyclerView.adapter!!.itemCount > 0, CoreMatchers.`is`(true))
46+
}
47+
}
48+
}

PagingSample/app/src/main/java/paging/android/example/com/pagingsample/MainActivity.kt

+13-10
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import androidx.appcompat.app.AppCompatActivity
2424
import androidx.lifecycle.lifecycleScope
2525
import androidx.recyclerview.widget.ItemTouchHelper
2626
import androidx.recyclerview.widget.RecyclerView
27-
import kotlinx.android.synthetic.main.activity_main.*
2827
import kotlinx.coroutines.flow.collectLatest
2928
import kotlinx.coroutines.launch
29+
import paging.android.example.com.pagingsample.databinding.ActivityMainBinding
3030

3131
/**
3232
* Shows a list of Cheeses, with swipe-to-delete, and an input field at the top to add.
@@ -35,15 +35,18 @@ import kotlinx.coroutines.launch
3535
* is updated automatically using paging components.
3636
*/
3737
class MainActivity : AppCompatActivity() {
38+
lateinit var binding: ActivityMainBinding
39+
private set
3840
private val viewModel by viewModels<CheeseViewModel>()
3941

4042
override fun onCreate(savedInstanceState: Bundle?) {
4143
super.onCreate(savedInstanceState)
42-
setContentView(R.layout.activity_main)
44+
binding = ActivityMainBinding.inflate(layoutInflater)
45+
setContentView(binding.root)
4346

4447
// Create adapter for the RecyclerView
4548
val adapter = CheeseAdapter()
46-
cheeseList.adapter = adapter
49+
binding.cheeseList.adapter = adapter
4750

4851
// Subscribe the adapter to the ViewModel, so the items in the adapter are refreshed
4952
// when the list changes
@@ -60,7 +63,7 @@ class MainActivity : AppCompatActivity() {
6063
// enable the items to swipe to the left or right
6164
override fun getMovementFlags(recyclerView: RecyclerView,
6265
viewHolder: RecyclerView.ViewHolder): Int =
63-
makeMovementFlags(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)
66+
makeMovementFlags(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)
6467

6568
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder,
6669
target: RecyclerView.ViewHolder): Boolean = false
@@ -72,32 +75,32 @@ class MainActivity : AppCompatActivity() {
7275
viewModel.remove(it)
7376
}
7477
}
75-
}).attachToRecyclerView(cheeseList)
78+
}).attachToRecyclerView(binding.cheeseList)
7679
}
7780

7881
private fun addCheese() {
79-
val newCheese = inputText.text.trim()
82+
val newCheese = binding.inputText.text.trim()
8083
if (newCheese.isNotEmpty()) {
8184
viewModel.insert(newCheese)
82-
inputText.setText("")
85+
binding.inputText.setText("")
8386
}
8487
}
8588

8689
private fun initAddButtonListener() {
87-
addButton.setOnClickListener {
90+
binding.addButton.setOnClickListener {
8891
addCheese()
8992
}
9093

9194
// when the user taps the "Done" button in the on screen keyboard, save the item.
92-
inputText.setOnEditorActionListener { _, actionId, _ ->
95+
binding.inputText.setOnEditorActionListener { _, actionId, _ ->
9396
if (actionId == EditorInfo.IME_ACTION_DONE) {
9497
addCheese()
9598
return@setOnEditorActionListener true
9699
}
97100
false // action that isn't DONE occurred - ignore
98101
}
99102
// When the user clicks on the button, or presses enter, save the item.
100-
inputText.setOnKeyListener { _, keyCode, event ->
103+
binding.inputText.setOnKeyListener { _, keyCode, event ->
101104
if (event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
102105
addCheese()
103106
return@setOnKeyListener true

0 commit comments

Comments
 (0)