#831 Migrate from SQLiteOpenHelper to Room

This commit is contained in:
Stefan Niedermann 2020-10-06 13:52:56 +02:00
parent 5597fd0dbc
commit dfcfb3d238
9 changed files with 41 additions and 21 deletions

View file

@ -253,7 +253,7 @@ public class MainActivity extends LockedActivity implements NoteClickListener, V
localAccount = db.getLocalAccountDao().getLocalAccountByAccountName(accountName);
if (localAccount != null) {
try {
BrandingUtil.saveBrandColors(this, Color.parseColor(localAccount.getColor()), Color.parseColor(localAccount.getTextColor()));
BrandingUtil.saveBrandColors(this, Color.parseColor('#' + localAccount.getColor()), Color.parseColor('#' + localAccount.getTextColor()));
ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(getApplicationContext());
new NotesListViewItemTouchHelper(ssoAccount, this, db, adapter, syncCallBack, this::refreshLists, swipeRefreshLayout, this, gridView)
.attachToRecyclerView(listView);
@ -376,7 +376,7 @@ public class MainActivity extends LockedActivity implements NoteClickListener, V
db.updateBrand(localAccount.getId(), capabilities);
localAccount.setColor(capabilities.getColor());
localAccount.setTextColor(capabilities.getTextColor());
BrandingUtil.saveBrandColors(this, Color.parseColor(localAccount.getColor()), Color.parseColor(localAccount.getTextColor()));
BrandingUtil.saveBrandColors(this, Color.parseColor('#' + localAccount.getColor()), Color.parseColor('#' + localAccount.getTextColor()));
db.updateApiVersion(localAccount.getId(), capabilities.getApiVersion());
Log.i(TAG, capabilities.toString());
} catch (Exception e) {

View file

@ -41,7 +41,7 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
List<NoteEntity> noteList;
NotesDatabase db = NotesDatabase.getInstance(context);
CategorySortingMethod sortingMethod = db.getCategoryOrder(accountId, category);
noteList = db.getNoteDao().searchNotesSubcategory(accountId, searchQuery.toString(), category.category, category.favorite, sortingMethod);
noteList = db.getNoteDao().searchNotesSubcategory(accountId, searchQuery == null ? "" : searchQuery.toString(), category.category, category.favorite, sortingMethod);
if (category.category == null) {
if (sortingMethod == CategorySortingMethod.SORT_MODIFIED_DESC) {

View file

@ -404,7 +404,7 @@ public class NoteServerSyncHelper {
db.deleteNote(note.getId(), DBStatus.LOCAL_DELETED);
break;
default:
throw new IllegalStateException("Unknown State of Note: " + note);
throw new IllegalStateException("Unknown State of Note " + note + ": " + note.getStatus());
}
} catch (NextcloudHttpRequestFailedException e) {
if (e.getStatusCode() == HTTP_NOT_MODIFIED) {

View file

@ -221,9 +221,9 @@ public abstract class NotesDatabase extends RoomDatabase {
*/
@NonNull
@WorkerThread
public List<NavigationAdapter.CategoryNavigationItem> searchCategories(long accountId, String search) {
public List<NavigationAdapter.CategoryNavigationItem> searchCategories(long accountId, @Nullable String search) {
validateAccountId(accountId);
List<CategoryWithNotesCount> counters = getCategoryDao().searchCategories(accountId, search.trim());
List<CategoryWithNotesCount> counters = getCategoryDao().searchCategories(accountId, search == null ? null : search.trim());
List<NavigationAdapter.CategoryNavigationItem> categories = new ArrayList<>(counters.size());
for (CategoryWithNotesCount counter : counters) {
Resources res = context.getResources();

View file

@ -81,7 +81,7 @@ public interface NoteDao {
*
* @return {@link List<NoteEntity>}
*/
@Query("SELECT * FROM NoteEntity WHERE status != 'VOID' AND accountId = :accountId")
@Query("SELECT * FROM NoteEntity WHERE status != '' AND accountId = :accountId")
List<NoteEntity> getLocalModifiedNotes(long accountId);
@Query("SELECT * FROM NoteEntity WHERE status != 'LOCAL_DELETED' AND accountId = :accountId ORDER BY modified DESC LIMIT 4")
@ -118,7 +118,7 @@ public interface NoteDao {
*/
@Query(
"UPDATE NoteEntity SET id = :id, title = :title, modified = :modified, title = :title, favorite = :favorite, etag = :eTag, content = :content " +
"WHERE id = :id AND status = 'VOID' AND (modified != :modified OR favorite != :favorite OR category_title != :categoryTitle OR (eTag == NULL OR eTag != :eTag) OR content != :content)"
"WHERE id = :id AND status = '' AND (modified != :modified OR favorite != :favorite OR category_title != :categoryTitle OR (eTag == NULL OR eTag != :eTag) OR content != :content)"
)
void updateIfNotModifiedLocallyAndRemoteColumnHasChanged(long id, long modified, String title, Boolean favorite, String categoryTitle, String eTag, String content);
}

View file

@ -31,17 +31,18 @@ public class Converters {
@TypeConverter
public static Calendar calendarFromLong(Long value) {
if (value == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(value * 1000);
if (value == null) {
calendar.setTimeInMillis(1000);
} else {
calendar.setTimeInMillis(value * 1000);
}
return calendar;
}
@TypeConverter
public static Long calendarToLong(Calendar calendar) {
return calendar == null ? null : calendar.getTimeInMillis() / 1000;
return calendar == null ? 0 : calendar.getTimeInMillis() / 1000;
}
}

View file

@ -2,6 +2,7 @@ package it.niedermann.owncloud.notes.persistence.entity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Size;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

View file

@ -1,5 +1,6 @@
package it.niedermann.owncloud.notes.persistence.entity;
import androidx.annotation.NonNull;
import androidx.room.Embedded;
import androidx.room.Entity;
import androidx.room.Ignore;
@ -153,6 +154,23 @@ public class NoteEntity implements Serializable, Item {
public boolean isSection() {
return false;
}
@NonNull
@Override
public String toString() {
return "NoteEntity{" +
"id=" + id +
", remoteId=" + remoteId +
", accountId=" + accountId +
", status=" + status +
", title='" + title + '\'' +
", modified=" + modified +
", favorite=" + favorite +
", eTag='" + eTag + '\'' +
", scrollY=" + scrollY +
", category=" + category +
'}';
}
}
// "FOREIGN KEY(" + key_category + ") REFERENCES " + table_category + "(" + key_category_id + "), " +
// "FOREIGN KEY(" + key_account_id + ") REFERENCES " + table_accounts + "(" + key_id + "))");

View file

@ -89,10 +89,10 @@ public class NoteListWidget extends AppWidgetProvider {
views.setEmptyView(R.id.note_list_widget_lv_dark, R.id.widget_note_list_placeholder_tv_dark);
awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.note_list_widget_lv_dark);
if (BrandingUtil.isBrandingEnabled(context)) {
views.setInt(R.id.widget_note_header_dark, "setBackgroundColor", Color.parseColor(localAccount.getColor()));
views.setInt(R.id.widget_note_header_icon_dark, "setColorFilter", Color.parseColor(localAccount.getTextColor()));
views.setInt(R.id.widget_note_list_create_icon_dark, "setColorFilter", Color.parseColor(localAccount.getTextColor()));
views.setTextColor(R.id.widget_note_list_title_tv_dark, Color.parseColor(localAccount.getTextColor()));
views.setInt(R.id.widget_note_header_dark, "setBackgroundColor", Color.parseColor('#' + localAccount.getColor()));
views.setInt(R.id.widget_note_header_icon_dark, "setColorFilter", Color.parseColor('#' + localAccount.getTextColor()));
views.setInt(R.id.widget_note_list_create_icon_dark, "setColorFilter", Color.parseColor('#' + localAccount.getTextColor()));
views.setTextColor(R.id.widget_note_list_title_tv_dark, Color.parseColor('#' + localAccount.getTextColor()));
} else {
views.setInt(R.id.widget_note_header_dark, "setBackgroundColor", context.getResources().getColor(R.color.defaultBrand));
views.setInt(R.id.widget_note_header_icon_dark, "setColorFilter", Color.WHITE);
@ -110,10 +110,10 @@ public class NoteListWidget extends AppWidgetProvider {
views.setEmptyView(R.id.note_list_widget_lv, R.id.widget_note_list_placeholder_tv);
awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.note_list_widget_lv);
if (BrandingUtil.isBrandingEnabled(context)) {
views.setInt(R.id.widget_note_header, "setBackgroundColor", Color.parseColor(localAccount.getColor()));
views.setInt(R.id.widget_note_header_icon, "setColorFilter", Color.parseColor(localAccount.getTextColor()));
views.setInt(R.id.widget_note_list_create_icon, "setColorFilter", Color.parseColor(localAccount.getTextColor()));
views.setTextColor(R.id.widget_note_list_title_tv, Color.parseColor(localAccount.getTextColor()));
views.setInt(R.id.widget_note_header, "setBackgroundColor", Color.parseColor('#' + localAccount.getColor()));
views.setInt(R.id.widget_note_header_icon, "setColorFilter", Color.parseColor('#' + localAccount.getTextColor()));
views.setInt(R.id.widget_note_list_create_icon, "setColorFilter", Color.parseColor('#' + localAccount.getTextColor()));
views.setTextColor(R.id.widget_note_list_title_tv, Color.parseColor('#' + localAccount.getTextColor()));
} else {
views.setInt(R.id.widget_note_header, "setBackgroundColor", context.getResources().getColor(R.color.defaultBrand));
views.setInt(R.id.widget_note_header_icon, "setColorFilter", Color.WHITE);