Commit a7a865d8 by Paktalin

Moving to another branch

parent a22cd23c
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<Objective-C-extensions>
<file>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
</file>
<class>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
</class>
<extensions>
<pair source="cpp" header="h" fileNamingConvention="NONE" />
<pair source="c" header="h" fileNamingConvention="NONE" />
</extensions>
</Objective-C-extensions>
</code_scheme>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="NullableNotNullManager">
<option name="myDefaultNullable" value="android.support.annotation.Nullable" />
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
<option name="myNullables">
<value>
<list size="5">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
<item index="2" class="java.lang.String" itemvalue="javax.annotation.CheckForNull" />
<item index="3" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" />
<item index="4" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
</list>
</value>
</option>
<option name="myNotNulls">
<value>
<list size="4">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />
</list>
</value>
</option>
</component>
</project>
\ No newline at end of file
...@@ -6,6 +6,7 @@ import com.paktalin.wordbook.database.DatabaseEntries.COLUMN_TRANSLATION ...@@ -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.COLUMN_WORD
import com.paktalin.wordbook.database.DatabaseEntries.TABLE_NAME import com.paktalin.wordbook.database.DatabaseEntries.TABLE_NAME
import com.paktalin.wordbook.log import com.paktalin.wordbook.log
import com.paktalin.wordbook.ui.Entry
import com.paktalin.wordbook.ui.Vocabulary import com.paktalin.wordbook.ui.Vocabulary
class DataManager { class DataManager {
...@@ -27,19 +28,18 @@ class DataManager { ...@@ -27,19 +28,18 @@ class DataManager {
return vocabulary 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 val db = dbHelper.writableDatabase
for (i in updatedPositions) { for (updatedEntry in updatedEntries) {
val values = ContentValues().apply { val values = ContentValues().apply {
put(COLUMN_WORD, vocabulary[i].word) put(COLUMN_WORD, updatedEntry.word)
put(COLUMN_TRANSLATION, vocabulary[i].translation) put(COLUMN_TRANSLATION, updatedEntry.translation)
} }
if (vocabulary[i].id != (-1).toLong()) db?.update(TABLE_NAME, values, "$_ID=${updatedEntry.id}", null)
db?.update(TABLE_NAME, values, "$_ID=${vocabulary[i].id}", null) }
else for (deletedId in deletedIds) {
log(db?.insert(TABLE_NAME, null, values).toString()) log(db?.delete(TABLE_NAME, "$_ID=$deletedId", null).toString())
} }
loadVocabulary(dbHelper)
} }
} }
} }
...@@ -9,7 +9,7 @@ class Entry(var word: String, var translation: String) { ...@@ -9,7 +9,7 @@ class Entry(var word: String, var translation: String) {
this.id = id this.id = id
} }
fun print() { fun print(position: Int = 0) {
log("$word - $translation - $id") log("$position $word - $translation - $id")
} }
} }
...@@ -9,6 +9,6 @@ import com.paktalin.wordbook.R ...@@ -9,6 +9,6 @@ import com.paktalin.wordbook.R
class LineFragment : Fragment() { class LineFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 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 ...@@ -7,7 +7,6 @@ import com.paktalin.wordbook.R
import com.paktalin.wordbook.database.DataManager.Companion.loadVocabulary import com.paktalin.wordbook.database.DataManager.Companion.loadVocabulary
import com.paktalin.wordbook.database.DataManager.Companion.updateVocabulary import com.paktalin.wordbook.database.DataManager.Companion.updateVocabulary
import com.paktalin.wordbook.database.DatabaseHelper import com.paktalin.wordbook.database.DatabaseHelper
import com.paktalin.wordbook.log
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
...@@ -31,6 +30,6 @@ class MainActivity : AppCompatActivity() { ...@@ -31,6 +30,6 @@ class MainActivity : AppCompatActivity() {
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
updateVocabulary(dbHelper, adapter.vocabulary, adapter.updatedPositions) updateVocabulary(dbHelper, adapter.updatedEntries, adapter.deletedIds)
} }
} }
...@@ -3,10 +3,12 @@ package com.paktalin.wordbook.ui ...@@ -3,10 +3,12 @@ package com.paktalin.wordbook.ui
import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView
import android.view.View import android.view.View
import android.widget.EditText import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView 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) { class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val wordEt: EditText = itemView.word val wordEt: EditText = itemView.word
val translationEt: EditText = itemView.translation val translationEt: EditText = itemView.translation
val entryLayout: LinearLayout = itemView.entry_layout
} }
\ No newline at end of file
package com.paktalin.wordbook.ui package com.paktalin.wordbook.ui
import com.paktalin.wordbook.log
import java.lang.IndexOutOfBoundsException
class Vocabulary: Iterable<Entry>{ class Vocabulary: Iterable<Entry>{
val entries = mutableListOf(Entry("", "")) val entries = mutableListOf<Entry>()
fun add(word: String, translation: String, id: Long) { fun add(word: String, translation: String, id: Long) {
entries.add(Entry(word, translation, id)) entries.add(Entry(word, translation, id))
} }
fun remove(position: Int) {
entries.removeAt(position)
log("removed from $position")
}
fun size(): Int { fun size(): Int {
return entries.size return entries.size
} }
fun updateWord(position: Int, word: String) { fun updateWord(position: Int, word: String) {
try {
entries[position].word = word entries[position].word = word
} catch (ignored : IndexOutOfBoundsException){}
} }
fun updateTranslation(position: Int, translation: String) { fun updateTranslation(position: Int, translation: String) {
...@@ -21,7 +31,8 @@ class Vocabulary: Iterable<Entry>{ ...@@ -21,7 +31,8 @@ class Vocabulary: Iterable<Entry>{
} }
fun print() { fun print() {
for (entry in this) entry.print() for (entry in this)
entry.print(entries.indexOf(entry))
} }
override fun iterator(): Iterator<Entry> { override fun iterator(): Iterator<Entry> {
......
...@@ -6,11 +6,10 @@ import android.text.TextWatcher ...@@ -6,11 +6,10 @@ import android.text.TextWatcher
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import com.paktalin.wordbook.R 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>() { class VocabularyAdapter(val vocabulary: Vocabulary) : RecyclerView.Adapter<ViewHolder>() {
var updatedPositions = mutableSetOf<Int>() var deletedIds = mutableSetOf<Long>()
var updatedEntries = mutableSetOf<Entry>()
override fun getItemCount(): Int { override fun getItemCount(): Int {
return vocabulary.size() return vocabulary.size()
...@@ -19,23 +18,29 @@ class VocabularyAdapter(val vocabulary: Vocabulary) : RecyclerView.Adapter<ViewH ...@@ -19,23 +18,29 @@ class VocabularyAdapter(val vocabulary: Vocabulary) : RecyclerView.Adapter<ViewH
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.wordEt.setText(vocabulary[position].word) holder.wordEt.setText(vocabulary[position].word)
holder.translationEt.setText(vocabulary[position].translation) holder.translationEt.setText(vocabulary[position].translation)
holder.wordEt.addTextChangedListener(MyTextWatcher(position, COLUMN_WORD)) holder.wordEt.addTextChangedListener(MyTextWatcher(position) {
holder.translationEt.addTextChangedListener(MyTextWatcher(position, COLUMN_TRANSLATION)) 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 { 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) 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?) { override fun afterTextChanged(editable: Editable?) {
val updatedField = editable?.toString() update(editable?.toString()!!, position)
if (updatedField != null) { updatedEntries.add(vocabulary[position])
if (column == COLUMN_WORD) vocabulary.updateWord(position, updatedField)
else vocabulary.updateTranslation(position, updatedField)
updatedPositions.add(position)
}
} }
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {} override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {} override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
tools:ignore="ContentDescription" /> tools:ignore="ContentDescription" />
<LinearLayout <LinearLayout
android:id="@+id/editable_word" android:id="@+id/entry_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" 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