@@ -3,15 +3,23 @@ package com.hackware.mormont.notebook
33import androidx.appcompat.app.AppCompatActivity
44import android.os.Bundle
55import android.os.Handler
6+ import android.view.Menu
7+ import android.view.MenuInflater
8+ import android.view.MenuItem
9+ import android.view.View
610import android.widget.EditText
11+ import androidx.appcompat.widget.SearchView
712import com.hackware.mormont.notebook.db.NotesDataBase
813import com.hackware.mormont.notebook.db.entity.NotesData
914import com.hackware.mormont.notebook.utils.DateUtil
15+ import kotlinx.android.synthetic.main.activity_main.*
16+ import org.wordpress.android.util.ToastUtils
1017import org.wordpress.aztec.Aztec
1118import org.wordpress.aztec.AztecText
1219import org.wordpress.aztec.ITextFormat
1320import org.wordpress.aztec.toolbar.AztecToolbar
1421import org.wordpress.aztec.toolbar.IAztecToolbarClickListener
22+ import java.util.*
1523import kotlin.random.Random
1624
1725
@@ -23,10 +31,12 @@ open class Editor : AppCompatActivity(),
2331
2432 private lateinit var title: EditText
2533 private lateinit var mDbWorkerThread: DbWorkerThread
34+ private lateinit var mToolbar: AztecToolbar
2635 private var mDb: NotesDataBase ? = null
2736 private val INTENT_NOTE_ID : String = " NoteId"
2837 private var isEditing = false
2938 private lateinit var mData: NotesData
39+ private var isShowToolbar = false
3040
3141 private val mUiHandler = Handler ()
3242
@@ -36,13 +46,15 @@ open class Editor : AppCompatActivity(),
3646 super .onCreate(savedInstanceState)
3747 setContentView(R .layout.activity_editor)
3848
49+ setSupportActionBar(findViewById(R .id.toolbar))
50+
3951 title = findViewById(R .id.title)
4052 mDb = NotesDataBase .getInstance(this )
4153
4254 val visualEditor = findViewById<AztecText >(R .id.aztec)
43- val toolbar = findViewById<AztecToolbar >(R .id.formatting_toolbar)
55+ mToolbar = findViewById<AztecToolbar >(R .id.formatting_toolbar)
4456
45- aztec = Aztec .with (visualEditor, toolbar , this )
57+ aztec = Aztec .with (visualEditor, mToolbar , this )
4658
4759 val noteId: Long = intent.getLongExtra(INTENT_NOTE_ID , 0 .toLong())
4860 if ( noteId != 0 .toLong()){
@@ -51,31 +63,69 @@ open class Editor : AppCompatActivity(),
5163 }
5264 }
5365
66+ override fun onCreateOptionsMenu (menu : Menu ): Boolean {
67+ val inflater: MenuInflater = menuInflater
68+ inflater.inflate(R .menu.appbar_editor_menu, menu)
69+ return true
70+ }
71+
72+ override fun onOptionsItemSelected (item : MenuItem ): Boolean {
73+ return when (item.itemId){
74+ R .id.save_note -> {
75+ when (title.text.isEmpty()){
76+ true -> ToastUtils .showToast(applicationContext, R .string.don_save_empty)
77+ false -> {
78+ if (isEditing) {
79+ saveEditedData()
80+ }
81+ else {
82+ isEditing = true
83+ saveNewNote()
84+ }
85+ }
86+ }
87+ true
88+ }
89+ R .id.show_tool -> {
90+ if (isShowToolbar){
91+ mToolbar.visibility = View .INVISIBLE
92+ isShowToolbar = false
93+ }
94+ else {
95+ mToolbar.visibility = View .VISIBLE
96+ isShowToolbar = true
97+ }
98+ true
99+ }
100+ else -> super .onOptionsItemSelected(item)
101+ }
102+ }
103+
54104 override fun onBackPressed () {
55- var data: NotesData
56105 if (isEditing){
57106 if (mData.title != title.text.toString() || mData.rawContent != aztec.visualEditor.toFormattedHtml() )
58107 saveEditedData()
59108 }else {
60- if (! title.text.isEmpty()) {
61- data = NotesData (
62- Random .nextLong(),
63- Random .nextLong(), DateUtil .getCurrentDate(), DateUtil .getCurrentDate(),
64- title.text.toString(),
65- aztec.visualEditor.text.toString(),
66- aztec.visualEditor.toFormattedHtml()
67- )
68-
69- val task = Runnable {
70- mDb?.notesDataDao()?.insertNote(data)
71- }
72- mDbWorkerThread.postTask(task)
109+ when (title.text.isEmpty()) {
110+ true -> ToastUtils .showToast(applicationContext, R .string.don_save_empty)
111+ false -> saveNewNote()
73112 }
74113 }
75-
76114 return super .onBackPressed()
77115 }
78-
116+ private fun saveNewNote (){
117+ mData = NotesData (
118+ Random .nextLong(),
119+ Random .nextLong(), DateUtil .getCurrentDate(), DateUtil .getCurrentDate(),
120+ title.text.toString(),
121+ aztec.visualEditor.text.toString(),
122+ aztec.visualEditor.toFormattedHtml()
123+ )
124+ val task = Runnable {
125+ mDb?.notesDataDao()?.insertNote(mData)
126+ }
127+ mDbWorkerThread.postTask(task)
128+ }
79129 private fun loadDataFromBd (id : Long ){
80130 val task = Runnable {
81131 mData = mDb!! .notesDataDao().loadNoteWithId(id)
@@ -89,6 +139,7 @@ open class Editor : AppCompatActivity(),
89139 mDbWorkerThread.postTask(task)
90140 }
91141
142+
92143 private fun saveEditedData (){
93144 mData.title = title.text.toString()
94145 mData.strContent = aztec.visualEditor.text.toString()
@@ -101,7 +152,6 @@ open class Editor : AppCompatActivity(),
101152 mDbWorkerThread.postTask(task)
102153 }
103154
104-
105155 override fun onToolbarCollapseButtonClicked () {
106156 }
107157
0 commit comments