Skip to content

Commit 63c8413

Browse files
committed
done a lot of work
1 parent a4a3acf commit 63c8413

File tree

17 files changed

+358
-180
lines changed

17 files changed

+358
-180
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ dependencies {
3030
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
3131
implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
3232
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
33+
implementation 'androidx.lifecycle:lifecycle-livedata:2.0.0'
3334
api ('com.github.wordpress-mobile.WordPress-Aztec-Android:aztec:v1.3.22')
3435
testImplementation 'junit:junit:4.12'
3536
androidTestImplementation 'androidx.test:runner:1.1.2-alpha02'
3637
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'
3738
implementation 'com.google.android.material:material:1.1.0-alpha04'
3839
implementation "android.arch.persistence.room:runtime:1.1.0"
3940
kapt "android.arch.persistence.room:compiler:1.1.0"
41+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
4042
}

app/src/main/java/com/hackware/mormont/notebook/Editor.kt

Lines changed: 63 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,106 @@
11
package com.hackware.mormont.notebook
22

3-
import android.annotation.SuppressLint
43
import androidx.appcompat.app.AppCompatActivity
5-
import android.content.res.Configuration
64
import android.os.Bundle
7-
import android.view.MotionEvent
8-
import android.view.View
9-
import org.wordpress.android.util.ToastUtils
5+
import android.os.Handler
6+
import android.widget.EditText
7+
import com.hackware.mormont.notebook.db.NotesDataBase
8+
import com.hackware.mormont.notebook.db.entity.NotesData
9+
import com.hackware.mormont.notebook.utils.DateUtil
1010
import org.wordpress.aztec.Aztec
1111
import org.wordpress.aztec.AztecText
12-
import org.wordpress.aztec.IHistoryListener
1312
import org.wordpress.aztec.ITextFormat
14-
import org.wordpress.aztec.plugins.CssUnderlinePlugin
15-
import org.wordpress.aztec.source.SourceViewEditText
1613
import org.wordpress.aztec.toolbar.AztecToolbar
1714
import org.wordpress.aztec.toolbar.IAztecToolbarClickListener
18-
import org.xml.sax.Attributes
15+
import kotlin.random.Random
16+
1917

2018

2119
open class Editor : AppCompatActivity(),
22-
View.OnTouchListener,
23-
IHistoryListener,
2420
IAztecToolbarClickListener {
2521

26-
27-
override fun onUndoEnabled() {
28-
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
29-
}
30-
31-
override fun onRedoEnabled() {
32-
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
33-
}
34-
3522
protected lateinit var aztec: Aztec
3623

37-
private var mIsKeyboardOpen = true
38-
private var mHideActionBarOnSoftKeyboardUp = false
24+
private lateinit var title: EditText
25+
private lateinit var mDbWorkerThread: DbWorkerThread
26+
private var mDb: NotesDataBase? = null
27+
private val INTENT_NOTE_ID: String = "NoteId"
28+
private var isEditing = false
29+
private lateinit var mData: NotesData
3930

40-
@SuppressLint("ClickableViewAccessibility")
41-
override fun onTouch(v: View, event: MotionEvent): Boolean {
42-
if (event.action == MotionEvent.ACTION_UP) {
43-
// If the WebView or EditText has received a touch event, the keyboard will be displayed and the action bar
44-
// should hide
45-
mIsKeyboardOpen = true
46-
hideActionBarIfNeeded()
47-
}
48-
return false }
31+
private val mUiHandler = Handler()
4932

5033
override fun onCreate(savedInstanceState: Bundle?) {
34+
mDbWorkerThread = DbWorkerThread("dbWorkerEditorThread")
35+
mDbWorkerThread.start()
5136
super.onCreate(savedInstanceState)
5237
setContentView(R.layout.activity_editor)
5338

54-
if (resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
55-
mHideActionBarOnSoftKeyboardUp = true
56-
}
39+
title = findViewById(R.id.title)
40+
mDb = NotesDataBase.getInstance(this)
41+
5742
val visualEditor = findViewById<AztecText>(R.id.aztec)
5843
val toolbar = findViewById<AztecToolbar>(R.id.formatting_toolbar)
5944

60-
6145
aztec = Aztec.with(visualEditor, toolbar, this)
62-
.setOnTouchListener(this)
63-
64-
// aztec.visualEditor.setCalypsoMode(true)
65-
66-
aztec.addPlugin(CssUnderlinePlugin())
67-
68-
}
69-
70-
override fun onPause() {
71-
super.onPause()
72-
mIsKeyboardOpen = false
73-
}
74-
75-
override fun onResume() {
76-
super.onResume()
7746

78-
showActionBarIfNeeded()
47+
val noteId: Long= intent.getLongExtra(INTENT_NOTE_ID, 0.toLong())
48+
if( noteId != 0.toLong()){
49+
isEditing = true
50+
loadDataFromBd(noteId)
51+
}
7952
}
8053

8154
override fun onBackPressed() {
82-
mIsKeyboardOpen = false
83-
showActionBarIfNeeded()
55+
var data: NotesData
56+
if (isEditing){
57+
if (mData.title != title.text.toString() || mData.rawContent != aztec.visualEditor.toFormattedHtml() )
58+
saveEditedData()
59+
}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)
73+
}
74+
}
8475

8576
return super.onBackPressed()
8677
}
8778

88-
private fun isHardwareKeyboardPresent(): Boolean {
89-
val config = resources.configuration
90-
var returnValue = false
91-
if (config.keyboard != Configuration.KEYBOARD_NOKEYS) {
92-
returnValue = true
79+
private fun loadDataFromBd(id: Long){
80+
val task = Runnable {
81+
mData = mDb!!.notesDataDao().loadNoteWithId(id)
82+
mUiHandler.post{
83+
if (mData.noteId == id){
84+
title.setText(mData.title)
85+
aztec.visualEditor.fromHtml(mData.rawContent)
86+
}
9387
}
94-
return returnValue
9588
}
89+
mDbWorkerThread.postTask(task)
90+
}
9691

97-
private fun hideActionBarIfNeeded() {
92+
private fun saveEditedData(){
93+
mData.title = title.text.toString()
94+
mData.strContent = aztec.visualEditor.text.toString()
95+
mData.rawContent = aztec.visualEditor.toFormattedHtml()
96+
mData.modifiedDate = DateUtil.getCurrentDate()
9897

99-
val actionBar = supportActionBar
100-
if (actionBar != null
101-
&& !isHardwareKeyboardPresent()
102-
&& mHideActionBarOnSoftKeyboardUp
103-
&& mIsKeyboardOpen
104-
&& actionBar.isShowing) {
105-
actionBar.hide()
106-
}
98+
val task = Runnable {
99+
mDb?.notesDataDao()?.updateNote(mData)
107100
}
101+
mDbWorkerThread.postTask(task)
102+
}
108103

109-
/**
110-
* Show the action bar if needed.
111-
*/
112-
private fun showActionBarIfNeeded() {
113-
114-
val actionBar = supportActionBar
115-
if (actionBar != null && !actionBar.isShowing) {
116-
actionBar.show()
117-
}
118-
}
119104

120105
override fun onToolbarCollapseButtonClicked() {
121106
}

app/src/main/java/com/hackware/mormont/notebook/MainActivity.kt

Lines changed: 95 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
package com.hackware.mormont.notebook
22

3+
34
import android.content.Intent
45
import androidx.appcompat.app.AppCompatActivity
56
import android.os.Bundle
67
import android.os.Handler
8+
import android.util.Log
79
import android.view.*
8-
import android.widget.ListView
9-
import android.widget.PopupMenu
10+
import android.widget.*
11+
import androidx.appcompat.widget.SearchView
12+
import androidx.lifecycle.Observer
13+
import androidx.recyclerview.widget.ItemTouchHelper
14+
import androidx.recyclerview.widget.LinearLayoutManager
15+
import androidx.recyclerview.widget.RecyclerView
1016
import com.hackware.mormont.notebook.adapters.NoteListAdapter
17+
import com.hackware.mormont.notebook.adapters.SearchNoteListAdapter
1118
import com.hackware.mormont.notebook.db.NotesDataBase
19+
import com.hackware.mormont.notebook.db.entity.NotesData
1220
import org.wordpress.android.util.ToastUtils.showToast
21+
import java.util.*
22+
import kotlin.collections.ArrayList
1323

1424
class MainActivity : AppCompatActivity(), PopupMenu.OnMenuItemClickListener {
1525

1626
private var mDb: NotesDataBase? = null
17-
18-
private lateinit var mListView: ListView
27+
private lateinit var searchView: SearchView
28+
private lateinit var recyclerView: RecyclerView
29+
private lateinit var viewManager :RecyclerView.LayoutManager
1930
private lateinit var mAdapter: NoteListAdapter
31+
private lateinit var mSearchAdapter: SearchNoteListAdapter
2032
private lateinit var mFab : View
21-
33+
private lateinit var mItems: ArrayList<NotesData>
2234
private lateinit var mDbWorkerThread: DbWorkerThread
2335

2436
private val mUiHandler = Handler()
@@ -33,14 +45,52 @@ class MainActivity : AppCompatActivity(), PopupMenu.OnMenuItemClickListener {
3345
mDbWorkerThread = DbWorkerThread("dbWorkerThread")
3446
mDbWorkerThread.start()
3547

36-
mListView = findViewById(R.id.listView)
48+
viewManager = LinearLayoutManager(this)
49+
recyclerView = findViewById(R.id.recycleView)
50+
recyclerView.layoutManager = viewManager
51+
3752
mDb = NotesDataBase.getInstance(this)
53+
loadDataInListView()
3854

3955
mFab.setOnClickListener { view ->
40-
startActivity(Intent(this, Editor::class.java))
56+
startActivity(Intent(this, Editor::class.java))
4157
}
58+
setRecyclerViewItemTouchListener()
4259

43-
loadDataInListView()
60+
}
61+
62+
private fun setRecyclerViewItemTouchListener() {
63+
val itemTouchCallback = object : ItemTouchHelper.SimpleCallback(0,
64+
ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
65+
override fun onMove(
66+
recyclerView: RecyclerView,
67+
viewHolder: RecyclerView.ViewHolder,
68+
target: RecyclerView.ViewHolder
69+
): Boolean {
70+
return false
71+
}
72+
73+
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
74+
val position = viewHolder.adapterPosition
75+
toDeleteElementFromDb(position)
76+
showToast(applicationContext, "Swipe Detected p:$position ))) ")
77+
}
78+
}
79+
val itemTouchHelper = ItemTouchHelper(itemTouchCallback)
80+
itemTouchHelper.attachToRecyclerView(recyclerView)
81+
}
82+
83+
private fun toDeleteElementFromDb(position: Int) {
84+
val id = mItems[position].noteId
85+
val size = mItems.size
86+
Log.d("test", "$size , $position $")
87+
mDbWorkerThread.postTask(Runnable{
88+
mDb?.notesDataDao()?.deleteNote(mItems[position])
89+
mUiHandler.post(Runnable {
90+
mItems.removeAt(position)
91+
recyclerView.adapter!!.notifyItemRemoved(position)
92+
})
93+
})
4494
}
4595

4696
override fun onResume() {
@@ -59,14 +109,44 @@ class MainActivity : AppCompatActivity(), PopupMenu.OnMenuItemClickListener {
59109
showPopup(findViewById(R.id.sett))
60110
true
61111
}
62-
R.id.find -> {
63-
true
112+
R.id.action_search -> {
113+
searchView = item.actionView as SearchView
114+
searchQueryTextListener(searchView)
64115
}
65116
else -> super.onOptionsItemSelected(item)
66117
}
67118
}
68119

69-
private fun showPopup(v: View): Boolean{
120+
private fun searchQueryTextListener(searchView: SearchView): Boolean {
121+
searchView.queryHint = "Search notes"
122+
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
123+
override fun onQueryTextChange(newText: String): Boolean {
124+
getNotesFromDb(newText)
125+
return true
126+
}
127+
128+
override fun onQueryTextSubmit(query: String): Boolean {
129+
getNotesFromDb(query)
130+
showToast(applicationContext, "Query Post")
131+
return true
132+
}
133+
})
134+
return true
135+
}
136+
137+
fun getNotesFromDb(query: String){
138+
val searchTextQuery = "%$query%"
139+
mDb!!.notesDataDao().getNotesForQuery(searchTextQuery)
140+
.observe(this,
141+
Observer<List<NotesData>> { notes ->
142+
mItems = ArrayList(notes)
143+
mSearchAdapter = SearchNoteListAdapter(mItems)
144+
recyclerView.adapter = mSearchAdapter
145+
})
146+
}
147+
148+
149+
private fun showPopup(v: View): Boolean{
70150
PopupMenu(this, v, Gravity.END).apply {
71151
// MainActivity implements OnMenuItemClickListener
72152
setOnMenuItemClickListener(this@MainActivity)
@@ -90,11 +170,13 @@ class MainActivity : AppCompatActivity(), PopupMenu.OnMenuItemClickListener {
90170
if (notesData == null || notesData.size == 0){
91171
showToast(this, "NoData in Cashe...")
92172
} else{
93-
mAdapter = NoteListAdapter(this, ArrayList(notesData))
94-
mListView.adapter = mAdapter
173+
mItems = ArrayList(notesData)
174+
mAdapter = NoteListAdapter(mItems)
175+
recyclerView.adapter = mAdapter
95176
}
96177
}
97178
}
98179
mDbWorkerThread.postTask(task)
99180
}
181+
100182
}

0 commit comments

Comments
 (0)