diff options
Diffstat (limited to 'app/src/main/java')
15 files changed, 146 insertions, 23 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/AccessControl.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/AccessControl.java index e2a3ccc3b..01612a952 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/model/AccessControl.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/AccessControl.java @@ -11,17 +11,23 @@ import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; import it.niedermann.nextcloud.deck.model.ocs.user.GroupMemberUIDs; @Entity(inheritSuperIndices = true, - indices = { - @Index(value = "accountId", name = "acl_accId"), - @Index("boardId") - }, - foreignKeys = { - @ForeignKey( - entity = Board.class, - parentColumns = "localId", - childColumns = "boardId", onDelete = ForeignKey.CASCADE - ) - } + indices = { + @Index(value = "accountId", name = "acl_accId"), + @Index("boardId") + }, + foreignKeys = { + @ForeignKey( + entity = Board.class, + parentColumns = "localId", + childColumns = "boardId", onDelete = ForeignKey.CASCADE + ), + @ForeignKey( + entity = Account.class, + parentColumns = "id", + childColumns = "accountId", onDelete = ForeignKey.CASCADE + + ) + } ) public class AccessControl extends AbstractRemoteEntity { diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/Attachment.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/Attachment.java index eaa50dfa1..8789e2ee7 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/model/Attachment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/Attachment.java @@ -19,6 +19,11 @@ import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; parentColumns = "localId", childColumns = "cardId", onDelete = ForeignKey.CASCADE + ), + @ForeignKey( + entity = Account.class, + parentColumns = "id", + childColumns = "accountId", onDelete = ForeignKey.CASCADE ) } ) diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/Board.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/Board.java index 27dcd1df8..ebe9805ed 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/model/Board.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/Board.java @@ -26,7 +26,12 @@ import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; @ForeignKey( entity = User.class, parentColumns = "localId", - childColumns = "ownerId", onDelete = ForeignKey.SET_NULL + childColumns = "ownerId", onDelete = ForeignKey.CASCADE + ), + @ForeignKey( + entity = Account.class, + parentColumns = "id", + childColumns = "accountId", onDelete = ForeignKey.CASCADE ) } ) diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/Card.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/Card.java index 9914377e0..a5650f124 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/model/Card.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/Card.java @@ -25,6 +25,11 @@ import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; entity = Stack.class, parentColumns = "localId", childColumns = "stackId", onDelete = ForeignKey.CASCADE + ), + @ForeignKey( + entity = Account.class, + parentColumns = "id", + childColumns = "accountId", onDelete = ForeignKey.CASCADE ) } ) diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/Label.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/Label.java index da24edb5a..5864823e4 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/model/Label.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/Label.java @@ -26,6 +26,11 @@ import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; parentColumns = "localId", childColumns = "boardId", onDelete = ForeignKey.CASCADE + ), + @ForeignKey( + entity = Account.class, + parentColumns = "id", + childColumns = "accountId", onDelete = ForeignKey.CASCADE ) } ) diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/Stack.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/Stack.java index 00f054605..5e75b1d31 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/model/Stack.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/Stack.java @@ -17,6 +17,11 @@ import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; entity = Board.class, parentColumns = "localId", childColumns = "boardId", onDelete = ForeignKey.CASCADE + ), + @ForeignKey( + entity = Account.class, + parentColumns = "id", + childColumns = "accountId", onDelete = ForeignKey.CASCADE ) } ) diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/User.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/User.java index fd318ce7a..87e613fb6 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/model/User.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/User.java @@ -1,6 +1,7 @@ package it.niedermann.nextcloud.deck.model; import androidx.room.Entity; +import androidx.room.ForeignKey; import androidx.room.Ignore; import androidx.room.Index; @@ -8,7 +9,18 @@ import java.io.Serializable; import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; -@Entity(inheritSuperIndices = true, indices = {@Index(value = "uid", name = "user_uid")}) +@Entity(inheritSuperIndices = true, + indices = { + @Index(value = "uid", name = "user_uid") + }, + foreignKeys = { + @ForeignKey( + entity = Account.class, + parentColumns = "id", + childColumns = "accountId", onDelete = ForeignKey.CASCADE + ) + } +) public class User extends AbstractRemoteEntity implements Serializable { diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/Activity.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/Activity.java index 863dff887..cd4fadc80 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/Activity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/Activity.java @@ -4,6 +4,7 @@ import androidx.room.Entity; import androidx.room.ForeignKey; import androidx.room.Index; +import it.niedermann.nextcloud.deck.model.Account; import it.niedermann.nextcloud.deck.model.Card; import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; @@ -18,6 +19,11 @@ import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; entity = Card.class, parentColumns = "localId", childColumns = "cardId", onDelete = ForeignKey.CASCADE + ), + @ForeignKey( + entity = Account.class, + parentColumns = "id", + childColumns = "accountId", onDelete = ForeignKey.CASCADE ) } ) diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/comment/DeckComment.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/comment/DeckComment.java index 8bba9740b..370850113 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/comment/DeckComment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/comment/DeckComment.java @@ -9,6 +9,7 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; +import it.niedermann.nextcloud.deck.model.Account; import it.niedermann.nextcloud.deck.model.Card; import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; @@ -28,6 +29,11 @@ import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; entity = DeckComment.class, parentColumns = "localId", childColumns = "parentId", onDelete = ForeignKey.CASCADE + ), + @ForeignKey( + entity = Account.class, + parentColumns = "id", + childColumns = "accountId", onDelete = ForeignKey.CASCADE ) } ) diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/projects/OcsProject.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/projects/OcsProject.java index 49bc7296d..2e163735e 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/projects/OcsProject.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/projects/OcsProject.java @@ -2,12 +2,14 @@ package it.niedermann.nextcloud.deck.model.ocs.projects; import androidx.annotation.NonNull; import androidx.room.Entity; +import androidx.room.ForeignKey; import androidx.room.Ignore; import androidx.room.Index; import java.util.ArrayList; import java.util.List; +import it.niedermann.nextcloud.deck.model.Account; import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; @Entity(inheritSuperIndices = true, @@ -15,6 +17,11 @@ import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; @Index(value = "accountId", name = "index_project_accID"), }, foreignKeys = { + @ForeignKey( + entity = Account.class, + parentColumns = "id", + childColumns = "accountId", onDelete = ForeignKey.CASCADE + ) } ) public class OcsProject extends AbstractRemoteEntity { diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/projects/OcsProjectResource.java b/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/projects/OcsProjectResource.java index cd63ccde4..9e9ea5910 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/projects/OcsProjectResource.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/model/ocs/projects/OcsProjectResource.java @@ -9,6 +9,7 @@ import androidx.room.RoomWarnings; import java.io.Serializable; +import it.niedermann.nextcloud.deck.model.Account; import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; @SuppressWarnings(RoomWarnings.INDEX_FROM_PARENT_IS_DROPPED) @@ -25,6 +26,11 @@ import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity; entity = OcsProject.class, parentColumns = "localId", childColumns = "projectId", onDelete = ForeignKey.CASCADE + ), + @ForeignKey( + entity = Account.class, + parentColumns = "id", + childColumns = "accountId", onDelete = ForeignKey.CASCADE ) } ) @@ -78,6 +84,7 @@ public class OcsProjectResource extends AbstractRemoteEntity implements Serializ /** * Caution: the Link might be a full url or only the relative path! + * * @return The link to the Resource */ @Nullable diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DeckDatabase.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DeckDatabase.java index 6f7ba45e0..34a0515e3 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DeckDatabase.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DeckDatabase.java @@ -95,6 +95,7 @@ import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.migration.Migra import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.migration.Migration_27_28; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.migration.Migration_28_29; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.migration.Migration_29_30; +import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.migration.Migration_30_31; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.migration.Migration_8_9; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.migration.Migration_9_10; @@ -133,7 +134,7 @@ import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.migration.Migra FilterWidgetSort.class, }, exportSchema = false, - version = 30 + version = 31 ) @TypeConverters({DateTypeConverter.class, EnumConverter.class}) public abstract class DeckDatabase extends RoomDatabase { @@ -184,6 +185,7 @@ public abstract class DeckDatabase extends RoomDatabase { .addMigrations(new Migration_27_28()) .addMigrations(new Migration_28_29()) .addMigrations(new Migration_29_30(context)) + .addMigrations(new Migration_30_31()) .fallbackToDestructiveMigration() .addCallback(ON_CREATE_CALLBACK) .build(); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/migration/Migration_30_31.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/migration/Migration_30_31.java new file mode 100644 index 000000000..d15c38daf --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/migration/Migration_30_31.java @@ -0,0 +1,59 @@ +package it.niedermann.nextcloud.deck.persistence.sync.adapters.db.migration; + +import android.database.Cursor; + +import androidx.annotation.NonNull; +import androidx.room.migration.Migration; +import androidx.sqlite.db.SupportSQLiteDatabase; + +import java.util.ArrayList; +import java.util.List; + +/** + * <a href="https://github.com/stefan-niedermann/nextcloud-deck/issues/923">Foreign keys don't cascade (Cards stay in the database after deleting an Account)</a> + */ +public class Migration_30_31 extends Migration { + + private static String[] TABLES = new String[] {"AccessControl", "Activity", "Attachment", "Board", "Card", "DeckComment", "Label", "OcsProject", "OcsProjectResource", "Stack", "User"}; + + public Migration_30_31() { + super(30, 31); + } + + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + for (String table : TABLES) { + Cursor cursor = database.query("SELECT sql FROM sqlite_master WHERE type='table' AND name=?", new String[]{table}); + cursor.moveToNext(); + String originalCreateStatement = cursor.getString(0); + List<String> indexCreates = new ArrayList<>(); + cursor = database.query("SELECT sql FROM SQLite_master WHERE type = 'index' AND tbl_name=?", new String[]{table}); + while (cursor.moveToNext()) { + indexCreates.add(cursor.getString(0)); + } + String newTableName = "`" + table + "_tmp`"; + String create = originalCreateStatement.replace("CREATE TABLE `" + table + "`", "CREATE TABLE " + newTableName); + create = create.substring(0, create.lastIndexOf(')')); + create += ", FOREIGN KEY(`accountId`) REFERENCES `Account`(`id`) ON DELETE CASCADE )"; + if ("Board".equals(table)) { + create = create.replace("SET NULL", "CASCADE"); + } + + // create copied table + database.execSQL(create); + // delete obsolete data from original one + database.execSQL("DELETE FROM `"+table+"` where accountId not in (select id from `Account`)"); + // copy remaining rows + database.execSQL("INSERT INTO "+newTableName+" select * from `"+table+"`"); + // remove old table + database.execSQL("DROP TABLE `"+table+"`"); + // let copied table take place of original one + database.execSQL("ALTER TABLE "+newTableName+" RENAME TO `"+table+"`"); + // recreate indices + for (String indexCreate : indexCreates) { + database.execSQL(indexCreate); + } + } + + } +} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/upcomingcards/UpcomingCardsViewModel.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/upcomingcards/UpcomingCardsViewModel.java index 2cff86806..e0ee8c558 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/upcomingcards/UpcomingCardsViewModel.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/upcomingcards/UpcomingCardsViewModel.java @@ -7,7 +7,6 @@ import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; import java.util.List; -import java.util.stream.Collectors; import it.niedermann.nextcloud.deck.api.ResponseCallback; import it.niedermann.nextcloud.deck.model.Account; @@ -16,8 +15,6 @@ import it.niedermann.nextcloud.deck.model.full.FullCard; import it.niedermann.nextcloud.deck.persistence.sync.SyncManager; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.WrappedLiveData; -import static androidx.lifecycle.Transformations.map; - @SuppressWarnings("WeakerAccess") public class UpcomingCardsViewModel extends AndroidViewModel { @@ -29,10 +26,7 @@ public class UpcomingCardsViewModel extends AndroidViewModel { } public LiveData<List<UpcomingCardsAdapterItem>> getUpcomingCards() { - // FIXME remove mapping after https://github.com/stefan-niedermann/nextcloud-deck/issues/923 - return map(this.syncManager.getCardsForUpcomingCards(), (cards) -> - cards.stream().filter(card -> card.getAccount() != null).collect(Collectors.toList()) - ); + return this.syncManager.getCardsForUpcomingCards(); } public void assignUser(@NonNull Account account, @NonNull Card card) { diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/upcoming/UpcomingWidgetFactory.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/upcoming/UpcomingWidgetFactory.java index 6a6d8ade3..7da242179 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/upcoming/UpcomingWidgetFactory.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/upcoming/UpcomingWidgetFactory.java @@ -12,7 +12,6 @@ import androidx.annotation.NonNull; import java.util.ArrayList; import java.util.List; import java.util.NoSuchElementException; -import java.util.stream.Collectors; import it.niedermann.android.util.DimensionUtil; import it.niedermann.nextcloud.deck.DeckLog; @@ -50,7 +49,7 @@ public class UpcomingWidgetFactory implements RemoteViewsService.RemoteViewsFact @Override public void onDataSetChanged() { try { - final List<UpcomingCardsAdapterItem> response = syncManager.getCardsForUpcomingCardsForWidget().stream().filter(card -> card.getAccount() != null).collect(Collectors.toList()); + final List<UpcomingCardsAdapterItem> response = syncManager.getCardsForUpcomingCardsForWidget(); DeckLog.verbose(UpcomingWidgetFactory.class.getSimpleName(), "with id", appWidgetId, "fetched", response.size(), "cards from the database."); data.clear(); data.addAll(UpcomingCardsUtil.addDueDateSeparators(context, response)); |