Skip to content

Commit

Permalink
#4 Download Images from REST API (v2)
Browse files Browse the repository at this point in the history
- Added Image entity to Room
- Added REST integration
  • Loading branch information
Nya Elimu committed Mar 28, 2020
1 parent 30276d3 commit fd52b6a
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 75 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "ai.elimu.content_provider"
minSdkVersion 24
targetSdkVersion 29
versionCode 1000000
versionName "1.0.0-SNAPSHOT"
versionCode 1000001
versionName "1.0.1-SNAPSHOT"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
Expand Down
84 changes: 84 additions & 0 deletions app/schemas/ai.elimu.content_provider.room.db.RoomDb/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "2344eeef50e1c72dc29a552f597f8c6a",
"entities": [
{
"tableName": "Image",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`title` TEXT NOT NULL, `revisionNumber` INTEGER NOT NULL, `id` INTEGER, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "revisionNumber",
"columnName": "revisionNumber",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "StoryBook",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`title` TEXT NOT NULL, `description` TEXT, `revisionNumber` INTEGER NOT NULL, `id` INTEGER, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "description",
"columnName": "description",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "revisionNumber",
"columnName": "revisionNumber",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '2344eeef50e1c72dc29a552f597f8c6a')"
]
}
}
46 changes: 0 additions & 46 deletions app/schemas/ai.elimu.content_provider.room.db.RoomDb/1000000.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ai.elimu.content_provider.rest;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;

public interface ImagesService {

@GET("content/images")
Call<ResponseBody> listImages();
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
package ai.elimu.content_provider.room;

import ai.elimu.content_provider.room.entity.Image;
import ai.elimu.content_provider.room.entity.StoryBook;
import ai.elimu.model.gson.content.StoryBookGson;
import ai.elimu.model.gson.content.multimedia.ImageGson;

public class GsonToRoomConverter {

public static Image getImage(ImageGson imageGson) {
if (imageGson == null) {
return null;
} else {
Image image = new Image();

// BaseEntity
image.setId(imageGson.getId());

// Content
image.setRevisionNumber(imageGson.getRevisionNumber());

// Image
image.setTitle(imageGson.getTitle());

return image;
}
}

public static StoryBook getStoryBook(StoryBookGson storyBookGson) {
if (storyBookGson == null) {
return null;
} else {
StoryBook storyBook = new StoryBook();

// BaseEntity
storyBook.setId(storyBookGson.getId());

// Content
storyBook.setRevisionNumber(storyBookGson.getRevisionNumber());

// StoryBook
storyBook.setTitle(storyBookGson.getTitle());
storyBook.setDescription(storyBookGson.getDescription());

Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/ai/elimu/content_provider/room/dao/ImageDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ai.elimu.content_provider.room.dao;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

import ai.elimu.content_provider.room.entity.Image;

@Dao
public interface ImageDao {

@Insert
void insert(Image image);

@Query("SELECT * FROM Image i WHERE i.id = :id")
Image load(Long id);

@Query("SELECT * FROM Image")
List<Image> loadAll();

@Update
void update(Image image);
}
13 changes: 8 additions & 5 deletions app/src/main/java/ai/elimu/content_provider/room/db/RoomDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import ai.elimu.content_provider.BuildConfig;
import ai.elimu.content_provider.room.dao.ImageDao;
import ai.elimu.content_provider.room.dao.StoryBookDao;
import ai.elimu.content_provider.room.entity.Image;
import ai.elimu.content_provider.room.entity.StoryBook;

@Database(entities = {StoryBook.class}, version = BuildConfig.VERSION_CODE)
@Database(entities = {Image.class, StoryBook.class}, version = 1)
public abstract class RoomDb extends RoomDatabase {

public abstract ImageDao imageDao();

public abstract StoryBookDao storyBookDao();

private static volatile RoomDb INSTANCE;
Expand All @@ -33,7 +36,7 @@ public static RoomDb getDatabase(final Context context) {
"content_provider_db"
)
// .addMigrations(
// MIGRATION_1000000_1000001
// MIGRATION_1_2
// )
.build();
}
Expand All @@ -42,10 +45,10 @@ public static RoomDb getDatabase(final Context context) {
return INSTANCE;
}

// private static final Migration MIGRATION_1000000_1000001 = new Migration(1000000, 1000001) {
// private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
// @Override
// public void migrate(@NonNull SupportSQLiteDatabase database) {
// Log.i(getClass().getName(), "migrate (1000000 --> 1000001)");
// Log.i(getClass().getName(), "migrate (1 --> 2)");
// Calendar timestamp = Calendar.getInstance();
// String sql = "ALTER TABLE StoryBookLearningEvent ADD COLUMN timestamp INTEGER NOT NULL DEFAULT " + timestamp.getTimeInMillis();
// Log.i(getClass().getName(), "sql: " + sql);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ai.elimu.content_provider.room.entity;

import androidx.room.PrimaryKey;

/**
* For documentation, see https://github.com/elimu-ai/webapp/tree/master/src/main/java/ai/elimu/model
*/
public class BaseEntity {

/**
* Reflects the ID stored in the backend webapp's database. Therefore, {@code @PrimaryKey(autoGenerate = true)} is
* not used.
*/
@PrimaryKey
private Long id;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ai.elimu.content_provider.room.entity;

import androidx.annotation.NonNull;

/**
* For documentation, see https://github.com/elimu-ai/webapp/tree/master/src/main/java/ai/elimu/model
*/
public class Content extends BaseEntity {

@NonNull
private Integer revisionNumber;

public Integer getRevisionNumber() {
return revisionNumber;
}

public void setRevisionNumber(Integer revisionNumber) {
this.revisionNumber = revisionNumber;
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/ai/elimu/content_provider/room/entity/Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ai.elimu.content_provider.room.entity;

import androidx.annotation.NonNull;
import androidx.room.Entity;

/**
* For documentation, see https://github.com/elimu-ai/webapp/tree/master/src/main/java/ai/elimu/model
*/
@Entity
public class Image extends Content {

@NonNull
private String title;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,23 @@

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

/**
* For documentation, see https://github.com/elimu-ai/webapp/tree/master/src/main/java/ai/elimu/model
*/
@Entity
public class StoryBook {

/**
* The ID reflects the ID stored in the backend database. Therefore, {@code @PrimaryKey(autoGenerate = true)} is
* not used.
*/
@PrimaryKey
private Long id;
public class StoryBook extends Content {

@NonNull
private String title;

private String description;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@NonNull
public String getTitle() {
return title;
}

public void setTitle(@NonNull String title) {
public void setTitle(String title) {
this.title = title;
}

Expand Down
Loading

0 comments on commit fd52b6a

Please sign in to comment.