From cfc7c79ac52361d458f2913b1f36ec412a27dec6 Mon Sep 17 00:00:00 2001 From: Stefan Niedermann Date: Tue, 6 Oct 2020 10:32:59 +0200 Subject: [PATCH] #831 Migrate from SQLiteOpenHelper to Room Fix some build issues --- .../owncloud/notes/edit/NotePreviewFragment.java | 4 +++- .../owncloud/notes/main/MainActivity.java | 7 ++----- .../notes/persistence/NotesClientV1.java | 2 +- .../owncloud/notes/persistence/dao/NoteDao.java | 7 +++++-- .../shared/util/text/NoteLinksProcessor.java | 16 +++++++++++----- .../NoteListWidgetConfigurationActivity.java | 7 ++----- .../shared/util/text/NoteLinksProcessorTest.java | 8 ++++---- 7 files changed, 28 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/edit/NotePreviewFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/edit/NotePreviewFragment.java index 2a5fc325..dfcdfa18 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/edit/NotePreviewFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/edit/NotePreviewFragment.java @@ -32,6 +32,8 @@ import com.nextcloud.android.sso.model.SingleSignOnAccount; import com.yydcdut.markdown.MarkdownProcessor; import com.yydcdut.markdown.syntax.text.TextFactory; +import java.util.HashSet; + import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.databinding.FragmentNotePreviewBinding; import it.niedermann.owncloud.notes.persistence.NotesDatabase; @@ -237,7 +239,7 @@ public class NotePreviewFragment extends SearchableBaseNoteFragment implements O private TextProcessorChain defaultTextProcessorChain(DBNote note) { TextProcessorChain chain = new TextProcessorChain(); - chain.add(new NoteLinksProcessor(roomDatabase.getNoteDao().getRemoteIds(note.getAccountId()))); + chain.add(new NoteLinksProcessor(new HashSet<>(roomDatabase.getNoteDao().getRemoteIds(note.getAccountId())))); chain.add(new WwwLinksProcessor()); return chain; } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java index e99a6dde..356fe9cb 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java @@ -532,11 +532,8 @@ public class MainActivity extends LockedActivity implements NoteClickListener, V itemUncategorized = null; } - Map favorites = roomDatabase.getNoteDao().getFavoritesCount(localAccount.getId()); - //noinspection ConstantConditions - int numFavorites = favorites.containsKey("1") ? favorites.get("1") : 0; - //noinspection ConstantConditions - int numNonFavorites = favorites.containsKey("0") ? favorites.get("0") : 0; + int numFavorites = roomDatabase.getNoteDao().getFavoritesCount(localAccount.getId()); + int numNonFavorites = roomDatabase.getNoteDao().getNonFavoritesCount(localAccount.getId()); itemFavorites.count = numFavorites; itemRecent.count = numFavorites + numNonFavorites; diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClientV1.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClientV1.java index b4990468..9ac35860 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClientV1.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClientV1.java @@ -35,7 +35,7 @@ public class NotesClientV1 extends NotesClient { JSONObject paramObject = new JSONObject(); paramObject.accumulate(JSON_TITLE, note.getTitle()); paramObject.accumulate(JSON_CONTENT, note.getContent()); - paramObject.accumulate(JSON_MODIFIED, note.getModified() / 1000); + paramObject.accumulate(JSON_MODIFIED, note.getModified().getTimeInMillis() / 1000); paramObject.accumulate(JSON_FAVORITE, note.getFavorite()); paramObject.accumulate(JSON_CATEGORY, note.getCategory()); return new NoteResponse(requestServer(ssoAccount, path, method, null, paramObject, null)); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/NoteDao.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/NoteDao.java index 36665725..9bfdd69e 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/NoteDao.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/NoteDao.java @@ -64,8 +64,11 @@ public interface NoteDao { @Query("SELECT id FROM NoteEntity WHERE accountId = :accountId AND remoteId = :remoteId AND status != \"LOCAL_DELETED\"") Long getLocalIdByRemoteId(long accountId, long remoteId); - @Query("SELECT favorite, COUNT(*) FROM NoteEntity WHERE status != \"LOCAL_DELETED\" AND accountId = :accountId GROUP BY favorite ORDER BY favorite") - Map getFavoritesCount(long accountId); + @Query("SELECT COUNT(*) FROM NoteEntity WHERE status != 'LOCAL_DELETED' AND accountId = :accountId AND favorite = 1") + Integer getFavoritesCount(long accountId); + + @Query("SELECT COUNT(*) FROM NoteEntity WHERE status != 'LOCAL_DELETED' AND accountId = :accountId AND favorite = 0") + Integer getNonFavoritesCount(long accountId); /** * Returns a list of all Notes in the Database which were modified locally diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/NoteLinksProcessor.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/NoteLinksProcessor.java index 81f138a1..ac4c3680 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/NoteLinksProcessor.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/NoteLinksProcessor.java @@ -1,6 +1,7 @@ package it.niedermann.owncloud.notes.shared.util.text; import android.text.TextUtils; +import android.util.Log; import java.util.HashSet; import java.util.Set; @@ -11,15 +12,16 @@ import androidx.annotation.VisibleForTesting; public class NoteLinksProcessor extends TextProcessor { + private static final String TAG = NoteLinksProcessor.class.getSimpleName(); public static final String RELATIVE_LINK_WORKAROUND_PREFIX = "https://nextcloudnotes/notes/"; @VisibleForTesting private static final String linksThatLookLikeNoteLinksRegEx = "\\[[^]]*]\\((\\d+)\\)"; private static final String replaceNoteRemoteIdsRegEx = "\\[([^\\]]*)\\]\\((%s)\\)"; - private Set existingNoteRemoteIds; + private Set existingNoteRemoteIds; - public NoteLinksProcessor(Set existingNoteRemoteIds) { + public NoteLinksProcessor(Set existingNoteRemoteIds) { this.existingNoteRemoteIds = existingNoteRemoteIds; } @@ -37,15 +39,19 @@ public class NoteLinksProcessor extends TextProcessor { return replaceNoteLinksWithDummyUrls(s, existingNoteRemoteIds); } - private static String replaceNoteLinksWithDummyUrls(String markdown, Set existingNoteRemoteIds) { + private static String replaceNoteLinksWithDummyUrls(String markdown, Set existingNoteRemoteIds) { Pattern noteLinkCandidates = Pattern.compile(linksThatLookLikeNoteLinksRegEx); Matcher matcher = noteLinkCandidates.matcher(markdown); Set noteRemoteIdsToReplace = new HashSet<>(); while (matcher.find()) { String presumedNoteId = matcher.group(1); - if (existingNoteRemoteIds.contains(presumedNoteId)) { - noteRemoteIdsToReplace.add(presumedNoteId); + try { + if (presumedNoteId != null && existingNoteRemoteIds.contains(Long.parseLong(presumedNoteId))) { + noteRemoteIdsToReplace.add(presumedNoteId); + } + } catch (NumberFormatException e) { + Log.w(TAG, e); } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetConfigurationActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetConfigurationActivity.java index ac6cfdd7..978e49ee 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetConfigurationActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetConfigurationActivity.java @@ -157,11 +157,8 @@ public class NoteListWidgetConfigurationActivity extends LockedActivity { itemUncategorized.icon = NavigationAdapter.ICON_NOFOLDER; } - Map favorites = roomDatabase.getNoteDao().getFavoritesCount(localAccount.getId()); - //noinspection ConstantConditions - int numFavorites = favorites.containsKey("1") ? favorites.get("1") : 0; - //noinspection ConstantConditions - int numNonFavorites = favorites.containsKey("0") ? favorites.get("0") : 0; + int numFavorites = roomDatabase.getNoteDao().getFavoritesCount(localAccount.getId()); + int numNonFavorites = roomDatabase.getNoteDao().getNonFavoritesCount(localAccount.getId()); itemFavorites.count = numFavorites; itemRecent.count = numFavorites + numNonFavorites; diff --git a/app/src/test/java/it/niedermann/owncloud/notes/shared/util/text/NoteLinksProcessorTest.java b/app/src/test/java/it/niedermann/owncloud/notes/shared/util/text/NoteLinksProcessorTest.java index 8b96a207..f30d4670 100644 --- a/app/src/test/java/it/niedermann/owncloud/notes/shared/util/text/NoteLinksProcessorTest.java +++ b/app/src/test/java/it/niedermann/owncloud/notes/shared/util/text/NoteLinksProcessorTest.java @@ -45,7 +45,7 @@ public class NoteLinksProcessorTest extends TestCase { @SuppressWarnings("MarkdownUnresolvedFileReference") public void testDoNotReplaceNormalLinks() { - TextProcessor sut = new NoteLinksProcessor(Collections.singleton("123456")); + TextProcessor sut = new NoteLinksProcessor(Collections.singleton(123456L)); //language=md String markdown = "[normal link](https://example.com) and another [note link](123456)"; @@ -54,9 +54,9 @@ public class NoteLinksProcessorTest extends TestCase { } public void testReplaceOnlyNotesInDB() { - Set remoteIdsOfExistingNotes = new HashSet<>(); - remoteIdsOfExistingNotes.add("123456"); - remoteIdsOfExistingNotes.add("321456"); + Set remoteIdsOfExistingNotes = new HashSet<>(); + remoteIdsOfExistingNotes.add(123456L); + remoteIdsOfExistingNotes.add(321456L); TextProcessor sut = new NoteLinksProcessor(remoteIdsOfExistingNotes);