Commit 54c05326 by Paktalin

Words are sorted after one was added or edited

parent 51f65fec
......@@ -18,7 +18,9 @@
<activity
android:name=".ui.MainActivity"
android:windowSoftInputMode="adjustResize"/>
<activity android:name=".ui.LogInActivity">
<activity
android:name=".ui.LogInActivity"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......
......@@ -5,7 +5,7 @@ import com.google.firebase.auth.FirebaseUser
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.ui.LogInActivity
import com.paktalin.vocabularynotebook.pojo.UserPojo
import com.paktalin.vocabularynotebook.pojo.VocabularyPojo
import com.paktalin.vocabularynotebook.pojo.Vocabulary
import java.util.*
class UserManager {
......@@ -23,7 +23,7 @@ class UserManager {
val db = ConfiguredFirestore.instance
val user = UserPojo(newUser.email)
db.collection("vocabularies").add(VocabularyPojo())
db.collection("vocabularies").add(Vocabulary.Pojo(null))
.addOnSuccessListener { firstVocabularyRef ->
Log.d(TAG, "VocabularyPojo successfully created: " + firstVocabularyRef.path)
user.vocabularies = Collections.singletonList(firstVocabularyRef)
......
......@@ -61,12 +61,14 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
fun addWordItem(newWordItem: WordItem) {
wordItems.add(0, newWordItem)
this.sort()
this.notifyItemInserted(0)
}
fun updateWordItem(updatedWordItem: WordItem) {
val updatedItemId = wordItems.indexOf(updatedWordItem)
wordItems[updatedItemId] = updatedWordItem
this.sort()
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() {
val password = etPassword!!.text.toString()
if (Utils.fieldsNotEmpty(email, password, "Please, enter email and password", this)) {
progress.visibility = View.VISIBLE
showProgressBar()
mAuth!!.signInWithEmailAndPassword(email, password)
.addOnCompleteListener { progress.visibility = View.GONE }
.addOnCompleteListener { hideProgressBar() }
.addOnSuccessListener {
Log.d(TAG, "Successfully signed in")
startUserActivity()
......@@ -59,7 +59,9 @@ class LogInActivity : AppCompatActivity() {
if (Utils.fieldsNotEmpty(email, password, "Please, enter email and password", this)) {
//todo check if the password is good
// todo verify email
showProgressBar()
mAuth!!.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener { hideProgressBar() }
.addOnSuccessListener { _ ->
Log.d(TAG, "Successfully signed up a new user")
UserManager.addNewUserToDb(mAuth!!.currentUser!!, this)
......@@ -77,6 +79,10 @@ class LogInActivity : AppCompatActivity() {
startActivity(userActivityIntent)
}
private fun showProgressBar() { progress.visibility = View.VISIBLE }
private fun hideProgressBar() { progress.visibility = View.GONE }
@SuppressLint("SetTextI18n")
private fun createRandomUser() {
etEmail.setText("random@gmail.com")
......
......@@ -15,6 +15,7 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import com.paktalin.vocabularynotebook.VocabularyAdapter
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import kotlinx.android.synthetic.main.fragment_vocabulary.*
......@@ -71,11 +72,10 @@ class MainActivity : AppCompatActivity() {
hideProgressBar()
if (task.get("vocabularies") != null) {
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)
vocabularyId = vocabulary.id
vocabularyFragment.retrieveWordsData(vocabularyId)
}
} else { showToastNoWords() }
}
}
......@@ -92,12 +92,13 @@ class MainActivity : AppCompatActivity() {
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
fun showProgressBar() {
progress.visibility = View.VISIBLE
}
fun showProgressBar() { progress.visibility = View.VISIBLE }
fun hideProgressBar() { progress.visibility = View.GONE }
fun hideProgressBar() {
progress.visibility = View.GONE
fun showToastNoWords() {
Toast.makeText(this@MainActivity,
"You don't have any words yet. Add your fist one!", Toast.LENGTH_SHORT).show()
}
override fun onPause() {
......
......@@ -44,7 +44,10 @@ class VocabularyFragment : Fragment() {
fun retrieveWordsData(vocabularyId: String) {
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) {
......
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