Skip to content

Commit

Permalink
Merge pull request #16
Browse files Browse the repository at this point in the history
[APP] Add Children
  • Loading branch information
MichelineMedhat authored Aug 27, 2019
2 parents 8bcc380 + 7e6dc3c commit 82a30da
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 15 deletions.
29 changes: 29 additions & 0 deletions app/app/src/main/java/uah/vaccination/ChildrenAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package uah.vaccination

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import uah.vaccination.models.Child

class ChildrenAdapter(val children: ArrayList<Child>) :
RecyclerView.Adapter<ChildrenAdapter.ChildHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
ChildHolder(LayoutInflater.from(parent.context).inflate(R.layout.child_item, parent, false))

override fun getItemCount(): Int {
return children.size
}

override fun onBindViewHolder(holder: ChildHolder, position: Int) {
holder.childName.text = children[position].name
holder.childBirthDate.text = children[position].dateOfBirth!!.toDate().toLocaleString()
}

inner class ChildHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
internal var childName: TextView = itemView.findViewById(R.id.child_name_text_view)
internal var childBirthDate: TextView = itemView.findViewById(R.id.child_birth_date_text_view)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_sign_in.*
import kotlinx.android.synthetic.main.activity_sign_in.email_edit_text
import kotlinx.android.synthetic.main.activity_sign_up.*
import uah.vaccination.R

class SignInActivity : AppCompatActivity() {
Expand All @@ -16,7 +14,7 @@ class SignInActivity : AppCompatActivity() {
override fun onStart() {
super.onStart()
FirebaseAuth.getInstance().currentUser ?: return
startMainActivity(true)
navigateToActivity(true, MainActivity::class.java)
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -31,7 +29,7 @@ class SignInActivity : AppCompatActivity() {
}

createAnAccount_text_view.setOnClickListener {
startMainActivity(false)
navigateToActivity(false, SignUpActivity::class.java)
}

}
Expand All @@ -55,13 +53,13 @@ class SignInActivity : AppCompatActivity() {
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
startMainActivity(true)
navigateToActivity(true, MainActivity::class.java)
}
}
}

private fun startMainActivity(addFlags: Boolean) {
val intent = Intent(this, SignUpActivity::class.java)
private fun navigateToActivity(addFlags: Boolean, activity: Class<out AppCompatActivity>) {
val intent = Intent(this, activity)
if (addFlags) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,105 @@
package uah.vaccination.fragments

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.Timestamp
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.FieldValue
import com.google.firebase.firestore.FirebaseFirestore
import kotlinx.android.synthetic.main.dialog_add_child.view.*
import kotlinx.android.synthetic.main.fragment_children.*
import kotlinx.android.synthetic.main.fragment_children.view.*
import uah.vaccination.ChildrenAdapter
import uah.vaccination.R
import uah.vaccination.models.Child
import java.util.*

class ChildrenFragment : Fragment() {

class ChildrenFragment : Fragment() {
lateinit var dialogView: View
lateinit var adapter: ChildrenAdapter
val db = FirebaseFirestore.getInstance()
val auth = FirebaseAuth.getInstance()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
db.collection("parents")
.document(auth.currentUser!!.email.toString())
.get().addOnSuccessListener {
val childrenDocs = it.data!!["children"] as ArrayList<HashMap<String, Object>>
Log.d("lol", childrenDocs.toString())
val children = arrayListOf<Child>()
childrenDocs.forEach { doc ->
children.add(
Child(
name = doc["name"].toString(),
dateOfBirth = doc["dateOfBirth"] as Timestamp
)
)
}

adapter = ChildrenAdapter(children)
children_recycler_view.adapter = adapter
children_recycler_view.layoutManager = LinearLayoutManager(context)
}
return inflater.inflate(R.layout.fragment_children, container, false)
}


@SuppressLint("SimpleDateFormat")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.fab.setOnClickListener {
//TODO: Add Child Dialog

dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_add_child, null)
val dialogBuilder =
AlertDialog.Builder(context).setView(dialogView).setTitle("إضافة طفل")
val dialog = dialogBuilder.show()

dialogView.sign_up_button.setOnClickListener {
if (validateInput()) {
val child = Child(
name = dialogView.name_edit_text.text.toString(),
surname = dialogView.surname_edit_text.text.toString(),
dateOfBirth = Timestamp(Date(dialogView.dateOfBirth_edit_text.text.toString()))
)
Log.d("lol", child.dateOfBirth.toString())
addNewChild(child)
adapter.children.add(child)
adapter.notifyItemInserted(adapter.children.lastIndex)
dialog.dismiss()
}
}

}
}

private fun validateInput(): Boolean {
val isNameEmpty = dialogView.name_edit_text.text.isNullOrEmpty()
val isSurnameEmpty = dialogView.surname_edit_text.text.isNullOrEmpty()
val isDateOfBirthEmpty = dialogView.dateOfBirth_edit_text.text.isNullOrEmpty()

if (!isNameEmpty &&
!isSurnameEmpty &&
!isDateOfBirthEmpty
)
return true
else {
if (isNameEmpty) dialogView.name_edit_text.error = "Required Field."
if (isSurnameEmpty) dialogView.surname_edit_text.error = "Required Field."
if (isDateOfBirthEmpty) dialogView.dateOfBirth_edit_text.error = "Required Field."
}
return false
}

private fun addNewChild(child: Child) {
db.collection("parents").document(auth.currentUser!!.email.toString())
.update("children", FieldValue.arrayUnion(child))
}
}
4 changes: 2 additions & 2 deletions app/app/src/main/java/uah/vaccination/models/Child.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package uah.vaccination.models
import com.google.firebase.Timestamp

data class Child(
val name: String? = null,
var name: String? = null,
val surname: String? = null,
val dateOfBirth: Timestamp? = null
var dateOfBirth: Timestamp? = null
)
2 changes: 1 addition & 1 deletion app/app/src/main/java/uah/vaccination/models/Parent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ data class Parent(
val city: String? = null,
val email: String? = null,
val phoneNumber: String? = null,
val child: ArrayList<Child>? = arrayListOf()
val children: ArrayList<Child>? = arrayListOf()
)
5 changes: 4 additions & 1 deletion app/app/src/main/res/layout/activity_sign_in.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:layout_marginBottom="16dp"/>

<EditText android:layout_width="match_parent"
<EditText android:id="@+id/password_edit_text"
android:layout_width="match_parent"
android:layout_height="48dp"
android:inputType="textPassword"
android:hint="@string/password"
android:layout_marginBottom="16dp"/>

Expand Down
21 changes: 21 additions & 0 deletions app/app/src/main/res/layout/child_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">

<TextView android:id="@+id/child_name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="24sp"
android:textColor="@color/colorPrimary"/>

<TextView android:id="@+id/child_birth_date_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:textColor="@color/colorPrimary"/>
</LinearLayout>
38 changes: 38 additions & 0 deletions app/app/src/main/res/layout/dialog_add_child.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="32dp">

<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">

<EditText android:id="@+id/name_edit_text"
android:hint="@string/name"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp"/>

<EditText android:id="@+id/surname_edit_text"
android:hint="@string/surname"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp"/>
</LinearLayout>

<EditText android:id="@+id/dateOfBirth_edit_text"
android:hint="@string/dateOfBirth"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="date"/>

<Button android:id="@+id/sign_up_button"
android:text="@string/signUp"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>
9 changes: 7 additions & 2 deletions app/app/src/main/res/layout/fragment_children.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".fragments.ChildrenFragment">


Expand All @@ -12,5 +12,10 @@
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
app:srcCompat="@drawable/ic_add"/>
<androidx.recyclerview.widget.RecyclerView android:id="@+id/children_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView>

</FrameLayout>
2 changes: 2 additions & 0 deletions app/app/src/main/res/values/strings-ar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
<string name="signUp">تسجيل</string>
<string name="sign_in">تسجيل دخول</string>
<string name="sign_up">انشاء حساب</string>
<string name="dateOfBirth">تاريخ الميلاد</string>
<string name="parentId">رقم ولي الأمر</string>
</resources>

0 comments on commit 82a30da

Please sign in to comment.