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
9e553faa
authored
Sep 09, 2018
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
VocabularyId is retrieved inside MainActivity
parent
8eee40c0
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
44 deletions
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/ui/MainActivity.kt
View file @
9e553faa
...
...
@@ -5,31 +5,64 @@ import android.os.Bundle
import
android.support.v7.app.AppCompatActivity
import
android.util.Log
import
com.google.firebase.auth.FirebaseAuth
import
com.google.firebase.firestore.DocumentReference
import
com.google.firebase.firestore.FirebaseFirestore
import
com.paktalin.vocabularynotebook.R
import
kotlinx.android.synthetic.main.activity_main.*
class
MainActivity
:
AppCompatActivity
()
{
lateinit
var
vocabularyId
:
String
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
setContentView
(
R
.
layout
.
activity_main
)
setUpNavigationView
()
extractVocabularyData
()
startVocabularyFragment
()
startNewWordFragment
()
}
private
fun
logOut
()
{
Log
.
i
(
TAG
,
"User logged out"
)
FirebaseAuth
.
getInstance
()
!!
.
signOut
()
val
intentLogInActivity
=
Intent
(
this
@MainActivity
,
LogInActivity
::
class
.
java
)
startActivity
(
intentLogInActivity
)
}
private
fun
setUpNavigationView
()
{
navigationView
.
setNavigationItemSelectedListener
{
menuItem
->
menuItem
.
isChecked
=
true
if
(
menuItem
.
itemId
==
R
.
id
.
logOut
)
{
logOut
()
}
drawerLayout
!!
.
closeDrawers
()
true
}
}
private
fun
logOut
()
{
Log
.
i
(
TAG
,
"User logged out"
)
FirebaseAuth
.
getInstance
()
!!
.
signOut
()
val
intentLogInActivity
=
Intent
(
this
@MainActivity
,
LogInActivity
::
class
.
java
)
startActivity
(
intentLogInActivity
)
private
fun
extractVocabularyData
()
{
val
userId
=
FirebaseAuth
.
getInstance
().
currentUser
!!
.
uid
val
db
=
FirebaseFirestore
.
getInstance
()
val
userDocument
=
db
.
collection
(
"users"
).
document
(
userId
)
userDocument
.
get
().
addOnSuccessListener
{
task
->
val
vocabularies
:
List
<
DocumentReference
>
=
task
.
get
(
"vocabularies"
)
as
List
<
DocumentReference
>
//todo represent specific vocabulary instead of the first one
val
vocabulary
=
db
.
collection
(
"vocabularies"
).
document
(
vocabularies
[
0
].
id
)
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 @
9e553faa
...
...
@@ -9,6 +9,7 @@ import android.view.LayoutInflater
import
android.view.View
import
android.view.ViewGroup
import
android.widget.ImageButton
import
com.google.firebase.firestore.FirebaseFirestore
import
com.paktalin.vocabularynotebook.R
import
kotlinx.android.synthetic.main.fragment_new_word.*
...
...
@@ -80,6 +81,12 @@ class NewWordFragment : Fragment() {
private
fun
addWord
()
{
//todo get word data from edit texts and save it
val
word
=
etWord
.
text
.
toString
()
val
translation
=
etTranslation
.
text
.
toString
()
Log
.
d
(
TAG
,
"vocabularyId: ${(activity as MainActivity).vocabularyId})"
)
FirebaseFirestore
.
getInstance
().
collection
(
"vocabularies"
).
document
()
}
companion
object
{
private
val
TAG
=
"VN/"
+
NewWordFragment
::
class
.
java
.
simpleName
}
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/VocabularyFragment.kt
View file @
9e553faa
package
com.paktalin.vocabularynotebook.ui
import
android.content.Intent
import
android.os.Bundle
import
android.support.v4.app.Fragment
import
android.support.v7.widget.LinearLayoutManager
...
...
@@ -8,7 +7,6 @@ import android.view.LayoutInflater
import
android.view.View
import
android.view.ViewGroup
import
com.google.firebase.auth.FirebaseAuth
import
com.google.firebase.firestore.DocumentReference
import
com.google.firebase.firestore.DocumentSnapshot
import
com.google.firebase.firestore.FirebaseFirestore
import
com.paktalin.vocabularynotebook.R
...
...
@@ -21,14 +19,9 @@ class VocabularyFragment : Fragment() {
private
val
TAG
=
"VN/"
+
VocabularyFragment
::
class
.
simpleName
private
const
val
VOCABULARIES
=
"vocabularies"
private
const
val
WORDS
=
"words"
private
const
val
USERS
=
"users"
}
private
lateinit
var
userDocument
:
DocumentReference
private
val
db
=
FirebaseFirestore
.
getInstance
()
private
lateinit
var
vocabulary
:
DocumentReference
//todo move data process to onCreate method and update the views later
override
fun
onCreateView
(
inflater
:
LayoutInflater
,
container
:
ViewGroup
?,
savedInstanceState
:
Bundle
?):
View
?
{
return
inflater
.
inflate
(
R
.
layout
.
fragment_vocabulary
,
container
,
false
)
...
...
@@ -37,7 +30,6 @@ class VocabularyFragment : Fragment() {
override
fun
onActivityCreated
(
savedInstanceState
:
Bundle
?)
{
super
.
onActivityCreated
(
savedInstanceState
)
setEmptyAdapter
()
retrieveData
()
}
override
fun
onDestroy
()
{
...
...
@@ -46,47 +38,24 @@ class VocabularyFragment : Fragment() {
}
private
fun
setEmptyAdapter
()
{
val
emptyList
:
MutableList
<
WordItem
>
=
mutableListOf
()
val
emptyList
:
MutableList
<
WordItem
>
=
mutableListOf
()
recyclerView
.
adapter
=
VocabularyAdapter
(
emptyList
,
activity
!!
)
val
mLayoutManager
=
LinearLayoutManager
(
activity
)
recyclerView
.
layoutManager
=
mLayoutManager
}
private
fun
retrieveData
()
{
val
userId
=
FirebaseAuth
.
getInstance
().
currentUser
!!
.
uid
userDocument
=
db
.
collection
(
USERS
).
document
(
userId
)
userDocument
.
get
().
addOnSuccessListener
{
task
->
setVocabularyId
(
task
)
retrieveVocabularyData
()
}
}
private
fun
setVocabularyId
(
task
:
DocumentSnapshot
)
{
//todo if only one vocabulary exists, open it
val
vocabularies
:
List
<
DocumentReference
>
=
task
.
get
(
"vocabularies"
)
as
List
<
DocumentReference
>
vocabulary
=
db
.
collection
(
VOCABULARIES
).
document
(
vocabularies
[
0
].
id
)
}
private
fun
retrieveVocabularyData
()
{
//todo if only one vocabulary exists, open it
vocabulary
.
collection
(
WORDS
).
get
()
.
addOnSuccessListener
{
setVocabularyAdapter
(
it
.
documents
)
}
}
private
fun
addWord
()
{
val
addWordIntent
=
Intent
(
activity
,
AddWordActivity
::
class
.
java
)
addWordIntent
.
putExtra
(
"vocabularyId"
,
vocabulary
.
id
)
startActivity
(
addWordIntent
)
fun
retrieveWordsData
(
vocabularyId
:
String
)
{
db
.
collection
(
VOCABULARIES
).
document
(
vocabularyId
).
collection
(
WORDS
).
get
()
.
addOnSuccessListener
{
setVocabularyAdapter
(
it
.
documents
,
vocabularyId
)
}
}
private
fun
setVocabularyAdapter
(
documents
:
MutableList
<
DocumentSnapshot
>)
{
private
fun
setVocabularyAdapter
(
documents
:
MutableList
<
DocumentSnapshot
>
,
vocabularyId
:
String
)
{
val
wordItems
:
MutableList
<
WordItem
>
=
mutableListOf
()
for
(
ref
in
documents
)
{
val
word
=
ref
.
get
(
"word"
).
toString
()
val
translation
=
ref
.
get
(
"translation"
).
toString
()
wordItems
.
add
(
WordItem
(
word
,
translation
,
ref
.
id
,
vocabulary
.
i
d
))
wordItems
.
add
(
WordItem
(
word
,
translation
,
ref
.
id
,
vocabulary
I
d
))
}
val
adapter
=
VocabularyAdapter
(
wordItems
,
activity
!!
)
...
...
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