Implement appending text received from external application

This commit is contained in:
Nikola Trubitsyn
2017-06-18 02:05:33 +03:00
parent 58eee25da5
commit 80edf02089
15 changed files with 54 additions and 1 deletions

View File

@ -11,10 +11,12 @@ import android.text.method.LinkMovementMethod
import android.util.TypedValue
import android.view.*
import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.LICENSE_KOTLIN
import com.simplemobiletools.commons.helpers.LICENSE_RTL
import com.simplemobiletools.commons.helpers.LICENSE_STETHO
import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.commons.models.Release
import com.simplemobiletools.commons.views.MyEditText
import com.simplemobiletools.notes.BuildConfig
@ -59,13 +61,30 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
intent.apply {
if (action == Intent.ACTION_SEND && type == MIME_TEXT_PLAIN) {
getStringExtra(Intent.EXTRA_TEXT)?.let {
displayNewNoteDialog(it)
handleText(it)
intent.removeExtra(Intent.EXTRA_TEXT)
}
}
}
}
private fun handleText(text: String) {
val notes = DBHelper.newInstance(this@MainActivity).getNotes()
val list = arrayListOf<RadioItem>().apply {
add(RadioItem(0, getString(R.string.create_new_note)))
notes.forEachIndexed { index, note -> add(RadioItem(index + 1, note.title)) }
}
RadioGroupDialog(this@MainActivity, list, -1, R.string.add_to_note, {
if (it as Int == 0) {
displayNewNoteDialog(text)
} else {
updateSelectedNote(notes[it - 1].id)
addTextToCurrentNote(if (mCurrentNote.value.isEmpty()) text else "\n$text")
}
}).create()
}
private fun initViewPager() {
mNotes = mDb.getNotes()
mCurrentNote = mNotes[0]
@ -269,6 +288,8 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
private fun getCurrentNoteText() = (view_pager.adapter as NotesPagerAdapter).getCurrentNoteViewText(view_pager.currentItem)
private fun addTextToCurrentNote(text: String) = (view_pager.adapter as NotesPagerAdapter).appendText(view_pager.currentItem, text)
private fun saveCurrentNote() = (view_pager.adapter as NotesPagerAdapter).saveCurrentNote(view_pager.currentItem)
private fun displayDeleteNotePrompt() {

View File

@ -36,6 +36,12 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity
fun getCurrentNoteViewText(position: Int) = fragments[position]?.getCurrentNoteViewText()
fun appendText(position: Int, text: String) {
val fragment = fragments[position]
val finalText = fragment?.getCurrentNoteViewText() + text
fragment?.getNotesView()?.setText(finalText)
}
fun saveCurrentNote(position: Int) = fragments[position]?.saveText()
fun showKeyboard(position: Int) = fragments[position]?.showKeyboard()

View File

@ -54,6 +54,8 @@ class NoteFragment : Fragment() {
saveText()
}
fun getNotesView() = view.notes_view
fun saveText() {
if (note.path.isNotEmpty() && !File(note.path).exists())
return