Commit 307bfc47 by Paktalin

Minor refactorings

parent e6f638d2
...@@ -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'}
...@@ -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 {
......
...@@ -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 {
vocabularyCopy.addAll(displayedVocabulary.get()) fullVocabulary.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(vocabularyCopy) displayedVocabulary.addWords(fullVocabulary)
else else
displayedVocabulary.addWordsFittingQuery(vocabularyCopy, query.toLowerCase()) displayedVocabulary.addWordsFittingQuery(fullVocabulary, query.toLowerCase())
notifyDataSetChanged() notifyDataSetChanged()
} }
......
...@@ -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()
} }
......
...@@ -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() setEmptyVocabularyAdapter()
extractWordsData(arguments!!["vocabularyId"] as String) extractVocabularyById()
} }
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) }
......
...@@ -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()
......
...@@ -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 getSavedVocabularyIdFromPreferences(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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment