Commit 115907ef by Paktalin

moved constant strings words and vocabularies under the Vocabulary class

parent 1e099358
......@@ -6,6 +6,7 @@ import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.ui.LogInActivity
import com.paktalin.vocabularynotebook.firestoreitems.UserPojo
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
import java.util.*
class UserManager {
......@@ -23,7 +24,7 @@ class UserManager {
val db = ConfiguredFirestore.instance
val user = UserPojo(newUser.email)
db.collection("vocabularies").add(Vocabulary.Pojo(null))
db.collection(VOCABULARIES).add(Vocabulary.Pojo(null))
.addOnSuccessListener { firstVocabularyRef ->
Log.d(TAG, "VocabularyPojo successfully created: " + firstVocabularyRef.path)
user.vocabularies = Collections.singletonList(firstVocabularyRef)
......
......@@ -7,6 +7,9 @@ class Vocabulary(words: MutableList<WordItem>) {
private const val SORT_BY_TIME = 0
private const val SORT_BY_WORD = 1
private const val SORT_BY_TRANSLATION = 2
const val WORDS = "words"
const val VOCABULARIES = "vocabularies"
}
var pojo:Pojo
......
......@@ -2,6 +2,8 @@ package com.paktalin.vocabularynotebook.firestoreitems
import android.util.Log
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.WORDS
import java.io.Serializable
import java.util.Date
......@@ -19,8 +21,8 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p
: this(pojo.word, pojo.translation, pojo.time, id, vocabularyId)
fun delete() {
ConfiguredFirestore.instance.collection("vocabularies").document(vocabularyId)
.collection("words").document(id).delete()
ConfiguredFirestore.instance.collection(VOCABULARIES).document(vocabularyId)
.collection(WORDS).document(id).delete()
.addOnSuccessListener { Log.i(TAG, "Successfully deleted word with id $id") }
.addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) }
}
......
package com.paktalin.vocabularynotebook.ui
import android.util.Log
import android.view.View
import android.widget.Toast
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.WORDS
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import kotlinx.android.synthetic.main.fragment_new_word.*
class AddWordFragment : WordFragment() {
......@@ -27,5 +31,16 @@ class AddWordFragment : WordFragment() {
mainActivity.vocabularyFragment.addWord(wordItem)
}
override fun updateButtons() {
super.updateButtons()
if (!wordEmpty || !translationEmpty) showClearButton()
if (wordEmpty && translationEmpty) hideClearButton()
}
private fun hideClearButton() { btnClear.visibility = View.INVISIBLE }
private fun showClearButton() { btnClear.visibility = View.VISIBLE }
companion object { private val TAG = "VN/" + AddWordFragment::class.java.simpleName }
}
\ No newline at end of file
......@@ -21,6 +21,7 @@ import com.paktalin.vocabularynotebook.VocabularyAdapter
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import kotlinx.android.synthetic.main.fragment_vocabulary.*
import android.support.v7.widget.SearchView
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
class MainActivity : AppCompatActivity() {
......@@ -75,9 +76,9 @@ class MainActivity : AppCompatActivity() {
.addOnSuccessListener { task ->
hideProgressBar()
//todo move Firestore logic and collections names to a separate class
if (task.get("vocabularies") != null) {
val vocabularies: List<DocumentReference> = task.get("vocabularies") as List<DocumentReference>
val vocabulary = db.collection("vocabularies").document(vocabularies[0].id)
if (task.get(VOCABULARIES) != null) {
val vocabularies: List<DocumentReference> = task.get(VOCABULARIES) as List<DocumentReference>
val vocabulary = db.collection(VOCABULARIES).document(vocabularies[0].id)
vocabularyId = vocabulary.id
// start VocabularyFragment
......
......@@ -14,14 +14,14 @@ import com.google.firebase.firestore.Query
import com.paktalin.vocabularynotebook.*
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.WORDS
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import kotlinx.android.synthetic.main.fragment_vocabulary.*
class VocabularyFragment : Fragment() {
companion object {
private val TAG = "VN/" + VocabularyFragment::class.simpleName
private const val VOCABULARIES = "vocabularies"
private const val WORDS = "words"
}
private val db = ConfiguredFirestore.instance
......
......@@ -13,17 +13,12 @@ import kotlinx.android.synthetic.main.fragment_new_word.*
import kotlinx.android.synthetic.main.notebook_sheet.*
abstract class WordFragment : Fragment() {
companion object {
const val VOCABULARIES = "vocabularies"
const val WORDS = "words"
}
protected lateinit var mainActivity: MainActivity
private var wordEmpty: Boolean = true
internal var wordEmpty: Boolean = true
set(value) { field = value; updateButtons() }
private var translationEmpty: Boolean = true
internal var translationEmpty: Boolean = true
set(value) { field = value; updateButtons() }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
......@@ -43,15 +38,9 @@ abstract class WordFragment : Fragment() {
btnClear.setOnClickListener { clearFields() }
}
private fun updateButtons() {
if (!wordEmpty || !translationEmpty)
showClearButton()
if (!wordEmpty && !translationEmpty)
showSubmitButton()
if (wordEmpty || translationEmpty)
hideSubmitButton()
if (wordEmpty && translationEmpty)
hideClearButton()
open fun updateButtons() {
if (!wordEmpty && !translationEmpty) showSubmitButton()
if (wordEmpty || translationEmpty) hideSubmitButton()
}
private fun textWatcher(setEmpty: () -> Unit): TextWatcher {
......@@ -68,10 +57,6 @@ abstract class WordFragment : Fragment() {
protected fun hideSubmitButton() {
mainActivity.btnSubmitLayout.visibility = View.GONE }
private fun hideClearButton() { btnClear.visibility = View.INVISIBLE }
private fun showClearButton() { btnClear.visibility = View.VISIBLE }
fun submitWord() {
mainActivity.hideKeyboardNotFromActivity(mainActivity)
......
......@@ -15,4 +15,7 @@
<string name="menu_option_edit">Edit</string>
<string name="option_sort">Sort</string>
<string name="option_search">Search</string>
<!--Toast messages-->
<string name="toast_no_words">You don\'t have any words yet. Add your fist one!</string>
</resources>
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