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
39d2e21a
authored
Sep 12, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Words are stored in Vocabulary class
parent
3134bda9
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
51 additions
and
29 deletions
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
app/src/main/java/com/paktalin/vocabularynotebook/firestoreitems/Vocabulary.kt
app/src/main/java/com/paktalin/vocabularynotebook/firestoreitems/WordItem.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/AddWordFragment.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/EditWordFragment.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/MainActivity.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/VocabularyFragment.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/WordFragment.kt
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
View file @
39d2e21a
...
...
@@ -6,16 +6,16 @@ import android.os.Build
import
android.os.Bundle
import
android.support.v7.widget.PopupMenu
import
android.support.v7.widget.RecyclerView
import
android.util.Log
import
android.view.*
import
android.widget.LinearLayout
import
android.widget.TextView
import
com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import
com.paktalin.vocabularynotebook.firestoreitems.WordItem
import
com.paktalin.vocabularynotebook.ui.EditWordFragment
import
com.paktalin.vocabularynotebook.ui.MainActivity
import
java.util.*
class
VocabularyAdapter
(
private
val
wordItems
:
MutableList
<
WordItem
>
,
private
val
activity
:
Activity
)
:
RecyclerView
.
Adapter
<
VocabularyAdapter
.
ViewHolder
>()
{
class
VocabularyAdapter
(
private
val
vocabulary
:
Vocabulary
,
private
val
activity
:
Activity
)
:
RecyclerView
.
Adapter
<
VocabularyAdapter
.
ViewHolder
>()
{
private
lateinit
var
recyclerView
:
RecyclerView
private
var
sortOrder
:
Int
=
0
...
...
@@ -32,14 +32,14 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
}
override
fun
onBindViewHolder
(
holder
:
ViewHolder
,
position
:
Int
)
{
val
wordItem
=
wordItem
s
[
position
]
val
wordItem
=
vocabulary
.
word
s
[
position
]
holder
.
tvWord
.
text
=
wordItem
.
pojo
.
word
holder
.
tvTranslation
.
text
=
wordItem
.
pojo
.
translation
holder
.
itemView
.
setOnClickListener
{
showPopupMenu
(
holder
.
itemView
,
position
)
}
//todo set click listener to menu
}
override
fun
getItemCount
():
Int
{
return
wordItem
s
.
size
}
override
fun
getItemCount
():
Int
{
return
vocabulary
.
word
s
.
size
}
private
fun
showPopupMenu
(
v
:
View
,
position
:
Int
)
{
val
popup
=
PopupMenu
(
activity
,
v
)
...
...
@@ -47,43 +47,43 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
inflater
.
inflate
(
R
.
menu
.
word_item_menu
,
popup
.
menu
)
popup
.
setOnMenuItemClickListener
{
if
(
it
.
itemId
==
R
.
id
.
option_delete
)
{
deleteWordItem
(
position
)
}
if
(
it
.
itemId
==
R
.
id
.
option_edit
)
{
editWordItem
(
v
,
wordItem
s
[
position
])
}
if
(
it
.
itemId
==
R
.
id
.
option_edit
)
{
editWordItem
(
v
,
vocabulary
.
word
s
[
position
])
}
true
}
popup
.
show
()
}
private
fun
deleteWordItem
(
position
:
Int
)
{
wordItem
s
[
position
].
delete
()
wordItem
s
.
removeAt
(
position
)
vocabulary
.
word
s
[
position
].
delete
()
vocabulary
.
word
s
.
removeAt
(
position
)
recyclerView
.
removeViewAt
(
position
)
this
.
notifyItemRemoved
(
position
)
this
.
notifyItemRangeChanged
(
position
,
wordItem
s
.
size
)
this
.
notifyItemRangeChanged
(
position
,
vocabulary
.
word
s
.
size
)
}
fun
addWordItem
(
newWordItem
:
WordItem
)
{
wordItem
s
.
add
(
0
,
newWordItem
)
vocabulary
.
word
s
.
add
(
0
,
newWordItem
)
this
.
sort
()
}
fun
updateWordItem
(
updatedWordItem
:
WordItem
)
{
val
updatedItemId
=
wordItem
s
.
indexOf
(
updatedWordItem
)
wordItem
s
[
updatedItemId
]
=
updatedWordItem
val
updatedItemId
=
vocabulary
.
word
s
.
indexOf
(
updatedWordItem
)
vocabulary
.
word
s
[
updatedItemId
]
=
updatedWordItem
this
.
sort
()
}
private
fun
sortByTranslation
()
{
wordItem
s
.
sortWith
(
Comparator
{
item1
,
item2
->
vocabulary
.
word
s
.
sortWith
(
Comparator
{
item1
,
item2
->
item1
.
pojo
.
translation
.
compareTo
(
item2
.
pojo
.
translation
)
})
}
private
fun
sortByWord
()
{
wordItem
s
.
sortWith
(
Comparator
{
item1
,
item2
->
vocabulary
.
word
s
.
sortWith
(
Comparator
{
item1
,
item2
->
item1
.
pojo
.
word
.
compareTo
(
item2
.
pojo
.
word
)
})
}
private
fun
sortByTime
()
{
wordItem
s
.
sortWith
(
Comparator
{
item1
,
item2
->
vocabulary
.
word
s
.
sortWith
(
Comparator
{
item1
,
item2
->
-
item1
.
pojo
.
time
!!
.
compareTo
(
item2
.
pojo
.
time
)
})
}
...
...
app/src/main/java/com/paktalin/vocabularynotebook/firestoreitems/Vocabulary.kt
View file @
39d2e21a
package
com.paktalin.vocabularynotebook.firestoreitems
class
Vocabulary
{
class
Vocabulary
(
var
words
:
MutableList
<
WordItem
>)
{
companion
object
{
private
const
val
SORT_BY_TIME
=
0
private
const
val
SORT_BY_WORD
=
1
private
const
val
SORT_BY_TRANSLATION
=
2
}
var
pojo
:
Pojo
private
lateinit
var
words
:
MutableList
<
WordItem
>
class
Pojo
(
var
title
:
String
?)
{
init
{
...
...
@@ -14,5 +19,16 @@ class Vocabulary {
pojo
=
Pojo
(
null
)
}
fun
sort
(
index
:
Int
)
{
when
(
index
)
{
SORT_BY_TIME
->
sortByTime
()
SORT_BY_WORD
->
sortByWord
()
SORT_BY_TRANSLATION
->
sortByTranslation
()
}
}
private
fun
sortByTime
()
{
}
private
fun
sortByWord
()
{
}
private
fun
sortByTranslation
()
{
}
}
app/src/main/java/com/paktalin/vocabularynotebook/firestoreitems/WordItem.kt
View file @
39d2e21a
...
...
@@ -24,7 +24,7 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p
fun
delete
()
{
ConfiguredFirestore
.
instance
.
collection
(
"vocabularies"
).
document
(
vocabularyId
)
.
collection
(
"
words
"
).
document
(
id
).
delete
()
.
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
())
}
}
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/AddWordFragment.kt
View file @
39d2e21a
...
...
@@ -20,8 +20,8 @@ class AddWordFragment : WordFragment() {
override
fun
saveToFirestore
(
wordPojo
:
WordItem
.
Pojo
,
vocabularyId
:
String
)
{
mainActivity
.
showProgressBar
()
ConfiguredFirestore
.
instance
.
collection
(
vocabularies
).
document
(
vocabularyId
)
.
collection
(
words
).
add
(
wordPojo
)
.
collection
(
VOCABULARIES
).
document
(
vocabularyId
)
.
collection
(
WORDS
).
add
(
wordPojo
)
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"Successfully added a new word"
)
clearFields
()
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/EditWordFragment.kt
View file @
39d2e21a
...
...
@@ -53,8 +53,8 @@ class EditWordFragment : WordFragment() {
override
fun
saveToFirestore
(
wordPojo
:
WordItem
.
Pojo
,
vocabularyId
:
String
)
{
mainActivity
.
showProgressBar
()
ConfiguredFirestore
.
instance
.
collection
(
vocabularies
).
document
(
vocabularyId
)
.
collection
(
words
).
document
(
wordItem
.
id
).
set
(
wordPojo
)
.
collection
(
VOCABULARIES
).
document
(
vocabularyId
)
.
collection
(
WORDS
).
document
(
wordItem
.
id
).
set
(
wordPojo
)
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"Successfully updated the word"
)
hideSubmitButton
()
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/MainActivity.kt
View file @
39d2e21a
...
...
@@ -80,7 +80,9 @@ class MainActivity : AppCompatActivity() {
arguments
.
putString
(
"vocabularyId"
,
vocabularyId
)
vocabularyFragment
.
arguments
=
arguments
supportFragmentManager
.
beginTransaction
().
add
(
R
.
id
.
fragment_container
,
vocabularyFragment
).
commitNowAllowingStateLoss
()
}
else
{
showToastNoWords
()
}
}
else
{
Log
.
w
(
TAG
,
"There's no collection \"vocabularies\""
)
showToastNoWords
()
}
}
}
...
...
@@ -103,7 +105,7 @@ class MainActivity : AppCompatActivity() {
fun
showToastNoWords
()
{
Toast
.
makeText
(
this
@MainActivity
,
"You don't have any
words
yet. Add your fist one!"
,
Toast
.
LENGTH_SHORT
).
show
()
"You don't have any
WORDS
yet. Add your fist one!"
,
Toast
.
LENGTH_SHORT
).
show
()
}
override
fun
onPause
()
{
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/VocabularyFragment.kt
View file @
39d2e21a
...
...
@@ -13,9 +13,9 @@ import com.google.firebase.firestore.DocumentSnapshot
import
com.google.firebase.firestore.Query
import
com.paktalin.vocabularynotebook.*
import
com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import
com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import
com.paktalin.vocabularynotebook.firestoreitems.WordItem
import
kotlinx.android.synthetic.main.fragment_vocabulary.*
import
java.util.*
class
VocabularyFragment
:
Fragment
()
{
companion
object
{
...
...
@@ -44,7 +44,7 @@ class VocabularyFragment : Fragment() {
private
fun
setEmptyAdapter
()
{
val
emptyList
:
MutableList
<
WordItem
>
=
mutableListOf
()
recyclerView
.
adapter
=
VocabularyAdapter
(
emptyList
,
activity
!!
)
recyclerView
.
adapter
=
VocabularyAdapter
(
Vocabulary
(
emptyList
)
,
activity
!!
)
val
mLayoutManager
=
LinearLayoutManager
(
activity
)
recyclerView
.
layoutManager
=
mLayoutManager
}
...
...
@@ -57,7 +57,7 @@ class VocabularyFragment : Fragment() {
if
(
it
.
documents
.
size
!=
0
)
setVocabularyAdapter
(
it
.
documents
,
vocabularyId
)
else
{
Log
.
i
(
TAG
,
"There are no documents in collection \"
words
\""
)
Log
.
i
(
TAG
,
"There are no documents in collection \"
WORDS
\""
)
(
activity
as
MainActivity
).
showToastNoWords
()
}}
}
...
...
@@ -72,7 +72,8 @@ class VocabularyFragment : Fragment() {
wordItems
.
add
(
WordItem
(
word
,
translation
,
time
.
toDate
(),
ref
.
id
,
vocabularyId
))
}
val
adapter
=
VocabularyAdapter
(
wordItems
,
activity
!!
)
val
vocabulary
=
Vocabulary
(
wordItems
)
val
adapter
=
VocabularyAdapter
(
vocabulary
,
activity
!!
)
recyclerView
.
adapter
=
adapter
}
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/WordFragment.kt
View file @
39d2e21a
...
...
@@ -12,8 +12,11 @@ import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import
kotlinx.android.synthetic.main.fragment_new_word.*
abstract
class
WordFragment
:
Fragment
()
{
protected
val
vocabularies
=
"vocabularies"
protected
val
words
=
"words"
companion
object
{
const
val
VOCABULARIES
=
"vocabularies"
const
val
WORDS
=
"words"
}
protected
lateinit
var
mainActivity
:
MainActivity
private
var
wordEmpty
:
Boolean
=
true
...
...
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