-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[APP] Add Children
- Loading branch information
Showing
10 changed files
with
189 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 80 additions & 2 deletions
82
app/app/src/main/java/uah/vaccination/fragments/ChildrenFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters