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 {
versionCode
4
versionName
"1.3"
multiDexEnabled
true
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
vectorDrawables
{
useSupportLibrary
=
true
}
testInstrumentationRunner
"androidx.test.runner.AndroidJUnitRunner"
}
buildTypes
{
release
{
...
...
@@ -48,4 +49,8 @@ dependencies {
testImplementation
'junit:junit:4.12'
androidTestImplementation
'com.android.support.test:runner:1.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
class
Vocabulary
()
{
private
var
words
:
MutableList
<
WordItem
>
=
mutableListOf
()
constructor
(
words
:
MutableList
<
WordItem
>)
:
this
()
{
/*
constructor(words: MutableList<WordItem>) : this() {
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
{
...
...
app/src/main/java/com/paktalin/vocabularynotebook/VocabularyAdapter.kt
View file @
307bfc47
...
...
@@ -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
>()
{
private
var
fullVocabulary
:
MutableList
<
WordItem
>
=
mutableListOf
()
// stores all the words loaded from the db
private
lateinit
var
recyclerView
:
RecyclerView
var
sortOrder
:
Int
=
0
...
...
@@ -23,10 +24,8 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
field
=
value
;
sort
()
}
private
var
vocabularyCopy
:
MutableList
<
WordItem
>
=
mutableListOf
()
// stores all the words loaded from the db
init
{
vocabularyCop
y
.
addAll
(
displayedVocabulary
.
get
())
fullVocabular
y
.
addAll
(
displayedVocabulary
.
get
())
}
override
fun
onAttachedToRecyclerView
(
recyclerView
:
RecyclerView
)
{
...
...
@@ -75,7 +74,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
fun
refresh
()
{
/*displayedVocabulary.clear()
FirestoreManager().
retrieveWordsData
{ documents ->
FirestoreManager().
extractVocabulary
{ documents ->
displayedVocabulary.addWordsAsDocuments(documents)
this.notifyDataSetChanged()
}*/
...
...
@@ -120,9 +119,9 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
fun
filter
(
query
:
String
)
{
displayedVocabulary
.
clear
()
if
(
query
.
isEmpty
())
displayedVocabulary
.
addWords
(
vocabularyCop
y
)
displayedVocabulary
.
addWords
(
fullVocabular
y
)
else
displayedVocabulary
.
addWordsFittingQuery
(
vocabularyCop
y
,
query
.
toLowerCase
())
displayedVocabulary
.
addWordsFittingQuery
(
fullVocabular
y
,
query
.
toLowerCase
())
notifyDataSetChanged
()
}
...
...
app/src/main/java/com/paktalin/vocabularynotebook/ui/activities/MainActivity.kt
View file @
307bfc47
...
...
@@ -29,11 +29,10 @@ class MainActivity : AppCompatActivity() {
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
setContentView
(
R
.
layout
.
activity_main
)
FirestoreManager
.
vocabularyId
=
getSavedVocabularyId
(
this
@MainActivity
)
swipeRefresh
.
setOnRefreshListener
{
refreshVocabulary
()
}
swipeRefresh
.
setColorSchemeResources
(
R
.
color
.
colorAccent
)
hideKeyboard
()
FirestoreManager
.
vocabularyId
=
getSavedVocabularyIdFromPreferences
(
this
@MainActivity
)
setUpSwipeRefresh
()
setUpNavigationView
()
hideKeyboard
()
}
override
fun
onCreateOptionsMenu
(
menu
:
Menu
?):
Boolean
{
...
...
@@ -41,7 +40,7 @@ class MainActivity : AppCompatActivity() {
searchView
=
menu
!!
.
findItem
(
R
.
id
.
search
).
actionView
as
SearchView
// extract vocabulary data only after searchView is initialized,
// since it needs to be called in the VocabularyFragment initialization
extractVocabularyData
()
setUpVocabularyFragment
()
return
true
}
...
...
@@ -78,19 +77,21 @@ class MainActivity : AppCompatActivity() {
}
}
private
fun
extractVocabularyData
()
{
addProgressBar
()
FirestoreManager
().
extractVocabularyId
(
{
addVocabularyFragment
(
FirestoreManager
.
vocabularyId
!!
)
},
{
showToastNoWords
()
},
{
removeProgressBar
()
},
this
)
private
fun
setUpSwipeRefresh
()
{
swipeRefresh
.
setOnRefreshListener
{
refreshVocabulary
()
}
swipeRefresh
.
setColorSchemeResources
(
R
.
color
.
colorAccent
)
}
private
fun
addVocabularyFragment
(
vocabularyId
:
String
)
{
vocabularyFragment
=
VocabularyFragment
()
private
fun
setUpVocabularyFragment
()
{
addProgressBar
()
FirestoreManager
().
extractVocabularyId
(
{
vocabularyFragment
=
VocabularyFragment
()
val
arguments
=
Bundle
()
arguments
.
putString
(
"vocabularyId"
,
vocabularyId
)
arguments
.
putString
(
"vocabularyId"
,
FirestoreManager
.
vocabularyId
!!
)
addFragment
(
supportFragmentManager
,
vocabularyFragment
,
R
.
id
.
swipeRefresh
,
arguments
)
},
{
showToastNoWords
()
},
{
removeProgressBar
()
},
this
)
}
private
fun
hideKeyboard
()
{
...
...
@@ -120,6 +121,7 @@ class MainActivity : AppCompatActivity() {
override
fun
onPause
()
{
super
.
onPause
()
// TODO save changes
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
import
com.paktalin.vocabularynotebook.ui.activities.MainActivity
import
com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
import
com.paktalin.vocabularynotebook.utils.FirestoreManager
import
com.paktalin.vocabularynotebook.utils.Log
import
kotlinx.android.synthetic.main.fragment_vocabulary.*
class
VocabularyFragment
:
Fragment
()
{
...
...
@@ -29,36 +28,26 @@ class VocabularyFragment : Fragment() {
override
fun
onActivityCreated
(
savedInstanceState
:
Bundle
?)
{
super
.
onActivityCreated
(
savedInstanceState
)
mainActivity
=
activity
as
MainActivity
setEmptyAdapter
()
extract
WordsData
(
arguments
!!
[
"vocabularyId"
]
as
String
)
setEmpty
Vocabulary
Adapter
()
extract
VocabularyById
(
)
}
private
fun
setEmptyAdapter
()
{
val
emptyList
:
MutableList
<
WordItem
>
=
mutableListOf
()
recyclerView
.
adapter
=
VocabularyAdapter
(
Vocabulary
(
emptyList
),
mainActivity
)
val
mLayoutManager
=
LockableLayoutManager
(
mainActivity
)
recyclerView
.
layoutManager
=
mLayoutManager
private
fun
extractVocabularyById
()
{
FirestoreManager
().
extractVocabulary
{
documents
->
run
{
if
(
documents
.
isNotEmpty
())
setVocabularyAdapter
(
documents
)
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
)
{
val
vocabulary
=
Vocabulary
()
vocabulary
.
addWordsAsDocuments
(
documents
)
val
adapter
=
VocabularyAdapter
(
vocabulary
,
mainActivity
)
recyclerView
.
adapter
=
adapter
private
fun
setVocabularyAdapter
(
documents
:
MutableList
<
DocumentSnapshot
>)
{
recyclerView
.
adapter
=
VocabularyAdapter
(
Vocabulary
(
documents
),
mainActivity
)
}
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 {
.
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
)
.
orderBy
(
"time"
,
Query
.
Direction
.
DESCENDING
)
.
get
()
...
...
app/src/main/java/com/paktalin/vocabularynotebook/utils/PreferenceUtil.kt
View file @
307bfc47
...
...
@@ -10,7 +10,7 @@ fun saveVocabularyId(context: Context, vocabularyId: String) {
sharedPreferences
.
edit
().
putString
(
vocabularyIdKey
,
vocabularyId
).
apply
()
}
fun
getSavedVocabularyId
(
context
:
Context
):
String
?
{
fun
getSavedVocabularyId
FromPreferences
(
context
:
Context
):
String
?
{
val
sharedPreferences
=
PreferenceManager
.
getDefaultSharedPreferences
(
context
)
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