Commit 307bfc47 by Paktalin

Minor refactorings

parent e6f638d2
......@@ -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'}
......@@ -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 {
......
......@@ -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 {
vocabularyCopy.addAll(displayedVocabulary.get())
fullVocabulary.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(vocabularyCopy)
displayedVocabulary.addWords(fullVocabulary)
else
displayedVocabulary.addWordsFittingQuery(vocabularyCopy, query.toLowerCase())
displayedVocabulary.addWordsFittingQuery(fullVocabulary, query.toLowerCase())
notifyDataSetChanged()
}
......
......@@ -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,21 +77,23 @@ class MainActivity : AppCompatActivity() {
}
}
private fun extractVocabularyData() {
private fun setUpSwipeRefresh() {
swipeRefresh.setOnRefreshListener { refreshVocabulary() }
swipeRefresh.setColorSchemeResources(R.color.colorAccent)
}
private fun setUpVocabularyFragment() {
addProgressBar()
FirestoreManager().extractVocabularyId(
{ addVocabularyFragment(FirestoreManager.vocabularyId!!) },
{ vocabularyFragment = VocabularyFragment()
val arguments = Bundle()
arguments.putString("vocabularyId", FirestoreManager.vocabularyId!!)
addFragment(supportFragmentManager, vocabularyFragment, R.id.swipeRefresh, arguments)
},
{ showToastNoWords() },
{ removeProgressBar() }, this)
}
private fun addVocabularyFragment(vocabularyId: String) {
vocabularyFragment = VocabularyFragment()
val arguments = Bundle()
arguments.putString("vocabularyId", vocabularyId)
addFragment(supportFragmentManager, vocabularyFragment, R.id.swipeRefresh, arguments)
}
private fun hideKeyboard() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
}
......@@ -120,6 +121,7 @@ class MainActivity : AppCompatActivity() {
override fun onPause() {
super.onPause()
// TODO save changes
hideKeyboard()
}
......
......@@ -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()
extractWordsData(arguments!!["vocabularyId"] as String)
setEmptyVocabularyAdapter()
extractVocabularyById()
}
private fun setEmptyAdapter() {
val emptyList: MutableList<WordItem> = mutableListOf()
recyclerView.adapter = VocabularyAdapter(Vocabulary(emptyList), mainActivity)
val mLayoutManager = LockableLayoutManager(mainActivity)
recyclerView.layoutManager = mLayoutManager
}
private fun extractWordsData(vocabularyId: String) {
FirestoreManager().retrieveWordsData { documents ->
onSuccessfulWordDataExtraction(documents, vocabularyId)}
private fun extractVocabularyById() {
FirestoreManager().extractVocabulary { documents ->
run {
if (documents.isNotEmpty()) setVocabularyAdapter(documents)
else mainActivity.showToastNoWords()
}
}
}
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) }
......
......@@ -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()
......
......@@ -10,7 +10,7 @@ fun saveVocabularyId(context: Context, vocabularyId: String) {
sharedPreferences.edit().putString(vocabularyIdKey, vocabularyId).apply()
}
fun getSavedVocabularyId(context: Context): String? {
fun getSavedVocabularyIdFromPreferences(context: Context): String? {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
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