Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
likorn
/
wordbook
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a22cd23c
authored
Apr 06, 2019
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Entries are added to the DB in onPause method
parent
81afc171
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
80 additions
and
48 deletions
.idea/vcs.xml
app/src/main/java/com/paktalin/wordbook/database/DataManager.kt
app/src/main/java/com/paktalin/wordbook/ui/Entry.kt
app/src/main/java/com/paktalin/wordbook/ui/MainActivity.kt
app/src/main/java/com/paktalin/wordbook/ui/MyTextWatcher.kt
app/src/main/java/com/paktalin/wordbook/ui/ViewHolder.kt
app/src/main/java/com/paktalin/wordbook/ui/Vocabulary.kt
app/src/main/java/com/paktalin/wordbook/ui/VocabularyAdapter.kt
.idea/vcs.xml
0 → 100644
View file @
a22cd23c
<?xml version="1.0" encoding="UTF-8"?>
<project
version=
"4"
>
<component
name=
"VcsDirectoryMappings"
>
<mapping
directory=
"$PROJECT_DIR$"
vcs=
"Git"
/>
</component>
</project>
\ No newline at end of file
app/src/main/java/com/paktalin/wordbook/database/DataManager.kt
View file @
a22cd23c
package
com.paktalin.wordbook.database
import
android.content.ContentValues
import
android.provider.BaseColumns._ID
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.Vocabulary
class
DataManager
{
companion
object
{
fun
loadVocabulary
(
dbHelper
:
DatabaseHelper
):
Vocabulary
{
val
db
=
dbHelper
.
writ
ableDatabase
val
cursor
=
db
.
query
(
DatabaseEntries
.
TABLE_NAME
,
arrayOf
(
DatabaseEntries
.
COLUMN_WORD
,
DatabaseEntries
.
COLUMN_TRANSLATION
),
null
,
null
,
null
,
null
,
null
)
val
db
=
dbHelper
.
read
ableDatabase
val
cursor
=
db
.
query
(
TABLE_NAME
,
arrayOf
(
COLUMN_WORD
,
COLUMN_TRANSLATION
,
_ID
),
null
,
null
,
null
,
null
,
null
)
val
vocabulary
=
Vocabulary
()
with
(
cursor
)
{
while
(
moveToNext
())
{
val
word
=
getString
(
getColumnIndexOrThrow
(
DatabaseEntries
.
COLUMN_WORD
))
val
translation
=
getString
(
getColumnIndexOrThrow
(
DatabaseEntries
.
COLUMN_TRANSLATION
))
val
word
=
getString
(
getColumnIndexOrThrow
(
COLUMN_WORD
))
val
translation
=
getString
(
getColumnIndexOrThrow
(
COLUMN_TRANSLATION
))
val
id
=
getLong
(
getColumnIndexOrThrow
(
_ID
))
vocabulary
.
add
(
word
,
translation
,
id
)
}
}
cursor
.
close
()
vocabulary
.
print
()
return
vocabulary
}
// fun replaceEntry()
fun
updateVocabulary
(
dbHelper
:
DatabaseHelper
,
vocabulary
:
Vocabulary
,
updatedPositions
:
MutableSet
<
Int
>)
{
val
db
=
dbHelper
.
writableDatabase
for
(
i
in
updatedPositions
)
{
val
values
=
ContentValues
().
apply
{
put
(
COLUMN_WORD
,
vocabulary
[
i
].
word
)
put
(
COLUMN_TRANSLATION
,
vocabulary
[
i
].
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
())
}
loadVocabulary
(
dbHelper
)
}
}
}
app/src/main/java/com/paktalin/wordbook/ui/Entry.kt
View file @
a22cd23c
...
...
@@ -10,6 +10,6 @@ class Entry(var word: String, var translation: String) {
}
fun
print
()
{
log
(
"$word - $translation"
)
log
(
"$word - $translation
- $id
"
)
}
}
app/src/main/java/com/paktalin/wordbook/ui/MainActivity.kt
View file @
a22cd23c
package
com.paktalin.wordbook.ui
import
android.content.ContentValues
import
android.support.v7.app.AppCompatActivity
import
android.os.Bundle
import
android.support.v7.app.AppCompatActivity
import
android.support.v7.widget.LinearLayoutManager
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.*
import
com.paktalin.wordbook.database.DatabaseEntries.COLUMN_TRANSLATION
import
com.paktalin.wordbook.database.DatabaseEntries.COLUMN_WORD
import
com.paktalin.wordbook.database.DatabaseEntries.TABLE_NAME
class
MainActivity
:
AppCompatActivity
()
{
lateinit
var
vocabulary
:
Vocabulary
private
val
dbHelper
=
DatabaseHelper
(
this
@MainActivity
)
private
lateinit
var
adapter
:
VocabularyAdapter
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
setContentView
(
R
.
layout
.
activity_main
)
}
vocabulary
=
loadVocabulary
(
DatabaseHelper
(
this
@MainActivity
))
override
fun
onResume
()
{
super
.
onResume
()
val
vocabulary
=
loadVocabulary
(
dbHelper
)
adapter
=
VocabularyAdapter
(
vocabulary
)
recycler_view
.
apply
{
layoutManager
=
LinearLayoutManager
(
this
@MainActivity
)
adapter
=
VocabularyAdapter
(
vocabulary
)
adapter
=
this
@MainActivity
.
adapter
}
}
fun
fillDb
()
{
val
dbHelper
=
DatabaseHelper
(
this
@MainActivity
)
val
db
=
dbHelper
.
writableDatabase
val
values
=
ContentValues
().
apply
{
put
(
COLUMN_WORD
,
"ragazzo"
)
put
(
COLUMN_TRANSLATION
,
"boy"
)
}
db
?.
insert
(
TABLE_NAME
,
null
,
values
)
override
fun
onPause
()
{
super
.
onPause
()
updateVocabulary
(
dbHelper
,
adapter
.
vocabulary
,
adapter
.
updatedPositions
)
}
}
app/src/main/java/com/paktalin/wordbook/ui/MyTextWatcher.kt
deleted
100644 → 0
View file @
81afc171
package
com.paktalin.wordbook.ui
import
android.text.Editable
import
android.text.TextWatcher
import
com.paktalin.wordbook.log
class
MyTextWatcher
(
private
val
position
:
Int
)
:
TextWatcher
{
override
fun
afterTextChanged
(
editable
:
Editable
?)
{
log
(
"$position"
)
}
override
fun
beforeTextChanged
(
p0
:
CharSequence
?,
p1
:
Int
,
p2
:
Int
,
p3
:
Int
)
{
}
override
fun
onTextChanged
(
p0
:
CharSequence
?,
p1
:
Int
,
p2
:
Int
,
p3
:
Int
)
{
}
}
\ No newline at end of file
app/src/main/java/com/paktalin/wordbook/ui/ViewHolder.kt
View file @
a22cd23c
...
...
@@ -9,9 +9,4 @@ import kotlinx.android.synthetic.main.fragment_line.view.*
class
ViewHolder
(
itemView
:
View
)
:
RecyclerView
.
ViewHolder
(
itemView
)
{
val
wordEt
:
EditText
=
itemView
.
word
val
translationEt
:
EditText
=
itemView
.
translation
init
{
// wordEt.addTextChangedListener(MyTextWatcher())
// translationEt.addTextChangedListener(MyTextWatcher())
}
}
\ No newline at end of file
app/src/main/java/com/paktalin/wordbook/ui/Vocabulary.kt
View file @
a22cd23c
...
...
@@ -12,6 +12,18 @@ class Vocabulary: Iterable<Entry>{
return
entries
.
size
}
fun
updateWord
(
position
:
Int
,
word
:
String
)
{
entries
[
position
].
word
=
word
}
fun
updateTranslation
(
position
:
Int
,
translation
:
String
)
{
entries
[
position
].
translation
=
translation
}
fun
print
()
{
for
(
entry
in
this
)
entry
.
print
()
}
override
fun
iterator
():
Iterator
<
Entry
>
{
return
entries
.
iterator
()
}
...
...
app/src/main/java/com/paktalin/wordbook/ui/VocabularyAdapter.kt
View file @
a22cd23c
package
com.paktalin.wordbook.ui
import
android.support.v7.widget.RecyclerView
import
android.text.Editable
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
(
private
val
vocabulary
:
Vocabulary
)
:
RecyclerView
.
Adapter
<
ViewHolder
>()
{
class
VocabularyAdapter
(
val
vocabulary
:
Vocabulary
)
:
RecyclerView
.
Adapter
<
ViewHolder
>()
{
var
updatedPositions
=
mutableSetOf
<
Int
>()
override
fun
getItemCount
():
Int
{
return
vocabulary
.
size
()
...
...
@@ -14,7 +19,8 @@ class VocabularyAdapter(private val vocabulary: Vocabulary) : RecyclerView.Adapt
override
fun
onBindViewHolder
(
holder
:
ViewHolder
,
position
:
Int
)
{
holder
.
wordEt
.
setText
(
vocabulary
[
position
].
word
)
holder
.
translationEt
.
setText
(
vocabulary
[
position
].
translation
)
holder
.
wordEt
.
addTextChangedListener
(
MyTextWatcher
(
position
))
holder
.
wordEt
.
addTextChangedListener
(
MyTextWatcher
(
position
,
COLUMN_WORD
))
holder
.
translationEt
.
addTextChangedListener
(
MyTextWatcher
(
position
,
COLUMN_TRANSLATION
))
}
override
fun
onCreateViewHolder
(
parent
:
ViewGroup
,
viewType
:
Int
):
ViewHolder
{
...
...
@@ -22,4 +28,17 @@ class VocabularyAdapter(private val vocabulary: Vocabulary) : RecyclerView.Adapt
return
ViewHolder
(
view
)
}
inner
class
MyTextWatcher
(
private
val
position
:
Int
,
private
val
column
:
String
)
:
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
)
}
}
override
fun
beforeTextChanged
(
p0
:
CharSequence
?,
p1
:
Int
,
p2
:
Int
,
p3
:
Int
)
{}
override
fun
onTextChanged
(
p0
:
CharSequence
?,
p1
:
Int
,
p2
:
Int
,
p3
:
Int
)
{}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment