Commit 4dd660d5 by Paktalin

New words are added to the database

parent 6277fd14
package com.paktalin.vocabularynotebook
import android.content.Context
import android.text.TextUtils
import android.widget.Toast
class Utils {
companion object {
fun fieldsNotEmpty(text1: String, text2: String, toastMessage: String, context: Context): Boolean {
if (TextUtils.isEmpty(text1) || TextUtils.isEmpty(text2)) {
Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show()
return false
}
return true
}
}
}
\ No newline at end of file
package com.paktalin.vocabularynotebook;
public class WordPojo {
private String word, translation;
public WordPojo(String word, String translation) {
this.word = word;
this.translation = translation;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getTranslation() {
return translation;
}
public void setTranslation(String translation) {
this.translation = translation;
}
}
...@@ -2,13 +2,50 @@ package com.paktalin.vocabularynotebook.activities ...@@ -2,13 +2,50 @@ package com.paktalin.vocabularynotebook.activities
import android.os.Bundle import android.os.Bundle
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.Toast
import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.Utils
import com.paktalin.vocabularynotebook.WordPojo
import kotlinx.android.synthetic.main.activity_add_word.*
class AddWordActivity : AppCompatActivity() { class AddWordActivity : AppCompatActivity() {
companion object {
private val TAG = "VN/" + AddWordActivity::class.simpleName
private const val VOCABULARIES = "vocabularies"
private const val WORDS = "words"
}
private lateinit var vocabularyId: String
private val db = FirebaseFirestore.getInstance()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_word) setContentView(R.layout.activity_add_word)
vocabularyId = intent.getStringExtra("vocabularyId")
btnSubmitNewWord.setOnClickListener( { addWordToDb() })
}
private fun addWordToDb() {
val word = etWord.text.toString()
val translation = etTranslation.text.toString()
if (Utils.fieldsNotEmpty(word, translation, "Please, enter word and translation", this)) {
db.collection(VOCABULARIES).document(vocabularyId)
.collection(WORDS).add(WordPojo(word, translation)).addOnSuccessListener {
Log.i(TAG, "Successfully added a new word $word")
clearFields()
}
.addOnFailureListener {
Log.w(TAG, "addNewWordToDb:failure", it.fillInStackTrace())
Toast.makeText(this, "Couldn't add the word", Toast.LENGTH_SHORT).show()
}
}
}
private fun clearFields() {
etWord.text.clear()
etTranslation.text.clear()
} }
} }
...@@ -4,7 +4,6 @@ import android.annotation.SuppressLint ...@@ -4,7 +4,6 @@ import android.annotation.SuppressLint
import android.content.Intent import android.content.Intent
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.text.TextUtils
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
...@@ -12,9 +11,9 @@ import com.google.firebase.auth.FirebaseAuth ...@@ -12,9 +11,9 @@ import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_log_in.* import kotlinx.android.synthetic.main.activity_log_in.*
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.UserManager import com.paktalin.vocabularynotebook.UserManager
import com.paktalin.vocabularynotebook.Utils
class LogInActivity : AppCompatActivity() { class LogInActivity : AppCompatActivity() {
private var mAuth: FirebaseAuth? = null private var mAuth: FirebaseAuth? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
...@@ -39,7 +38,7 @@ class LogInActivity : AppCompatActivity() { ...@@ -39,7 +38,7 @@ class LogInActivity : AppCompatActivity() {
val email = etEmail!!.text.toString() val email = etEmail!!.text.toString()
val password = etPassword!!.text.toString() val password = etPassword!!.text.toString()
if (fieldsNotEmpty(email, password)) { if (Utils.fieldsNotEmpty(email, password, "Please, enter email and password", this)) {
mAuth!!.signInWithEmailAndPassword(email, password) mAuth!!.signInWithEmailAndPassword(email, password)
.addOnCompleteListener { task -> .addOnCompleteListener { task ->
if (task.isSuccessful) { if (task.isSuccessful) {
...@@ -59,7 +58,7 @@ class LogInActivity : AppCompatActivity() { ...@@ -59,7 +58,7 @@ class LogInActivity : AppCompatActivity() {
val email = etEmail!!.text.toString() val email = etEmail!!.text.toString()
val password = etPassword!!.text.toString() val password = etPassword!!.text.toString()
if (fieldsNotEmpty(email, password)) { if (Utils.fieldsNotEmpty(email, password, "Please, enter email and password", this)) {
//todo check if the password is good //todo check if the password is good
// todo verify email // todo verify email
mAuth!!.createUserWithEmailAndPassword(email, password) mAuth!!.createUserWithEmailAndPassword(email, password)
...@@ -80,14 +79,6 @@ class LogInActivity : AppCompatActivity() { ...@@ -80,14 +79,6 @@ class LogInActivity : AppCompatActivity() {
startActivity(userActivityIntent) startActivity(userActivityIntent)
} }
private fun fieldsNotEmpty(email: String, password: String): Boolean {
if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
Toast.makeText(this@LogInActivity, "Please, enter email and password", Toast.LENGTH_SHORT).show()
return false
}
return true
}
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
private fun createRandomUser() { private fun createRandomUser() {
etEmail.setText("random@gmail.com") etEmail.setText("random@gmail.com")
......
...@@ -19,6 +19,7 @@ class VocabularyFragment : Fragment() { ...@@ -19,6 +19,7 @@ class VocabularyFragment : Fragment() {
private lateinit var userDocument: DocumentReference private lateinit var userDocument: DocumentReference
private val db = FirebaseFirestore.getInstance() private val db = FirebaseFirestore.getInstance()
private lateinit var vocabularyId: String
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_vocabulary, container, false) return inflater.inflate(R.layout.fragment_vocabulary, container, false)
...@@ -42,21 +43,26 @@ class VocabularyFragment : Fragment() { ...@@ -42,21 +43,26 @@ class VocabularyFragment : Fragment() {
userDocument = db.collection("users").document(userId) userDocument = db.collection("users").document(userId)
} }
private fun setVocabularyId(task: DocumentSnapshot) {
//todo if only one vocabulary exists, open it
val vocabularies: List<DocumentReference> = task.get("vocabularies") as List<DocumentReference>
vocabularyId = vocabularies[0].id
}
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
private fun printUserData() { private fun printUserData() {
userDocument.get().addOnSuccessListener { task -> userDocument.get().addOnSuccessListener { task ->
val email = task.get("email").toString() val email = task.get("email").toString()
tvUserData.text = email tvUserData.text = email
retrieveVocabulary(task) setVocabularyId(task)
retrieveVocabularyData()
} }
} }
private fun retrieveVocabulary(task: DocumentSnapshot) { private fun retrieveVocabularyData() {
//todo if only one vocabulary exists, open it //todo if only one vocabulary exists, open it
val vocabularies: List<DocumentReference> = task.get("vocabularies") as List<DocumentReference> db.collection("vocabularies").document(vocabularyId).get().addOnSuccessListener { task ->
val firstVocab = vocabularies[0].id
db.collection("vocabularies").document(firstVocab).get().addOnSuccessListener { task ->
val vocabTitle = task.get("title").toString() val vocabTitle = task.get("title").toString()
tvUserData.append("\n\nvocabularies:\n$vocabTitle") tvUserData.append("\n\nvocabularies:\n$vocabTitle")
...@@ -65,10 +71,9 @@ class VocabularyFragment : Fragment() { ...@@ -65,10 +71,9 @@ class VocabularyFragment : Fragment() {
private fun addWord() { private fun addWord() {
val addWordIntent = Intent(activity, AddWordActivity::class.java) val addWordIntent = Intent(activity, AddWordActivity::class.java)
addWordIntent.putExtra("vocabularyId", vocabularyId)
startActivity(addWordIntent) startActivity(addWordIntent)
} }
companion object { companion object { private val TAG = "VN/" + VocabularyFragment::class.simpleName }
private val TAG = "VN/" + VocabularyFragment::class.simpleName
}
} }
\ No newline at end of file
...@@ -23,13 +23,13 @@ ...@@ -23,13 +23,13 @@
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:ems="10" android:ems="10"
android:hint="Translation" android:hint="Translation"
app:layout_constraintBottom_toTopOf="@+id/btnSubmit" app:layout_constraintBottom_toTopOf="@+id/btnSubmitNewWord"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etWord" /> app:layout_constraintTop_toBottomOf="@+id/etWord" />
<Button <Button
android:id="@+id/btnSubmit" android:id="@+id/btnSubmitNewWord"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
......
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