Przeglądaj źródła

chore: refurbished notes

beautusg 7 lat temu
rodzic
commit
4d68302de2

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

@@ -11,7 +11,7 @@ data class Note(
         @field:ColumnInfo(name = "body")
         var body: String,
         @field:ColumnInfo(name = "edited")
-        var edited: Long
+        var edited: Long = -1L
 ) : Serializable {
     @field:ColumnInfo(name = "id")
     @PrimaryKey(autoGenerate = true)

+ 61 - 0
app/src/main/java/com/sduduzog/slimlauncher/ui/main/EditNoteFragment.kt

@@ -2,17 +2,41 @@ package com.sduduzog.slimlauncher.ui.main
 
 import android.content.Context
 import android.os.Bundle
+import android.util.Log
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
 import android.view.inputmethod.InputMethodManager
+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.edit_note_fragment.*
+import java.security.MessageDigest
+import java.util.*
 
 class EditNoteFragment : BaseFragment() {
 
     override fun getFragmentView(): ViewGroup = edit_note_fragment
+    private lateinit var note: Note
+    private lateinit var viewModel: MainViewModel
+    private lateinit var initialDigest: String
+    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("")
+            }
+        }
+        initialDigest = hash(note.title + note.body)
+        activity?.let {
+            viewModel = ViewModelProviders.of(it).get(MainViewModel::class.java)
+        } ?: throw Error("Activity null, something here is fucked up")
+    }
 
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
         return inflater.inflate(R.layout.edit_note_fragment, container, false)
@@ -20,9 +44,46 @@ class EditNoteFragment : BaseFragment() {
 
     override fun onActivityCreated(savedInstanceState: Bundle?) {
         super.onActivityCreated(savedInstanceState)
+
+        if (note.edited == -1L) {
+            // Wait
+        } else {
+            edit_note_fragment_title.setText(note.title.orEmpty())
+            edit_note_fragment_body.setText(note.body)
+        }
+
         if (edit_note_fragment_body.requestFocus()) {
             val imm = activity!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
             imm.showSoftInput(edit_note_fragment_title, InputMethodManager.SHOW_IMPLICIT)
         }
+        edit_note_fragment_save.setOnClickListener {
+            save()?.let {
+                val bundle = Bundle()
+                bundle.putSerializable(getString(R.string.nav_key_note), it)
+                Log.d("check", "$it")
+                Navigation.findNavController(edit_note_fragment).navigate(R.id.action_editNoteFragment_to_noteFragment, bundle)
+            }
+        }
+    }
+
+    private fun hash(input: String): String {
+        val bytes = input.toByteArray(charset("UTF-8"))
+        val md = MessageDigest.getInstance("MD5")
+        md.update(bytes)
+        return String(md.digest())
+    }
+
+    private fun save(): Note? {
+        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
+        newNote.body = body.trim()
+        newNote.id = note.id
+        val currentDigest = hash(newNote.title + newNote.body)
+        if (body.isEmpty()) return null
+        if (initialDigest == currentDigest) return null
+        if (note.id == null) viewModel.add(newNote) else viewModel.update(newNote)
+        return newNote
     }
 }

+ 11 - 77
app/src/main/java/com/sduduzog/slimlauncher/ui/main/NoteFragment.kt

@@ -5,14 +5,11 @@ import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
-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.*
-import java.security.MessageDigest
 
 
 class NoteFragment : BaseFragment() {
@@ -20,96 +17,33 @@ class NoteFragment : BaseFragment() {
     override fun getFragmentView(): ViewGroup = note_fragment
 
     private lateinit var note: Note
-    private lateinit var viewModel: MainViewModel
-    private lateinit var initialDigest: String
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         arguments.let {
-            note = if (it !=null && it.containsKey("note")) {
-                it.get("note") as Note
+            note = if (it != null && it.containsKey(getString(R.string.nav_key_note))) {
+                it.get(getString(R.string.nav_key_note)) as Note
             } else {
-                Note("", -1L)
+                Note("")
             }
         }
-        initialDigest = hash(note.title + note.body)
-        activity?.let {
-            viewModel = ViewModelProviders.of(it).get(MainViewModel::class.java)
-        } ?: throw Error("Activity null, something here is fucked up")
     }
 
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                               savedInstanceState: Bundle?): View? {
-        // Inflate the layout for this fragment
         return inflater.inflate(R.layout.note_fragment, container, false)
     }
 
     override fun onActivityCreated(savedInstanceState: Bundle?) {
         super.onActivityCreated(savedInstanceState)
-        note_fragment_edit.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_noteFragment_to_editNoteFragment))
-    }
-
-//    override fun onActivityCreated(savedInstanceState: Bundle?) {
-//        super.onActivityCreated(savedInstanceState)
-//        if (note.edited == -1L) {
-//            editBody()
-//        } else {
-//            bodyEditText.visibility = View.GONE
-//            textBody.visibility = View.VISIBLE
-//            textBody.text = note.body
-//            titleEditText.setText(note.title.orEmpty())
-//            bodyEditText.setText(note.body)
-//            titleEditText.isEnabled = false
-//        }
-//        titleEditText.setOnEditorActionListener { _, _, _ ->
-//            editBody()
-//            true
-//        }
-//        textBody.setOnClickListener(object : DoubleClickListener() {
-//            override fun onDoubleClick(v: View) {
-//                titleEditText.isEnabled = true
-//                editBody()
-//            }
-//
-//            override fun onSingleClick(v: View) {
-//            //     Do nothing
-//            }
-//        })
-//    }
-
-//    private fun editBody() {
-//        textBody.visibility = View.GONE
-//        bodyEditText.visibility = View.VISIBLE
-//        if (bodyEditText.requestFocus()) {
-//            val imm = activity!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
-//            imm.showSoftInput(bodyEditText, InputMethodManager.SHOW_IMPLICIT)
-//            bodyEditText.setSelection(note.body.length)
-//        }
-//    }
-
-//    private fun saveNote() {
-//        val body = bodyEditText.text.toString()
-//        val title = titleEditText.text.toString()
-//        val newNote = Note(body, Date().time)
-//        newNote.title = if (title.isEmpty()) null else title
-//        newNote.body = body.trim()
-//        newNote.id = note.id
-//        val currentDigest = hash(newNote.title + newNote.body)
-//        if (body.isEmpty()) return
-//        if (initialDigest == currentDigest) return
-//        if (note.id == null) viewModel.add(newNote) else viewModel.update(newNote)
-//
-//    }
-
-    private fun hash(input: String): String {
-        val bytes = input.toByteArray(charset("UTF-8"))
-        val md = MessageDigest.getInstance("MD5")
-        md.update(bytes)
-        return String(md.digest())
+        note.title?.let {
+            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))
     }
 
-    override fun onBack(): Boolean {
-        //saveNote()
-        return false
-    }
+    override fun onBack(): Boolean = false
 }

+ 2 - 0
app/src/main/res/layout/edit_note_fragment.xml

@@ -23,6 +23,7 @@
             android:padding="12dp"
             android:hint="@string/edit_note_fragment_untitled"
             android:textSize="18sp"
+            android:inputType="textCapSentences"
             app:layout_constraintEnd_toStartOf="@+id/edit_note_fragment_save"
             app:layout_constraintStart_toStartOf="parent"
             android:singleLine="true"
@@ -49,6 +50,7 @@
             android:imeOptions="normal"
             android:inputType="textMultiLine|textCapSentences"
             android:padding="12dp"
+            android:textSize="14sp"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toBottomOf="@id/edit_note_fragment_title"

+ 2 - 1
app/src/main/res/layout/note_fragment.xml

@@ -38,10 +38,11 @@
             app:layout_constraintTop_toTopOf="parent" />
 
         <TextView
-            android:id="@+id/textView4"
+            android:id="@+id/note_fragment_body"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:padding="12dp"
+            android:textSize="14sp"
             android:textAppearance="@style/Base.TextAppearance.AppCompat"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintStart_toStartOf="parent"

+ 10 - 6
app/src/main/res/navigation/nav_graph.xml

@@ -24,7 +24,7 @@
         android:id="@+id/optionsFragment"
         android:name="com.sduduzog.slimlauncher.ui.options.OptionsFragment"
         android:label="options_fragment"
-        tools:layout="@layout/options_fragment" >
+        tools:layout="@layout/options_fragment">
         <action
             android:id="@+id/action_optionsFragment_to_customiseAppsFragment"
             app:destination="@id/customiseAppsFragment" />
@@ -33,7 +33,7 @@
         android:id="@+id/customiseAppsFragment"
         android:name="com.sduduzog.slimlauncher.ui.options.CustomiseAppsFragment"
         android:label="customise_apps_fragment"
-        tools:layout="@layout/customise_apps_fragment" >
+        tools:layout="@layout/customise_apps_fragment">
         <action
             android:id="@+id/action_customiseAppsFragment_to_addAppFragment"
             app:destination="@id/addAppFragment" />
@@ -56,14 +56,13 @@
         android:id="@+id/notesFragment"
         android:name="com.sduduzog.slimlauncher.ui.main.NotesFragment"
         android:label="notes_fragment"
-        tools:layout="@layout/notes_fragment" >
+        tools:layout="@layout/notes_fragment">
         <action
             android:id="@+id/action_notesFragment_to_noteFragment"
             app:destination="@id/noteFragment" />
         <action
             android:id="@+id/action_notesFragment_to_editNoteFragment"
-            app:destination="@id/editNoteFragment"
-            app:popUpTo="@+id/noteFragment" />
+            app:destination="@id/editNoteFragment" />
     </fragment>
     <fragment
         android:id="@+id/tasksFragment"
@@ -74,6 +73,11 @@
         android:id="@+id/editNoteFragment"
         android:name="com.sduduzog.slimlauncher.ui.main.EditNoteFragment"
         android:label="edit_note_fragment"
-        tools:layout="@layout/edit_note_fragment" />
+        tools:layout="@layout/edit_note_fragment">
+        <action
+            android:id="@+id/action_editNoteFragment_to_noteFragment"
+            app:destination="@id/noteFragment"
+            app:popUpTo="@+id/notesFragment" />
+    </fragment>
 
 </navigation>

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -110,5 +110,6 @@ Because of this, we are ruthless at driving efficiency and automating the rote s
     <string name="edit_note_fragment_save">SAVE</string>
     <string name="edit_note_fragment_untitled">Untitled</string>
     <string name="note_fragment_edit">EDIT</string>
+    <string name="nav_key_note">note</string>
 
 </resources>