#831 Migrate from SQLiteOpenHelper to Room

Starting widgets
This commit is contained in:
Stefan Niedermann 2020-10-05 16:24:02 +02:00
parent f0b57a2ad8
commit 21ba77dc68
8 changed files with 71 additions and 21 deletions

View file

@ -564,11 +564,6 @@ public class NotesDatabase extends AbstractNotesDatabase {
return data;
}
public void removeSingleNoteWidget(int appWidgetId) {
final SQLiteDatabase db = getWritableDatabase();
db.delete(table_widget_single_notes, key_id + " = ?", new String[]{String.valueOf(appWidgetId)});
}
public void createOrUpdateSingleNoteWidgetData(@NonNull SingleNoteWidgetData data) throws SQLException {
validateAccountId(data.getAccountId());
final SQLiteDatabase db = getWritableDatabase();
@ -598,11 +593,6 @@ public class NotesDatabase extends AbstractNotesDatabase {
return data;
}
public void removeNoteListWidget(int appWidgetId) {
final SQLiteDatabase db = getWritableDatabase();
db.delete(table_widget_note_list, key_id + " = ?", new String[]{String.valueOf(appWidgetId)});
}
public void createOrUpdateNoteListWidgetData(@NonNull NoteListsWidgetData data) throws SQLException {
validateAccountId(data.getAccountId());
final SQLiteDatabase db = getWritableDatabase();

View file

@ -1,12 +1,9 @@
package it.niedermann.owncloud.notes.persistence;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.text.TextUtils;
@ -35,10 +32,14 @@ import it.niedermann.owncloud.notes.edit.EditNoteActivity;
import it.niedermann.owncloud.notes.persistence.dao.CategoryDao;
import it.niedermann.owncloud.notes.persistence.dao.LocalAccountDao;
import it.niedermann.owncloud.notes.persistence.dao.NoteDao;
import it.niedermann.owncloud.notes.persistence.dao.WidgetNotesListDao;
import it.niedermann.owncloud.notes.persistence.dao.WidgetSingleNoteDao;
import it.niedermann.owncloud.notes.persistence.entity.CategoryEntity;
import it.niedermann.owncloud.notes.persistence.entity.Converters;
import it.niedermann.owncloud.notes.persistence.entity.LocalAccountEntity;
import it.niedermann.owncloud.notes.persistence.entity.NoteEntity;
import it.niedermann.owncloud.notes.persistence.entity.WidgetNotesListEntity;
import it.niedermann.owncloud.notes.persistence.entity.WidgetSingleNoteEntity;
import it.niedermann.owncloud.notes.shared.model.Capabilities;
import it.niedermann.owncloud.notes.shared.model.CloudNote;
import it.niedermann.owncloud.notes.shared.model.DBNote;
@ -55,7 +56,9 @@ import static it.niedermann.owncloud.notes.widget.singlenote.SingleNoteWidget.up
entities = {
LocalAccountEntity.class,
NoteEntity.class,
CategoryEntity.class
CategoryEntity.class,
WidgetSingleNoteEntity.class,
WidgetNotesListEntity.class
}, version = 18
)
@TypeConverters({Converters.class})
@ -102,10 +105,10 @@ public abstract class NotesRoomDatabase extends RoomDatabase {
};
public abstract LocalAccountDao getLocalAccountDao();
public abstract CategoryDao getCategoryDao();
public abstract NoteDao getNoteDao();
public abstract WidgetSingleNoteDao getWidgetSingleNoteDao();
public abstract WidgetNotesListDao getWidgetNotesListDao();
@SuppressWarnings("UnusedReturnValue")
public long addAccount(@NonNull String url, @NonNull String username, @NonNull String accountName, @NonNull Capabilities capabilities) {
@ -328,7 +331,7 @@ public abstract class NotesRoomDatabase extends RoomDatabase {
public Map<Long, Long> getIdMap(long accountId) {
validateAccountId(accountId);
Map<Long, Long> result = new HashMap<>();
for(NoteEntity note : getNoteDao().getRemoteIdAndId(accountId)) {
for (NoteEntity note : getNoteDao().getRemoteIdAndId(accountId)) {
result.put(note.getRemoteId(), note.getId());
}
return result;

View file

@ -0,0 +1,12 @@
package it.niedermann.owncloud.notes.persistence.dao;
import androidx.room.Dao;
import androidx.room.Query;
@Dao
public interface WidgetNotesListDao {
@Query("DELETE FROM WidgetNotesListEntity WHERE id = :appWidgetId")
void removeNoteListWidget(int appWidgetId);
}

View file

@ -0,0 +1,18 @@
package it.niedermann.owncloud.notes.persistence.dao;
import android.database.sqlite.SQLiteDatabase;
import androidx.annotation.NonNull;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import it.niedermann.owncloud.notes.persistence.entity.CategoryEntity;
@Dao
public interface WidgetSingleNoteDao {
@Query("DELETE FROM WidgetSingleNoteEntity WHERE id = :appWidgetId")
void removeSingleNoteWidget(int appWidgetId);
}

View file

@ -0,0 +1,10 @@
package it.niedermann.owncloud.notes.persistence.entity;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity()
public class WidgetNotesListEntity {
@PrimaryKey
private long id;
}

View file

@ -0,0 +1,16 @@
package it.niedermann.owncloud.notes.persistence.entity;
import androidx.room.Embedded;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import java.util.Calendar;
import it.niedermann.owncloud.notes.shared.model.DBNote;
import it.niedermann.owncloud.notes.shared.model.DBStatus;
@Entity()
public class WidgetSingleNoteEntity {
@PrimaryKey
private long id;
}

View file

@ -162,10 +162,10 @@ public class NoteListWidget extends AppWidgetProvider {
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
final NotesDatabase db = NotesDatabase.getInstance(context);
final NotesRoomDatabase db = NotesRoomDatabase.getInstance(context);
for (int appWidgetId : appWidgetIds) {
db.removeNoteListWidget(appWidgetId);
db.getWidgetNotesListDao().removeNoteListWidget(appWidgetId);
}
}

View file

@ -13,6 +13,7 @@ import android.widget.RemoteViews;
import java.util.NoSuchElementException;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.persistence.NotesRoomDatabase;
import it.niedermann.owncloud.notes.preferences.DarkModeSetting;
import it.niedermann.owncloud.notes.edit.EditNoteActivity;
import it.niedermann.owncloud.notes.edit.BaseNoteFragment;
@ -79,10 +80,10 @@ public class SingleNoteWidget extends AppWidgetProvider {
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
final NotesDatabase db = NotesDatabase.getInstance(context);
final NotesRoomDatabase roomDatabase = NotesRoomDatabase.getInstance(context);
for (int appWidgetId : appWidgetIds) {
db.removeSingleNoteWidget(appWidgetId);
roomDatabase.getWidgetSingleNoteDao().removeSingleNoteWidget(appWidgetId);
}
super.onDeleted(context, appWidgetIds);
}