Commit 54c05326 by Paktalin

Words are sorted after one was added or edited

parent 51f65fec
...@@ -18,7 +18,9 @@ ...@@ -18,7 +18,9 @@
<activity <activity
android:name=".ui.MainActivity" android:name=".ui.MainActivity"
android:windowSoftInputMode="adjustResize"/> android:windowSoftInputMode="adjustResize"/>
<activity android:name=".ui.LogInActivity"> <activity
android:name=".ui.LogInActivity"
android:windowSoftInputMode="adjustResize">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
......
...@@ -5,7 +5,7 @@ import com.google.firebase.auth.FirebaseUser ...@@ -5,7 +5,7 @@ import com.google.firebase.auth.FirebaseUser
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.ui.LogInActivity import com.paktalin.vocabularynotebook.ui.LogInActivity
import com.paktalin.vocabularynotebook.pojo.UserPojo import com.paktalin.vocabularynotebook.pojo.UserPojo
import com.paktalin.vocabularynotebook.pojo.VocabularyPojo import com.paktalin.vocabularynotebook.pojo.Vocabulary
import java.util.* import java.util.*
class UserManager { class UserManager {
...@@ -23,7 +23,7 @@ class UserManager { ...@@ -23,7 +23,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(VocabularyPojo()) 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)
......
...@@ -61,12 +61,14 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va ...@@ -61,12 +61,14 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
fun addWordItem(newWordItem: WordItem) { fun addWordItem(newWordItem: WordItem) {
wordItems.add(0, newWordItem) wordItems.add(0, newWordItem)
this.sort()
this.notifyItemInserted(0) this.notifyItemInserted(0)
} }
fun updateWordItem(updatedWordItem: WordItem) { fun updateWordItem(updatedWordItem: WordItem) {
val updatedItemId = wordItems.indexOf(updatedWordItem) val updatedItemId = wordItems.indexOf(updatedWordItem)
wordItems[updatedItemId] = updatedWordItem wordItems[updatedItemId] = updatedWordItem
this.sort()
this.notifyDataSetChanged() this.notifyDataSetChanged()
} }
......
package com.paktalin.vocabularynotebook.pojo
class Vocabulary {
var pojo:Pojo
class Pojo(var title:String?) {
init {
if (title == null) title = "Untitled vocabulary"
}
}
init {
pojo = Pojo(null)
}
}
package com.paktalin.vocabularynotebook.pojo;
public class VocabularyPojo {
private String title;
public VocabularyPojo() {
title = "First vocabulary";
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
...@@ -37,9 +37,9 @@ class LogInActivity : AppCompatActivity() { ...@@ -37,9 +37,9 @@ class LogInActivity : AppCompatActivity() {
val password = etPassword!!.text.toString() val password = etPassword!!.text.toString()
if (Utils.fieldsNotEmpty(email, password, "Please, enter email and password", this)) { if (Utils.fieldsNotEmpty(email, password, "Please, enter email and password", this)) {
progress.visibility = View.VISIBLE showProgressBar()
mAuth!!.signInWithEmailAndPassword(email, password) mAuth!!.signInWithEmailAndPassword(email, password)
.addOnCompleteListener { progress.visibility = View.GONE } .addOnCompleteListener { hideProgressBar() }
.addOnSuccessListener { .addOnSuccessListener {
Log.d(TAG, "Successfully signed in") Log.d(TAG, "Successfully signed in")
startUserActivity() startUserActivity()
...@@ -59,7 +59,9 @@ class LogInActivity : AppCompatActivity() { ...@@ -59,7 +59,9 @@ class LogInActivity : AppCompatActivity() {
if (Utils.fieldsNotEmpty(email, password, "Please, enter email and password", this)) { 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
showProgressBar()
mAuth!!.createUserWithEmailAndPassword(email, password) mAuth!!.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener { hideProgressBar() }
.addOnSuccessListener { _ -> .addOnSuccessListener { _ ->
Log.d(TAG, "Successfully signed up a new user") Log.d(TAG, "Successfully signed up a new user")
UserManager.addNewUserToDb(mAuth!!.currentUser!!, this) UserManager.addNewUserToDb(mAuth!!.currentUser!!, this)
...@@ -77,6 +79,10 @@ class LogInActivity : AppCompatActivity() { ...@@ -77,6 +79,10 @@ class LogInActivity : AppCompatActivity() {
startActivity(userActivityIntent) startActivity(userActivityIntent)
} }
private fun showProgressBar() { progress.visibility = View.VISIBLE }
private fun hideProgressBar() { progress.visibility = View.GONE }
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
private fun createRandomUser() { private fun createRandomUser() {
etEmail.setText("random@gmail.com") etEmail.setText("random@gmail.com")
......
...@@ -15,6 +15,7 @@ import android.view.Menu ...@@ -15,6 +15,7 @@ import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import com.paktalin.vocabularynotebook.VocabularyAdapter 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.*
...@@ -71,11 +72,10 @@ class MainActivity : AppCompatActivity() { ...@@ -71,11 +72,10 @@ class MainActivity : AppCompatActivity() {
hideProgressBar() hideProgressBar()
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>
//todo represent specific vocabulary instead of the first one
val vocabulary = db.collection("vocabularies").document(vocabularies[0].id) val vocabulary = db.collection("vocabularies").document(vocabularies[0].id)
vocabularyId = vocabulary.id vocabularyId = vocabulary.id
vocabularyFragment.retrieveWordsData(vocabularyId) vocabularyFragment.retrieveWordsData(vocabularyId)
} } else { showToastNoWords() }
} }
} }
...@@ -92,12 +92,13 @@ class MainActivity : AppCompatActivity() { ...@@ -92,12 +92,13 @@ class MainActivity : AppCompatActivity() {
imm.hideSoftInputFromWindow(view.windowToken, 0) imm.hideSoftInputFromWindow(view.windowToken, 0)
} }
fun showProgressBar() { fun showProgressBar() { progress.visibility = View.VISIBLE }
progress.visibility = View.VISIBLE
} fun hideProgressBar() { progress.visibility = View.GONE }
fun hideProgressBar() { fun showToastNoWords() {
progress.visibility = View.GONE Toast.makeText(this@MainActivity,
"You don't have any words yet. Add your fist one!", Toast.LENGTH_SHORT).show()
} }
override fun onPause() { override fun onPause() {
......
...@@ -44,7 +44,10 @@ class VocabularyFragment : Fragment() { ...@@ -44,7 +44,10 @@ class VocabularyFragment : Fragment() {
fun retrieveWordsData(vocabularyId: String) { fun retrieveWordsData(vocabularyId: String) {
db.collection(VOCABULARIES).document(vocabularyId).collection(WORDS).orderBy("word").get() db.collection(VOCABULARIES).document(vocabularyId).collection(WORDS).orderBy("word").get()
.addOnSuccessListener { setVocabularyAdapter(it.documents, vocabularyId) } .addOnSuccessListener {
if (it.documents.size != 0)
setVocabularyAdapter(it.documents, vocabularyId)
else (activity as MainActivity).showToastNoWords()}
} }
private fun setVocabularyAdapter(documents: MutableList<DocumentSnapshot>, vocabularyId: String) { private fun setVocabularyAdapter(documents: MutableList<DocumentSnapshot>, vocabularyId: String) {
......
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