Skip to content

Commit 078b353

Browse files
authored
Merge pull request #498 from danysantiago/room-2.1.0
Add Fts Usage to BasicSample
2 parents b9e8898 + c0d72f2 commit 078b353

File tree

23 files changed

+195
-21
lines changed

23 files changed

+195
-21
lines changed

BasicRxJavaSample/versions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
ext.deps = [:]
2424
def versions = [:]
2525
versions.arch_core = "2.0.0"
26-
versions.room = "2.0.0"
26+
versions.room = "2.1.0-alpha02"
2727
versions.lifecycle = "2.0.0"
2828
versions.support = "1.0.0"
2929
versions.dagger = "2.16"

BasicRxJavaSampleKotlin/versions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
ext.deps = [:]
2424
def versions = [:]
2525
versions.arch_core = "2.0.0"
26-
versions.room = "2.0.0"
26+
versions.room = "2.1.0-alpha02"
2727
versions.lifecycle = "2.0.0"
2828
versions.support = "1.0.0"
2929
versions.dagger = "2.16"

BasicSample/app/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ android {
2727
versionCode 1
2828
versionName "1.0"
2929
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
30+
31+
vectorDrawables {
32+
useSupportLibrary = true
33+
}
3034
}
3135

3236
buildTypes {

BasicSample/app/src/main/java/com/example/android/persistence/DataRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@ public LiveData<ProductEntity> loadProduct(final int productId) {
5555
public LiveData<List<CommentEntity>> loadComments(final int productId) {
5656
return mDatabase.commentDao().loadComments(productId);
5757
}
58+
59+
public LiveData<List<ProductEntity>> searchProducts(String query) {
60+
return mDatabase.productDao().searchAllProducts(query);
61+
}
5862
}

BasicSample/app/src/main/java/com/example/android/persistence/db/AppDatabase.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import androidx.lifecycle.LiveData;
2020
import androidx.lifecycle.MutableLiveData;
21+
import androidx.room.migration.Migration;
2122
import androidx.sqlite.db.SupportSQLiteDatabase;
2223
import androidx.room.Database;
2324
import androidx.room.Room;
@@ -33,9 +34,10 @@
3334
import com.example.android.persistence.db.entity.CommentEntity;
3435
import com.example.android.persistence.db.entity.ProductEntity;
3536

37+
import com.example.android.persistence.db.entity.ProductFtsEntity;
3638
import java.util.List;
3739

38-
@Database(entities = {ProductEntity.class, CommentEntity.class}, version = 1)
40+
@Database(entities = {ProductEntity.class, ProductFtsEntity.class, CommentEntity.class}, version = 2)
3941
@TypeConverters(DateConverter.class)
4042
public abstract class AppDatabase extends RoomDatabase {
4143

@@ -88,7 +90,9 @@ public void onCreate(@NonNull SupportSQLiteDatabase db) {
8890
database.setDatabaseCreated();
8991
});
9092
}
91-
}).build();
93+
})
94+
.addMigrations(MIGRATION_1_2)
95+
.build();
9296
}
9397

9498
/**
@@ -122,4 +126,16 @@ private static void addDelay() {
122126
public LiveData<Boolean> getDatabaseCreated() {
123127
return mIsDatabaseCreated;
124128
}
129+
130+
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
131+
132+
@Override
133+
public void migrate(@NonNull SupportSQLiteDatabase database) {
134+
database.execSQL("CREATE VIRTUAL TABLE IF NOT EXISTS `productsFts` USING FTS4("
135+
+ "`name` TEXT, `description` TEXT, content=`products`)");
136+
database.execSQL("INSERT INTO productsFts (`rowid`, `name`, `description`) "
137+
+ "SELECT `id`, `name`, `description` FROM products");
138+
139+
}
140+
};
125141
}

BasicSample/app/src/main/java/com/example/android/persistence/db/dao/ProductDao.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ public interface ProductDao {
3838

3939
@Query("select * from products where id = :productId")
4040
ProductEntity loadProductSync(int productId);
41+
42+
@Query("SELECT products.* FROM products JOIN productsFts ON (products.id = productsFts.rowid) "
43+
+ "WHERE productsFts MATCH :query")
44+
LiveData<List<ProductEntity>> searchAllProducts(String query);
4145
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2018, 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+
17+
package com.example.android.persistence.db.entity;
18+
19+
import androidx.room.Entity;
20+
import androidx.room.Fts4;
21+
22+
@Entity(tableName = "productsFts")
23+
@Fts4(contentEntity = ProductEntity.class)
24+
public class ProductFtsEntity {
25+
private String name;
26+
private String description;
27+
28+
public ProductFtsEntity(String name, String description) {
29+
this.name = name;
30+
this.description = description;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public String getDescription() {
38+
return description;
39+
}
40+
}

BasicSample/app/src/main/java/com/example/android/persistence/ui/ProductAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductV
3939

4040
public ProductAdapter(@Nullable ProductClickCallback clickCallback) {
4141
mProductClickCallback = clickCallback;
42+
setHasStableIds(true);
4243
}
4344

4445
public void setProductList(final List<? extends Product> productList) {
@@ -98,6 +99,11 @@ public int getItemCount() {
9899
return mProductList == null ? 0 : mProductList.size();
99100
}
100101

102+
@Override
103+
public long getItemId(int position) {
104+
return mProductList.get(position).getId();
105+
}
106+
101107
static class ProductViewHolder extends RecyclerView.ViewHolder {
102108

103109
final ProductItemBinding binding;

BasicSample/app/src/main/java/com/example/android/persistence/ui/ProductListFragment.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package com.example.android.persistence.ui;
1818

19+
import android.text.Editable;
20+
import android.view.View.OnClickListener;
1921
import androidx.lifecycle.Lifecycle;
22+
import androidx.lifecycle.LiveData;
2023
import androidx.lifecycle.Observer;
2124
import androidx.lifecycle.ViewModelProviders;
2225
import androidx.databinding.DataBindingUtil;
@@ -61,12 +64,24 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
6164
final ProductListViewModel viewModel =
6265
ViewModelProviders.of(this).get(ProductListViewModel.class);
6366

64-
subscribeUi(viewModel);
67+
mBinding.productsSearchBtn.setOnClickListener(new OnClickListener() {
68+
@Override
69+
public void onClick(View v) {
70+
Editable query = mBinding.productsSearchBox.getText();
71+
if (query == null || query.toString().isEmpty()) {
72+
subscribeUi(viewModel.getProducts());
73+
} else {
74+
subscribeUi(viewModel.searchProducts("*" + query + "*"));
75+
}
76+
}
77+
});
78+
79+
subscribeUi(viewModel.getProducts());
6580
}
6681

67-
private void subscribeUi(ProductListViewModel viewModel) {
82+
private void subscribeUi(LiveData<List<ProductEntity>> liveData) {
6883
// Update the list when the data changes
69-
viewModel.getProducts().observe(this, new Observer<List<ProductEntity>>() {
84+
liveData.observe(this, new Observer<List<ProductEntity>>() {
7085
@Override
7186
public void onChanged(@Nullable List<ProductEntity> myProducts) {
7287
if (myProducts != null) {

BasicSample/app/src/main/java/com/example/android/persistence/viewmodel/ProductListViewModel.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
import androidx.lifecycle.AndroidViewModel;
2121
import androidx.lifecycle.LiveData;
2222
import androidx.lifecycle.MediatorLiveData;
23-
2423
import com.example.android.persistence.BasicApp;
24+
import com.example.android.persistence.DataRepository;
2525
import com.example.android.persistence.db.entity.ProductEntity;
26-
2726
import java.util.List;
2827

2928
public class ProductListViewModel extends AndroidViewModel {
3029

30+
private final DataRepository mRepository;
31+
3132
// MediatorLiveData can observe other LiveData objects and react on their emissions.
3233
private final MediatorLiveData<List<ProductEntity>> mObservableProducts;
3334

@@ -38,8 +39,8 @@ public ProductListViewModel(Application application) {
3839
// set by default null, until we get data from the database.
3940
mObservableProducts.setValue(null);
4041

41-
LiveData<List<ProductEntity>> products = ((BasicApp) application).getRepository()
42-
.getProducts();
42+
mRepository = ((BasicApp) application).getRepository();
43+
LiveData<List<ProductEntity>> products = mRepository.getProducts();
4344

4445
// observe the changes of the products from the database and forward them
4546
mObservableProducts.addSource(products, mObservableProducts::setValue);
@@ -51,4 +52,8 @@ public ProductListViewModel(Application application) {
5152
public LiveData<List<ProductEntity>> getProducts() {
5253
return mObservableProducts;
5354
}
55+
56+
public LiveData<List<ProductEntity>> searchProducts(String query) {
57+
return mRepository.searchProducts(query);
58+
}
5459
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
9+
</vector>

BasicSample/app/src/main/res/layout/list_fragment.xml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
~ limitations under the License.
1616
-->
1717

18-
<layout xmlns:android="http://schemas.android.com/apk/res/android"
18+
<layout xmlns:tools="http://schemas.android.com/tools"
19+
xmlns:android="http://schemas.android.com/apk/res/android"
1920
xmlns:app="http://schemas.android.com/apk/res-auto">
2021

2122
<data>
@@ -30,6 +31,29 @@
3031
android:background="@color/cardview_light_background"
3132
android:orientation="vertical">
3233

34+
<LinearLayout
35+
android:layout_width="match_parent"
36+
android:layout_height="wrap_content"
37+
android:layout_marginStart="@dimen/item_horizontal_margin"
38+
android:layout_marginEnd="@dimen/item_horizontal_margin"
39+
android:orientation="horizontal">
40+
41+
<androidx.appcompat.widget.AppCompatEditText
42+
android:id="@+id/products_search_box"
43+
android:layout_width="0dp"
44+
android:layout_height="wrap_content"
45+
android:layout_weight="1"
46+
android:hint="@string/search_products_hint"/>
47+
48+
<ImageButton
49+
android:id="@+id/products_search_btn"
50+
android:layout_width="48dp"
51+
android:layout_height="48dp"
52+
android:contentDescription="@string/cd_search_products"
53+
app:srcCompat="@drawable/ic_search_black_24dp"/>
54+
55+
</LinearLayout>
56+
3357
<TextView
3458
android:id="@+id/loading_tv"
3559
android:layout_width="match_parent"

BasicSample/app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
<string name="no_comments">No comments</string>
2020
<string name="loading_comments">Loading comments...</string>
2121
<string name="loading_products">Loading products...</string>
22+
<string name="search_products_hint">Search Products...</string>
2223
<string name="cd_products_list">Products list</string>
2324
<string name="cd_comments_list">Comments list</string>
2425
<string name="cd_product_name">Name of the product</string>
26+
<string name="cd_search_products">Search products</string>
2527
</resources>

BasicSample/versions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
ext.deps = [:]
2424
def versions = [:]
2525
versions.arch_core = "2.0.0"
26-
versions.room = "2.0.0"
26+
versions.room = "2.1.0-alpha02"
2727
versions.lifecycle = "2.0.0"
2828
versions.support = "1.0.0"
2929
versions.dagger = "2.16"

GithubBrowserSample/versions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
ext.deps = [:]
2424
def versions = [:]
2525
versions.arch_core = "2.0.0"
26-
versions.room = "2.0.0"
26+
versions.room = "2.1.0-alpha02"
2727
versions.lifecycle = "2.0.0"
2828
versions.support = "1.0.0"
2929
versions.dagger = "2.16"

NavigationBasicSample/versions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
ext.deps = [:]
2424
def versions = [:]
2525
versions.arch_core = "2.0.0"
26-
versions.room = "2.0.0"
26+
versions.room = "2.1.0-alpha02"
2727
versions.lifecycle = "2.0.0"
2828
versions.support = "1.0.0"
2929
versions.dagger = "2.16"

PagingSample/versions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
ext.deps = [:]
2424
def versions = [:]
2525
versions.arch_core = "2.0.0"
26-
versions.room = "2.0.0"
26+
versions.room = "2.1.0-alpha02"
2727
versions.lifecycle = "2.0.0"
2828
versions.support = "1.0.0"
2929
versions.dagger = "2.16"

PagingWithNetworkSample/versions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
ext.deps = [:]
2424
def versions = [:]
2525
versions.arch_core = "2.0.0"
26-
versions.room = "2.0.0"
26+
versions.room = "2.1.0-alpha02"
2727
versions.lifecycle = "2.0.0"
2828
versions.support = "1.0.0"
2929
versions.dagger = "2.16"

PersistenceContentProviderSample/versions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
ext.deps = [:]
2424
def versions = [:]
2525
versions.arch_core = "2.0.0"
26-
versions.room = "2.0.0"
26+
versions.room = "2.1.0-alpha02"
2727
versions.lifecycle = "2.0.0"
2828
versions.support = "1.0.0"
2929
versions.dagger = "2.16"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"formatVersion": 1,
3+
"database": {
4+
"version": 5,
5+
"identityHash": "b2931d3e0c78dcc46b6aa80ce186b2dc",
6+
"entities": [
7+
{
8+
"tableName": "users",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`userid` TEXT NOT NULL, `username` TEXT, `last_update` INTEGER, PRIMARY KEY(`userid`))",
10+
"fields": [
11+
{
12+
"fieldPath": "mId",
13+
"columnName": "userid",
14+
"affinity": "TEXT",
15+
"notNull": true
16+
},
17+
{
18+
"fieldPath": "mUserName",
19+
"columnName": "username",
20+
"affinity": "TEXT",
21+
"notNull": false
22+
},
23+
{
24+
"fieldPath": "mDate",
25+
"columnName": "last_update",
26+
"affinity": "INTEGER",
27+
"notNull": false
28+
}
29+
],
30+
"primaryKey": {
31+
"columnNames": [
32+
"userid"
33+
],
34+
"autoGenerate": false
35+
},
36+
"indices": [],
37+
"foreignKeys": []
38+
}
39+
],
40+
"setupQueries": [
41+
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
42+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"b2931d3e0c78dcc46b6aa80ce186b2dc\")"
43+
]
44+
}
45+
}

0 commit comments

Comments
 (0)