mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-03-21 19:10:06 +01:00
adding Print support
This commit is contained in:
parent
84967ac4bc
commit
810f10c9aa
@ -57,7 +57,7 @@ android {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'com.simplemobiletools:commons:5.31.0'
|
||||
implementation 'com.simplemobiletools:commons:5.31.13'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
|
||||
implementation 'androidx.documentfile:documentfile:1.0.1'
|
||||
|
||||
|
@ -1,9 +1,12 @@
|
||||
package com.simplemobiletools.notes.pro.activities
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.print.PrintAttributes
|
||||
import android.print.PrintManager
|
||||
import android.text.method.ArrowKeyMovementMethod
|
||||
import android.text.method.LinkMovementMethod
|
||||
import android.util.TypedValue
|
||||
@ -12,6 +15,9 @@ import android.view.Gravity
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import android.webkit.WebResourceRequest
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog
|
||||
@ -175,6 +181,7 @@ class MainActivity : SimpleActivity() {
|
||||
R.id.import_folder -> openFolder()
|
||||
R.id.export_as_file -> tryExportAsFile()
|
||||
R.id.export_all_notes -> tryExportAllNotes()
|
||||
R.id.print -> printText()
|
||||
R.id.delete_note -> displayDeleteNotePrompt()
|
||||
R.id.settings -> startActivity(Intent(applicationContext, SettingsActivity::class.java))
|
||||
R.id.about -> launchAbout()
|
||||
@ -842,6 +849,32 @@ class MainActivity : SimpleActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun printText() {
|
||||
try {
|
||||
val webView = WebView(this)
|
||||
webView.webViewClient = object : WebViewClient() {
|
||||
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest) = false
|
||||
|
||||
override fun onPageFinished(view: WebView, url: String) {
|
||||
createWebPrintJob(view)
|
||||
}
|
||||
}
|
||||
|
||||
webView.loadData(getPrintableText(), "text/plain", "UTF-8")
|
||||
} catch (e: Exception) {
|
||||
showErrorToast(e)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createWebPrintJob(webView: WebView) {
|
||||
val jobName = mCurrentNote.title
|
||||
val printAdapter = webView.createPrintDocumentAdapter(jobName)
|
||||
|
||||
(getSystemService(Context.PRINT_SERVICE) as? PrintManager)?.apply {
|
||||
print(jobName, printAdapter, PrintAttributes.Builder().build())
|
||||
}
|
||||
}
|
||||
|
||||
private fun getPagerAdapter() = view_pager.adapter as NotesPagerAdapter
|
||||
|
||||
private fun getCurrentNoteText() = getPagerAdapter().getCurrentNoteViewText(view_pager.currentItem)
|
||||
@ -854,6 +887,18 @@ class MainActivity : SimpleActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun getPrintableText(): String {
|
||||
return if (mCurrentNote.type == NoteType.TYPE_TEXT.value) {
|
||||
getCurrentNoteText() ?: ""
|
||||
} else {
|
||||
var printableText = ""
|
||||
getPagerAdapter().getNoteChecklistRawItems(view_pager.currentItem)?.forEach {
|
||||
printableText += "${it.title}\n\n"
|
||||
}
|
||||
printableText
|
||||
}
|
||||
}
|
||||
|
||||
private fun addTextToCurrentNote(text: String) = getPagerAdapter().appendText(view_pager.currentItem, text)
|
||||
|
||||
private fun saveCurrentNote(force: Boolean) {
|
||||
|
@ -63,6 +63,8 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity
|
||||
|
||||
fun saveAllFragmentTexts() = fragments.values.forEach { (it as? TextFragment)?.saveText(false) }
|
||||
|
||||
fun getNoteChecklistRawItems(position: Int) = (fragments[position] as? ChecklistFragment)?.items
|
||||
|
||||
fun getNoteChecklistItems(position: Int) = (fragments[position] as? ChecklistFragment)?.checklistItems
|
||||
|
||||
fun undo(position: Int) = (fragments[position] as? TextFragment)?.undo()
|
||||
|
@ -26,11 +26,11 @@ import kotlinx.android.synthetic.main.fragment_checklist.view.*
|
||||
class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
|
||||
|
||||
private var noteId = 0L
|
||||
private var items = ArrayList<ChecklistItem>()
|
||||
private var note: Note? = null
|
||||
|
||||
lateinit var view: ViewGroup
|
||||
|
||||
var items = ArrayList<ChecklistItem>()
|
||||
val checklistItems get(): String = Gson().toJson(items)
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
|
@ -57,6 +57,10 @@
|
||||
android:id="@+id/export_all_notes"
|
||||
android:title="@string/export_all_notes"
|
||||
app:showAsAction="never"/>
|
||||
<item
|
||||
android:id="@+id/print"
|
||||
android:title="@string/print"
|
||||
app:showAsAction="never"/>
|
||||
<item
|
||||
android:id="@+id/delete_note"
|
||||
android:icon="@drawable/ic_delete_vector"
|
||||
|
Loading…
x
Reference in New Issue
Block a user