Skip to content

Commit

Permalink
move some note fetchin related functions in Room
Browse files Browse the repository at this point in the history
  • Loading branch information
tibbi committed Nov 7, 2018
1 parent 32837eb commit 885170e
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.adapters.NotesPagerAdapter
import com.simplemobiletools.notes.pro.databases.NotesDatabase
import com.simplemobiletools.notes.pro.dialogs.*
import com.simplemobiletools.notes.pro.extensions.config
import com.simplemobiletools.notes.pro.extensions.dbHelper
import com.simplemobiletools.notes.pro.extensions.getTextSize
import com.simplemobiletools.notes.pro.extensions.updateWidgets
import com.simplemobiletools.notes.pro.extensions.*
import com.simplemobiletools.notes.pro.helpers.MIME_TEXT_PLAIN
import com.simplemobiletools.notes.pro.helpers.NotesHelper
import com.simplemobiletools.notes.pro.helpers.OPEN_NOTE_ID
import com.simplemobiletools.notes.pro.helpers.TYPE_NOTE
import com.simplemobiletools.notes.pro.models.Note
Expand Down Expand Up @@ -69,7 +67,7 @@ class MainActivity : SimpleActivity() {
intent.apply {
if (action == Intent.ACTION_SEND && type == MIME_TEXT_PLAIN) {
getStringExtra(Intent.EXTRA_TEXT)?.let {
handleText(it)
handleTextIntent(it)
intent.removeExtra(Intent.EXTRA_TEXT)
}
}
Expand Down Expand Up @@ -213,21 +211,23 @@ class MainActivity : SimpleActivity() {
}
}

private fun handleText(text: String) {
val notes = dbHelper.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))
private fun handleTextIntent(text: String) {
NotesHelper(this).getNotes {
val notes = it
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, 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")
RadioGroupDialog(this, 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")
}
}
}
}
Expand All @@ -248,21 +248,23 @@ class MainActivity : SimpleActivity() {
}

private fun initViewPager() {
mNotes = dbHelper.getNotes()
mCurrentNote = mNotes[0]
mAdapter = NotesPagerAdapter(supportFragmentManager, mNotes, this)
view_pager.apply {
adapter = mAdapter
currentItem = getWantedNoteIndex()

onPageChangeListener {
mCurrentNote = mNotes[it]
config.currentNoteId = mCurrentNote.id!!
NotesHelper(this).getNotes {
mNotes = it
mCurrentNote = mNotes[0]
mAdapter = NotesPagerAdapter(supportFragmentManager, mNotes, this)
view_pager.apply {
adapter = mAdapter
currentItem = getWantedNoteIndex()

onPageChangeListener {
mCurrentNote = mNotes[it]
config.currentNoteId = mCurrentNote.id!!
}
}
}

if (!config.showKeyboard) {
hideKeyboard()
if (!config.showKeyboard) {
hideKeyboard()
}
}
}

Expand Down Expand Up @@ -302,15 +304,19 @@ class MainActivity : SimpleActivity() {
}

private fun addNewNote(note: Note) {
val id = dbHelper.insertNote(note)
mNotes = dbHelper.getNotes()
showSaveButton = false
invalidateOptionsMenu()
initViewPager()
updateSelectedNote(id)
view_pager.onGlobalLayout {
mAdapter?.focusEditText(getNoteIndexWithId(id))
}
Thread {
val id = notesDB.insertOrUpdate(note).toInt()
mNotes = notesDB.getNotes().toMutableList() as ArrayList<Note>
showSaveButton = false
runOnUiThread {
invalidateOptionsMenu()
initViewPager()
updateSelectedNote(id)
view_pager.onGlobalLayout {
mAdapter?.focusEditText(getNoteIndexWithId(id))
}
}
}.start()
}

private fun launchAbout() {
Expand Down Expand Up @@ -400,13 +406,16 @@ class MainActivity : SimpleActivity() {
FilePickerDialog(this, pickFile = false, canAddShowHiddenButton = true) {
openFolder(it) {
ImportFolderDialog(this, it.path) {
mNotes = dbHelper.getNotes()
showSaveButton = false
invalidateOptionsMenu()
initViewPager()
updateSelectedNote(it)
view_pager.onGlobalLayout {
mAdapter?.focusEditText(getNoteIndexWithId(it))
val noteId = it
NotesHelper(this).getNotes {
mNotes = it
showSaveButton = false
invalidateOptionsMenu()
initViewPager()
updateSelectedNote(noteId)
view_pager.onGlobalLayout {
mAdapter?.focusEditText(getNoteIndexWithId(noteId))
}
}
}
}
Expand Down Expand Up @@ -467,20 +476,22 @@ class MainActivity : SimpleActivity() {
private fun exportAllNotes() {
ExportFilesDialog(this, mNotes) { parent, extension ->
var failCount = 0
mNotes = dbHelper.getNotes()
mNotes.forEachIndexed { index, note ->
val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension"
val file = File(parent, filename)
if (!filename.isAValidFilename()) {
toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename)))
} else {
exportNoteValueToFile(file.absolutePath, note.value, false) {
if (!it) {
failCount++
}

if (index == mNotes.size - 1) {
toast(if (failCount == 0) R.string.exporting_successful else R.string.exporting_some_entries_failed)
NotesHelper(this).getNotes {
mNotes = it
mNotes.forEachIndexed { index, note ->
val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension"
val file = File(parent, filename)
if (!filename.isAValidFilename()) {
toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename)))
} else {
exportNoteValueToFile(file.absolutePath, note.value, false) {
if (!it) {
failCount++
}

if (index == mNotes.size - 1) {
toast(if (failCount == 0) R.string.exporting_successful else R.string.exporting_some_entries_failed)
}
}
}
}
Expand Down Expand Up @@ -570,22 +581,23 @@ class MainActivity : SimpleActivity() {

private fun doDeleteNote(note: Note, deleteFile: Boolean) {
dbHelper.deleteNote(mCurrentNote.id!!)
mNotes = dbHelper.getNotes()

val firstNoteId = mNotes[0].id
updateSelectedNote(firstNoteId!!)
if (config.widgetNoteId == note.id) {
config.widgetNoteId = mCurrentNote.id!!
updateWidgets()
}
NotesHelper(this).getNotes {
mNotes = it
val firstNoteId = mNotes[0].id
updateSelectedNote(firstNoteId!!)
if (config.widgetNoteId == note.id) {
config.widgetNoteId = mCurrentNote.id!!
updateWidgets()
}

invalidateOptionsMenu()
initViewPager()
invalidateOptionsMenu()
initViewPager()

if (deleteFile) {
deleteFile(FileDirItem(note.path, note.title)) {
if (!it) {
toast(R.string.unknown_error_occurred)
if (deleteFile) {
deleteFile(FileDirItem(note.path, note.title)) {
if (!it) {
toast(R.string.unknown_error_occurred)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.extensions.config
import com.simplemobiletools.notes.pro.extensions.dbHelper
import com.simplemobiletools.notes.pro.extensions.updateWidgets
import com.simplemobiletools.notes.pro.helpers.*
import kotlinx.android.synthetic.main.activity_settings.*
Expand Down Expand Up @@ -109,7 +108,10 @@ class SettingsActivity : SimpleActivity() {
}

private fun setupShowNotePicker() {
settings_show_note_picker_holder.beVisibleIf(dbHelper.getNotes().size > 1)
NotesHelper(this).getNotes {
settings_show_note_picker_holder.beVisibleIf(it.size > 1)
}

settings_show_note_picker.isChecked = config.showNotePicker
settings_show_note_picker_holder.setOnClickListener {
settings_show_note_picker.toggle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.simplemobiletools.notes.pro.extensions.config
import com.simplemobiletools.notes.pro.extensions.dbHelper
import com.simplemobiletools.notes.pro.extensions.getTextSize
import com.simplemobiletools.notes.pro.helpers.MyWidgetProvider
import com.simplemobiletools.notes.pro.helpers.NotesHelper
import com.simplemobiletools.notes.pro.models.Note
import com.simplemobiletools.notes.pro.models.Widget
import kotlinx.android.synthetic.main.widget_config.*
Expand Down Expand Up @@ -80,10 +81,12 @@ class WidgetConfigureActivity : SimpleActivity() {

mTextColor = config.widgetTextColor
updateTextColor()
mNotes = dbHelper.getNotes()
mIsCustomizingColors = intent.extras?.getBoolean(IS_CUSTOMIZING_COLORS) ?: false
notes_picker_holder.beVisibleIf(mNotes.size > 1 && !mIsCustomizingColors)
updateCurrentNote(mNotes.first())
NotesHelper(this).getNotes {
mNotes = it
notes_picker_holder.beVisibleIf(mNotes.size > 1 && !mIsCustomizingColors)
updateCurrentNote(mNotes.first())
}
}

private fun showNoteSelector() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import com.simplemobiletools.commons.extensions.setTextSize
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.R.id.widget_text_holder
import com.simplemobiletools.notes.pro.extensions.config
import com.simplemobiletools.notes.pro.extensions.dbHelper
import com.simplemobiletools.notes.pro.extensions.getTextSize
import com.simplemobiletools.notes.pro.extensions.notesDB
import com.simplemobiletools.notes.pro.helpers.GRAVITY_CENTER
import com.simplemobiletools.notes.pro.helpers.GRAVITY_RIGHT
import com.simplemobiletools.notes.pro.helpers.NOTE_ID
Expand All @@ -24,7 +24,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi
override fun getViewAt(position: Int): RemoteViews {
val noteId = intent.getIntExtra(NOTE_ID, 1)
val views = RemoteViews(context.packageName, R.layout.widget_text_layout).apply {
val note = context.dbHelper.getNoteWithId(noteId)
val note = context.notesDB.getNoteWithId(noteId)
if (note != null) {
val noteText = note.getNoteStoredValue() ?: ""
val textSize = context.getTextSize() / context.resources.displayMetrics.density
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.simplemobiletools.notes.pro.dialogs

import android.app.Activity
import android.view.View
import android.view.ViewGroup
import android.widget.RadioGroup
import androidx.appcompat.app.AlertDialog
Expand All @@ -10,17 +11,23 @@ import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.extensions.config
import com.simplemobiletools.notes.pro.extensions.dbHelper
import com.simplemobiletools.notes.pro.helpers.NotesHelper
import com.simplemobiletools.notes.pro.models.Note
import kotlinx.android.synthetic.main.dialog_open_note.view.*
import kotlinx.android.synthetic.main.open_note_item.view.*

class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> Unit) {
lateinit var dialog: AlertDialog
private var dialog: AlertDialog? = null

init {
val view = activity.layoutInflater.inflate(R.layout.dialog_open_note, null)
NotesHelper(activity).getNotes {
initDialog(it, view)
}
}

private fun initDialog(notes: ArrayList<Note>, view: View) {
val textColor = activity.config.textColor
val notes = activity.dbHelper.getNotes()
notes.forEach {
activity.layoutInflater.inflate(R.layout.open_note_item, null).apply {
val note = it
Expand All @@ -31,7 +38,7 @@ class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> U

setOnClickListener {
callback(id)
dialog.dismiss()
dialog?.dismiss()
}
}
open_note_item_icon.apply {
Expand Down
Loading

0 comments on commit 885170e

Please sign in to comment.