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
307bfc47
authored
Apr 19, 2019
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor refactorings
parent
e6f638d2
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
54 additions
and
49 deletions
.idea/caches/build_file_checksums.ser
app/build.gradle
app/src/main/java/com/paktalin/vocabularynotebook/Vocabulary.kt
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/VocabularyFragment.kt
app/src/main/java/com/paktalin/vocabularynotebook/utils/FirestoreManager.kt
app/src/main/java/com/paktalin/vocabularynotebook/utils/PreferenceUtil.kt
.idea/caches/build_file_checksums.ser
View file @
307bfc47
No preview for this file type
app/build.gradle
View file @
307bfc47
...
@@ -13,10 +13,11 @@ android {
...
@@ -13,10 +13,11 @@ android {
versionCode
4
versionCode
4
versionName
"1.3"
versionName
"1.3"
multiDexEnabled
true
multiDexEnabled
true
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
vectorDrawables
{
vectorDrawables
{
useSupportLibrary
=
true
useSupportLibrary
=
true
}
}
testInstrumentationRunner
"androidx.test.runner.AndroidJUnitRunner"
}
}
buildTypes
{
buildTypes
{
release
{
release
{
...
@@ -48,4 +49,8 @@ dependencies {
...
@@ -48,4 +49,8 @@ dependencies {
testImplementation
'junit:junit:4.12'
testImplementation
'junit:junit:4.12'
androidTestImplementation
'com.android.support.test:runner:1.0.2'
androidTestImplementation
'com.android.support.test:runner:1.0.2'
androidTestImplementation
'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation
'com.android.support.test.espresso:espresso-core:3.0.2'
}
androidTestImplementation
'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation
'androidx.test:rules:1.1.1'
androidTestImplementation
'androidx.test.ext:junit:1.1.0'
androidTestImplementation
'androidx.test.espresso:espresso-intents:3.1.1'
}
app/src/main/java/com/paktalin/vocabularynotebook/Vocabulary.kt
View file @
307bfc47
...
@@ -8,8 +8,17 @@ import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.vocabula
...
@@ -8,8 +8,17 @@ import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.vocabula
class
Vocabulary
()
{
class
Vocabulary
()
{
private
var
words
:
MutableList
<
WordItem
>
=
mutableListOf
()
private
var
words
:
MutableList
<
WordItem
>
=
mutableListOf
()
constructor
(
words
:
MutableList
<
WordItem
>)
:
this
()
{
/*
constructor(words: MutableList<WordItem>) : this() {
this.words = words
this.words = words
}*/
constructor
(
documents
:
MutableList
<
DocumentSnapshot
>)
:
this
()
{
for
(
ref
in
documents
)
{
val
word
=
ref
[
"word"
].
toString
()
val
translation
=
ref
[
"translation"
].
toString
()
val
time
=
ref
[
"time"
]
as
Timestamp
words
.
add
(
WordItem
(
word
,
translation
,
time
.
toDate
(),
ref
.
id
,
vocabularyId
!!
))
}
}
}
companion
object
{
companion
object
{
...
...
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
View file @
307bfc47
...
@@ -16,6 +16,7 @@ import kotlinx.android.synthetic.main.word_item.view.*
...
@@ -16,6 +16,7 @@ import kotlinx.android.synthetic.main.word_item.view.*
class
VocabularyAdapter
(
private
val
displayedVocabulary
:
Vocabulary
,
private
val
mainActivity
:
MainActivity
)
:
RecyclerView
.
Adapter
<
VocabularyAdapter
.
ViewHolder
>()
{
class
VocabularyAdapter
(
private
val
displayedVocabulary
:
Vocabulary
,
private
val
mainActivity
:
MainActivity
)
:
RecyclerView
.
Adapter
<
VocabularyAdapter
.
ViewHolder
>()
{
private
var
fullVocabulary
:
MutableList
<
WordItem
>
=
mutableListOf
()
// stores all the words loaded from the db
private
lateinit
var
recyclerView
:
RecyclerView
private
lateinit
var
recyclerView
:
RecyclerView
var
sortOrder
:
Int
=
0
var
sortOrder
:
Int
=
0
...
@@ -23,10 +24,8 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
...
@@ -23,10 +24,8 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
field
=
value
;
sort
()
field
=
value
;
sort
()
}
}
private
var
vocabularyCopy
:
MutableList
<
WordItem
>
=
mutableListOf
()
// stores all the words loaded from the db
init
{
init
{
vocabularyCop
y
.
addAll
(
displayedVocabulary
.
get
())
fullVocabular
y
.
addAll
(
displayedVocabulary
.
get
())
}
}
override
fun
onAttachedToRecyclerView
(
recyclerView
:
RecyclerView
)
{
override
fun
onAttachedToRecyclerView
(
recyclerView
:
RecyclerView
)
{
...
@@ -75,7 +74,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
...
@@ -75,7 +74,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
fun
refresh
()
{
fun
refresh
()
{
/*displayedVocabulary.clear()
/*displayedVocabulary.clear()
FirestoreManager().
retrieveWordsData
{ documents ->
FirestoreManager().
extractVocabulary
{ documents ->
displayedVocabulary.addWordsAsDocuments(documents)
displayedVocabulary.addWordsAsDocuments(documents)
this.notifyDataSetChanged()
this.notifyDataSetChanged()
}*/
}*/
...
@@ -120,9 +119,9 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
...
@@ -120,9 +119,9 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
fun
filter
(
query
:
String
)
{
fun
filter
(
query
:
String
)
{
displayedVocabulary
.
clear
()
displayedVocabulary
.
clear
()
if
(
query
.
isEmpty
())
if
(
query
.
isEmpty
())
displayedVocabulary
.
addWords
(
vocabularyCop
y
)
displayedVocabulary
.
addWords
(
fullVocabular
y
)
else
else
displayedVocabulary
.
addWordsFittingQuery
(
vocabularyCop
y
,
query
.
toLowerCase
())
displayedVocabulary
.
addWordsFittingQuery
(
fullVocabular
y
,
query
.
toLowerCase
())
notifyDataSetChanged
()
notifyDataSetChanged
()
}
}
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/MainActivity.kt
View file @
307bfc47
...
@@ -29,11 +29,10 @@ class MainActivity : AppCompatActivity() {
...
@@ -29,11 +29,10 @@ class MainActivity : AppCompatActivity() {
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
super
.
onCreate
(
savedInstanceState
)
setContentView
(
R
.
layout
.
activity_main
)
setContentView
(
R
.
layout
.
activity_main
)
FirestoreManager
.
vocabularyId
=
getSavedVocabularyId
(
this
@MainActivity
)
FirestoreManager
.
vocabularyId
=
getSavedVocabularyIdFromPreferences
(
this
@MainActivity
)
swipeRefresh
.
setOnRefreshListener
{
refreshVocabulary
()
}
setUpSwipeRefresh
()
swipeRefresh
.
setColorSchemeResources
(
R
.
color
.
colorAccent
)
hideKeyboard
()
setUpNavigationView
()
setUpNavigationView
()
hideKeyboard
()
}
}
override
fun
onCreateOptionsMenu
(
menu
:
Menu
?):
Boolean
{
override
fun
onCreateOptionsMenu
(
menu
:
Menu
?):
Boolean
{
...
@@ -41,7 +40,7 @@ class MainActivity : AppCompatActivity() {
...
@@ -41,7 +40,7 @@ class MainActivity : AppCompatActivity() {
searchView
=
menu
!!
.
findItem
(
R
.
id
.
search
).
actionView
as
SearchView
searchView
=
menu
!!
.
findItem
(
R
.
id
.
search
).
actionView
as
SearchView
// extract vocabulary data only after searchView is initialized,
// extract vocabulary data only after searchView is initialized,
// since it needs to be called in the VocabularyFragment initialization
// since it needs to be called in the VocabularyFragment initialization
extractVocabularyData
()
setUpVocabularyFragment
()
return
true
return
true
}
}
...
@@ -78,19 +77,21 @@ class MainActivity : AppCompatActivity() {
...
@@ -78,19 +77,21 @@ class MainActivity : AppCompatActivity() {
}
}
}
}
private
fun
extractVocabularyData
()
{
private
fun
setUpSwipeRefresh
()
{
addProgressBar
()
swipeRefresh
.
setOnRefreshListener
{
refreshVocabulary
()
}
FirestoreManager
().
extractVocabularyId
(
swipeRefresh
.
setColorSchemeResources
(
R
.
color
.
colorAccent
)
{
addVocabularyFragment
(
FirestoreManager
.
vocabularyId
!!
)
},
{
showToastNoWords
()
},
{
removeProgressBar
()
},
this
)
}
}
private
fun
addVocabularyFragment
(
vocabularyId
:
String
)
{
private
fun
setUpVocabularyFragment
()
{
vocabularyFragment
=
VocabularyFragment
()
addProgressBar
()
FirestoreManager
().
extractVocabularyId
(
{
vocabularyFragment
=
VocabularyFragment
()
val
arguments
=
Bundle
()
val
arguments
=
Bundle
()
arguments
.
putString
(
"vocabularyId"
,
vocabularyId
)
arguments
.
putString
(
"vocabularyId"
,
FirestoreManager
.
vocabularyId
!!
)
addFragment
(
supportFragmentManager
,
vocabularyFragment
,
R
.
id
.
swipeRefresh
,
arguments
)
addFragment
(
supportFragmentManager
,
vocabularyFragment
,
R
.
id
.
swipeRefresh
,
arguments
)
},
{
showToastNoWords
()
},
{
removeProgressBar
()
},
this
)
}
}
private
fun
hideKeyboard
()
{
private
fun
hideKeyboard
()
{
...
@@ -120,6 +121,7 @@ class MainActivity : AppCompatActivity() {
...
@@ -120,6 +121,7 @@ class MainActivity : AppCompatActivity() {
override
fun
onPause
()
{
override
fun
onPause
()
{
super
.
onPause
()
super
.
onPause
()
// TODO save changes
hideKeyboard
()
hideKeyboard
()
}
}
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/fragments/VocabularyFragment.kt
View file @
307bfc47
...
@@ -12,7 +12,6 @@ import com.paktalin.vocabularynotebook.firestoreitems.WordItem
...
@@ -12,7 +12,6 @@ import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import
com.paktalin.vocabularynotebook.ui.activities.MainActivity
import
com.paktalin.vocabularynotebook.ui.activities.MainActivity
import
com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
import
com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
import
com.paktalin.vocabularynotebook.utils.FirestoreManager
import
com.paktalin.vocabularynotebook.utils.FirestoreManager
import
com.paktalin.vocabularynotebook.utils.Log
import
kotlinx.android.synthetic.main.fragment_vocabulary.*
import
kotlinx.android.synthetic.main.fragment_vocabulary.*
class
VocabularyFragment
:
Fragment
()
{
class
VocabularyFragment
:
Fragment
()
{
...
@@ -29,36 +28,26 @@ class VocabularyFragment : Fragment() {
...
@@ -29,36 +28,26 @@ class VocabularyFragment : Fragment() {
override
fun
onActivityCreated
(
savedInstanceState
:
Bundle
?)
{
override
fun
onActivityCreated
(
savedInstanceState
:
Bundle
?)
{
super
.
onActivityCreated
(
savedInstanceState
)
super
.
onActivityCreated
(
savedInstanceState
)
mainActivity
=
activity
as
MainActivity
mainActivity
=
activity
as
MainActivity
setEmptyAdapter
()
setEmpty
Vocabulary
Adapter
()
extract
WordsData
(
arguments
!!
[
"vocabularyId"
]
as
String
)
extract
VocabularyById
(
)
}
}
private
fun
setEmptyAdapter
()
{
private
fun
extractVocabularyById
()
{
val
emptyList
:
MutableList
<
WordItem
>
=
mutableListOf
()
FirestoreManager
().
extractVocabulary
{
documents
->
recyclerView
.
adapter
=
VocabularyAdapter
(
Vocabulary
(
emptyList
),
mainActivity
)
run
{
val
mLayoutManager
=
LockableLayoutManager
(
mainActivity
)
if
(
documents
.
isNotEmpty
())
setVocabularyAdapter
(
documents
)
recyclerView
.
layoutManager
=
mLayoutManager
else
mainActivity
.
showToastNoWords
()
}
}
private
fun
extractWordsData
(
vocabularyId
:
String
)
{
FirestoreManager
().
retrieveWordsData
{
documents
->
onSuccessfulWordDataExtraction
(
documents
,
vocabularyId
)}
}
}
private
fun
onSuccessfulWordDataExtraction
(
documents
:
MutableList
<
DocumentSnapshot
>,
vocabularyId
:
String
)
{
if
(
documents
.
isNotEmpty
())
setVocabularyAdapter
(
documents
,
vocabularyId
)
else
{
Log
.
i
(
TAG
,
"There are no documents in collection \"words\""
)
mainActivity
.
showToastNoWords
()
}
}
private
fun
setEmptyVocabularyAdapter
()
{
recyclerView
.
adapter
=
VocabularyAdapter
(
Vocabulary
(),
mainActivity
)
recyclerView
.
layoutManager
=
LockableLayoutManager
(
mainActivity
)
}
}
private
fun
setVocabularyAdapter
(
documents
:
MutableList
<
DocumentSnapshot
>,
vocabularyId
:
String
)
{
private
fun
setVocabularyAdapter
(
documents
:
MutableList
<
DocumentSnapshot
>)
{
val
vocabulary
=
Vocabulary
()
recyclerView
.
adapter
=
VocabularyAdapter
(
Vocabulary
(
documents
),
mainActivity
)
vocabulary
.
addWordsAsDocuments
(
documents
)
val
adapter
=
VocabularyAdapter
(
vocabulary
,
mainActivity
)
recyclerView
.
adapter
=
adapter
}
}
fun
addWord
(
newWord
:
WordItem
)
{
(
recyclerView
.
adapter
as
VocabularyAdapter
).
addWord
(
newWord
)
}
fun
addWord
(
newWord
:
WordItem
)
{
(
recyclerView
.
adapter
as
VocabularyAdapter
).
addWord
(
newWord
)
}
...
...
app/src/main/java/com/paktalin/vocabularynotebook/utils/FirestoreManager.kt
View file @
307bfc47
...
@@ -81,7 +81,7 @@ class FirestoreManager {
...
@@ -81,7 +81,7 @@ class FirestoreManager {
.
addOnFailureListener
{
e
->
Log
.
w
(
TAG
,
"deleteWordWithId $id:failure"
,
e
.
fillInStackTrace
())
}
.
addOnFailureListener
{
e
->
Log
.
w
(
TAG
,
"deleteWordWithId $id:failure"
,
e
.
fillInStackTrace
())
}
}
}
fun
retrieveWordsData
(
onSuccess
:
(
documents
:
MutableList
<
DocumentSnapshot
>)
->
Unit
)
{
fun
extractVocabulary
(
onSuccess
:
(
documents
:
MutableList
<
DocumentSnapshot
>)
->
Unit
)
{
vocabularyDocument
().
collection
(
WORDS
)
vocabularyDocument
().
collection
(
WORDS
)
.
orderBy
(
"time"
,
Query
.
Direction
.
DESCENDING
)
.
orderBy
(
"time"
,
Query
.
Direction
.
DESCENDING
)
.
get
()
.
get
()
...
...
app/src/main/java/com/paktalin/vocabularynotebook/utils/PreferenceUtil.kt
View file @
307bfc47
...
@@ -10,7 +10,7 @@ fun saveVocabularyId(context: Context, vocabularyId: String) {
...
@@ -10,7 +10,7 @@ fun saveVocabularyId(context: Context, vocabularyId: String) {
sharedPreferences
.
edit
().
putString
(
vocabularyIdKey
,
vocabularyId
).
apply
()
sharedPreferences
.
edit
().
putString
(
vocabularyIdKey
,
vocabularyId
).
apply
()
}
}
fun
getSavedVocabularyId
(
context
:
Context
):
String
?
{
fun
getSavedVocabularyId
FromPreferences
(
context
:
Context
):
String
?
{
val
sharedPreferences
=
PreferenceManager
.
getDefaultSharedPreferences
(
context
)
val
sharedPreferences
=
PreferenceManager
.
getDefaultSharedPreferences
(
context
)
return
sharedPreferences
.
getString
(
vocabularyIdKey
,
null
)
return
sharedPreferences
.
getString
(
vocabularyIdKey
,
null
)
}
}
\ 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