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