Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public void getProductById() throws InterruptedException {
assertThat(product.getId(), is(PRODUCT_ENTITY.getId()));
assertThat(product.getName(), is(PRODUCT_ENTITY.getName()));
assertThat(product.getDescription(), is(PRODUCT_ENTITY.getDescription()));
assertThat(product.getCategory(), is(PRODUCT_ENTITY.getCategory()));
assertThat(product.getPrice(), is(PRODUCT_ENTITY.getPrice()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
public class TestData {

static final ProductEntity PRODUCT_ENTITY = new ProductEntity(1, "name", "desc",
3);
3, "category");
static final ProductEntity PRODUCT_ENTITY2 = new ProductEntity(2, "name2", "desc2",
20);
20, "category");

static final List<ProductEntity> PRODUCTS = Arrays.asList(PRODUCT_ENTITY, PRODUCT_ENTITY2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
import com.example.android.persistence.db.entity.ProductFtsEntity;
import java.util.List;

@Database(entities = {ProductEntity.class, ProductFtsEntity.class, CommentEntity.class}, version = 2)
@Database(entities = {ProductEntity.class, ProductFtsEntity.class, CommentEntity.class}, version = 3)
@TypeConverters(DateConverter.class)

public abstract class AppDatabase extends RoomDatabase {

private static AppDatabase sInstance;
Expand Down Expand Up @@ -91,7 +92,7 @@ public void onCreate(@NonNull SupportSQLiteDatabase db) {
});
}
})
.addMigrations(MIGRATION_1_2)
.addMigrations(MIGRATION_2_3)
.build();
}

Expand Down Expand Up @@ -127,14 +128,14 @@ public LiveData<Boolean> getDatabaseCreated() {
return mIsDatabaseCreated;
}

private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
private static final Migration MIGRATION_2_3 = new Migration(1, 3) {

@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("CREATE VIRTUAL TABLE IF NOT EXISTS `productsFts` USING FTS4("
+ "`name` TEXT, `description` TEXT, content=`products`)");
database.execSQL("INSERT INTO productsFts (`rowid`, `name`, `description`) "
+ "SELECT `id`, `name`, `description` FROM products");
+ "`name` TEXT, `description` TEXT, `category` TEXT, content=`products`)");
database.execSQL("INSERT INTO productsFts (`rowid`, `name`, `description`, `category`) "
+ "SELECT `id`, `name`, `description`, `category` FROM products");

}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,20 @@ public class DataGenerator {
"is the best sold product on Mêlée Island", "is \uD83D\uDCAF", "is ❤️", "is fine"};
private static final String[] COMMENTS = new String[]{
"Comment 1", "Comment 2", "Comment 3", "Comment 4", "Comment 5", "Comment 6"};
private static final String[] CATEGORIES = new String[]{
"category-1", "category-2", "category-3", "category-4"};

public static List<ProductEntity> generateProducts() {
List<ProductEntity> products = new ArrayList<>(FIRST.length * SECOND.length);
Random rnd = new Random();
for (int i = 0; i < FIRST.length; i++) {
for (int j = 0; j < SECOND.length; j++) {
ProductEntity product = new ProductEntity();
product.setName(FIRST[i] + " " + SECOND[j]);
product.setName(FIRST[i] + " " + SECOND[j] + " " + CATEGORIES[rnd.nextInt(3)]);
product.setDescription(product.getName() + " " + DESCRIPTION[j]);
product.setPrice(rnd.nextInt(240));
product.setId(FIRST.length * i + j + 1);
product.setCategory(CATEGORIES[rnd.nextInt(3)]);
products.add(product);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class ProductEntity implements Product {
private String name;
private String description;
private int price;
private String category;



@Override
public int getId() {
Expand Down Expand Up @@ -65,21 +68,27 @@ public void setPrice(int price) {
this.price = price;
}

public String getCategory() { return category; }

public void setCategory(String category) { this.category = category; }

public ProductEntity() {
}

@Ignore
public ProductEntity(int id, String name, String description, int price) {
public ProductEntity(int id, String name, String description, int price, String category) {
this.id = id;
this.name = name;
this.description = description;
this.price = price;
this.category = category;
}

public ProductEntity(Product product) {
this.id = product.getId();
this.name = product.getName();
this.description = product.getDescription();
this.price = product.getPrice();
this.category = product.getCategory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
public class ProductFtsEntity {
private String name;
private String description;
private String category;

public ProductFtsEntity(String name, String description) {
public ProductFtsEntity(String name, String description, String category) {
this.name = name;
this.description = description;
this.category = category;
}

public String getName() {
Expand All @@ -37,4 +39,8 @@ public String getName() {
public String getDescription() {
return description;
}

public String getCategory() {
return category;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ public interface Product {
String getName();
String getDescription();
int getPrice();
String getCategory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return newProduct.getId() == oldProduct.getId()
&& TextUtils.equals(newProduct.getDescription(), oldProduct.getDescription())
&& TextUtils.equals(newProduct.getName(), oldProduct.getName())
&& TextUtils.equals(newProduct.getCategory(), oldProduct.getCategory())
&& newProduct.getPrice() == oldProduct.getPrice();
}
});
Expand Down