move the function for getting note value in the note model itself

This commit is contained in:
tibbi 2018-04-05 21:55:03 +02:00
parent 696858e2bc
commit 5ca8290c97
4 changed files with 34 additions and 34 deletions

View File

@ -11,7 +11,6 @@ import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.R.id.widget_text_holder
import com.simplemobiletools.notes.extensions.config
import com.simplemobiletools.notes.extensions.dbHelper
import com.simplemobiletools.notes.extensions.getNoteStoredValue
import com.simplemobiletools.notes.extensions.getTextSize
import com.simplemobiletools.notes.helpers.GRAVITY_CENTER
import com.simplemobiletools.notes.helpers.GRAVITY_RIGHT
@ -19,14 +18,13 @@ import com.simplemobiletools.notes.helpers.OPEN_NOTE_ID
class WidgetAdapter(val context: Context) : RemoteViewsService.RemoteViewsFactory {
private val textIds = arrayOf(R.id.widget_text_left, R.id.widget_text_center, R.id.widget_text_right)
val config = context.config
private val widgetTextColor = config.widgetTextColor
private val widgetTextColor = context.config.widgetTextColor
override fun getViewAt(position: Int): RemoteViews {
val views = RemoteViews(context.packageName, R.layout.widget_text_layout).apply {
val note = context.dbHelper.getNote(context.config.widgetNoteId)
if (note != null) {
val noteText = context.getNoteStoredValue(note) ?: ""
val noteText = note.getNoteStoredValue() ?: ""
val textSize = context.getTextSize() / context.resources.displayMetrics.density
for (id in textIds) {
setText(id, noteText)

View File

@ -6,9 +6,10 @@ import android.content.Context
import android.content.Intent
import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.helpers.*
import com.simplemobiletools.notes.models.Note
import java.io.File
import java.io.FileNotFoundException
val Context.config: Config get() = Config.newInstance(applicationContext)
val Context.dbHelper: DBHelper get() = DBHelper.newInstance(applicationContext)
fun Context.getTextSize() =
when (config.fontSize) {
@ -19,28 +20,12 @@ fun Context.getTextSize() =
}
fun Context.updateWidget() {
val widgetManager = AppWidgetManager.getInstance(this)
val ids = widgetManager.getAppWidgetIds(ComponentName(this, MyWidgetProvider::class.java))
val widgetIDs = AppWidgetManager.getInstance(this).getAppWidgetIds(ComponentName(this, MyWidgetProvider::class.java))
if (widgetIDs.isNotEmpty()) {
Intent(this, MyWidgetProvider::class.java).apply {
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIDs)
sendBroadcast(this)
}
}
val Context.config: Config get() = Config.newInstance(applicationContext)
val Context.dbHelper: DBHelper get() = DBHelper.newInstance(applicationContext)
fun Context.getNoteStoredValue(note: Note): String? {
return if (note.path.isNotEmpty()) {
return try {
File(note.path).readText()
} catch (e: FileNotFoundException) {
null
}
} else {
note.value
}
}

View File

@ -16,7 +16,10 @@ import com.simplemobiletools.commons.extensions.beVisible
import com.simplemobiletools.commons.extensions.onGlobalLayout
import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.activities.MainActivity
import com.simplemobiletools.notes.extensions.*
import com.simplemobiletools.notes.extensions.config
import com.simplemobiletools.notes.extensions.dbHelper
import com.simplemobiletools.notes.extensions.getTextSize
import com.simplemobiletools.notes.extensions.updateWidget
import com.simplemobiletools.notes.helpers.*
import com.simplemobiletools.notes.models.Note
import kotlinx.android.synthetic.main.fragment_note.*
@ -61,8 +64,7 @@ class NoteFragment : Fragment() {
view.notes_view.apply {
typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT
val fileContents = context.getNoteStoredValue(note)
val fileContents = note.getNoteStoredValue()
if (fileContents == null) {
(activity as MainActivity).deleteNote(false)
return
@ -126,7 +128,7 @@ class NoteFragment : Fragment() {
}
val newText = getCurrentNoteViewText()
val oldText = context!!.getNoteStoredValue(note)
val oldText = note.getNoteStoredValue()
if (newText != null && newText != oldText) {
note.value = newText
saveNoteValue(note)

View File

@ -1,3 +1,18 @@
package com.simplemobiletools.notes.models
data class Note(var id: Int, var title: String, var value: String, val type: Int, var path: String = "")
import java.io.File
import java.io.FileNotFoundException
data class Note(var id: Int, var title: String, var value: String, val type: Int, var path: String = "") {
fun getNoteStoredValue(): String? {
return if (path.isNotEmpty()) {
return try {
File(path).readText()
} catch (e: FileNotFoundException) {
null
}
} else {
value
}
}
}