Skip to content

Commit

Permalink
Final commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JosueBonilla2 committed Jun 14, 2024
1 parent 3fe5718 commit ad92b30
Show file tree
Hide file tree
Showing 12 changed files with 433 additions and 61 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ android {
}

dependencies {

implementation ("androidx.fragment:fragment-ktx:1.5.2")
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/example/nutrivida/SharedViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.nutrivida

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class SharedViewModel : ViewModel() {
private val _progress = MutableLiveData<Int>()
val progress: LiveData<Int> get() = _progress

fun setProgress(value: Int) {
_progress.value = value
}
}
9 changes: 7 additions & 2 deletions app/src/main/java/com/example/nutrivida/actividad.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels

class actividad : Fragment() {

Expand All @@ -20,6 +21,8 @@ class actividad : Fragment() {
private val totalTime = 1 * 60 * 1000L
private var timeLeft = totalTime

private val sharedViewModel: SharedViewModel by activityViewModels()

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand Down Expand Up @@ -64,7 +67,8 @@ class actividad : Fragment() {
countDownTimer = object : CountDownTimer(timeLeft, 1000) {
override fun onTick(millisUntilFinished: Long) {
timeLeft = millisUntilFinished
updateProgressBar(progressBar, textEjercicioP)
val progress = updateProgressBar(progressBar, textEjercicioP)
sharedViewModel.setProgress(progress)
}

override fun onFinish() {
Expand All @@ -86,10 +90,11 @@ class actividad : Fragment() {
isTimerRunning = false
}

private fun updateProgressBar(progressBar: ProgressBar, textEjercicioP: TextView) {
private fun updateProgressBar(progressBar: ProgressBar, textEjercicioP: TextView): Int {
val progress = ((totalTime - timeLeft).toFloat() / totalTime * 100).toInt()
progressBar.progress = progress
textEjercicioP.text = "Progreso: $progress%"
return progress
}

private fun showToast() {
Expand Down
101 changes: 78 additions & 23 deletions app/src/main/java/com/example/nutrivida/home.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package com.example.nutrivida

import android.app.Dialog
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.TextView
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
* A simple [Fragment] subclass.
* Use the [home.newInstance] factory method to
* create an instance of this fragment.
*/
class home : Fragment() {
// TODO: Rename and change types of parameters

private val sharedViewModel: SharedViewModel by activityViewModels()
private var param1: String? = null
private var param2: String? = null
private var pro: Int = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -33,20 +32,76 @@ class home : Fragment() {
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
val view = inflater.inflate(R.layout.fragment_home, container, false)
val progressBarA: ProgressBar = view.findViewById(R.id.progressBar_a)
val progressBarS: ProgressBar = view.findViewById(R.id.progressBar_s)
val progressBarC: ProgressBar = view.findViewById(R.id.progressBar_c)
val progressBarV: ProgressBar = view.findViewById(R.id.progressBar_v)
val progressBarP: ProgressBar = view.findViewById(R.id.progressBar_principal)
val progreso: TextView = view.findViewById(R.id.text_pro)
val addRegis: Button = view.findViewById(R.id.btn_progreso)

addRegis.setOnClickListener {
val regisDialog = Dialog(requireContext())
regisDialog.setContentView(R.layout.add_progreso)

val agua = regisDialog.findViewById<EditText>(R.id.txt_agua)
val vasos: TextView = view.findViewById(R.id.text_numCup)

val sue = regisDialog.findViewById<EditText>(R.id.txt_sueno)
val horas: TextView = view.findViewById(R.id.text_h)

val cal = regisDialog.findViewById<EditText>(R.id.txt_calorias)
val calorias: TextView = view.findViewById(R.id.text_c)

regisDialog.findViewById<Button>(R.id.btn_reg).setOnClickListener {

val aguaInt = agua.text.toString().toInt()
val suenoInt = sue.text.toString().toInt()
val calInt = cal.text.toString().toInt()

val calR = (calInt / 2000.0) * 100

vasos.text = agua.text.toString()
horas.text = "Horas: " + sue.text.toString()
calorias.text = calR.toString()

progressBarS.progress = suenoInt
progressBarV.progress = aguaInt
progressBarC.progress = calR.toInt()

updatePrincipalProgress(progressBarP, progressBarA, progressBarS, progressBarC, progressBarV, progreso)

regisDialog.dismiss()
}
regisDialog.show()
}

sharedViewModel.progress.observe(viewLifecycleOwner, Observer { progress ->
progressBarA.progress = progress
updatePrincipalProgress(progressBarP, progressBarA, progressBarS, progressBarC, progressBarV, progreso)
})

return view
}

private fun updatePrincipalProgress(
progressBarP: ProgressBar,
progressBarA: ProgressBar,
progressBarS: ProgressBar,
progressBarC: ProgressBar,
progressBarV: ProgressBar,
progreso: TextView
) {
val totalProgress = ((progressBarA.progress + progressBarS.progress + progressBarC.progress + progressBarV.progress) - 23) / 2
progressBarP.progress = totalProgress
progreso.text = totalProgress.toString()
}

companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment home.
*/
// TODO: Rename and change types and number of parameters
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

@JvmStatic
fun newInstance(param1: String, param2: String) =
home().apply {
Expand All @@ -56,4 +111,4 @@ class home : Fragment() {
}
}
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/agua.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
android:viewportHeight="96">
<path
android:pathData="M49.015,0.803c-0.133,-1.071 -1.896,-1.071 -2.029,0C42.57,36.344 20,43.666 20,68.367C20,83.627 32.816,96 48,96s28,-12.373 28,-27.633C76,43.666 53.43,36.344 49.015,0.803zM44.751,40.09c-0.297,1.095 -0.615,2.223 -0.942,3.386c-2.007,7.123 -4.281,15.195 -4.281,24.537c0,5.055 -2.988,6.854 -5.784,6.854c-3.189,0 -5.782,-2.616 -5.782,-5.831c0,-11.034 5.315,-18.243 10.005,-24.604c1.469,-1.991 2.855,-3.873 3.983,-5.749c0.516,-0.856 1.903,-0.82 2.533,0.029C44.781,39.116 44.879,39.619 44.751,40.09z"
android:fillColor="@color/primary"/>
android:fillColor="#009688"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_add_circle_outline_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#9DC70F" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M13,7h-2v4L7,11v2h4v4h2v-4h4v-2h-4L13,7zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8z"/>

</vector>
17 changes: 17 additions & 0 deletions app/src/main/res/drawable/blue_progressbar_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="ring"
android:innerRadiusRatio="3"
android:thicknessRatio="30.0"
android:useLevel="true">
<gradient
android:startColor="#3F51B5"
android:centerColor="#03A9F4"
android:endColor="#00BCD4">

</gradient>
</shape>
</item>
</layer-list>
17 changes: 17 additions & 0 deletions app/src/main/res/drawable/gold_progressbar_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="ring"
android:innerRadiusRatio="3"
android:thicknessRatio="30.0"
android:useLevel="true">
<gradient
android:startColor="#6C6103"
android:centerColor="#A17C0F"
android:endColor="#FFC107">

</gradient>
</shape>
</item>
</layer-list>
14 changes: 14 additions & 0 deletions app/src/main/res/drawable/gray_progressbar_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="ring"
android:innerRadiusRatio="3"
android:thicknessRatio="30.0"
android:useLevel="false">
<solid
android:color="@color/white">
</solid>
</shape>
</item>
</layer-list>
17 changes: 17 additions & 0 deletions app/src/main/res/drawable/green_progressbar_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="ring"
android:innerRadiusRatio="3"
android:thicknessRatio="30.0"
android:useLevel="true">
<gradient
android:startColor="#8BC34A"
android:centerColor="#9DC56F"
android:endColor="#ABC38F">

</gradient>
</shape>
</item>
</layer-list>
89 changes: 89 additions & 0 deletions app/src/main/res/layout/add_progreso.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:layout_width="388dp"
android:layout_height="301dp"
android:layout_gravity="center_vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
android:layout_width="391dp"
android:layout_height="match_parent"
android:background="@drawable/edittext_login"
android:orientation="vertical">

<EditText
android:id="@+id/txt_agua"
android:layout_width="330dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:ems="10"
android:hint="Cuantos vasos de agua bebiste hoy?"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/txt_sueno"
android:layout_width="330dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:ems="10"
android:hint="Cantas horas dormiste hoy?"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_agua" />

<EditText
android:id="@+id/txt_calorias"
android:layout_width="330dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:ems="10"
android:hint="Calorias ingeridas hoy"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/btn_reg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_sueno" />

<Button
android:id="@+id/btn_reg"
android:layout_width="330dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:backgroundTint="@color/primary"
android:text="Registrar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="275dp" />
</LinearLayout>

</FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit ad92b30

Please sign in to comment.