Convert TaskAttachmentDao to Room

pull/618/head
Alex Baker 7 years ago
parent 84f71d0a8b
commit 2789ccc239

@ -0,0 +1,228 @@
{
"formatVersion": 1,
"database": {
"version": 42,
"identityHash": "13b646cebfd8c5994c08ad9e7d37661b",
"entities": [
{
"tableName": "notification",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `task` INTEGER NOT NULL, `timestamp` INTEGER NOT NULL, `type` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "uid",
"columnName": "uid",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "taskId",
"columnName": "task",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "timestamp",
"columnName": "timestamp",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "type",
"columnName": "type",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"uid"
],
"autoGenerate": true
},
"indices": [
{
"name": "index_notification_task",
"unique": true,
"columnNames": [
"task"
],
"createSql": "CREATE UNIQUE INDEX `index_notification_task` ON `${TABLE_NAME}` (`task`)"
}
],
"foreignKeys": []
},
{
"tableName": "tagdata",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`_id` INTEGER PRIMARY KEY AUTOINCREMENT, `remoteId` TEXT, `name` TEXT, `color` INTEGER, `tagOrdering` TEXT, `deleted` INTEGER)",
"fields": [
{
"fieldPath": "id",
"columnName": "_id",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "remoteId",
"columnName": "remoteId",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "color",
"columnName": "color",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "tagOrdering",
"columnName": "tagOrdering",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "deleted",
"columnName": "deleted",
"affinity": "INTEGER",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"_id"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "userActivity",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`_id` INTEGER PRIMARY KEY AUTOINCREMENT, `remoteId` TEXT, `action` TEXT, `message` TEXT, `picture` TEXT, `target_id` TEXT, `created_at` INTEGER, `deleted_at` INTEGER)",
"fields": [
{
"fieldPath": "id",
"columnName": "_id",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "remoteId",
"columnName": "remoteId",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "action",
"columnName": "action",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "message",
"columnName": "message",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "picture",
"columnName": "picture",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "targetId",
"columnName": "target_id",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "created",
"columnName": "created_at",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "deleted",
"columnName": "deleted_at",
"affinity": "INTEGER",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"_id"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "task_attachments",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`_id` INTEGER PRIMARY KEY AUTOINCREMENT, `remoteId` TEXT, `task_id` TEXT, `name` TEXT, `path` TEXT, `content_type` TEXT, `deleted_at` INTEGER)",
"fields": [
{
"fieldPath": "id",
"columnName": "_id",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "remoteId",
"columnName": "remoteId",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "taskId",
"columnName": "task_id",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "path",
"columnName": "path",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "contentType",
"columnName": "content_type",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "deleted",
"columnName": "deleted_at",
"affinity": "INTEGER",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"_id"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
}
],
"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, \"13b646cebfd8c5994c08ad9e7d37661b\")"
]
}
}

@ -40,14 +40,16 @@ import timber.log.Timber;
entities = {
Notification.class,
TagData.class,
UserActivity.class
UserActivity.class,
TaskAttachment.class
},
version = 41)
version = 42)
public abstract class Database extends RoomDatabase {
public abstract NotificationDao notificationDao();
public abstract TagDataDao getTagDataDao();
public abstract UserActivityDao getUserActivityDao();
public abstract TaskAttachmentDao getTaskAttachmentDao();
public static final String NAME = "database";
@ -55,7 +57,6 @@ public abstract class Database extends RoomDatabase {
Task.TABLE,
Metadata.TABLE,
StoreObject.TABLE,
TaskAttachment.TABLE,
TaskListMetadata.TABLE,
};

@ -5,48 +5,39 @@
*/
package com.todoroo.astrid.dao;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Query;
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.OnConflictStrategy;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Update;
import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.TaskAttachment;
import com.todoroo.astrid.helper.UUIDHelper;
import java.util.List;
import javax.inject.Inject;
@Dao
public abstract class TaskAttachmentDao {
public class TaskAttachmentDao {
@Query("SELECT * FROM task_attachments WHERE task_id = :taskUuid AND deleted_at = 0")
public abstract List<TaskAttachment> getAttachments(String taskUuid);
private final RemoteModelDao<TaskAttachment> dao;
@Delete
public abstract void delete(TaskAttachment taskAttachment);
@Inject
public TaskAttachmentDao(Database database) {
dao = new RemoteModelDao<>(database, TaskAttachment.class);
}
public boolean taskHasAttachments(String taskUuid) {
return dao.count(byUuid(taskUuid).limit(1)) > 0;
}
public List<TaskAttachment> getAttachments(String taskUuid) {
return dao.toList(byUuid(taskUuid));
}
@Insert(onConflict = OnConflictStrategy.REPLACE)
public abstract void insert(TaskAttachment attachment);
private static Query byUuid(String taskUuid) {
return Query.select(TaskAttachment.PROPERTIES).where(
Criterion.and(TaskAttachment.TASK_UUID.eq(taskUuid),
TaskAttachment.DELETED_AT.eq(0))
);
}
@Update
public abstract void update(TaskAttachment attachment);
public void createNew(TaskAttachment attachment) {
dao.createNew(attachment);
}
public void delete(long id) {
dao.delete(id);
}
public void saveExisting(TaskAttachment m) {
dao.saveExisting(m);
if (RemoteModel.isUuidEmpty(attachment.getRemoteId())) {
attachment.setRemoteId(UUIDHelper.newUUID());
}
insert(attachment);
}
}

@ -6,79 +6,46 @@
package com.todoroo.astrid.data;
import android.content.ContentValues;
import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import com.todoroo.andlib.data.AbstractModel;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.Property.LongProperty;
import com.todoroo.andlib.data.Property.StringProperty;
import com.todoroo.andlib.data.Table;
/**
* Data Model which represents a user.
*
* @author Tim Su <tim@todoroo.com>
*
*/
public final class TaskAttachment extends RemoteModel {
// --- table and uri
/** table for this model */
public static final Table TABLE = new Table("task_attachments", TaskAttachment.class);
// --- properties
/** ID */
public static final LongProperty ID = new LongProperty(
TABLE, ID_PROPERTY_NAME);
/** Remote id */
@SuppressWarnings("WeakerAccess")
public static final StringProperty UUID = new StringProperty(
TABLE, UUID_PROPERTY_NAME);
/** Task uuid */
public static final StringProperty TASK_UUID = new StringProperty(
TABLE, "task_id");
@Entity(tableName = "task_attachments")
public final class TaskAttachment {
/** File name */
@SuppressWarnings("WeakerAccess")
public static final StringProperty NAME = new StringProperty(
TABLE, "name");
@Deprecated
public static final Table TABLE = new Table("task_attachments", null);
/** File path (on local storage) */
public static final StringProperty FILE_PATH = new StringProperty(
TABLE, "path");
@Deprecated
public static final Property.LongProperty ID = new Property.LongProperty(
TABLE, "_id");
/** File mimetype */
public static final StringProperty CONTENT_TYPE = new StringProperty(
TABLE, "content_type");
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "_id")
private Long id;
/** Attachment deletion date */
public static final LongProperty DELETED_AT = new LongProperty(
TABLE, "deleted_at", Property.PROP_FLAG_DATE);
@ColumnInfo(name = "remoteId")
private String remoteId = RemoteModel.NO_UUID;
/** List of all properties for this model */
public static final Property<?>[] PROPERTIES = generateProperties(TaskAttachment.class);
@ColumnInfo(name = "task_id")
private String taskId = RemoteModel.NO_UUID;
// --- defaults
@ColumnInfo(name = "name")
private String name = "";
/** Default values container */
private static final ContentValues defaultValues = new ContentValues();
@ColumnInfo(name = "path")
private String path = "";
static {
defaultValues.put(UUID.name, NO_UUID);
defaultValues.put(TASK_UUID.name, NO_UUID);
defaultValues.put(NAME.name, "");
defaultValues.put(FILE_PATH.name, "");
defaultValues.put(CONTENT_TYPE.name, "");
defaultValues.put(DELETED_AT.name, 0);
}
@ColumnInfo(name = "content_type")
private String contentType = "";
@Override
public ContentValues getDefaultValues() {
return defaultValues;
}
@ColumnInfo(name = "deleted_at")
private Long deleted = 0L;
// -- Constants
/** default directory for files on external storage */
@ -92,52 +59,67 @@ public final class TaskAttachment extends RemoteModel {
public static TaskAttachment createNewAttachment(String taskUuid, String filePath, String fileName, String fileType) {
TaskAttachment attachment = new TaskAttachment();
attachment.setTaskUUID(taskUuid);
attachment.setTaskId(taskUuid);
attachment.setName(fileName);
attachment.setFilePath(filePath);
attachment.setPath(filePath);
attachment.setContentType(fileType);
attachment.setDeletedAt(0L);
attachment.setDeleted(0L);
return attachment;
}
@Override
public long getId() {
return getIdHelper(ID);
public Long getId() {
return id;
}
// --- parcelable helpers
public void setId(Long id) {
this.id = id;
}
public String getRemoteId() {
return remoteId;
}
public static final Creator<TaskAttachment> CREATOR = new ModelCreator<>(TaskAttachment.class);
public void setRemoteId(String remoteId) {
this.remoteId = remoteId;
}
private void setDeletedAt(Long deletedAt) {
setValue(DELETED_AT, deletedAt);
public String getTaskId() {
return taskId;
}
private void setTaskUUID(String taskUuid) {
setValue(TASK_UUID, taskUuid);
public void setTaskId(String taskId) {
this.taskId = taskId;
}
public String getName() {
return getValue(NAME);
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
private void setName(String name) {
setValue(NAME, name);
public void setPath(String path) {
this.path = path;
}
public String getContentType() {
return getValue(CONTENT_TYPE);
return contentType;
}
public void setContentType(String contentType) {
setValue(CONTENT_TYPE, contentType);
this.contentType = contentType;
}
public String getFilePath() {
return getValue(FILE_PATH);
public Long getDeleted() {
return deleted;
}
public void setFilePath(String filePath) {
setValue(FILE_PATH, filePath);
public void setDeleted(Long deleted) {
this.deleted = deleted;
}
}

@ -22,9 +22,9 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.common.base.Strings;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.astrid.dao.TaskAttachmentDao;
import com.todoroo.astrid.data.SyncFlags;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskAttachment;
@ -147,9 +147,9 @@ public class FilesControlSet extends TaskEditControlFragment {
View clearFile = fileRow.findViewById(R.id.clear);
clearFile.setOnClickListener(v -> dialogBuilder.newMessageDialog(R.string.premium_remove_file_confirm)
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
taskAttachmentDao.delete(taskAttachment.getId());
if (taskAttachment.containsNonNullValue(TaskAttachment.FILE_PATH)) {
File f = new File(taskAttachment.getFilePath());
taskAttachmentDao.delete(taskAttachment);
if (!Strings.isNullOrEmpty(taskAttachment.getPath())) {
File f = new File(taskAttachment.getPath());
f.delete();
}
attachmentContainer.removeView(fileRow);
@ -161,12 +161,12 @@ public class FilesControlSet extends TaskEditControlFragment {
private void validateFiles(List<TaskAttachment> files) {
for (int i = 0; i < files.size(); i++) {
TaskAttachment m = files.get(i);
if (m.containsNonNullValue(TaskAttachment.FILE_PATH)) {
File f = new File(m.getFilePath());
if (!Strings.isNullOrEmpty(m.getPath())) {
File f = new File(m.getPath());
if (!f.exists()) {
m.setFilePath(""); //$NON-NLS-1$
m.setPath(""); //$NON-NLS-1$
// No local file and no url -- delete the metadata
taskAttachmentDao.delete(m.getId());
taskAttachmentDao.delete(m);
files.remove(i);
i--;
}
@ -202,11 +202,11 @@ public class FilesControlSet extends TaskEditControlFragment {
@SuppressLint("NewApi")
private void showFile(final TaskAttachment m) {
final String fileType = m.containsNonNullValue(TaskAttachment.CONTENT_TYPE) ? m.getContentType() : TaskAttachment.FILE_TYPE_OTHER;
final String filePath = m.getFilePath();
final String fileType = !Strings.isNullOrEmpty(m.getContentType()) ? m.getContentType() : TaskAttachment.FILE_TYPE_OTHER;
final String filePath = m.getPath();
if (fileType.startsWith(TaskAttachment.FILE_TYPE_AUDIO)) {
play(m.getFilePath(), () -> showFromIntent(filePath, fileType));
play(m.getPath(), () -> showFromIntent(filePath, fileType));
} else if (fileType.startsWith(TaskAttachment.FILE_TYPE_IMAGE)) {
try {
Intent intent = FileHelper.getReadableActionView(context, filePath, TaskAttachment.FILE_TYPE_IMAGE + "*");
@ -227,8 +227,7 @@ public class FilesControlSet extends TaskEditControlFragment {
}
if (!useType.equals(guessedType)) {
m.setContentType(useType);
m.putTransitory(SyncFlags.ACTFM_SUPPRESS_OUTSTANDING_ENTRIES, true);
taskAttachmentDao.saveExisting(m);
taskAttachmentDao.update(m);
}
}
showFromIntent(filePath, useType);

@ -55,7 +55,7 @@ public class TaskListDataProvider {
String joinedQuery =
Join.left(Metadata.TABLE.as(TAGS_METADATA_JOIN),
tagsJoinCriterion).toString() //$NON-NLS-1$
+ Join.left(TaskAttachment.TABLE.as(FILE_METADATA_JOIN), Task.UUID.eq(Field.field(FILE_METADATA_JOIN + "." + TaskAttachment.TASK_UUID.name)))
+ Join.left(TaskAttachment.TABLE.as(FILE_METADATA_JOIN), Task.UUID.eq(Field.field(FILE_METADATA_JOIN + ".task_id")))
+ filter.getSqlQuery();
sqlQueryTemplate.set(SortHelper.adjustQueryForFlagsAndSort(

@ -44,27 +44,23 @@ public class Migrations {
}
};
private static final Migration MIGRATION_39_40 = new Migration(39, 40) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
}
};
private static Migration NOOP(int from, int to) {
return new Migration(from, to) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
private static final Migration MIGRATION_40_41 = new Migration(40, 41) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
}
};
}
};
}
public static Migration[] MIGRATIONS = new Migration[] {
MIGRATION_35_36,
MIGRATION_36_37,
MIGRATION_37_38,
MIGRATION_38_39,
MIGRATION_39_40,
MIGRATION_40_41
NOOP(39, 40),
NOOP(40, 41),
NOOP(41, 42)
};
public static RoomDatabase.Callback ON_CREATE = new RoomDatabase.Callback() {

@ -5,6 +5,7 @@ import android.content.Context;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.dao.TaskAttachmentDao;
import com.todoroo.astrid.dao.UserActivityDao;
import com.todoroo.astrid.provider.Astrid2TaskProvider;
@ -73,4 +74,9 @@ public class ApplicationModule {
public UserActivityDao getUserActivityDao(Database database) {
return database.getUserActivityDao();
}
@Provides
public TaskAttachmentDao getTaskAttachmentDao(Database database) {
return database.getTaskAttachmentDao();
}
}

Loading…
Cancel
Save