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
d4a4f3cf
authored
Apr 18, 2019
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Some changes (?)
parent
a22cd23c
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
51 additions
and
32 deletions
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/LineFragment.kt
app/src/main/java/com/paktalin/wordbook/ui/MainActivity.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
app/src/main/res/layout/fragment_line.xml → app/src/main/res/layout/fragment_entry.xml
app/src/main/java/com/paktalin/wordbook/database/DataManager.kt
View file @
d4a4f3cf
...
...
@@ -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
updatedPosition
s
)
{
for
(
updatedEntry
in
updatedEntrie
s
)
{
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
)
}
}
}
app/src/main/java/com/paktalin/wordbook/ui/Entry.kt
View file @
d4a4f3cf
...
...
@@ -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"
)
}
}
app/src/main/java/com/paktalin/wordbook/ui/LineFragment.kt
View file @
d4a4f3cf
...
...
@@ -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
app/src/main/java/com/paktalin/wordbook/ui/MainActivity.kt
View file @
d4a4f3cf
...
...
@@ -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
.
updatedPosition
s
)
updateVocabulary
(
dbHelper
,
adapter
.
updatedEntries
,
adapter
.
deletedId
s
)
}
}
app/src/main/java/com/paktalin/wordbook/ui/ViewHolder.kt
View file @
d4a4f3cf
...
...
@@ -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
app/src/main/java/com/paktalin/wordbook/ui/Vocabulary.kt
View file @
d4a4f3cf
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
>
{
...
...
app/src/main/java/com/paktalin/wordbook/ui/VocabularyAdapter.kt
View file @
d4a4f3cf
...
...
@@ -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
)
{}
...
...
app/src/main/res/layout/fragment_
line
.xml
→
app/src/main/res/layout/fragment_
entry
.xml
View file @
d4a4f3cf
...
...
@@ -21,7 +21,7 @@
tools:ignore=
"ContentDescription"
/>
<LinearLayout
android:id=
"@+id/e
ditable_word
"
android:id=
"@+id/e
ntry_layout
"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:orientation=
"horizontal"
...
...
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