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
e6d52044
authored
Nov 11, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Isolated firestore logic from EditWordFragment and AddWordFragment
parent
8a981912
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
87 additions
and
63 deletions
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/MainActivity.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/fragments/AddWordFragment.kt
app/src/main/java/com/paktalin/vocabularynotebook/ui/fragments/EditWordFragment.kt
app/src/main/java/com/paktalin/vocabularynotebook/utils/FirestoreManager.kt
app/src/main/java/com/paktalin/vocabularynotebook/utils/UserManager.kt
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
View file @
e6d52044
...
...
@@ -63,7 +63,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
if
(
it
.
itemId
==
R
.
id
.
option_edit
)
{
editWord
(
v
,
displayedVocabulary
.
getAt
(
position
))
}
true
}
if
(
EditWordFragment
.
notI
nEditMode
)
popup
.
show
()
if
(
!
mainActivity
.
i
nEditMode
)
popup
.
show
()
}
private
fun
deleteWord
(
position
:
Int
)
{
...
...
@@ -96,7 +96,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
@SuppressLint
(
"ResourceType"
)
private
fun
editWord
(
container
:
View
,
wordItem
:
WordItem
)
{
if
(
EditWordFragment
.
notI
nEditMode
)
{
if
(
!
mainActivity
.
i
nEditMode
)
{
//set container id
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
JELLY_BEAN_MR1
)
{
container
.
id
=
View
.
generateViewId
()
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/MainActivity.kt
View file @
e6d52044
...
...
@@ -20,6 +20,7 @@ class MainActivity : AppCompatActivity() {
lateinit
var
vocabularyFragment
:
VocabularyFragment
lateinit
var
searchView
:
SearchView
var
inEditMode
=
false
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
...
...
@@ -59,7 +60,7 @@ class MainActivity : AppCompatActivity() {
private
fun
extractVocabularyData
()
{
addProgressBar
()
FirestoreManager
().
extractVocabulary
Data
(
FirestoreManager
().
extractVocabulary
Id
(
{
id
->
addVocabularyFragment
(
id
)
},
{
showToastNoWords
()
},
{
removeProgressBar
()
})
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/fragments/AddWordFragment.kt
View file @
e6d52044
package
com.paktalin.vocabularynotebook.ui.fragments
import
android.util.Log
import
android.view.View
import
com.paktalin.vocabularynotebook.R
import
com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import
com.paktalin.vocabularynotebook.firestoreitems.WordItem
import
com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.VOCABULARIES
import
com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.WORDS
import
com.paktalin.vocabularynotebook.utils.FirestoreManager
import
com.paktalin.vocabularynotebook.utils.shortToast
import
kotlinx.android.synthetic.main.fragment_editable_word.*
...
...
@@ -14,19 +11,17 @@ class AddWordFragment : WordFragment() {
override
fun
saveToFirestore
(
word
:
String
,
translation
:
String
,
vocabularyId
:
String
)
{
val
wordPojo
=
WordItem
.
Pojo
(
word
,
translation
,
null
)
ConfiguredFirestore
.
instance
.
collection
(
VOCABULARIES
).
document
(
vocabularyId
)
.
collection
(
WORDS
).
add
(
wordPojo
)
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"Successfully added a new word"
)
clearFields
()
mainActivity
.
removeProgressBar
()
val
wordItem
=
WordItem
(
wordPojo
,
it
.
id
,
vocabularyId
)
updateRecycleView
(
wordItem
)
}
.
addOnFailureListener
{
Log
.
w
(
TAG
,
"addNewWordToDb:failure"
,
it
.
fillInStackTrace
())
shortToast
(
mainActivity
,
getString
(
R
.
string
.
toast_new_word_fail
))
}
FirestoreManager
().
saveNewWord
(
{
documentId
->
onSuccessfulSave
(
wordPojo
,
vocabularyId
,
documentId
)
},
{
shortToast
(
mainActivity
,
getString
(
R
.
string
.
toast_new_word_fail
))
},
wordPojo
,
vocabularyId
)
}
private
fun
onSuccessfulSave
(
wordPojo
:
WordItem
.
Pojo
,
vocabularyId
:
String
,
documentId
:
String
)
{
clearFields
()
mainActivity
.
removeProgressBar
()
val
wordItem
=
WordItem
(
wordPojo
,
documentId
,
vocabularyId
)
updateRecycleView
(
wordItem
)
}
override
fun
updateRecycleView
(
wordItem
:
WordItem
)
{
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/fragments/EditWordFragment.kt
View file @
e6d52044
...
...
@@ -2,21 +2,15 @@ package com.paktalin.vocabularynotebook.ui.fragments
import
android.content.Context
import
android.os.Bundle
import
android.
util.Log
import
android.
support.v4.content.ContextCompat
import
android.view.LayoutInflater
import
android.view.View
import
android.view.ViewGroup
import
android.view.inputmethod.InputMethodManager
import
com.paktalin.vocabularynotebook.*
import
com.paktalin.vocabularynotebook.firestoreitems.WordItem
import
com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import
com.paktalin.vocabularynotebook.ui.activities.MainActivity
import
com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.VOCABULARIES
import
com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.WORDS
import
com.paktalin.vocabularynotebook.utils.disableScrolling
import
com.paktalin.vocabularynotebook.utils.enableScrolling
import
com.paktalin.vocabularynotebook.utils.removeFragment
import
com.paktalin.vocabularynotebook.utils.shortToast
import
com.paktalin.vocabularynotebook.utils.*
import
kotlinx.android.synthetic.main.fragment_editable_word.*
import
kotlinx.android.synthetic.main.content_main.*
import
kotlinx.android.synthetic.main.word_item.view.*
...
...
@@ -25,8 +19,8 @@ class EditWordFragment : WordFragment() {
private
lateinit
var
wordItem
:
WordItem
override
fun
onCreateView
(
inflater
:
LayoutInflater
,
container
:
ViewGroup
?,
savedInstanceState
:
Bundle
?):
View
?
{
notInEditMode
=
false
mainActivity
=
activity
as
MainActivity
mainActivity
.
inEditMode
=
true
hidePreviousViews
(
container
)
wordItem
=
arguments
!!
[
"wordItem"
]
as
WordItem
return
super
.
onCreateView
(
inflater
,
container
,
savedInstanceState
)
...
...
@@ -42,7 +36,7 @@ class EditWordFragment : WordFragment() {
private
fun
setWordItemData
()
{
word
.
setText
(
wordItem
.
pojo
.
word
)
translation
.
setText
(
wordItem
.
pojo
.
translation
)
editable_word
.
setBackgroundColor
(
resources
.
getColor
(
R
.
color
.
green_highlight
))
editable_word
.
setBackgroundColor
(
ContextCompat
.
getColor
(
mainActivity
,
R
.
color
.
green_highlight
))
}
private
fun
setFocusOnWord
()
{
...
...
@@ -61,22 +55,23 @@ class EditWordFragment : WordFragment() {
override
fun
saveToFirestore
(
word
:
String
,
translation
:
String
,
vocabularyId
:
String
)
{
val
wordPojo
=
WordItem
.
Pojo
(
word
,
translation
,
wordItem
.
pojo
.
time
)
ConfiguredFirestore
.
instance
.
collection
(
VOCABULARIES
).
document
(
vocabularyId
)
.
collection
(
WORDS
).
document
(
wordItem
.
id
).
set
(
wordPojo
)
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"Successfully updated the word"
)
hideSubmitButton
()
mainActivity
.
removeProgressBar
()
wordItem
.
pojo
=
wordPojo
updateRecycleView
(
wordItem
)
stop
()
}
.
addOnFailureListener
{
Log
.
w
(
TAG
,
"updateExistingWord:failure"
,
it
.
fillInStackTrace
())
shortToast
(
mainActivity
,
getString
(
R
.
string
.
toast_update_word_failed
))
stop
()
}
FirestoreManager
().
updateWord
(
{
onSuccessfulSave
(
wordPojo
)
},
{
onFailure
()
},
wordItem
,
wordPojo
,
vocabularyId
)
}
private
fun
onSuccessfulSave
(
wordPojo
:
WordItem
.
Pojo
)
{
hideSubmitButton
()
mainActivity
.
removeProgressBar
()
wordItem
.
pojo
=
wordPojo
updateRecycleView
(
wordItem
)
stop
()
}
private
fun
onFailure
()
{
shortToast
(
mainActivity
,
getString
(
R
.
string
.
toast_update_word_failed
))
stop
()
}
private
fun
stop
()
{
...
...
@@ -84,7 +79,7 @@ class EditWordFragment : WordFragment() {
mainActivity
.
btnSubmit
.
setOnClickListener
{
(
mainActivity
.
fragmentAddWord
as
AddWordFragment
).
submitWord
()
}
enableScrolling
(
mainActivity
)
removeFragment
(
mainActivity
.
supportFragmentManager
,
this
)
notInEditMode
=
tru
e
mainActivity
.
inEditMode
=
fals
e
}
override
fun
updateRecycleView
(
wordItem
:
WordItem
)
{
...
...
@@ -93,6 +88,5 @@ class EditWordFragment : WordFragment() {
companion
object
{
private
val
TAG
=
"VN/"
+
EditWordFragment
::
class
.
java
.
simpleName
var
notInEditMode
=
true
}
}
\ No newline at end of file
app/src/main/java/com/paktalin/vocabularynotebook/utils/FirestoreManager.kt
View file @
e6d52044
...
...
@@ -9,30 +9,34 @@ import com.google.firebase.firestore.FirebaseFirestore
import
com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import
com.paktalin.vocabularynotebook.firestoreitems.UserPojo
import
com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import
com.paktalin.vocabularynotebook.firestoreitems.WordItem
import
com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import
java.util.*
class
FirestoreManager
{
private
val
db
:
FirebaseFirestore
=
ConfiguredFirestore
.
instance
private
val
vocabularyCollection
=
db
.
collection
(
VOCABULARIES
)
fun
extractVocabulary
Data
(
onSuccess
:
(
vocabularyId
:
String
)
->
Unit
,
showToastNoWords
:
()
->
Unit
,
removeProgressBar
:
()
->
Unit
)
{
fun
extractVocabulary
Id
(
onSuccess
:
(
vocabularyId
:
String
)
->
Unit
,
showToastNoWords
:
()
->
Unit
,
removeProgressBar
:
()
->
Unit
)
{
val
userId
=
FirebaseAuth
.
getInstance
()
!!
.
currentUser
!!
.
uid
userDocument
(
userId
).
get
()
.
addOnSuccessListener
{
task
->
.
addOnSuccessListener
{
snapshot
->
removeProgressBar
()
if
(
task
.
get
(
VOCABULARIES
)
!=
null
)
{
setVocabularyID
(
task
,
db
)
if
(
snapshot
.
get
(
VOCABULARIES
)
!=
null
)
{
setVocabularyID
(
snapshot
)
onSuccess
(
vocabularyId
)
}
else
{
Log
.
w
(
TAG
,
"There's no collection \"vocabularies\""
)
showToastNoWords
()
}
}
showToastNoWords
()
}
}
}
fun
addNewUser
ToDb
(
firebaseUser
:
FirebaseUser
,
logInActivity
:
LogInActivity
)
{
fun
addNewUser
(
firebaseUser
:
FirebaseUser
,
logInActivity
:
LogInActivity
)
{
//todo add condition to writing to the db in Firebase Console (request.auth.uid)
db
.
collection
(
VOCABULARIES
)
.
add
(
Vocabulary
.
Pojo
(
null
))
vocabularyCollection
.
add
(
Vocabulary
.
Pojo
(
null
))
.
addOnSuccessListener
{
firstVocabularyRef
->
Log
.
d
(
TAG
,
"VocabularyPojo successfully created: "
+
firstVocabularyRef
.
path
)
setNewUserWithVocabularyData
(
firebaseUser
,
firstVocabularyRef
,
logInActivity
)
...
...
@@ -43,8 +47,31 @@ class FirestoreManager {
}
}
private
fun
userDocument
(
userId
:
String
):
DocumentReference
{
return
db
.
collection
(
USERS
).
document
(
userId
)
fun
saveNewWord
(
onSuccess
:
(
documentId
:
String
)
->
Unit
,
onFailure
:
()
->
Unit
,
wordPojo
:
WordItem
.
Pojo
,
vocabularyId
:
String
)
{
vocabularyDocument
(
vocabularyId
)
.
collection
(
WORDS
).
add
(
wordPojo
)
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"Successfully added a new word"
)
onSuccess
(
it
.
id
)
}
.
addOnFailureListener
{
Log
.
w
(
TAG
,
"addNewWordToDb:failure"
,
it
.
fillInStackTrace
())
onFailure
()
}
}
fun
updateWord
(
onSuccess
:
()
->
Unit
,
onFailure
:
()
->
Unit
,
wordItem
:
WordItem
,
wordPojo
:
WordItem
.
Pojo
,
vocabularyId
:
String
)
{
vocabularyDocument
(
vocabularyId
)
.
collection
(
WORDS
).
document
(
wordItem
.
id
).
set
(
wordPojo
)
.
addOnSuccessListener
{
Log
.
i
(
TAG
,
"Successfully updated the word"
)
onSuccess
()
}
.
addOnFailureListener
{
Log
.
w
(
TAG
,
"updateExistingWord:failure"
,
it
.
fillInStackTrace
())
onFailure
()
}
}
private
fun
setNewUserWithVocabularyData
(
firebaseUser
:
FirebaseUser
,
...
...
@@ -62,12 +89,20 @@ class FirestoreManager {
}
}
private
fun
setVocabularyID
(
task
:
DocumentSnapshot
,
db
:
FirebaseFirestore
)
{
val
vocabularies
:
List
<
DocumentReference
>
=
task
.
get
(
VOCABULARIES
)
as
List
<
DocumentReference
>
val
vocabulary
=
db
.
collection
(
VOCABULARIES
).
d
ocument
(
vocabularies
[
0
].
id
)
private
fun
setVocabularyID
(
snapshot
:
DocumentSnapshot
)
{
val
vocabularies
:
List
<
DocumentReference
>
=
snapshot
.
get
(
VOCABULARIES
)
as
List
<
DocumentReference
>
val
vocabulary
=
vocabularyD
ocument
(
vocabularies
[
0
].
id
)
vocabularyId
=
vocabulary
.
id
}
private
fun
userDocument
(
userId
:
String
):
DocumentReference
{
return
db
.
collection
(
USERS
).
document
(
userId
)
}
private
fun
vocabularyDocument
(
id
:
String
):
DocumentReference
{
return
vocabularyCollection
.
document
(
id
)
}
companion
object
{
const
val
USERS
=
"users"
const
val
WORDS
=
"words"
...
...
app/src/main/java/com/paktalin/vocabularynotebook/utils/UserManager.kt
View file @
e6d52044
...
...
@@ -33,7 +33,7 @@ fun mSignUp(activity: LogInActivity, email: String, password: String) {
.
addOnCompleteListener
{
removeProgressBar
(
activity
.
supportFragmentManager
)
}
.
addOnSuccessListener
{
Log
.
d
(
TAG
,
"Successfully signed up a new user"
)
FirestoreManager
().
addNewUser
ToDb
(
mAuth
!!
.
currentUser
!!
,
activity
)
FirestoreManager
().
addNewUser
(
mAuth
!!
.
currentUser
!!
,
activity
)
activity
.
login
()
}
.
addOnFailureListener
{
...
...
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