Commit d4a4f3cf by Paktalin

Some changes (?)

parent a22cd23c
......@@ -6,6 +6,7 @@ import com.paktalin.wordbook.database.DatabaseEntries.COLUMN_TRANSLATION
import com.paktalin.wordbook.database.DatabaseEntries.COLUMN_WORD
import com.paktalin.wordbook.database.DatabaseEntries.TABLE_NAME
import com.paktalin.wordbook.log
import com.paktalin.wordbook.ui.Entry
import com.paktalin.wordbook.ui.Vocabulary
class DataManager {
......@@ -27,19 +28,18 @@ class DataManager {
return vocabulary
}
fun updateVocabulary(dbHelper: DatabaseHelper, vocabulary: Vocabulary, updatedPositions: MutableSet<Int>) {
fun updateVocabulary(dbHelper: DatabaseHelper, updatedEntries: MutableSet<Entry>, deletedIds: MutableSet<Long>) {
val db = dbHelper.writableDatabase
for (i in updatedPositions) {
for (updatedEntry in updatedEntries) {
val values = ContentValues().apply {
put(COLUMN_WORD, vocabulary[i].word)
put(COLUMN_TRANSLATION, vocabulary[i].translation)
put(COLUMN_WORD, updatedEntry.word)
put(COLUMN_TRANSLATION, updatedEntry.translation)
}
if (vocabulary[i].id != (-1).toLong())
db?.update(TABLE_NAME, values, "$_ID=${vocabulary[i].id}", null)
else
log(db?.insert(TABLE_NAME, null, values).toString())
db?.update(TABLE_NAME, values, "$_ID=${updatedEntry.id}", null)
}
for (deletedId in deletedIds) {
log(db?.delete(TABLE_NAME, "$_ID=$deletedId", null).toString())
}
loadVocabulary(dbHelper)
}
}
}
......@@ -9,7 +9,7 @@ class Entry(var word: String, var translation: String) {
this.id = id
}
fun print() {
log("$word - $translation - $id")
fun print(position: Int = 0) {
log("$position $word - $translation - $id")
}
}
......@@ -9,6 +9,6 @@ import com.paktalin.wordbook.R
class LineFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_line, container, false)
return inflater.inflate(R.layout.fragment_entry, container, false)
}
}
\ No newline at end of file
......@@ -7,7 +7,6 @@ import com.paktalin.wordbook.R
import com.paktalin.wordbook.database.DataManager.Companion.loadVocabulary
import com.paktalin.wordbook.database.DataManager.Companion.updateVocabulary
import com.paktalin.wordbook.database.DatabaseHelper
import com.paktalin.wordbook.log
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
......@@ -31,6 +30,6 @@ class MainActivity : AppCompatActivity() {
override fun onPause() {
super.onPause()
updateVocabulary(dbHelper, adapter.vocabulary, adapter.updatedPositions)
updateVocabulary(dbHelper, adapter.updatedEntries, adapter.deletedIds)
}
}
......@@ -3,10 +3,12 @@ package com.paktalin.wordbook.ui
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import kotlinx.android.synthetic.main.fragment_line.view.*
import kotlinx.android.synthetic.main.fragment_entry.view.*
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val wordEt: EditText = itemView.word
val translationEt: EditText = itemView.translation
val entryLayout: LinearLayout = itemView.entry_layout
}
\ No newline at end of file
package com.paktalin.wordbook.ui
import com.paktalin.wordbook.log
import java.lang.IndexOutOfBoundsException
class Vocabulary: Iterable<Entry>{
val entries = mutableListOf(Entry("", ""))
val entries = mutableListOf<Entry>()
fun add(word: String, translation: String, id: Long) {
entries.add(Entry(word, translation, id))
}
fun remove(position: Int) {
entries.removeAt(position)
log("removed from $position")
}
fun size(): Int {
return entries.size
}
fun updateWord(position: Int, word: String) {
entries[position].word = word
try {
entries[position].word = word
} catch (ignored : IndexOutOfBoundsException){}
}
fun updateTranslation(position: Int, translation: String) {
......@@ -21,7 +31,8 @@ class Vocabulary: Iterable<Entry>{
}
fun print() {
for (entry in this) entry.print()
for (entry in this)
entry.print(entries.indexOf(entry))
}
override fun iterator(): Iterator<Entry> {
......
......@@ -6,11 +6,10 @@ import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.ViewGroup
import com.paktalin.wordbook.R
import com.paktalin.wordbook.database.DatabaseEntries.COLUMN_TRANSLATION
import com.paktalin.wordbook.database.DatabaseEntries.COLUMN_WORD
class VocabularyAdapter(val vocabulary: Vocabulary) : RecyclerView.Adapter<ViewHolder>() {
var updatedPositions = mutableSetOf<Int>()
var deletedIds = mutableSetOf<Long>()
var updatedEntries = mutableSetOf<Entry>()
override fun getItemCount(): Int {
return vocabulary.size()
......@@ -19,23 +18,29 @@ class VocabularyAdapter(val vocabulary: Vocabulary) : RecyclerView.Adapter<ViewH
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.wordEt.setText(vocabulary[position].word)
holder.translationEt.setText(vocabulary[position].translation)
holder.wordEt.addTextChangedListener(MyTextWatcher(position, COLUMN_WORD))
holder.translationEt.addTextChangedListener(MyTextWatcher(position, COLUMN_TRANSLATION))
holder.wordEt.addTextChangedListener(MyTextWatcher(position) {
v, p -> vocabulary.updateWord(p, v)} )
holder.translationEt.addTextChangedListener(MyTextWatcher(position) {
v, p -> vocabulary.updateTranslation(p, v)})
holder.entryLayout.setOnLongClickListener {
vocabulary.remove(position)
this@VocabularyAdapter.notifyItemRemoved(position)
this@VocabularyAdapter.notifyDataSetChanged()
deletedIds.add(vocabulary[position].id)
// TODO(solve the problem with indexOutOfBounds)
true
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.fragment_line, parent, false)
val view = LayoutInflater.from(parent.context).inflate(R.layout.fragment_entry, parent, false)
return ViewHolder(view)
}
inner class MyTextWatcher(private val position: Int, private val column: String) : TextWatcher {
inner class MyTextWatcher(private val position: Int, val update: (value: String, position: Int) -> Unit) : TextWatcher {
override fun afterTextChanged(editable: Editable?) {
val updatedField = editable?.toString()
if (updatedField != null) {
if (column == COLUMN_WORD) vocabulary.updateWord(position, updatedField)
else vocabulary.updateTranslation(position, updatedField)
updatedPositions.add(position)
}
update(editable?.toString()!!, position)
updatedEntries.add(vocabulary[position])
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
......
......@@ -21,7 +21,7 @@
tools:ignore="ContentDescription" />
<LinearLayout
android:id="@+id/editable_word"
android:id="@+id/entry_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment