|
| 1 | +package eu.fliegendewurst.triliumdroid.fragment.note |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.os.Bundle |
| 5 | +import android.os.Handler |
| 6 | +import android.util.Log |
| 7 | +import android.view.LayoutInflater |
| 8 | +import android.view.View |
| 9 | +import android.view.ViewGroup |
| 10 | +import android.webkit.ConsoleMessage |
| 11 | +import android.widget.TextView |
| 12 | +import androidx.constraintlayout.widget.ConstraintLayout |
| 13 | +import androidx.core.view.iterator |
| 14 | +import androidx.fragment.app.Fragment |
| 15 | +import androidx.lifecycle.lifecycleScope |
| 16 | +import eu.fliegendewurst.triliumdroid.FrontendBackendApi |
| 17 | +import eu.fliegendewurst.triliumdroid.R |
| 18 | +import eu.fliegendewurst.triliumdroid.activity.main.MainActivity |
| 19 | +import eu.fliegendewurst.triliumdroid.data.Blob |
| 20 | +import eu.fliegendewurst.triliumdroid.data.Note |
| 21 | +import eu.fliegendewurst.triliumdroid.database.Attributes |
| 22 | +import eu.fliegendewurst.triliumdroid.database.Cache |
| 23 | +import eu.fliegendewurst.triliumdroid.database.Notes |
| 24 | +import eu.fliegendewurst.triliumdroid.databinding.FragmentCanvasNoteBinding |
| 25 | +import eu.fliegendewurst.triliumdroid.fragment.NoteRelatedFragment |
| 26 | +import eu.fliegendewurst.triliumdroid.util.MyWebChromeClient |
| 27 | +import kotlinx.coroutines.launch |
| 28 | +import kotlinx.coroutines.runBlocking |
| 29 | + |
| 30 | + |
| 31 | +class CanvasNoteFragment : Fragment(R.layout.fragment_canvas_note), NoteRelatedFragment { |
| 32 | + companion object { |
| 33 | + private const val TAG: String = "CanvasNoteFragment" |
| 34 | + const val WEBVIEW_DOMAIN: String = "https://trilium-notes.invalid/" |
| 35 | + } |
| 36 | + |
| 37 | + private lateinit var binding: FragmentCanvasNoteBinding |
| 38 | + private var handler: Handler? = null |
| 39 | + private var note: Note? = null |
| 40 | + private var blob: Blob? = null |
| 41 | + private var load: Boolean = false |
| 42 | + private var subCodeNotes: List<Note>? = null |
| 43 | + var console: MutableList<ConsoleMessage> = mutableListOf() |
| 44 | + |
| 45 | + override fun getNoteId(): String? { |
| 46 | + return note?.id |
| 47 | + } |
| 48 | + |
| 49 | + @SuppressLint("SetJavaScriptEnabled") |
| 50 | + override fun onCreateView( |
| 51 | + inflater: LayoutInflater, |
| 52 | + container: ViewGroup?, |
| 53 | + savedInstanceState: Bundle? |
| 54 | + ): View { |
| 55 | + handler = Handler(requireContext().mainLooper) |
| 56 | + |
| 57 | + binding = FragmentCanvasNoteBinding.inflate(inflater, container, false) |
| 58 | + binding.webview.settings.javaScriptEnabled = true |
| 59 | + binding.webview.addJavascriptInterface( |
| 60 | + FrontendBackendApi(this, this.requireContext(), handler!!), |
| 61 | + "api" |
| 62 | + ) |
| 63 | + binding.webview.webChromeClient = MyWebChromeClient( |
| 64 | + { (this@CanvasNoteFragment.activity as MainActivity?) }, |
| 65 | + { this@CanvasNoteFragment.console.add(it) }) |
| 66 | + val wvc = NoteWebViewClient( |
| 67 | + { return@NoteWebViewClient note }, |
| 68 | + { return@NoteWebViewClient blob }, |
| 69 | + { return@NoteWebViewClient subCodeNotes }, |
| 70 | + { return@NoteWebViewClient activity as MainActivity? }, |
| 71 | + { startActivity(it) }) |
| 72 | + |
| 73 | + binding.webview.webViewClient = wvc |
| 74 | + |
| 75 | + if (load) { |
| 76 | + viewLifecycleOwner.lifecycleScope.launch { |
| 77 | + load(note, blob) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return binding.root |
| 82 | + } |
| 83 | + |
| 84 | + fun loadLater(note: Note?) { |
| 85 | + load = true |
| 86 | + this.note = note |
| 87 | + this.blob = null |
| 88 | + } |
| 89 | + |
| 90 | + suspend fun load(noteToLoad: Note?, blobToDisplay: Blob? = null) { |
| 91 | + var note = noteToLoad |
| 92 | + // if called before proper creation |
| 93 | + if (this.activity == null) { |
| 94 | + loadLater(note) |
| 95 | + return |
| 96 | + } |
| 97 | + console.clear() |
| 98 | + subCodeNotes = emptyList() |
| 99 | + |
| 100 | + this.note = note |
| 101 | + this.blob = blobToDisplay |
| 102 | + this.load = true |
| 103 | + Log.i(TAG, "loading ${note?.id}") |
| 104 | + if (note == null) { |
| 105 | + return |
| 106 | + } |
| 107 | + binding.textId.text = note.id |
| 108 | + if (note.content() == null && blob == null) { |
| 109 | + Cache.initializeDatabase(requireContext()) |
| 110 | + note = Notes.getNoteWithContent(note.id) |
| 111 | + } |
| 112 | + if (note == null) { |
| 113 | + (this@CanvasNoteFragment.activity as MainActivity).handleEmptyNote() |
| 114 | + return |
| 115 | + } |
| 116 | + val consoleLog = false |
| 117 | + var execute = false |
| 118 | + var share = false |
| 119 | + |
| 120 | + refreshHeader(note) |
| 121 | + |
| 122 | + binding.webview.loadUrl(WEBVIEW_DOMAIN + note.id) |
| 123 | + |
| 124 | + val main = (this@CanvasNoteFragment.activity ?: return) as MainActivity |
| 125 | + // FABs obscure excalidraw menu items |
| 126 | + main.fixVisibilityFABs(true) |
| 127 | + |
| 128 | + main.setupActions( |
| 129 | + consoleLog, |
| 130 | + execute, |
| 131 | + share, |
| 132 | + note.id == "root" |
| 133 | + ) |
| 134 | + } |
| 135 | + |
| 136 | + suspend fun refreshHeader(note: Note) { |
| 137 | + if (!this.load || context == null) { |
| 138 | + return |
| 139 | + } |
| 140 | + if (blob != null) { |
| 141 | + // previous revision: hide labels |
| 142 | + binding.labelNoteRevisionInfo.text = blob!!.dateModified |
| 143 | + binding.noteHeaderAttributes.visibility = View.GONE |
| 144 | + return |
| 145 | + } |
| 146 | + binding.labelNoteRevisionInfo.text = "" |
| 147 | + val constraintLayout = binding.noteHeader |
| 148 | + val flow = binding.noteHeaderAttributes |
| 149 | + val attributeContentDesc = getString(R.string.attribute) |
| 150 | + // remove previously shown attributes |
| 151 | + constraintLayout.iterator().also { iterator -> |
| 152 | + iterator.forEach { view -> |
| 153 | + if (view.contentDescription == attributeContentDesc) { |
| 154 | + iterator.remove() |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + for (attribute in note.getLabels()) { |
| 159 | + if (!attribute.promoted) { |
| 160 | + continue |
| 161 | + } |
| 162 | + val view = |
| 163 | + LayoutInflater.from(context) |
| 164 | + .inflate(R.layout.item_attribute, constraintLayout, false) |
| 165 | + view.findViewById<TextView>(R.id.label_attribute_name).text = attribute.name |
| 166 | + val textInput = view.findViewById<TextView>(R.id.label_attribute_value) |
| 167 | + textInput.text = attribute.value() |
| 168 | + textInput.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus -> |
| 169 | + if (hasFocus) { |
| 170 | + return@OnFocusChangeListener |
| 171 | + } |
| 172 | + val newValue = textInput.text |
| 173 | + if (newValue != attribute.value) { |
| 174 | + runBlocking { |
| 175 | + Attributes.updateLabel( |
| 176 | + note, |
| 177 | + attribute.name, |
| 178 | + newValue.toString(), |
| 179 | + attribute.inheritable |
| 180 | + ) |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + view.layoutParams = ConstraintLayout.LayoutParams( |
| 185 | + ConstraintLayout.LayoutParams.WRAP_CONTENT, |
| 186 | + ConstraintLayout.LayoutParams.WRAP_CONTENT |
| 187 | + ) |
| 188 | + view.id = View.generateViewId() |
| 189 | + constraintLayout.addView(view) |
| 190 | + flow.addView(view) |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments