#831 Migrate from SQLiteOpenHelper to Room

Fix some build bugs
This commit is contained in:
Stefan Niedermann 2020-10-05 16:18:30 +02:00
parent e58623a078
commit f0b57a2ad8
4 changed files with 43 additions and 22 deletions

View file

@ -436,7 +436,7 @@ public class NoteServerSyncHelper {
private boolean pullRemoteChanges() {
Log.d(TAG, "pullRemoteChanges() for account " + localAccount.getAccountName());
try {
final Map<Long, Long> idMap = sqliteOpenHelperDatabase.getIdMap(localAccount.getId());
final Map<Long, Long> idMap = roomDatabase.getIdMap(localAccount.getId());
final ServerResponse.NotesResponse response = notesClient.getNotes(ssoAccount, localAccount.getModified(), localAccount.getEtag());
List<CloudNote> remoteNotes = response.getNotes();
Set<Long> remoteIDs = new HashSet<>();

View file

@ -160,20 +160,6 @@ public class NotesDatabase extends AbstractNotesDatabase {
);
}
@NonNull
@WorkerThread
public Map<Long, Long> getIdMap(long accountId) {
validateAccountId(accountId);
Map<Long, Long> result = new HashMap<>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(table_notes, new String[]{key_remote_id, key_id}, key_status + " != ? AND " + key_account_id + " = ? ", new String[]{DBStatus.LOCAL_DELETED.getTitle(), "" + accountId}, null, null, null);
while (cursor.moveToNext()) {
result.put(cursor.getLong(0), cursor.getLong(1));
}
cursor.close();
return result;
}
/**
* Returns a list of all Notes in the Database
*
@ -186,13 +172,6 @@ public class NotesDatabase extends AbstractNotesDatabase {
return getNotesCustom(accountId, key_status + " != ? AND " + key_account_id + " = ?", new String[]{DBStatus.LOCAL_DELETED.getTitle(), "" + accountId}, default_order, false);
}
@NonNull
@WorkerThread
public List<DBNote> getRecentNotes(long accountId) {
validateAccountId(accountId);
return getNotesCustom(accountId, key_status + " != ? AND " + key_account_id + " = ?", new String[]{DBStatus.LOCAL_DELETED.getTitle(), "" + accountId}, key_modified + " DESC", "4", true);
}
/**
* This method is overloading searchNotes method.
* In order to keep the original code (called this method) still work.
@ -511,6 +490,32 @@ public class NotesDatabase extends AbstractNotesDatabase {
return false;
}
/**
* Set the category for a given note.
* This method will search in the database to find out the category id in the db.
* If there is no such category existing, this method will create it and search again.
*
* @param ssoAccount The single sign on account
* @param note The note which will be updated
* @param category The category title which should be used to find the category id.
* @param callback When the synchronization is finished, this callback will be invoked (optional).
*/
public void setCategory(SingleSignOnAccount ssoAccount, @NonNull DBNote note, @NonNull String category, @Nullable ISyncCallback callback) {
note.setCategory(category);
note.setStatus(DBStatus.LOCAL_EDITED);
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues(2);
values.put(key_status, note.getStatus().getTitle());
int id = getCategoryIdByTitle(note.getAccountId(), note.getCategory());
values.put(key_category, id);
db.update(table_notes, values, key_id + " = ?", new String[]{String.valueOf(note.getId())});
removeEmptyCategory(note.getAccountId());
if (callback != null) {
serverSyncHelper.addCallbackPush(ssoAccount, callback);
}
serverSyncHelper.scheduleSync(ssoAccount, true);
}
/**
* @param localAccount the {@link LocalAccount} that should be deleted
* @throws IllegalArgumentException if no account has been deleted by the given accountId

View file

@ -5,6 +5,7 @@ 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;
@ -25,7 +26,9 @@ import com.nextcloud.android.sso.model.SingleSignOnAccount;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.edit.EditNoteActivity;
@ -319,4 +322,15 @@ public abstract class NotesRoomDatabase extends RoomDatabase {
}
syncHelper.scheduleSync(ssoAccount, true);
}
@NonNull
@WorkerThread
public Map<Long, Long> getIdMap(long accountId) {
validateAccountId(accountId);
Map<Long, Long> result = new HashMap<>();
for(NoteEntity note : getNoteDao().getRemoteIdAndId(accountId)) {
result.put(note.getRemoteId(), note.getId());
}
return result;
}
}

View file

@ -44,6 +44,8 @@ public interface NoteDao {
@Query("SELECT DISTINCT remoteId FROM NoteEntity WHERE accountId = :accountId AND status != \"LOCAL_DELETED\"")
List<Long> getRemoteIds(long accountId);
@Query("SELECT * FROM NoteEntity WHERE accountId = :accountId AND status != \"LOCAL_DELETED\"")
List<NoteEntity> getRemoteIdAndId(long accountId);
/**
* Get a single Note by remote Id (aka. nextcloud file id)