mirror of
https://github.com/nextcloud/notes-android.git
synced 2024-11-23 13:26:15 +03:00
parent
a3aa4a8b66
commit
cfc7c79ac5
7 changed files with 28 additions and 23 deletions
|
@ -32,6 +32,8 @@ import com.nextcloud.android.sso.model.SingleSignOnAccount;
|
||||||
import com.yydcdut.markdown.MarkdownProcessor;
|
import com.yydcdut.markdown.MarkdownProcessor;
|
||||||
import com.yydcdut.markdown.syntax.text.TextFactory;
|
import com.yydcdut.markdown.syntax.text.TextFactory;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
import it.niedermann.owncloud.notes.R;
|
import it.niedermann.owncloud.notes.R;
|
||||||
import it.niedermann.owncloud.notes.databinding.FragmentNotePreviewBinding;
|
import it.niedermann.owncloud.notes.databinding.FragmentNotePreviewBinding;
|
||||||
import it.niedermann.owncloud.notes.persistence.NotesDatabase;
|
import it.niedermann.owncloud.notes.persistence.NotesDatabase;
|
||||||
|
@ -237,7 +239,7 @@ public class NotePreviewFragment extends SearchableBaseNoteFragment implements O
|
||||||
|
|
||||||
private TextProcessorChain defaultTextProcessorChain(DBNote note) {
|
private TextProcessorChain defaultTextProcessorChain(DBNote note) {
|
||||||
TextProcessorChain chain = new TextProcessorChain();
|
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());
|
chain.add(new WwwLinksProcessor());
|
||||||
return chain;
|
return chain;
|
||||||
}
|
}
|
||||||
|
|
|
@ -532,11 +532,8 @@ public class MainActivity extends LockedActivity implements NoteClickListener, V
|
||||||
itemUncategorized = null;
|
itemUncategorized = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, Integer> favorites = roomDatabase.getNoteDao().getFavoritesCount(localAccount.getId());
|
int numFavorites = roomDatabase.getNoteDao().getFavoritesCount(localAccount.getId());
|
||||||
//noinspection ConstantConditions
|
int numNonFavorites = roomDatabase.getNoteDao().getNonFavoritesCount(localAccount.getId());
|
||||||
int numFavorites = favorites.containsKey("1") ? favorites.get("1") : 0;
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
int numNonFavorites = favorites.containsKey("0") ? favorites.get("0") : 0;
|
|
||||||
itemFavorites.count = numFavorites;
|
itemFavorites.count = numFavorites;
|
||||||
itemRecent.count = numFavorites + numNonFavorites;
|
itemRecent.count = numFavorites + numNonFavorites;
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ public class NotesClientV1 extends NotesClient {
|
||||||
JSONObject paramObject = new JSONObject();
|
JSONObject paramObject = new JSONObject();
|
||||||
paramObject.accumulate(JSON_TITLE, note.getTitle());
|
paramObject.accumulate(JSON_TITLE, note.getTitle());
|
||||||
paramObject.accumulate(JSON_CONTENT, note.getContent());
|
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_FAVORITE, note.getFavorite());
|
||||||
paramObject.accumulate(JSON_CATEGORY, note.getCategory());
|
paramObject.accumulate(JSON_CATEGORY, note.getCategory());
|
||||||
return new NoteResponse(requestServer(ssoAccount, path, method, null, paramObject, null));
|
return new NoteResponse(requestServer(ssoAccount, path, method, null, paramObject, null));
|
||||||
|
|
|
@ -64,8 +64,11 @@ public interface NoteDao {
|
||||||
@Query("SELECT id FROM NoteEntity WHERE accountId = :accountId AND remoteId = :remoteId AND status != \"LOCAL_DELETED\"")
|
@Query("SELECT id FROM NoteEntity WHERE accountId = :accountId AND remoteId = :remoteId AND status != \"LOCAL_DELETED\"")
|
||||||
Long getLocalIdByRemoteId(long accountId, long remoteId);
|
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")
|
@Query("SELECT COUNT(*) FROM NoteEntity WHERE status != 'LOCAL_DELETED' AND accountId = :accountId AND favorite = 1")
|
||||||
Map<String, Integer> getFavoritesCount(long accountId);
|
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
|
* Returns a list of all Notes in the Database which were modified locally
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package it.niedermann.owncloud.notes.shared.util.text;
|
package it.niedermann.owncloud.notes.shared.util.text;
|
||||||
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -11,15 +12,16 @@ import androidx.annotation.VisibleForTesting;
|
||||||
|
|
||||||
public class NoteLinksProcessor extends TextProcessor {
|
public class NoteLinksProcessor extends TextProcessor {
|
||||||
|
|
||||||
|
private static final String TAG = NoteLinksProcessor.class.getSimpleName();
|
||||||
public static final String RELATIVE_LINK_WORKAROUND_PREFIX = "https://nextcloudnotes/notes/";
|
public static final String RELATIVE_LINK_WORKAROUND_PREFIX = "https://nextcloudnotes/notes/";
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
private static final String linksThatLookLikeNoteLinksRegEx = "\\[[^]]*]\\((\\d+)\\)";
|
private static final String linksThatLookLikeNoteLinksRegEx = "\\[[^]]*]\\((\\d+)\\)";
|
||||||
private static final String replaceNoteRemoteIdsRegEx = "\\[([^\\]]*)\\]\\((%s)\\)";
|
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;
|
this.existingNoteRemoteIds = existingNoteRemoteIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,16 +39,20 @@ public class NoteLinksProcessor extends TextProcessor {
|
||||||
return replaceNoteLinksWithDummyUrls(s, existingNoteRemoteIds);
|
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);
|
Pattern noteLinkCandidates = Pattern.compile(linksThatLookLikeNoteLinksRegEx);
|
||||||
Matcher matcher = noteLinkCandidates.matcher(markdown);
|
Matcher matcher = noteLinkCandidates.matcher(markdown);
|
||||||
|
|
||||||
Set<String> noteRemoteIdsToReplace = new HashSet<>();
|
Set<String> noteRemoteIdsToReplace = new HashSet<>();
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
String presumedNoteId = matcher.group(1);
|
String presumedNoteId = matcher.group(1);
|
||||||
if (existingNoteRemoteIds.contains(presumedNoteId)) {
|
try {
|
||||||
|
if (presumedNoteId != null && existingNoteRemoteIds.contains(Long.parseLong(presumedNoteId))) {
|
||||||
noteRemoteIdsToReplace.add(presumedNoteId);
|
noteRemoteIdsToReplace.add(presumedNoteId);
|
||||||
}
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
Log.w(TAG, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String noteRemoteIdsCondition = TextUtils.join("|", noteRemoteIdsToReplace);
|
String noteRemoteIdsCondition = TextUtils.join("|", noteRemoteIdsToReplace);
|
||||||
|
|
|
@ -157,11 +157,8 @@ public class NoteListWidgetConfigurationActivity extends LockedActivity {
|
||||||
itemUncategorized.icon = NavigationAdapter.ICON_NOFOLDER;
|
itemUncategorized.icon = NavigationAdapter.ICON_NOFOLDER;
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, Integer> favorites = roomDatabase.getNoteDao().getFavoritesCount(localAccount.getId());
|
int numFavorites = roomDatabase.getNoteDao().getFavoritesCount(localAccount.getId());
|
||||||
//noinspection ConstantConditions
|
int numNonFavorites = roomDatabase.getNoteDao().getNonFavoritesCount(localAccount.getId());
|
||||||
int numFavorites = favorites.containsKey("1") ? favorites.get("1") : 0;
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
int numNonFavorites = favorites.containsKey("0") ? favorites.get("0") : 0;
|
|
||||||
itemFavorites.count = numFavorites;
|
itemFavorites.count = numFavorites;
|
||||||
itemRecent.count = numFavorites + numNonFavorites;
|
itemRecent.count = numFavorites + numNonFavorites;
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ public class NoteLinksProcessorTest extends TestCase {
|
||||||
|
|
||||||
@SuppressWarnings("MarkdownUnresolvedFileReference")
|
@SuppressWarnings("MarkdownUnresolvedFileReference")
|
||||||
public void testDoNotReplaceNormalLinks() {
|
public void testDoNotReplaceNormalLinks() {
|
||||||
TextProcessor sut = new NoteLinksProcessor(Collections.singleton("123456"));
|
TextProcessor sut = new NoteLinksProcessor(Collections.singleton(123456L));
|
||||||
|
|
||||||
//language=md
|
//language=md
|
||||||
String markdown = "[normal link](https://example.com) and another [note link](123456)";
|
String markdown = "[normal link](https://example.com) and another [note link](123456)";
|
||||||
|
@ -54,9 +54,9 @@ public class NoteLinksProcessorTest extends TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReplaceOnlyNotesInDB() {
|
public void testReplaceOnlyNotesInDB() {
|
||||||
Set<String> remoteIdsOfExistingNotes = new HashSet<>();
|
Set<Long> remoteIdsOfExistingNotes = new HashSet<>();
|
||||||
remoteIdsOfExistingNotes.add("123456");
|
remoteIdsOfExistingNotes.add(123456L);
|
||||||
remoteIdsOfExistingNotes.add("321456");
|
remoteIdsOfExistingNotes.add(321456L);
|
||||||
|
|
||||||
TextProcessor sut = new NoteLinksProcessor(remoteIdsOfExistingNotes);
|
TextProcessor sut = new NoteLinksProcessor(remoteIdsOfExistingNotes);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue