Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions app/src/main/java/com/orgzly/android/query/HierarchyType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.orgzly.android.query

enum class HierarchyType { LEAF }
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() == HierarchyType.LEAF.toString().lowercase())
options.copy(searchLeafNodes = true) else null
}
)
}