Commit 541c3b78 by Paktalin

Moved new word logic to NewWordFragment

parent ed16421e
......@@ -15,8 +15,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activities.MainActivity" />
<activity android:name=".activities.LogInActivity">
<activity android:name=".ui.MainActivity" />
<activity android:name=".ui.LogInActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......@@ -24,9 +24,9 @@
</intent-filter>
</activity>
<activity
android:name=".activities.AddWordActivity"
android:name=".ui.AddWordActivity"
android:windowSoftInputMode="adjustResize"/>
<activity android:name=".activities.WordItemInfoActivity" />
<activity android:name=".ui.WordItemInfoActivity" />
</application>
</manifest>
\ No newline at end of file
......@@ -3,7 +3,7 @@ package com.paktalin.vocabularynotebook
import android.util.Log
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.activities.LogInActivity
import com.paktalin.vocabularynotebook.ui.LogInActivity
import com.paktalin.vocabularynotebook.pojo.UserPojo
import com.paktalin.vocabularynotebook.pojo.VocabularyPojo
import java.util.*
......
package com.paktalin.vocabularynotebook
import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.TextView
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tvWord: TextView = itemView.findViewById(R.id.etWord)
val tvTranslation: TextView = itemView.findViewById(R.id.etTranslation)
val btnPopupMenu: ImageButton = itemView.findViewById(R.id.btnContextMenu)
val layout: LinearLayout = itemView.findViewById(R.id.tableLayout)
private var etWordEmpty = true
private var etTranslationEmpty = true
fun showEmptyItem() {
tvWord.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun afterTextChanged(editable: Editable) {
if (!tvWord.text.isEmpty()) {
showCancelButton()
etWordEmpty = false
} else etWordEmpty = true
if (!etWordEmpty && !etTranslationEmpty)
showAddWordButton()
}
})
this.tvTranslation.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun afterTextChanged(editable: Editable) {
if (!tvTranslation.text.isEmpty()) {
showCancelButton()
etTranslationEmpty = false
} else etTranslationEmpty = true
if (!etWordEmpty && !etTranslationEmpty)
showAddWordButton()
}
})
}
private fun showCancelButton() {
Log.d(TAG, "empty word is focused")
btnPopupMenu.setImageResource(R.drawable.ic_cancel_icon)
btnPopupMenu.visibility = View.VISIBLE
//todo add button click listener
}
private fun showAddWordButton() {
//todo show add word button
}
companion object { private val TAG = "VN/" + ViewHolder::class.java.simpleName }
}
\ No newline at end of file
......@@ -5,9 +5,12 @@ import android.content.Intent
import android.support.v7.widget.PopupMenu
import android.support.v7.widget.RecyclerView
import android.view.*
import com.paktalin.vocabularynotebook.activities.WordItemInfoActivity
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.TextView
import com.paktalin.vocabularynotebook.ui.WordItemInfoActivity
class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private val context: Activity) : RecyclerView.Adapter<ViewHolder>() {
class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private val context: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private lateinit var recyclerView: RecyclerView
......@@ -65,6 +68,13 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
this.notifyItemRangeChanged(position, wordItems.size)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tvWord: TextView = itemView.findViewById(R.id.etWord)
val tvTranslation: TextView = itemView.findViewById(R.id.etTranslation)
val btnPopupMenu: ImageButton = itemView.findViewById(R.id.btnClear)
val layout: LinearLayout = itemView.findViewById(R.id.tableLayout)
}
companion object {
private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName
}
......
package com.paktalin.vocabularynotebook.activities
import android.content.Intent
import android.os.Bundle
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.Utils
import com.paktalin.vocabularynotebook.WordItem.WordItemPojo
import kotlinx.android.synthetic.main.activity_add_word.*
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?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_word)
vocabularyId = intent.getStringExtra("vocabularyId")
btnSubmitNewWord.setOnClickListener { addWordToDb() }
btnCancel.setOnClickListener { cancel() }
}
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(WordItemPojo(word, translation)).addOnSuccessListener {
Log.i(TAG, "Successfully added a new word $word")
clearFields()
cancel()
}
.addOnFailureListener {
Log.w(TAG, "addNewWordToDb:failure", it.fillInStackTrace())
Toast.makeText(this, "Couldn't add the word", Toast.LENGTH_SHORT).show()
}
}
}
private fun cancel() {
val intentMainActivity = Intent(this, MainActivity::class.java)
startActivity(intentMainActivity)
}
private fun clearFields() {
etWord.text.clear()
etTranslation.text.clear()
}
}
package com.paktalin.vocabularynotebook.activities
import android.annotation.SuppressLint
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_log_in.*
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.UserManager
import com.paktalin.vocabularynotebook.Utils
class LogInActivity : AppCompatActivity() {
private var mAuth: FirebaseAuth? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_log_in)
mAuth = FirebaseAuth.getInstance()
btnLogIn!!.setOnClickListener({ logIn() })
btnSignUp!!.setOnClickListener({ signUp() })
btnRandomUser!!.setOnClickListener({ createRandomUser() })
}
override fun onStart() {
super.onStart()
if (mAuth!!.currentUser != null) {
Log.d(TAG, "there is a logged in user")
startUserActivity()
}
}
private fun logIn() {
val email = etEmail!!.text.toString()
val password = etPassword!!.text.toString()
if (Utils.fieldsNotEmpty(email, password, "Please, enter email and password", this)) {
mAuth!!.signInWithEmailAndPassword(email, password)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.d(TAG, "Successfully signed in")
startUserActivity()
}
else {
Log.w(TAG, "signInWithEmail:failure", task.exception)
Toast.makeText(this@LogInActivity, "Authentication failed.",
Toast.LENGTH_SHORT).show()
}
}
}
}
private fun signUp() {
val email = etEmail!!.text.toString()
val password = etPassword!!.text.toString()
if (Utils.fieldsNotEmpty(email, password, "Please, enter email and password", this)) {
//todo check if the password is good
// todo verify email
mAuth!!.createUserWithEmailAndPassword(email, password)
.addOnSuccessListener { _ ->
Log.d(TAG, "Successfully signed up a new user")
UserManager.addNewUserToDb(mAuth!!.currentUser!!, this)
}
.addOnFailureListener {
Log.d(TAG, "createUserWithEmail:failure", it.fillInStackTrace())
Toast.makeText(this@LogInActivity, it.message, Toast.LENGTH_SHORT).show()
}
}
}
fun startUserActivity() {
Log.d(TAG, "Logged in successfully")
val userActivityIntent = Intent(this@LogInActivity, MainActivity::class.java)
startActivity(userActivityIntent)
}
@SuppressLint("SetTextI18n")
private fun createRandomUser() {
etEmail.setText("random@gmail.com")
etPassword.setText("123456")
signUp()
}
companion object {
private val TAG = "VN/" + LogInActivity::class.simpleName
}
}
\ No newline at end of file
package com.paktalin.vocabularynotebook.activities
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import com.google.firebase.auth.FirebaseAuth
import com.paktalin.vocabularynotebook.R
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigationView.setNavigationItemSelectedListener { menuItem ->
menuItem.isChecked = true
if(menuItem.itemId == R.id.logOut) { logOut() }
drawerLayout!!.closeDrawers()
true
}
}
private fun logOut() {
Log.i(TAG, "User logged out")
FirebaseAuth.getInstance()!!.signOut()
val intentLogInActivity = Intent(this@MainActivity, LogInActivity::class.java)
startActivity(intentLogInActivity)
}
companion object {
private val TAG = "VN/" + MainActivity::class.simpleName
}
}
package com.paktalin.vocabularynotebook.activities
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.VocabularyAdapter
import com.paktalin.vocabularynotebook.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 const val USERS = "users"
}
private lateinit var userDocument: DocumentReference
private val db = FirebaseFirestore.getInstance()
private lateinit var vocabulary: DocumentReference
//todo move data process to onCreate method and update the views later
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_vocabulary, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setEmptyAdapter()
retrieveData()
}
override fun onDestroy() {
super.onDestroy()
FirebaseAuth.getInstance().signOut()
}
private fun setEmptyAdapter() {
val emptyList:MutableList<WordItem> = mutableListOf()
recyclerView.adapter = VocabularyAdapter(emptyList, activity!!)
val mLayoutManager = LinearLayoutManager(activity)
recyclerView.layoutManager = mLayoutManager
}
private fun retrieveData() {
val userId = FirebaseAuth.getInstance().currentUser!!.uid
userDocument = db.collection(USERS).document(userId)
userDocument.get().addOnSuccessListener { task ->
setVocabularyId(task)
retrieveVocabularyData()
}
}
private fun setVocabularyId(task: DocumentSnapshot) {
//todo if only one vocabulary exists, open it
val vocabularies: List<DocumentReference> = task.get("vocabularies") as List<DocumentReference>
vocabulary = db.collection(VOCABULARIES).document(vocabularies[0].id)
}
private fun retrieveVocabularyData() {
//todo if only one vocabulary exists, open it
vocabulary.collection(WORDS).get()
.addOnSuccessListener { setVocabularyAdapter(it.documents) }
}
private fun addWord() {
val addWordIntent = Intent(activity, AddWordActivity::class.java)
addWordIntent.putExtra("vocabularyId", vocabulary.id)
startActivity(addWordIntent)
}
private fun setVocabularyAdapter(documents: MutableList<DocumentSnapshot>) {
val wordItems: MutableList<WordItem> = mutableListOf()
for (ref in documents) {
val word = ref.get("word").toString()
val translation = ref.get("translation").toString()
wordItems.add(WordItem(word, translation, ref.id, vocabulary.id))
}
val adapter = VocabularyAdapter(wordItems, activity!!)
recyclerView.adapter = adapter
}
}
\ No newline at end of file
package com.paktalin.vocabularynotebook.activities
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem
import kotlinx.android.synthetic.main.activity_word_info.*
class WordItemInfoActivity: AppCompatActivity() {
private lateinit var wordItem: WordItem
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_word_info)
wordItem = intent.getSerializableExtra("wordItem") as WordItem
setData()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.word_item_info_menu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item!!.itemId) {
R.id.item_delete -> {
wordItem.delete()
cancel()
}
R.id.item_edit -> {
//todo edit item
}
}
return true
}
private fun setData() {
etWord.text = wordItem.pojo!!.word
etTranslation.text = wordItem.pojo!!.translation
}
private fun cancel() {
val intentMainActivity = Intent(this, MainActivity::class.java)
startActivity(intentMainActivity)
}
companion object { private val TAG = "VN/" + WordItemInfoActivity::class.java.simpleName }
}
\ No newline at end of file
......@@ -5,7 +5,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.LogInActivity">
tools:context=".ui.LogInActivity">
<EditText
android:id="@+id/etEmail"
......
......@@ -16,7 +16,7 @@
<fragment
android:id="@+id/fragment_new_word"
android:name="com.paktalin.vocabularynotebook.NewWordFragment"
android:name="com.paktalin.vocabularynotebook.ui.NewWordFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
......@@ -30,7 +30,7 @@
<fragment
android:id="@+id/fragment_vocabulary"
android:name="com.paktalin.vocabularynotebook.activities.VocabularyFragment"
android:name="com.paktalin.vocabularynotebook.ui.VocabularyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
......
......@@ -29,10 +29,11 @@
tools:ignore="LabelFor" />
<ImageButton
android:id="@+id/btnContextMenu"
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="invisible"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_cancel_icon"
tools:ignore="ContentDescription" />
......
......@@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.paktalin.vocabularynotebook.activities.VocabularyFragment">
tools:context="com.paktalin.vocabularynotebook.ui.VocabularyFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
......
......@@ -32,7 +32,7 @@
android:inputType="text"/>
<ImageButton
android:id="@+id/btnContextMenu"
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
......
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