From 13b0d488c144e82743cf662ec474687487279e32 Mon Sep 17 00:00:00 2001 From: Asalle Date: Sun, 3 May 2020 09:40:27 +0200 Subject: [PATCH 1/2] LinkFindTarget: make functions work with arguments path was shadowed by LinkFindTarget::path, let's call the function arguments something different so we can actually operate on them. --- .../java/com/orgzly/android/usecase/LinkFindTarget.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/orgzly/android/usecase/LinkFindTarget.kt b/app/src/main/java/com/orgzly/android/usecase/LinkFindTarget.kt index 5a86228a4..40d6f7d4f 100644 --- a/app/src/main/java/com/orgzly/android/usecase/LinkFindTarget.kt +++ b/app/src/main/java/com/orgzly/android/usecase/LinkFindTarget.kt @@ -29,12 +29,12 @@ class LinkFindTarget(val path: String) : UseCase() { } } - private fun isAbsolute(path: String): Boolean { - return path.startsWith('/') + private fun isAbsolute(bookPath: String): Boolean { + return bookPath.startsWith('/') } - private fun isMaybeBook(path: String): BookName? { - val file = File(path) + private fun isMaybeBook(bookPath: String): BookName? { + val file = File(bookPath) return if (!hasParent(file) && BookName.isSupportedFormatFileName(file.name)) { BookName.fromFileName(file.name) From 210ce4a55fbaa021f9d682c4261f525a16519228 Mon Sep 17 00:00:00 2001 From: Asalle Date: Sun, 3 May 2020 09:42:33 +0200 Subject: [PATCH 2/2] Match "book::notebook"-like links and follow them --- .../orgzly/android/ui/main/MainActivity.java | 12 ++++++++++++ .../orgzly/android/usecase/LinkFindTarget.kt | 18 +++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/orgzly/android/ui/main/MainActivity.java b/app/src/main/java/com/orgzly/android/ui/main/MainActivity.java index 36adb0444..4171dcdef 100644 --- a/app/src/main/java/com/orgzly/android/ui/main/MainActivity.java +++ b/app/src/main/java/com/orgzly/android/ui/main/MainActivity.java @@ -41,6 +41,7 @@ import com.orgzly.android.db.dao.NoteDao; import com.orgzly.android.db.entity.Book; import com.orgzly.android.db.entity.Note; +import com.orgzly.android.db.entity.NoteView; import com.orgzly.android.db.entity.Repo; import com.orgzly.android.db.entity.SavedSearch; import com.orgzly.android.prefs.AppPreferences; @@ -113,6 +114,8 @@ import java.util.List; import java.util.Set; +import kotlin.Pair; + public class MainActivity extends CommonActivity implements ActionModeListener, @@ -397,6 +400,15 @@ private void setupViewModel() { File file = (File) userData; openFileIfExists(file); } + else if (userData instanceof Pair) { + if (((Pair) userData).getFirst() instanceof Book && ((Pair) userData).getSecond() instanceof NoteView) { + NoteView noteView = (NoteView) ((Pair) userData).getSecond(); + Intent intent = new Intent(AppIntent.ACTION_OPEN_NOTE); + intent.putExtra(AppIntent.EXTRA_NOTE_ID, noteView.getNote().getId()); + intent.putExtra(AppIntent.EXTRA_BOOK_ID, ((Book) ((Pair) userData).getFirst()).getId()); + LocalBroadcastManager.getInstance(this).sendBroadcast(intent); + } + } } }); diff --git a/app/src/main/java/com/orgzly/android/usecase/LinkFindTarget.kt b/app/src/main/java/com/orgzly/android/usecase/LinkFindTarget.kt index 40d6f7d4f..35eebff75 100644 --- a/app/src/main/java/com/orgzly/android/usecase/LinkFindTarget.kt +++ b/app/src/main/java/com/orgzly/android/usecase/LinkFindTarget.kt @@ -3,6 +3,8 @@ package com.orgzly.android.usecase import android.os.Environment import com.orgzly.android.BookName import com.orgzly.android.data.DataRepository +import com.orgzly.android.db.entity.Book +import com.orgzly.android.db.entity.NoteView import java.io.File class LinkFindTarget(val path: String) : UseCase() { @@ -15,9 +17,23 @@ class LinkFindTarget(val path: String) : UseCase() { } private fun openLink(dataRepository: DataRepository, path: String): Any { + val regex = """(.*)\:\:(.*)""".toRegex() return if (isAbsolute(path)) { File(path) - + } else if (regex.matches(path)) { + val matchResults = regex.matchEntire(path) + val (_, notebook, heading) = matchResults!!.groupValues + isMaybeBook(notebook)?.let { bookName -> + dataRepository.getBook(bookName.name)?.let { + val allNotes = dataRepository.getNotes(it.name) + for (note in allNotes) { + if (note.note.title == heading.substring(1)) { // fist char of heading is '*' + return Pair(it, note); + } + } + } + } + File(Environment.getExternalStorageDirectory(), path) } else { isMaybeBook(path)?.let { bookName -> dataRepository.getBook(bookName.name)?.let {