Przeglądaj źródła

chore: blocked thought v3

beautusg 7 lat temu
rodzic
commit
32bf930431

+ 0 - 2
app/src/main/java/com/sduduzog/slimlauncher/data/BaseDatabase.kt

@@ -68,11 +68,9 @@ abstract class BaseDatabase : RoomDatabase() {
 
         private val MIGRATION_4_5 = object : Migration(4, 5) {
             override fun migrate(database: SupportSQLiteDatabase) {
-                database.beginTransaction()
                 database.execSQL("ALTER TABLE `notes` RENAME TO `notes_old`")
                 database.execSQL("CREATE TABLE IF NOT EXISTS `notes` (`body` TEXT NOT NULL, `edited` INTEGER NOT NULL, `is_voice` INTEGER NOT NULL DEFAULT 0, `id` INTEGER PRIMARY KEY, `title` TEXT, `path` TEXT)")
                 database.execSQL("INSERT INTO `notes` (`body`, `edited`, `id`, `title`) SELECT `body`, `edited`, `id`, `title` FROM `notes_old`")
-                database.endTransaction()
             }
         }
     }

+ 3 - 3
app/src/main/java/com/sduduzog/slimlauncher/data/model/Note.kt

@@ -8,15 +8,15 @@ import java.io.Serializable
 
 @Entity(tableName = "notes")
 data class Note(
+        @field:ColumnInfo(name = "id")
+        @PrimaryKey
+        var id: Long,
         @field:ColumnInfo(name = "body")
         var body: String,
         @field:ColumnInfo(name = "edited")
         var edited: Long = -1L,
         @field:ColumnInfo(name = "is_voice")
         var is_voice: Boolean = false,
-        @field:ColumnInfo(name = "id")
-        @PrimaryKey(autoGenerate = true)
-        var id: Int? = null,
         @field:ColumnInfo(name = "title")
         var title: String? = null,
         @field:ColumnInfo(name = "path")

+ 20 - 17
app/src/main/java/com/sduduzog/slimlauncher/ui/main/NoteFragment.kt

@@ -5,8 +5,11 @@ import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import androidx.lifecycle.Observer
+import androidx.lifecycle.ViewModelProviders
 import androidx.navigation.Navigation
 import com.sduduzog.slimlauncher.R
+import com.sduduzog.slimlauncher.data.MainViewModel
 import com.sduduzog.slimlauncher.data.model.Note
 import com.sduduzog.slimlauncher.utils.BaseFragment
 import kotlinx.android.synthetic.main.note_fragment.*
@@ -15,20 +18,9 @@ import kotlinx.android.synthetic.main.note_fragment.*
 class NoteFragment : BaseFragment() {
 
     override fun getFragmentView(): ViewGroup = note_fragment
-
+    private lateinit var viewModel: MainViewModel
     private lateinit var note: Note
 
-    override fun onCreate(savedInstanceState: Bundle?) {
-        super.onCreate(savedInstanceState)
-        arguments.let {
-            note = if (it != null && it.containsKey(getString(R.string.nav_key_note))) {
-                it.get(getString(R.string.nav_key_note)) as Note
-            } else {
-                Note("")
-            }
-        }
-    }
-
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                               savedInstanceState: Bundle?): View? {
         return inflater.inflate(R.layout.note_fragment, container, false)
@@ -38,12 +30,23 @@ class NoteFragment : BaseFragment() {
         super.onActivityCreated(savedInstanceState)
         note.title?.let { if (it.isNotBlank()) note_fragment_title.text = it }
         note_fragment_body.text = note.body
-        val bundle = Bundle()
-        bundle.putSerializable(getString(R.string.nav_key_note), note)
-        note_fragment_edit.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_noteFragment_to_editNoteFragment, bundle))
-        if (note.is_voice) {
-
+        activity?.let {
+            viewModel = ViewModelProviders.of(it).get(MainViewModel::class.java)
+        } ?: throw Error("This is just dumb")
+        var id = 0L
+        arguments.let {
+            id = if (it != null && it.containsKey(getString(R.string.nav_key_note))) {
+                it.getLong(getString(R.string.nav_key_note))
+            } else throw Exception("How did we get here??")
         }
+        viewModel.notes.observe(this, Observer {
+            it?.let { notes ->
+                note = notes.first { note.id == id }
+                val bundle = Bundle()
+                bundle.putSerializable(getString(R.string.nav_key_note), note)
+                note_fragment_edit.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_noteFragment_to_editNoteFragment, bundle))
+            }
+        })
     }
 
     override fun onBack(): Boolean = false

+ 1 - 1
app/src/main/java/com/sduduzog/slimlauncher/ui/main/NotesFragment.kt

@@ -115,7 +115,7 @@ class NotesFragment : BaseFragment(), OnShitDoneToNotesListener {
 
     override fun onView(note: Note) {
         val bundle = Bundle()
-        bundle.putInt(getString(R.string.nav_key_note), note.id!!)
+        bundle.putLong(getString(R.string.nav_key_note), note.id)
         if (note.is_voice)
             Navigation.findNavController(notes_fragment)
                     .navigate(R.id.action_notesFragment_to_voiceNoteFragment, bundle)

+ 14 - 10
app/src/main/java/com/sduduzog/slimlauncher/ui/notes/EditNoteFragment.kt

@@ -28,7 +28,7 @@ class EditNoteFragment : BaseFragment() {
             note = if (it != null && it.containsKey(getString(R.string.nav_key_note))) {
                 it.get(getString(R.string.nav_key_note)) as Note
             } else {
-                Note("")
+                Note(-1L, "")
             }
         }
         initialDigest = hash(note.title + note.body)
@@ -71,17 +71,21 @@ class EditNoteFragment : BaseFragment() {
         return String(md.digest())
     }
 
-    private fun save(): Note? {
+    private fun save(): Long? {
         val body = edit_note_fragment_body.text.toString()
-        val title = edit_note_fragment_title.text.toString()
-        val newNote = Note(body, Date().time)
-        newNote.title = title.trim()
-        newNote.body = body.trim()
-        newNote.id = note.id
-        val currentDigest = hash(newNote.title + newNote.body)
         if (body.isEmpty()) return null
+        val title = edit_note_fragment_title.text.toString()
+        val timestamp = Date().time
+        if (note.id == -1L) {
+            val newNote = Note(timestamp, body, timestamp)
+            newNote.title = title.trim()
+            newNote.body = body.trim()
+            viewModel.add(newNote)
+            return newNote.id
+        }
+        val currentDigest = hash(title + body)
         if (initialDigest == currentDigest) return null
-        if (note.edited == -1L) viewModel.add(newNote) else viewModel.update(newNote)
-        return newNote
+        viewModel.update(note)
+        return note.id
     }
 }

+ 1 - 1
app/src/main/java/com/sduduzog/slimlauncher/utils/VoiceRecorder.kt

@@ -26,7 +26,7 @@ class VoiceRecorder private constructor() {
         val title = "Voice note : ${DateFormat.getDateInstance().format(Date(timestamp))}"
         val body = "Recorded at ${DateFormat.getTimeInstance().format(Date(timestamp))}"
         val path = "$fileName/$timestamp.3gp"
-        note = Note(body, timestamp, true).apply { this.title = title; this.path = path }
+        note = Note(timestamp, body, timestamp, true).apply { this.title = title; this.path = path }
         mediaRecorder?.setAudioSource(MediaRecorder.AudioSource.MIC)
         mediaRecorder?.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
         mediaRecorder?.setOutputFile(path)