Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions app/src/main/java/com/orgzly/android/data/DataRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.net.Uri
import android.os.Handler
import android.text.TextUtils
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.localbroadcastmanager.content.LocalBroadcastManager
Expand Down Expand Up @@ -1159,6 +1160,32 @@ class DataRepository @Inject constructor(

val sqlQuery = buildSqlQuery(query)

if (query.options.searchLeafNodes) { // If search contained "h.leaf" in it,
// display only leaf nodes of the live data query
val result = MediatorLiveData<List<NoteView>>()
val parentNotesLiveData = db.noteView().runQueryLiveData(sqlQuery)

result.addSource(parentNotesLiveData) { parentNotes ->
if (parentNotes.isNotEmpty()) {
var parentIds = parentNotes.map { it.note.id }

val leafNotesLiveData = db.noteView().runQueryLeafNotesData(parentIds) // This is a recursive
// CTE (Common Table Expression) that searches all the child nodes of the parent ids,
// the returning result filters out the notes with ids that have children in the same selection

result.removeSource(parentNotesLiveData)

result.addSource(leafNotesLiveData) { leafNotes ->
result.value = leafNotes
}
} else {
result.value = emptyList()
}
}

return result
}

return db.noteView().runQueryLiveData(sqlQuery)
}

Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/orgzly/android/db/dao/NoteViewDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ abstract class NoteViewDao {
""")
abstract fun getVisibleLiveData(bookId: Long): LiveData<List<NoteView>>

@Query("""
WITH RECURSIVE temp_notes AS (
$QUERY
GROUP BY notes.id, event_timestamp
),
rec_search_leaf_notes AS (
select * from temp_notes
WHERE temp_notes.id IN (:noteIdList)
UNION ALL
SELECT
a.*
FROM temp_notes a
JOIN rec_search_leaf_notes b ON b.id = a.parent_id
)
SELECT DISTINCT b.*
FROM rec_search_leaf_notes b
WHERE b.id NOT IN (
SELECT parent_id FROM rec_search_leaf_notes WHERE parent_id IS NOT NULL
)
""")
abstract fun runQueryLeafNotesData(noteIdList: List<Long>): LiveData<List<NoteView>>

@Query("""
$QUERY
WHERE notes.book_id = :bookId
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/orgzly/android/query/Options.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package com.orgzly.android.query

data class Options(val agendaDays: Int = 0)
data class Options(
val agendaDays: Int = 0,
val searchLeafNodes: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ open class DottedQueryParser : QueryParser() {
OptionMatch("""^ad\.(\d+)$""") { match, options ->
val days = match.groupValues[1].toInt()
if (days > 0) options.copy(agendaDays = days) else null
},
OptionMatch("""^(\.)?h\.(.+)""") { match, options ->
if (match.groupValues[2].lowercase() == HyerarchyType.LEAF.toString().lowercase())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the file that defines HyerarchyType is missing?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will check this out Bob.

Copy link
Author

@TheAttentionSeeker5050 TheAttentionSeeker5050 Mar 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the HierarchyType enum class was not added into the Git commit. As it was part of a previous iteration to solve this problem.

I am working on it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added HierarchyType and fixed its name typo (before, it was spelled incorrectly as HyerarchyType)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks! I guess the filename just needs the typo change too?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the typo

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Youre right the file name sorry I will fix it ASAP

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, thanks!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File renamed

options.copy(searchLeafNodes = true) else null
}
)
}