diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/room/NoteDatabase.java b/app/src/main/java/org/secuso/privacyfriendlynotes/room/NoteDatabase.java index 94ec1f90..cf5edf12 100644 --- a/app/src/main/java/org/secuso/privacyfriendlynotes/room/NoteDatabase.java +++ b/app/src/main/java/org/secuso/privacyfriendlynotes/room/NoteDatabase.java @@ -47,7 +47,7 @@ ) public abstract class NoteDatabase extends RoomDatabase { - public static final int VERSION = 4; + public static final int VERSION = 5; public static final String DATABASE_NAME = "allthenotes"; private static NoteDatabase instance; @@ -93,6 +93,43 @@ public void onCreate(@NonNull SupportSQLiteDatabase db) { } }; + static final Migration MIGRATION_4_5 = new Migration(4,5) { + + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + + // Adds new color field + database.execSQL("ALTER TABLE categories ADD COLUMN color TEXT"); + + // Adds new fields to sort by + database.execSQL( + "CREATE TABLE notes_new (_id INTEGER NOT NULL DEFAULT 0," + + "in_trash INTEGER NOT NULL DEFAULT 0," + + "name TEXT NOT NULL DEFAULT 'TEXT'," + + "type INTEGER NOT NULL DEFAULT 0," + + "category INTEGER NOT NULL DEFAULT 0," + + "content TEXT NOT NULL DEFAULT 'TEXT'," + + "last_modified TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP," + + "custom_order INTEGER NOT NULL DEFAULT 0," + + "PRIMARY KEY(_id));"); + database.execSQL("INSERT INTO notes_new(_id, in_trash,name,type,category,content,custom_order) SELECT _id, in_trash,name,type,category,content,_id as custom_order FROM notes ORDER BY _id ASC;"); + database.execSQL("DROP TABLE notes;"); + database.execSQL("ALTER TABLE notes_new RENAME TO notes"); + database.execSQL( + "CREATE TRIGGER [UpdateLastModified] AFTER UPDATE ON notes FOR EACH ROW WHEN NEW.last_modified = OLD.last_modified " + + "BEGIN " + + "UPDATE notes SET last_modified = DateTime('now') WHERE _id=NEW._id; " + + "END;" + ); + database.execSQL( + "CREATE TRIGGER [InsertCustomOrder] AFTER INSERT ON notes FOR EACH ROW " + + "BEGIN " + + "UPDATE notes SET custom_order = _id WHERE _id=NEW._id; " + + "END;" + ); + } + }; + /** * Provides data migration from database version 3 to 4 which checks for an error in the previous * migration when a backup was imported @@ -233,6 +270,7 @@ public void migrate(SupportSQLiteDatabase database) { MIGRATION_1_2, MIGRATION_1_3, MIGRATION_2_3, - MIGRATION_3_4 + MIGRATION_3_4, + MIGRATION_4_5 }; } diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/room/model/Category.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/room/model/Category.kt index 0065267d..f56563a5 100644 --- a/app/src/main/java/org/secuso/privacyfriendlynotes/room/model/Category.kt +++ b/app/src/main/java/org/secuso/privacyfriendlynotes/room/model/Category.kt @@ -24,11 +24,19 @@ import androidx.room.PrimaryKey data class Category( @PrimaryKey(autoGenerate = true) val _id: Int, - val name: String) { + val name: String, + val color: String? +) { - constructor(name: String) : this( + constructor(name: String): this( name = name, - _id = 0 + _id = 0, + color = null + ) + constructor(name: String, color: String?) : this( + name = name, + _id = 0, + color = color ) } \ No newline at end of file diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/room/model/Note.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/room/model/Note.kt index 491d1652..bf4bf864 100644 --- a/app/src/main/java/org/secuso/privacyfriendlynotes/room/model/Note.kt +++ b/app/src/main/java/org/secuso/privacyfriendlynotes/room/model/Note.kt @@ -13,8 +13,11 @@ */ package org.secuso.privacyfriendlynotes.room.model +import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey +import java.time.Instant +import java.util.Calendar /** * Provides note class with variables and constructor. @@ -28,7 +31,11 @@ data class Note( var content: String, var type: Int, var category: Int, - var in_trash: Int = 0) { + var in_trash: Int = 0, + @ColumnInfo(defaultValue = "CURRENT_TIMESTAMP") + var last_modified: String, + var custom_order: Int +) { constructor(name: String, content: String, type: Int, category: Int) : this( name = name, @@ -36,6 +43,8 @@ data class Note( type = type, category = category, in_trash = 0, - _id = 0 + _id = 0, + last_modified = Calendar.getInstance().time.toString(), + custom_order = 0 ) } \ No newline at end of file