#831 Migrate from SQLiteOpenHelper to Room

Fix some build issues
This commit is contained in:
Stefan Niedermann 2020-10-06 10:32:59 +02:00
parent a3aa4a8b66
commit cfc7c79ac5
7 changed files with 28 additions and 23 deletions

View file

@ -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;
}

View file

@ -532,11 +532,8 @@ public class MainActivity extends LockedActivity implements NoteClickListener, V
itemUncategorized = null;
}
Map<String, Integer> 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;

View file

@ -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));

View file

@ -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<String, Integer> 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

View file

@ -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<String> existingNoteRemoteIds;
private Set<Long> existingNoteRemoteIds;
public NoteLinksProcessor(Set<String> existingNoteRemoteIds) {
public NoteLinksProcessor(Set<Long> existingNoteRemoteIds) {
this.existingNoteRemoteIds = existingNoteRemoteIds;
}
@ -37,15 +39,19 @@ public class NoteLinksProcessor extends TextProcessor {
return replaceNoteLinksWithDummyUrls(s, existingNoteRemoteIds);
}
private static String replaceNoteLinksWithDummyUrls(String markdown, Set<String> existingNoteRemoteIds) {
private static String replaceNoteLinksWithDummyUrls(String markdown, Set<Long> existingNoteRemoteIds) {
Pattern noteLinkCandidates = Pattern.compile(linksThatLookLikeNoteLinksRegEx);
Matcher matcher = noteLinkCandidates.matcher(markdown);
Set<String> 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);
}
}

View file

@ -157,11 +157,8 @@ public class NoteListWidgetConfigurationActivity extends LockedActivity {
itemUncategorized.icon = NavigationAdapter.ICON_NOFOLDER;
}
Map<String, Integer> 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;

View file

@ -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<String> remoteIdsOfExistingNotes = new HashSet<>();
remoteIdsOfExistingNotes.add("123456");
remoteIdsOfExistingNotes.add("321456");
Set<Long> remoteIdsOfExistingNotes = new HashSet<>();
remoteIdsOfExistingNotes.add(123456L);
remoteIdsOfExistingNotes.add(321456L);
TextProcessor sut = new NoteLinksProcessor(remoteIdsOfExistingNotes);