Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
likorn
/
vocabulary_notebook
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
57728fc2
authored
Sep 09, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
RecyclerView is updated when a new word is added
parent
24371d24
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
28 additions
and
81 deletions
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
app/src/main/java/com/paktalin/vocabularynotebook/WordItem.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/AddWordActivity.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/MainActivity.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/NewWordFragment.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/VocabularyFragment.kt
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
View file @
57728fc2
...
...
@@ -10,7 +10,7 @@ import android.widget.LinearLayout
import
android.widget.TextView
import
com.paktalin.vocabularynotebook.ui.WordItemInfoActivity
class
VocabularyAdapter
(
private
val
wordItems
:
MutableList
<
WordItem
>,
private
val
context
:
Activity
)
:
RecyclerView
.
Adapter
<
VocabularyAdapter
.
ViewHolder
>()
{
class
VocabularyAdapter
(
val
wordItems
:
MutableList
<
WordItem
>,
private
val
context
:
Activity
)
:
RecyclerView
.
Adapter
<
VocabularyAdapter
.
ViewHolder
>()
{
private
lateinit
var
recyclerView
:
RecyclerView
...
...
@@ -68,6 +68,11 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
this
.
notifyItemRangeChanged
(
position
,
wordItems
.
size
)
}
fun
addWordItem
(
newWordItem
:
WordItem
)
{
wordItems
.
add
(
0
,
newWordItem
)
this
.
notifyItemInserted
(
0
)
}
inner
class
ViewHolder
(
itemView
:
View
)
:
RecyclerView
.
ViewHolder
(
itemView
)
{
val
tvWord
:
TextView
=
itemView
.
findViewById
(
R
.
id
.
etWord
)
val
tvTranslation
:
TextView
=
itemView
.
findViewById
(
R
.
id
.
etTranslation
)
...
...
@@ -78,4 +83,5 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
companion
object
{
private
val
TAG
=
"VN/"
+
VocabularyAdapter
::
class
.
java
.
simpleName
}
}
\ No newline at end of file
app/src/main/java/com/paktalin/vocabularynotebook/WordItem.kt
View file @
57728fc2
...
...
@@ -9,7 +9,7 @@ import com.google.firebase.firestore.FirebaseFirestore
import
java.io.Serializable
class
WordItem
(
word
:
String
?,
translation
:
String
?,
var
id
:
String
?,
private
val
vocabularyId
:
String
?
)
:
Serializable
{
class
WordItem
(
word
:
String
,
translation
:
String
,
var
id
:
String
,
private
val
vocabularyId
:
String
)
:
Serializable
{
var
pojo
:
WordItemPojo
?
=
null
class
WordItemPojo
(
var
word
:
String
?,
var
translation
:
String
?)
:
Serializable
...
...
@@ -18,20 +18,17 @@ class WordItem(word: String?, translation: String?, var id: String?, private val
this
.
pojo
=
WordItemPojo
(
word
,
translation
)
}
constructor
(
pojo
:
WordItemPojo
,
id
:
String
,
vocabularyId
:
String
)
:
this
(
pojo
.
word
!!
,
pojo
.
translation
!!
,
id
,
vocabularyId
)
fun
delete
()
{
if
(
vocabularyId
!=
null
)
{
FirebaseFirestore
.
getInstance
().
collection
(
"vocabularies"
).
document
(
vocabularyId
)
.
collection
(
"words"
).
document
(
id
!!
).
delete
()
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"Successfully deleted word with id $id"
)
}
.
addOnFailureListener
{
e
->
Log
.
w
(
TAG
,
"deleteWordWithId $id:failure"
,
e
.
fillInStackTrace
())
}
}
}
companion
object
{
private
val
TAG
=
"VN/"
+
WordItem
::
class
.
java
.
simpleName
fun
createEmpty
()
:
WordItem
{
return
WordItem
(
null
,
null
,
null
,
null
)
}
}
}
app/src/main/java/com/paktalin/vocabularynotebook/ui/AddWordActivity.kt
deleted
100644 → 0
View file @
24371d24
package
com.paktalin.vocabularynotebook.ui
import
android.content.Intent
import
android.os.Bundle
import
android.support.v7.app.AppCompatActivity
import
android.util.Log
import
android.widget.Toast
import
com.google.firebase.firestore.FirebaseFirestore
import
com.paktalin.vocabularynotebook.R
import
com.paktalin.vocabularynotebook.Utils
import
com.paktalin.vocabularynotebook.WordItem.WordItemPojo
import
kotlinx.android.synthetic.main.activity_add_word.*
class
AddWordActivity
:
AppCompatActivity
()
{
companion
object
{
private
val
TAG
=
"VN/"
+
AddWordActivity
::
class
.
simpleName
private
const
val
VOCABULARIES
=
"vocabularies"
private
const
val
WORDS
=
"words"
}
private
lateinit
var
vocabularyId
:
String
private
val
db
=
FirebaseFirestore
.
getInstance
()
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
setContentView
(
R
.
layout
.
activity_add_word
)
vocabularyId
=
intent
.
getStringExtra
(
"vocabularyId"
)
btnSubmitNewWord
.
setOnClickListener
{
addWordToDb
()
}
btnCancel
.
setOnClickListener
{
cancel
()
}
}
private
fun
addWordToDb
()
{
val
word
=
etWord
.
text
.
toString
()
val
translation
=
etTranslation
.
text
.
toString
()
if
(
Utils
.
fieldsNotEmpty
(
word
,
translation
,
"Please, enter word and translation"
,
this
))
{
db
.
collection
(
VOCABULARIES
).
document
(
vocabularyId
)
.
collection
(
WORDS
).
add
(
WordItemPojo
(
word
,
translation
)).
addOnSuccessListener
{
Log
.
i
(
TAG
,
"Successfully added a new word $word"
)
clearFields
()
cancel
()
}
.
addOnFailureListener
{
Log
.
w
(
TAG
,
"addNewWordToDb:failure"
,
it
.
fillInStackTrace
())
Toast
.
makeText
(
this
,
"Couldn't add the word"
,
Toast
.
LENGTH_SHORT
).
show
()
}
}
}
private
fun
cancel
()
{
val
intentMainActivity
=
Intent
(
this
,
MainActivity
::
class
.
java
)
startActivity
(
intentMainActivity
)
}
private
fun
clearFields
()
{
etWord
.
text
.
clear
()
etTranslation
.
text
.
clear
()
}
}
app/src/main/java/com/paktalin/vocabularynotebook/ui/MainActivity.kt
View file @
57728fc2
...
...
@@ -20,8 +20,6 @@ class MainActivity : AppCompatActivity() {
setContentView
(
R
.
layout
.
activity_main
)
setUpNavigationView
()
extractVocabularyData
()
startVocabularyFragment
()
startNewWordFragment
()
}
private
fun
logOut
()
{
...
...
@@ -53,16 +51,7 @@ class MainActivity : AppCompatActivity() {
vocabularyId
=
vocabulary
.
id
(
supportFragmentManager
.
findFragmentById
(
R
.
id
.
fragment_vocabulary
)
as
VocabularyFragment
)
.
retrieveWordsData
(
vocabularyId
)
}
}
private
fun
startVocabularyFragment
()
{
}
private
fun
startNewWordFragment
()
{
}
companion
object
{
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/NewWordFragment.kt
View file @
57728fc2
...
...
@@ -86,14 +86,15 @@ class NewWordFragment : Fragment() {
val
word
=
etWord
.
text
.
toString
()
val
translation
=
etTranslation
.
text
.
toString
()
val
vocabularyId
=
(
activity
as
MainActivity
).
vocabularyId
val
newWordItemPojo
=
WordItem
.
WordItemPojo
(
word
,
translation
)
FirebaseFirestore
.
getInstance
()
.
collection
(
"vocabularies"
).
document
(
vocabularyId
)
.
collection
(
"words"
).
add
(
WordItem
.
WordItemPojo
(
word
,
translation
)
)
.
collection
(
"words"
).
add
(
newWordItemPojo
)
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"Successfully added a new word"
)
clearFields
()
//todo update recycleView
}
val
wordItem
=
WordItem
(
newWordItemPojo
,
it
.
id
,
vocabularyId
)
updateRecycleView
(
wordItem
)
}
.
addOnFailureListener
{
Log
.
w
(
TAG
,
"addNewWordToDb:failure"
,
it
.
fillInStackTrace
())
Toast
.
makeText
(
activity
,
"Couldn't add the word"
,
Toast
.
LENGTH_SHORT
).
show
()}
...
...
@@ -104,5 +105,11 @@ class NewWordFragment : Fragment() {
etTranslation
.
text
.
clear
()
}
private
fun
updateRecycleView
(
wordItem
:
WordItem
)
{
(
activity
!!
.
supportFragmentManager
.
findFragmentById
(
R
.
id
.
fragment_vocabulary
)
as
VocabularyFragment
)
.
addWordItem
(
wordItem
)
}
companion
object
{
private
val
TAG
=
"VN/"
+
NewWordFragment
::
class
.
java
.
simpleName
}
}
\ No newline at end of file
app/src/main/java/com/paktalin/vocabularynotebook/ui/VocabularyFragment.kt
View file @
57728fc2
...
...
@@ -61,4 +61,8 @@ class VocabularyFragment : Fragment() {
val
adapter
=
VocabularyAdapter
(
wordItems
,
activity
!!
)
recyclerView
.
adapter
=
adapter
}
fun
addWordItem
(
newWordItem
:
WordItem
)
{
(
recyclerView
.
adapter
as
VocabularyAdapter
).
addWordItem
(
newWordItem
)
}
}
\ 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