-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b6b848
commit e667da9
Showing
8 changed files
with
106 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/orgzly/android/ui/AttachmentSpanLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.orgzly.android.ui | ||
|
||
import android.text.Spannable | ||
import com.orgzly.android.ui.views.TextViewWithMarkup | ||
import com.orgzly.android.ui.views.style.AttachmentLinkSpan | ||
import com.orgzly.android.usecase.FindAttachmentPath | ||
import com.orgzly.android.usecase.UseCaseRunner | ||
|
||
object AttachmentSpanLoader { | ||
/** Find all `attachment:` links and set up the prefix directory based on `ID` property. */ | ||
fun loadAttachmentPaths(noteId: Long, textWithMarkup: TextViewWithMarkup) { | ||
SpanUtils.forEachSpan(textWithMarkup.text as Spannable, AttachmentLinkSpan::class.java) { span -> | ||
val prefix = UseCaseRunner.run(FindAttachmentPath(noteId)).userData | ||
if (prefix != null) { | ||
span.prefix = prefix as String | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/orgzly/android/ui/views/style/AttachmentLinkSpan.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.orgzly.android.ui.views.style | ||
|
||
import android.os.Handler | ||
import android.text.style.ClickableSpan | ||
import android.view.View | ||
import com.orgzly.android.ui.views.TextViewWithMarkup | ||
|
||
/** | ||
* This [ClickableSpan] corresponds to "attachment:" link. What comes after `:` is `path`. The full | ||
* path also needs a prefix which is derived from `ID` property for example. | ||
*/ | ||
class AttachmentLinkSpan(val path: String) : ClickableSpan() { | ||
var prefix: String? = null | ||
|
||
override fun onClick(widget: View) { | ||
if (widget is TextViewWithMarkup && prefix != null) { | ||
Handler().post { // Run after onClick to prevent Snackbar from closing immediately | ||
widget.followLinkToFile(getPrefixedPath()) | ||
} | ||
} | ||
} | ||
|
||
fun getPrefixedPath(): String { | ||
return "$prefix/$path" | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
app/src/main/java/com/orgzly/android/usecase/FindAttachmentPath.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.orgzly.android.usecase | ||
|
||
import android.content.Context | ||
import com.orgzly.android.App | ||
import com.orgzly.android.data.DataRepository | ||
import com.orgzly.android.db.entity.NoteProperty | ||
import com.orgzly.android.util.AttachmentUtils | ||
|
||
/** | ||
* An [UseCase] that finds the attachment directory path with the given [noteId]. | ||
* Corresponds to `org-attach-dir`. Currently checks the ID property of the given node. | ||
* | ||
* Note that this finds the expected directory path, even if the directory doesn't exist. | ||
* | ||
* TODO: Also check DIR property. | ||
* TODO: Also check inherited property, based on a preference as in `org-attach-use-inheritance` | ||
*/ | ||
class FindAttachmentPath(val noteId: Long) : UseCase() { | ||
val context: Context = App.getAppContext(); | ||
|
||
override fun run(dataRepository: DataRepository): UseCaseResult { | ||
val noteProperties = dataRepository.getNoteProperties(noteId) | ||
val idStr = getProperty(noteProperties, "ID") | ||
|
||
val path = if (idStr == null) null else AttachmentUtils.getAttachDir(context, idStr) | ||
|
||
return UseCaseResult( | ||
userData = path | ||
) | ||
} | ||
|
||
private fun getProperty(noteProperties: List<NoteProperty>, propertyName: String): String? { | ||
for (property: NoteProperty in noteProperties) { | ||
if (property.name == propertyName) { | ||
return property.value | ||
} | ||
} | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters